"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Linker = void 0; const tsparticles_engine_1 = require("tsparticles-engine"); const CircleWarp_1 = require("./CircleWarp"); const Links_1 = require("./Options/Classes/Links"); function getLinkDistance(pos1, pos2, optDistance, canvasSize, warp) { let distance = (0, tsparticles_engine_1.getDistance)(pos1, pos2); if (!warp || distance <= optDistance) { return distance; } const pos2NE = { x: pos2.x - canvasSize.width, y: pos2.y, }; distance = (0, tsparticles_engine_1.getDistance)(pos1, pos2NE); if (distance <= optDistance) { return distance; } const pos2SE = { x: pos2.x - canvasSize.width, y: pos2.y - canvasSize.height, }; distance = (0, tsparticles_engine_1.getDistance)(pos1, pos2SE); if (distance <= optDistance) { return distance; } const pos2SW = { x: pos2.x, y: pos2.y - canvasSize.height, }; distance = (0, tsparticles_engine_1.getDistance)(pos1, pos2SW); return distance; } class Linker extends tsparticles_engine_1.ParticlesInteractorBase { constructor(container) { super(container); this.linkContainer = container; } clear() { } init() { this.linkContainer.particles.linksColor = undefined; this.linkContainer.particles.linksColors = new Map(); } interact(p1) { var _a; return __awaiter(this, void 0, void 0, function* () { if (!p1.options.links) { return; } p1.links = []; const pos1 = p1.getPosition(), container = this.container, canvasSize = container.canvas.size; if (pos1.x < 0 || pos1.y < 0 || pos1.x > canvasSize.width || pos1.y > canvasSize.height) { return; } const linkOpt1 = p1.options.links, optOpacity = linkOpt1.opacity, optDistance = (_a = p1.retina.linksDistance) !== null && _a !== void 0 ? _a : 0, warp = linkOpt1.warp, range = warp ? new CircleWarp_1.CircleWarp(pos1.x, pos1.y, optDistance, canvasSize) : new tsparticles_engine_1.Circle(pos1.x, pos1.y, optDistance), query = container.particles.quadTree.query(range); for (const p2 of query) { const linkOpt2 = p2.options.links; if (p1 === p2 || !(linkOpt2 === null || linkOpt2 === void 0 ? void 0 : linkOpt2.enable) || linkOpt1.id !== linkOpt2.id || p2.spawning || p2.destroyed || !p2.links || p1.links.map((t) => t.destination).indexOf(p2) !== -1 || p2.links.map((t) => t.destination).indexOf(p1) !== -1) { continue; } const pos2 = p2.getPosition(); if (pos2.x < 0 || pos2.y < 0 || pos2.x > canvasSize.width || pos2.y > canvasSize.height) { continue; } const distance = getLinkDistance(pos1, pos2, optDistance, canvasSize, warp && linkOpt2.warp); if (distance > optDistance) { return; } const opacityLine = (1 - distance / optDistance) * optOpacity; this.setColor(p1); p1.links.push({ destination: p2, opacity: opacityLine, }); } }); } isEnabled(particle) { var _a; return !!((_a = particle.options.links) === null || _a === void 0 ? void 0 : _a.enable); } loadParticlesOptions(options, ...sources) { var _a, _b; if (!options.links) { options.links = new Links_1.Links(); } for (const source of sources) { options.links.load((_b = (_a = source === null || source === void 0 ? void 0 : source.links) !== null && _a !== void 0 ? _a : source === null || source === void 0 ? void 0 : source.lineLinked) !== null && _b !== void 0 ? _b : source === null || source === void 0 ? void 0 : source.line_linked); } } reset() { } setColor(p1) { if (!p1.options.links) { return; } const container = this.linkContainer, linksOptions = p1.options.links; let linkColor = linksOptions.id === undefined ? container.particles.linksColor : container.particles.linksColors.get(linksOptions.id); if (linkColor) { return; } const optColor = linksOptions.color; linkColor = (0, tsparticles_engine_1.getLinkRandomColor)(optColor, linksOptions.blink, linksOptions.consent); if (linksOptions.id === undefined) { container.particles.linksColor = linkColor; } else { container.particles.linksColors.set(linksOptions.id, linkColor); } } } exports.Linker = Linker;