@tsparticles/interaction-particles-links 3.0.0-alpha.1 → 3.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/README.md +25 -19
  2. package/browser/CircleWarp.js +7 -23
  3. package/browser/LinkInstance.js +121 -114
  4. package/browser/Linker.js +37 -54
  5. package/browser/Options/Classes/Links.js +1 -1
  6. package/browser/Utils.js +24 -8
  7. package/browser/index.js +5 -5
  8. package/browser/interaction.js +2 -2
  9. package/browser/plugin.js +2 -2
  10. package/cjs/CircleWarp.js +7 -23
  11. package/cjs/LinkInstance.js +123 -127
  12. package/cjs/Linker.js +66 -94
  13. package/cjs/Options/Classes/Links.js +1 -1
  14. package/cjs/Utils.js +26 -8
  15. package/cjs/index.js +3 -14
  16. package/cjs/interaction.js +4 -15
  17. package/cjs/plugin.js +5 -16
  18. package/esm/CircleWarp.js +7 -23
  19. package/esm/LinkInstance.js +121 -114
  20. package/esm/Linker.js +37 -54
  21. package/esm/Options/Classes/Links.js +1 -1
  22. package/esm/Utils.js +24 -8
  23. package/esm/index.js +5 -5
  24. package/esm/interaction.js +2 -2
  25. package/esm/plugin.js +2 -2
  26. package/package.json +6 -5
  27. package/report.html +4 -4
  28. package/tsparticles.interaction.particles.links.js +244 -204
  29. package/tsparticles.interaction.particles.links.min.js +1 -1
  30. package/tsparticles.interaction.particles.links.min.js.LICENSE.txt +1 -8
  31. package/types/CircleWarp.d.ts +1 -2
  32. package/types/Interfaces.d.ts +22 -0
  33. package/types/LinkInstance.d.ts +7 -8
  34. package/types/Linker.d.ts +3 -7
  35. package/types/Options/Classes/Links.d.ts +1 -2
  36. package/types/Options/Classes/LinksShadow.d.ts +1 -2
  37. package/types/Options/Classes/LinksTriangle.d.ts +1 -2
  38. package/types/Types.d.ts +47 -0
  39. package/types/Utils.d.ts +5 -4
  40. package/types/index.d.ts +1 -1
  41. package/types/interaction.d.ts +1 -1
  42. package/types/plugin.d.ts +1 -1
  43. package/umd/CircleWarp.js +7 -23
  44. package/umd/LinkInstance.js +121 -114
  45. package/umd/Linker.js +36 -53
  46. package/umd/Options/Classes/Links.js +2 -2
  47. package/umd/Utils.js +26 -8
  48. package/umd/index.js +3 -3
  49. package/umd/interaction.js +4 -4
  50. package/umd/plugin.js +4 -4
  51. package/browser/Options/Interfaces/IParticlesLinkOptions.js +0 -1
  52. package/cjs/LinkParticle.js +0 -2
  53. package/cjs/Options/Classes/ParticlesLinkOptions.js +0 -2
  54. package/cjs/Options/Interfaces/IParticlesLinkOptions.js +0 -2
  55. package/esm/ILink.js +0 -1
  56. package/esm/LinkContainer.js +0 -1
  57. package/esm/LinkParticle.js +0 -1
  58. package/esm/Options/Classes/ParticlesLinkOptions.js +0 -1
  59. package/esm/Options/Interfaces/IParticlesLinkOptions.js +0 -1
  60. package/types/ILink.d.ts +0 -9
  61. package/types/LinkContainer.d.ts +0 -7
  62. package/types/LinkParticle.d.ts +0 -11
  63. package/types/Options/Classes/ParticlesLinkOptions.d.ts +0 -5
  64. package/types/Options/Interfaces/IParticlesLinkOptions.d.ts +0 -7
  65. package/umd/LinkParticle.js +0 -12
  66. package/umd/Options/Classes/ParticlesLinkOptions.js +0 -12
  67. package/umd/Options/Interfaces/IParticlesLinkOptions.js +0 -12
  68. /package/browser/{ILink.js → Interfaces.js} +0 -0
  69. /package/browser/{LinkContainer.js → Types.js} +0 -0
  70. /package/cjs/{ILink.js → Interfaces.js} +0 -0
  71. /package/cjs/{LinkContainer.js → Types.js} +0 -0
  72. /package/{browser/LinkParticle.js → esm/Interfaces.js} +0 -0
  73. /package/{browser/Options/Classes/ParticlesLinkOptions.js → esm/Types.js} +0 -0
  74. /package/umd/{ILink.js → Interfaces.js} +0 -0
  75. /package/umd/{LinkContainer.js → Types.js} +0 -0
@@ -0,0 +1,22 @@
1
+ import type { IRangeColor, RangeValue } from "@tsparticles/engine";
2
+ import type { LinkParticle } from "./Types";
3
+ export interface ILink {
4
+ destination: LinkParticle;
5
+ opacity: number;
6
+ }
7
+ export interface ILinkTriangle {
8
+ opacity: number;
9
+ vertices: LinkParticle[];
10
+ }
11
+ export interface IParticlesFrequencies {
12
+ links: Map<string, number>;
13
+ triangles: Map<string, number>;
14
+ }
15
+ export interface ITwinkle {
16
+ lines: {
17
+ color: IRangeColor;
18
+ enable: boolean;
19
+ frequency: number;
20
+ opacity: RangeValue;
21
+ };
22
+ }
@@ -1,6 +1,5 @@
1
- import type { IContainerPlugin } from "@tsparticles/engine";
2
- import type { LinkContainer } from "./LinkContainer";
3
- import type { LinkParticle } from "./LinkParticle";
1
+ import { type IContainerPlugin } from "@tsparticles/engine";
2
+ import type { LinkContainer, LinkParticle } from "./Types";
4
3
  export declare class LinkInstance implements IContainerPlugin {
5
4
  private readonly container;
6
5
  private readonly _freqs;
@@ -9,9 +8,9 @@ export declare class LinkInstance implements IContainerPlugin {
9
8
  init(): Promise<void>;
10
9
  particleCreated(particle: LinkParticle): void;
11
10
  particleDestroyed(particle: LinkParticle): void;
12
- private drawLinkLine;
13
- private drawLinkTriangle;
14
- private drawTriangles;
15
- private getLinkFrequency;
16
- private getTriangleFrequency;
11
+ private readonly _drawLinkLine;
12
+ private readonly _drawLinkTriangle;
13
+ private readonly _drawTriangles;
14
+ private readonly _getLinkFrequency;
15
+ private readonly _getTriangleFrequency;
17
16
  }
package/types/Linker.d.ts CHANGED
@@ -1,9 +1,5 @@
1
- import { ParticlesInteractorBase } from "@tsparticles/engine";
2
- import type { RecursivePartial } from "@tsparticles/engine";
3
- import type { IParticlesLinkOptions } from "./Options/Interfaces/IParticlesLinkOptions";
4
- import type { LinkContainer } from "./LinkContainer";
5
- import type { LinkParticle } from "./LinkParticle";
6
- import type { ParticlesLinkOptions } from "./Options/Classes/ParticlesLinkOptions";
1
+ import { ParticlesInteractorBase, type RecursivePartial } from "@tsparticles/engine";
2
+ import type { IParticlesLinkOptions, LinkContainer, LinkParticle, ParticlesLinkOptions } from "./Types";
7
3
  export declare class Linker extends ParticlesInteractorBase {
8
4
  linkContainer: LinkContainer;
9
5
  constructor(container: LinkContainer);
@@ -13,5 +9,5 @@ export declare class Linker extends ParticlesInteractorBase {
13
9
  isEnabled(particle: LinkParticle): boolean;
14
10
  loadParticlesOptions(options: ParticlesLinkOptions, ...sources: (RecursivePartial<IParticlesLinkOptions> | undefined)[]): void;
15
11
  reset(): void;
16
- private setColor;
12
+ private readonly _setColor;
17
13
  }
@@ -1,8 +1,7 @@
1
- import type { IOptionLoader, RecursivePartial } from "@tsparticles/engine";
1
+ import { type IOptionLoader, OptionsColor, type RecursivePartial } from "@tsparticles/engine";
2
2
  import type { ILinks } from "../Interfaces/ILinks";
3
3
  import { LinksShadow } from "./LinksShadow";
4
4
  import { LinksTriangle } from "./LinksTriangle";
5
- import { OptionsColor } from "@tsparticles/engine";
6
5
  export declare class Links implements ILinks, IOptionLoader<ILinks> {
7
6
  blink: boolean;
8
7
  color: OptionsColor;
@@ -1,6 +1,5 @@
1
- import type { IOptionLoader, RecursivePartial } from "@tsparticles/engine";
1
+ import { type IOptionLoader, OptionsColor, type RecursivePartial } from "@tsparticles/engine";
2
2
  import type { ILinksShadow } from "../Interfaces/ILinksShadow";
3
- import { OptionsColor } from "@tsparticles/engine";
4
3
  export declare class LinksShadow implements ILinksShadow, IOptionLoader<ILinksShadow> {
5
4
  blur: number;
6
5
  color: OptionsColor;
@@ -1,6 +1,5 @@
1
- import type { IOptionLoader, RecursivePartial } from "@tsparticles/engine";
1
+ import { type IOptionLoader, OptionsColor, type RecursivePartial } from "@tsparticles/engine";
2
2
  import type { ILinksTriangle } from "../Interfaces/ILinksTriangle";
3
- import { OptionsColor } from "@tsparticles/engine";
4
3
  export declare class LinksTriangle implements ILinksTriangle, IOptionLoader<ILinksTriangle> {
5
4
  color?: OptionsColor;
6
5
  enable: boolean;
@@ -0,0 +1,47 @@
1
+ import type { BackgroundMask, Container, ICoordinates, IDimension, IParticlesOptions, IRgb, Particle, ParticlesOptions } from "@tsparticles/engine";
2
+ import type { ILink } from "./Interfaces";
3
+ import type { ILinks } from "./Options/Interfaces/ILinks";
4
+ import type { Links } from "./Options/Classes/Links";
5
+ export type LinkContainer = Container & {
6
+ particles: {
7
+ linksColor?: IRgb | string;
8
+ linksColors: Map<string, IRgb | string | undefined>;
9
+ };
10
+ };
11
+ export type LinkParticle = Particle & {
12
+ links?: ILink[];
13
+ options: ParticlesLinkOptions;
14
+ retina: {
15
+ linksDistance?: number;
16
+ linksWidth?: number;
17
+ };
18
+ };
19
+ export type LinkLineDrawParams = {
20
+ backgroundMask: BackgroundMask;
21
+ begin: ICoordinates;
22
+ canvasSize: IDimension;
23
+ colorLine: IRgb;
24
+ context: CanvasRenderingContext2D;
25
+ end: ICoordinates;
26
+ links: Links;
27
+ maxDistance: number;
28
+ opacity: number;
29
+ width: number;
30
+ };
31
+ export type LinkTriangleDrawParams = {
32
+ backgroundMask: BackgroundMask;
33
+ colorTriangle: IRgb;
34
+ context: CanvasRenderingContext2D;
35
+ opacityTriangle: number;
36
+ pos1: ICoordinates;
37
+ pos2: ICoordinates;
38
+ pos3: ICoordinates;
39
+ };
40
+ export type IParticlesLinkOptions = IParticlesOptions & {
41
+ lineLinked?: ILinks;
42
+ line_linked?: ILinks;
43
+ links?: ILinks;
44
+ };
45
+ export type ParticlesLinkOptions = ParticlesOptions & {
46
+ links?: Links;
47
+ };
package/types/Utils.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import type { ICoordinates, IDimension, IRgb } from "@tsparticles/engine";
2
- import type { ILinksShadow } from "./Options/Interfaces/ILinksShadow";
3
- export declare function drawLinkLine(context: CanvasRenderingContext2D, width: number, begin: ICoordinates, end: ICoordinates, maxDistance: number, canvasSize: IDimension, warp: boolean, backgroundMask: boolean, composite: GlobalCompositeOperation, colorLine: IRgb, opacity: number, shadow: ILinksShadow): void;
4
- export declare function drawLinkTriangle(context: CanvasRenderingContext2D, pos1: ICoordinates, pos2: ICoordinates, pos3: ICoordinates, backgroundMask: boolean, composite: GlobalCompositeOperation, colorTriangle: IRgb, opacityTriangle: number): void;
1
+ import type { LinkLineDrawParams, LinkParticle, LinkTriangleDrawParams } from "./Types";
2
+ export declare function drawLinkLine(params: LinkLineDrawParams): void;
3
+ export declare function drawLinkTriangle(params: LinkTriangleDrawParams): void;
4
+ export declare function getLinkKey(ids: number[]): string;
5
+ export declare function setLinkFrequency(particles: LinkParticle[], dictionary: Map<string, number>): number;
package/types/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Engine } from "@tsparticles/engine";
2
- export declare function loadParticlesLinksInteraction(engine: Engine): Promise<void>;
2
+ export declare function loadParticlesLinksInteraction(engine: Engine, refresh?: boolean): Promise<void>;
3
3
  export * from "./Options/Classes/Links";
4
4
  export * from "./Options/Classes/LinksShadow";
5
5
  export * from "./Options/Classes/LinksTriangle";
@@ -1,2 +1,2 @@
1
1
  import type { Engine } from "@tsparticles/engine";
2
- export declare function loadInteraction(engine: Engine): Promise<void>;
2
+ export declare function loadLinksInteraction(engine: Engine, refresh?: boolean): Promise<void>;
package/types/plugin.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  import type { Engine } from "@tsparticles/engine";
2
- export declare function loadPlugin(engine: Engine): Promise<void>;
2
+ export declare function loadLinksPlugin(engine: Engine, refresh?: boolean): Promise<void>;
package/umd/CircleWarp.js CHANGED
@@ -15,31 +15,15 @@
15
15
  constructor(x, y, radius, canvasSize) {
16
16
  super(x, y, radius);
17
17
  this.canvasSize = canvasSize;
18
- this.canvasSize = Object.assign({}, canvasSize);
18
+ this.canvasSize = { ...canvasSize };
19
19
  }
20
20
  contains(point) {
21
- if (super.contains(point)) {
22
- return true;
23
- }
24
- const posNE = {
25
- x: point.x - this.canvasSize.width,
26
- y: point.y,
27
- };
28
- if (super.contains(posNE)) {
29
- return true;
30
- }
31
- const posSE = {
32
- x: point.x - this.canvasSize.width,
33
- y: point.y - this.canvasSize.height,
34
- };
35
- if (super.contains(posSE)) {
36
- return true;
37
- }
38
- const posSW = {
39
- x: point.x,
40
- y: point.y - this.canvasSize.height,
41
- };
42
- return super.contains(posSW);
21
+ const { width, height } = this.canvasSize;
22
+ const { x, y } = point;
23
+ return (super.contains(point) ||
24
+ super.contains({ x: x - width, y }) ||
25
+ super.contains({ x: x - width, y: y - height }) ||
26
+ super.contains({ x, y: y - height }));
43
27
  }
44
28
  intersects(range) {
45
29
  if (super.intersects(range)) {
@@ -4,46 +4,142 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./Utils", "@tsparticles/engine"], factory);
7
+ define(["require", "exports", "@tsparticles/engine", "./Utils"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.LinkInstance = void 0;
13
- const Utils_1 = require("./Utils");
14
13
  const engine_1 = require("@tsparticles/engine");
15
- function getLinkKey(ids) {
16
- ids.sort((a, b) => a - b);
17
- return ids.join("_");
18
- }
19
- function setLinkFrequency(particles, dictionary) {
20
- const key = getLinkKey(particles.map((t) => t.id));
21
- let res = dictionary.get(key);
22
- if (res === undefined) {
23
- res = (0, engine_1.getRandom)();
24
- dictionary.set(key, res);
25
- }
26
- return res;
27
- }
14
+ const Utils_1 = require("./Utils");
28
15
  class LinkInstance {
29
16
  constructor(container) {
30
17
  this.container = container;
18
+ this._drawLinkLine = (p1, link) => {
19
+ const p1LinksOptions = p1.options.links;
20
+ if (!p1LinksOptions?.enable) {
21
+ return;
22
+ }
23
+ const container = this.container, options = container.actualOptions, p2 = link.destination, pos1 = p1.getPosition(), pos2 = p2.getPosition();
24
+ let opacity = link.opacity;
25
+ container.canvas.draw((ctx) => {
26
+ let colorLine;
27
+ const twinkle = p1.options.twinkle?.lines;
28
+ if (twinkle?.enable) {
29
+ const twinkleFreq = twinkle.frequency, twinkleRgb = (0, engine_1.rangeColorToRgb)(twinkle.color), twinkling = (0, engine_1.getRandom)() < twinkleFreq;
30
+ if (twinkling && twinkleRgb) {
31
+ colorLine = twinkleRgb;
32
+ opacity = (0, engine_1.getRangeValue)(twinkle.opacity);
33
+ }
34
+ }
35
+ if (!colorLine) {
36
+ const linkColor = p1LinksOptions.id !== undefined
37
+ ? container.particles.linksColors.get(p1LinksOptions.id)
38
+ : container.particles.linksColor;
39
+ colorLine = (0, engine_1.getLinkColor)(p1, p2, linkColor);
40
+ }
41
+ if (!colorLine) {
42
+ return;
43
+ }
44
+ const width = p1.retina.linksWidth ?? 0, maxDistance = p1.retina.linksDistance ?? 0, { backgroundMask } = options;
45
+ (0, Utils_1.drawLinkLine)({
46
+ context: ctx,
47
+ width,
48
+ begin: pos1,
49
+ end: pos2,
50
+ maxDistance,
51
+ canvasSize: container.canvas.size,
52
+ links: p1LinksOptions,
53
+ backgroundMask: backgroundMask,
54
+ colorLine,
55
+ opacity,
56
+ });
57
+ });
58
+ };
59
+ this._drawLinkTriangle = (p1, link1, link2) => {
60
+ const linksOptions = p1.options.links;
61
+ if (!linksOptions?.enable) {
62
+ return;
63
+ }
64
+ const triangleOptions = linksOptions.triangles;
65
+ if (!triangleOptions.enable) {
66
+ return;
67
+ }
68
+ const container = this.container, options = container.actualOptions, p2 = link1.destination, p3 = link2.destination, opacityTriangle = triangleOptions.opacity ?? (link1.opacity + link2.opacity) / 2;
69
+ if (opacityTriangle <= 0) {
70
+ return;
71
+ }
72
+ container.canvas.draw((ctx) => {
73
+ const pos1 = p1.getPosition(), pos2 = p2.getPosition(), pos3 = p3.getPosition(), linksDistance = p1.retina.linksDistance ?? 0;
74
+ if ((0, engine_1.getDistance)(pos1, pos2) > linksDistance ||
75
+ (0, engine_1.getDistance)(pos3, pos2) > linksDistance ||
76
+ (0, engine_1.getDistance)(pos3, pos1) > linksDistance) {
77
+ return;
78
+ }
79
+ let colorTriangle = (0, engine_1.rangeColorToRgb)(triangleOptions.color);
80
+ if (!colorTriangle) {
81
+ const linkColor = linksOptions.id !== undefined
82
+ ? container.particles.linksColors.get(linksOptions.id)
83
+ : container.particles.linksColor;
84
+ colorTriangle = (0, engine_1.getLinkColor)(p1, p2, linkColor);
85
+ }
86
+ if (!colorTriangle) {
87
+ return;
88
+ }
89
+ (0, Utils_1.drawLinkTriangle)({
90
+ context: ctx,
91
+ pos1,
92
+ pos2,
93
+ pos3,
94
+ backgroundMask: options.backgroundMask,
95
+ colorTriangle,
96
+ opacityTriangle,
97
+ });
98
+ });
99
+ };
100
+ this._drawTriangles = (options, p1, link, p1Links) => {
101
+ const p2 = link.destination;
102
+ if (!(options.links?.triangles.enable && p2.options.links?.triangles.enable)) {
103
+ return;
104
+ }
105
+ const vertices = p2.links?.filter((t) => {
106
+ const linkFreq = this._getLinkFrequency(p2, t.destination);
107
+ return (p2.options.links &&
108
+ linkFreq <= p2.options.links.frequency &&
109
+ p1Links.findIndex((l) => l.destination === t.destination) >= 0);
110
+ });
111
+ if (!vertices?.length) {
112
+ return;
113
+ }
114
+ for (const vertex of vertices) {
115
+ const p3 = vertex.destination, triangleFreq = this._getTriangleFrequency(p1, p2, p3);
116
+ if (triangleFreq > options.links.triangles.frequency) {
117
+ continue;
118
+ }
119
+ this._drawLinkTriangle(p1, link, vertex);
120
+ }
121
+ };
122
+ this._getLinkFrequency = (p1, p2) => {
123
+ return (0, Utils_1.setLinkFrequency)([p1, p2], this._freqs.links);
124
+ };
125
+ this._getTriangleFrequency = (p1, p2, p3) => {
126
+ return (0, Utils_1.setLinkFrequency)([p1, p2, p3], this._freqs.triangles);
127
+ };
31
128
  this._freqs = {
32
129
  links: new Map(),
33
130
  triangles: new Map(),
34
131
  };
35
132
  }
36
133
  drawParticle(context, particle) {
37
- var _a;
38
- const pOptions = particle.options;
39
- if (!particle.links || particle.links.length <= 0) {
134
+ const { links, options } = particle;
135
+ if (!links || links.length <= 0) {
40
136
  return;
41
137
  }
42
- const p1Links = particle.links.filter((l) => pOptions.links && this.getLinkFrequency(particle, l.destination) <= pOptions.links.frequency);
138
+ const p1Links = links.filter((l) => options.links && this._getLinkFrequency(particle, l.destination) <= options.links.frequency);
43
139
  for (const link of p1Links) {
44
- this.drawTriangles(pOptions, particle, link, p1Links);
45
- if (link.opacity > 0 && ((_a = particle.retina.linksWidth) !== null && _a !== void 0 ? _a : 0) > 0) {
46
- this.drawLinkLine(particle, link);
140
+ this._drawTriangles(options, particle, link, p1Links);
141
+ if (link.opacity > 0 && (particle.retina.linksWidth ?? 0) > 0) {
142
+ this._drawLinkLine(particle, link);
47
143
  }
48
144
  }
49
145
  }
@@ -56,102 +152,13 @@
56
152
  if (!particle.options.links) {
57
153
  return;
58
154
  }
59
- const ratio = this.container.retina.pixelRatio;
60
- particle.retina.linksDistance = particle.options.links.distance * ratio;
61
- particle.retina.linksWidth = particle.options.links.width * ratio;
155
+ const ratio = this.container.retina.pixelRatio, { retina } = particle, { distance, width } = particle.options.links;
156
+ retina.linksDistance = distance * ratio;
157
+ retina.linksWidth = width * ratio;
62
158
  }
63
159
  particleDestroyed(particle) {
64
160
  particle.links = [];
65
161
  }
66
- drawLinkLine(p1, link) {
67
- const container = this.container, options = container.actualOptions, p2 = link.destination, pos1 = p1.getPosition(), pos2 = p2.getPosition();
68
- let opacity = link.opacity;
69
- container.canvas.draw((ctx) => {
70
- var _a, _b, _c;
71
- if (!p1.options.links) {
72
- return;
73
- }
74
- let colorLine;
75
- const twinkle = (_a = p1.options.twinkle) === null || _a === void 0 ? void 0 : _a.lines;
76
- if (twinkle === null || twinkle === void 0 ? void 0 : twinkle.enable) {
77
- const twinkleFreq = twinkle.frequency, twinkleRgb = (0, engine_1.rangeColorToRgb)(twinkle.color), twinkling = (0, engine_1.getRandom)() < twinkleFreq;
78
- if (twinkling && twinkleRgb) {
79
- colorLine = twinkleRgb;
80
- opacity = (0, engine_1.getRangeValue)(twinkle.opacity);
81
- }
82
- }
83
- if (!colorLine) {
84
- const linksOptions = p1.options.links, linkColor = (linksOptions === null || linksOptions === void 0 ? void 0 : linksOptions.id) !== undefined
85
- ? container.particles.linksColors.get(linksOptions.id)
86
- : container.particles.linksColor;
87
- colorLine = (0, engine_1.getLinkColor)(p1, p2, linkColor);
88
- }
89
- if (!colorLine) {
90
- return;
91
- }
92
- const width = (_b = p1.retina.linksWidth) !== null && _b !== void 0 ? _b : 0, maxDistance = (_c = p1.retina.linksDistance) !== null && _c !== void 0 ? _c : 0;
93
- (0, Utils_1.drawLinkLine)(ctx, width, pos1, pos2, maxDistance, container.canvas.size, p1.options.links.warp, options.backgroundMask.enable, options.backgroundMask.composite, colorLine, opacity, p1.options.links.shadow);
94
- });
95
- }
96
- drawLinkTriangle(p1, link1, link2) {
97
- var _a;
98
- if (!p1.options.links) {
99
- return;
100
- }
101
- const container = this.container, options = container.actualOptions, p2 = link1.destination, p3 = link2.destination, triangleOptions = p1.options.links.triangles, opacityTriangle = (_a = triangleOptions.opacity) !== null && _a !== void 0 ? _a : (link1.opacity + link2.opacity) / 2;
102
- if (opacityTriangle <= 0) {
103
- return;
104
- }
105
- container.canvas.draw((ctx) => {
106
- var _a;
107
- const pos1 = p1.getPosition(), pos2 = p2.getPosition(), pos3 = p3.getPosition(), linksDistance = (_a = p1.retina.linksDistance) !== null && _a !== void 0 ? _a : 0;
108
- if ((0, engine_1.getDistance)(pos1, pos2) > linksDistance ||
109
- (0, engine_1.getDistance)(pos3, pos2) > linksDistance ||
110
- (0, engine_1.getDistance)(pos3, pos1) > linksDistance) {
111
- return;
112
- }
113
- let colorTriangle = (0, engine_1.rangeColorToRgb)(triangleOptions.color);
114
- if (!colorTriangle) {
115
- const linksOptions = p1.options.links, linkColor = (linksOptions === null || linksOptions === void 0 ? void 0 : linksOptions.id) !== undefined
116
- ? container.particles.linksColors.get(linksOptions.id)
117
- : container.particles.linksColor;
118
- colorTriangle = (0, engine_1.getLinkColor)(p1, p2, linkColor);
119
- }
120
- if (!colorTriangle) {
121
- return;
122
- }
123
- (0, Utils_1.drawLinkTriangle)(ctx, pos1, pos2, pos3, options.backgroundMask.enable, options.backgroundMask.composite, colorTriangle, opacityTriangle);
124
- });
125
- }
126
- drawTriangles(options, p1, link, p1Links) {
127
- var _a, _b, _c;
128
- const p2 = link.destination;
129
- if (!(((_a = options.links) === null || _a === void 0 ? void 0 : _a.triangles.enable) && ((_b = p2.options.links) === null || _b === void 0 ? void 0 : _b.triangles.enable))) {
130
- return;
131
- }
132
- const vertices = (_c = p2.links) === null || _c === void 0 ? void 0 : _c.filter((t) => {
133
- const linkFreq = this.getLinkFrequency(p2, t.destination);
134
- return (p2.options.links &&
135
- linkFreq <= p2.options.links.frequency &&
136
- p1Links.findIndex((l) => l.destination === t.destination) >= 0);
137
- });
138
- if (!(vertices === null || vertices === void 0 ? void 0 : vertices.length)) {
139
- return;
140
- }
141
- for (const vertex of vertices) {
142
- const p3 = vertex.destination, triangleFreq = this.getTriangleFrequency(p1, p2, p3);
143
- if (triangleFreq > options.links.triangles.frequency) {
144
- continue;
145
- }
146
- this.drawLinkTriangle(p1, link, vertex);
147
- }
148
- }
149
- getLinkFrequency(p1, p2) {
150
- return setLinkFrequency([p1, p2], this._freqs.links);
151
- }
152
- getTriangleFrequency(p1, p2, p3) {
153
- return setLinkFrequency([p1, p2, p3], this._freqs.triangles);
154
- }
155
162
  }
156
163
  exports.LinkInstance = LinkInstance;
157
164
  });
package/umd/Linker.js CHANGED
@@ -14,36 +14,42 @@
14
14
  const CircleWarp_1 = require("./CircleWarp");
15
15
  const Links_1 = require("./Options/Classes/Links");
16
16
  function getLinkDistance(pos1, pos2, optDistance, canvasSize, warp) {
17
- let distance = (0, engine_1.getDistance)(pos1, pos2);
17
+ const { dx, dy, distance } = (0, engine_1.getDistances)(pos1, pos2);
18
18
  if (!warp || distance <= optDistance) {
19
19
  return distance;
20
20
  }
21
- const pos2NE = {
22
- x: pos2.x - canvasSize.width,
23
- y: pos2.y,
21
+ const absDiffs = {
22
+ x: Math.abs(dx),
23
+ y: Math.abs(dy),
24
+ }, warpDistances = {
25
+ x: Math.min(absDiffs.x, canvasSize.width - absDiffs.x),
26
+ y: Math.min(absDiffs.y, canvasSize.height - absDiffs.y),
24
27
  };
25
- distance = (0, engine_1.getDistance)(pos1, pos2NE);
26
- if (distance <= optDistance) {
27
- return distance;
28
- }
29
- const pos2SE = {
30
- x: pos2.x - canvasSize.width,
31
- y: pos2.y - canvasSize.height,
32
- };
33
- distance = (0, engine_1.getDistance)(pos1, pos2SE);
34
- if (distance <= optDistance) {
35
- return distance;
36
- }
37
- const pos2SW = {
38
- x: pos2.x,
39
- y: pos2.y - canvasSize.height,
40
- };
41
- distance = (0, engine_1.getDistance)(pos1, pos2SW);
42
- return distance;
28
+ return Math.sqrt(warpDistances.x ** 2 + warpDistances.y ** 2);
43
29
  }
44
30
  class Linker extends engine_1.ParticlesInteractorBase {
45
31
  constructor(container) {
46
32
  super(container);
33
+ this._setColor = (p1) => {
34
+ if (!p1.options.links) {
35
+ return;
36
+ }
37
+ const container = this.linkContainer, linksOptions = p1.options.links;
38
+ let linkColor = linksOptions.id === undefined
39
+ ? container.particles.linksColor
40
+ : container.particles.linksColors.get(linksOptions.id);
41
+ if (linkColor) {
42
+ return;
43
+ }
44
+ const optColor = linksOptions.color;
45
+ linkColor = (0, engine_1.getLinkRandomColor)(optColor, linksOptions.blink, linksOptions.consent);
46
+ if (linksOptions.id === undefined) {
47
+ container.particles.linksColor = linkColor;
48
+ }
49
+ else {
50
+ container.particles.linksColors.set(linksOptions.id, linkColor);
51
+ }
52
+ };
47
53
  this.linkContainer = container;
48
54
  }
49
55
  clear() {
@@ -53,7 +59,6 @@
53
59
  this.linkContainer.particles.linksColors = new Map();
54
60
  }
55
61
  async interact(p1) {
56
- var _a;
57
62
  if (!p1.options.links) {
58
63
  return;
59
64
  }
@@ -62,19 +67,19 @@
62
67
  if (pos1.x < 0 || pos1.y < 0 || pos1.x > canvasSize.width || pos1.y > canvasSize.height) {
63
68
  return;
64
69
  }
65
- const linkOpt1 = p1.options.links, optOpacity = linkOpt1.opacity, optDistance = (_a = p1.retina.linksDistance) !== null && _a !== void 0 ? _a : 0, warp = linkOpt1.warp, range = warp
70
+ const linkOpt1 = p1.options.links, optOpacity = linkOpt1.opacity, optDistance = p1.retina.linksDistance ?? 0, warp = linkOpt1.warp, range = warp
66
71
  ? new CircleWarp_1.CircleWarp(pos1.x, pos1.y, optDistance, canvasSize)
67
72
  : new engine_1.Circle(pos1.x, pos1.y, optDistance), query = container.particles.quadTree.query(range);
68
73
  for (const p2 of query) {
69
74
  const linkOpt2 = p2.options.links;
70
75
  if (p1 === p2 ||
71
- !(linkOpt2 === null || linkOpt2 === void 0 ? void 0 : linkOpt2.enable) ||
76
+ !linkOpt2?.enable ||
72
77
  linkOpt1.id !== linkOpt2.id ||
73
78
  p2.spawning ||
74
79
  p2.destroyed ||
75
80
  !p2.links ||
76
- p1.links.map((t) => t.destination).indexOf(p2) !== -1 ||
77
- p2.links.map((t) => t.destination).indexOf(p1) !== -1) {
81
+ p1.links.some((t) => t.destination === p2) ||
82
+ p2.links.some((t) => t.destination === p1)) {
78
83
  continue;
79
84
  }
80
85
  const pos2 = p2.getPosition();
@@ -83,10 +88,10 @@
83
88
  }
84
89
  const distance = getLinkDistance(pos1, pos2, optDistance, canvasSize, warp && linkOpt2.warp);
85
90
  if (distance > optDistance) {
86
- return;
91
+ continue;
87
92
  }
88
93
  const opacityLine = (1 - distance / optDistance) * optOpacity;
89
- this.setColor(p1);
94
+ this._setColor(p1);
90
95
  p1.links.push({
91
96
  destination: p2,
92
97
  opacity: opacityLine,
@@ -94,40 +99,18 @@
94
99
  }
95
100
  }
96
101
  isEnabled(particle) {
97
- var _a;
98
- return !!((_a = particle.options.links) === null || _a === void 0 ? void 0 : _a.enable);
102
+ return !!particle.options.links?.enable;
99
103
  }
100
104
  loadParticlesOptions(options, ...sources) {
101
- var _a, _b;
102
105
  if (!options.links) {
103
106
  options.links = new Links_1.Links();
104
107
  }
105
108
  for (const source of sources) {
106
- 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);
109
+ options.links.load(source?.links ?? source?.lineLinked ?? source?.line_linked);
107
110
  }
108
111
  }
109
112
  reset() {
110
113
  }
111
- setColor(p1) {
112
- if (!p1.options.links) {
113
- return;
114
- }
115
- const container = this.linkContainer, linksOptions = p1.options.links;
116
- let linkColor = linksOptions.id === undefined
117
- ? container.particles.linksColor
118
- : container.particles.linksColors.get(linksOptions.id);
119
- if (linkColor) {
120
- return;
121
- }
122
- const optColor = linksOptions.color;
123
- linkColor = (0, engine_1.getLinkRandomColor)(optColor, linksOptions.blink, linksOptions.consent);
124
- if (linksOptions.id === undefined) {
125
- container.particles.linksColor = linkColor;
126
- }
127
- else {
128
- container.particles.linksColors.set(linksOptions.id, linkColor);
129
- }
130
- }
131
114
  }
132
115
  exports.Linker = Linker;
133
116
  });
@@ -4,15 +4,15 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./LinksShadow", "./LinksTriangle", "@tsparticles/engine"], factory);
7
+ define(["require", "exports", "@tsparticles/engine", "./LinksShadow", "./LinksTriangle"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Links = void 0;
13
+ const engine_1 = require("@tsparticles/engine");
13
14
  const LinksShadow_1 = require("./LinksShadow");
14
15
  const LinksTriangle_1 = require("./LinksTriangle");
15
- const engine_1 = require("@tsparticles/engine");
16
16
  class Links {
17
17
  constructor() {
18
18
  this.blink = false;