2023-02-17 11:06:01 +01:00

120 lines
4.4 KiB
JavaScript

import { Circle, ParticlesInteractorBase, getDistance, getLinkRandomColor } from "tsparticles-engine";
import { CircleWarp } from "./CircleWarp";
import { Links } from "./Options/Classes/Links";
function getLinkDistance(pos1, pos2, optDistance, canvasSize, warp) {
let distance = getDistance(pos1, pos2);
if (!warp || distance <= optDistance) {
return distance;
}
const pos2NE = {
x: pos2.x - canvasSize.width,
y: pos2.y,
};
distance = getDistance(pos1, pos2NE);
if (distance <= optDistance) {
return distance;
}
const pos2SE = {
x: pos2.x - canvasSize.width,
y: pos2.y - canvasSize.height,
};
distance = getDistance(pos1, pos2SE);
if (distance <= optDistance) {
return distance;
}
const pos2SW = {
x: pos2.x,
y: pos2.y - canvasSize.height,
};
distance = getDistance(pos1, pos2SW);
return distance;
}
export class Linker extends ParticlesInteractorBase {
constructor(container) {
super(container);
this.linkContainer = container;
}
clear() {
}
init() {
this.linkContainer.particles.linksColor = undefined;
this.linkContainer.particles.linksColors = new Map();
}
async interact(p1) {
var _a;
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(pos1.x, pos1.y, optDistance, canvasSize)
: new 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();
}
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 = getLinkRandomColor(optColor, linksOptions.blink, linksOptions.consent);
if (linksOptions.id === undefined) {
container.particles.linksColor = linkColor;
}
else {
container.particles.linksColors.set(linksOptions.id, linkColor);
}
}
}