@tsparticles/interaction-external-connect 3.0.0-alpha.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 (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +70 -0
  3. package/browser/Connector.js +95 -0
  4. package/browser/Options/Classes/Connect.js +33 -0
  5. package/browser/Options/Classes/ConnectLinks.js +13 -0
  6. package/browser/Options/Classes/ConnectOptions.js +1 -0
  7. package/browser/Options/Interfaces/IConnect.js +1 -0
  8. package/browser/Options/Interfaces/IConnectLinks.js +1 -0
  9. package/browser/Types.js +1 -0
  10. package/browser/index.js +8 -0
  11. package/cjs/Connector.js +110 -0
  12. package/cjs/Options/Classes/Connect.js +37 -0
  13. package/cjs/Options/Classes/ConnectLinks.js +17 -0
  14. package/cjs/Options/Classes/ConnectOptions.js +2 -0
  15. package/cjs/Options/Interfaces/IConnect.js +2 -0
  16. package/cjs/Options/Interfaces/IConnectLinks.js +2 -0
  17. package/cjs/Types.js +2 -0
  18. package/cjs/index.js +37 -0
  19. package/esm/Connector.js +95 -0
  20. package/esm/Options/Classes/Connect.js +33 -0
  21. package/esm/Options/Classes/ConnectLinks.js +13 -0
  22. package/esm/Options/Classes/ConnectOptions.js +1 -0
  23. package/esm/Options/Interfaces/IConnect.js +1 -0
  24. package/esm/Options/Interfaces/IConnectLinks.js +1 -0
  25. package/esm/Types.js +1 -0
  26. package/esm/index.js +8 -0
  27. package/package.json +82 -0
  28. package/report.html +39 -0
  29. package/tsparticles.interaction.external.connect.js +268 -0
  30. package/tsparticles.interaction.external.connect.min.js +2 -0
  31. package/tsparticles.interaction.external.connect.min.js.LICENSE.txt +8 -0
  32. package/types/Connector.d.ts +12 -0
  33. package/types/Options/Classes/Connect.d.ts +14 -0
  34. package/types/Options/Classes/ConnectLinks.d.ts +7 -0
  35. package/types/Options/Classes/ConnectOptions.d.ts +7 -0
  36. package/types/Options/Interfaces/IConnect.d.ts +8 -0
  37. package/types/Options/Interfaces/IConnectLinks.d.ts +3 -0
  38. package/types/Types.d.ts +17 -0
  39. package/types/index.d.ts +6 -0
  40. package/umd/Connector.js +109 -0
  41. package/umd/Options/Classes/Connect.js +47 -0
  42. package/umd/Options/Classes/ConnectLinks.js +27 -0
  43. package/umd/Options/Classes/ConnectOptions.js +12 -0
  44. package/umd/Options/Interfaces/IConnect.js +12 -0
  45. package/umd/Options/Interfaces/IConnectLinks.js +12 -0
  46. package/umd/Types.js +12 -0
  47. package/umd/index.js +36 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Matteo Bruni
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ [![banner](https://particles.js.org/images/banner2.png)](https://particles.js.org)
2
+
3
+ # tsParticles External Connect Interaction
4
+
5
+ [![jsDelivr](https://data.jsdelivr.com/v1/package/npm/tsparticles-interaction-external-connect/badge)](https://www.jsdelivr.com/package/npm/tsparticles-interaction-external-connect)
6
+ [![npmjs](https://badge.fury.io/js/tsparticles-interaction-external-connect.svg)](https://www.npmjs.com/package/tsparticles-interaction-external-connect)
7
+ [![npmjs](https://img.shields.io/npm/dt/tsparticles-interaction-external-connect)](https://www.npmjs.com/package/tsparticles-interaction-external-connect) [![GitHub Sponsors](https://img.shields.io/github/sponsors/matteobruni)](https://github.com/sponsors/matteobruni)
8
+
9
+ [tsParticles](https://github.com/matteobruni/tsparticles) interaction plugin for connect effect around mouse or HTML
10
+ elements.
11
+
12
+ ## How to use it
13
+
14
+ ### CDN / Vanilla JS / jQuery
15
+
16
+ The CDN/Vanilla version JS has one required file in vanilla configuration:
17
+
18
+ Including the `tsparticles.interaction.external.connect.min.js` file will export the function to load the interaction
19
+ plugin:
20
+
21
+ ```javascript
22
+ loadExternalConnectInteraction;
23
+ ```
24
+
25
+ ### Usage
26
+
27
+ Once the scripts are loaded you can set up `tsParticles` and the interaction plugin like this:
28
+
29
+ ```javascript
30
+ loadExternalConnectInteraction(tsParticles);
31
+
32
+ tsParticles.load({
33
+ id: "tsparticles",
34
+ options: {
35
+ /* options */
36
+ },
37
+ });
38
+ ```
39
+
40
+ ### ESM / CommonJS
41
+
42
+ This package is compatible also with ES or CommonJS modules, firstly this needs to be installed, like this:
43
+
44
+ ```shell
45
+ $ npm install tsparticles-interaction-external-connect
46
+ ```
47
+
48
+ or
49
+
50
+ ```shell
51
+ $ yarn add tsparticles-interaction-external-connect
52
+ ```
53
+
54
+ Then you need to import it in the app, like this:
55
+
56
+ ```javascript
57
+ const { tsParticles } = require("tsparticles-engine");
58
+ const { loadExternalConnectInteraction } = require("tsparticles-interaction-external-connect");
59
+
60
+ loadExternalConnectInteraction(tsParticles);
61
+ ```
62
+
63
+ or
64
+
65
+ ```javascript
66
+ import { tsParticles } from "tsparticles-engine";
67
+ import { loadExternalConnectInteraction } from "tsparticles-interaction-external-connect";
68
+
69
+ loadExternalConnectInteraction(tsParticles);
70
+ ```
@@ -0,0 +1,95 @@
1
+ import { ExternalInteractorBase, colorMix, drawLine, getStyleFromHsl, getStyleFromRgb, isInArray, } from "@tsparticles/engine";
2
+ import { Connect } from "./Options/Classes/Connect";
3
+ function gradient(context, p1, p2, opacity) {
4
+ const gradStop = Math.floor(p2.getRadius() / p1.getRadius()), color1 = p1.getFillColor(), color2 = p2.getFillColor();
5
+ if (!color1 || !color2) {
6
+ return;
7
+ }
8
+ const sourcePos = p1.getPosition(), destPos = p2.getPosition(), midRgb = colorMix(color1, color2, p1.getRadius(), p2.getRadius()), grad = context.createLinearGradient(sourcePos.x, sourcePos.y, destPos.x, destPos.y);
9
+ grad.addColorStop(0, getStyleFromHsl(color1, opacity));
10
+ grad.addColorStop(gradStop > 1 ? 1 : gradStop, getStyleFromRgb(midRgb, opacity));
11
+ grad.addColorStop(1, getStyleFromHsl(color2, opacity));
12
+ return grad;
13
+ }
14
+ function drawConnectLine(context, width, lineStyle, begin, end) {
15
+ drawLine(context, begin, end);
16
+ context.lineWidth = width;
17
+ context.strokeStyle = lineStyle;
18
+ context.stroke();
19
+ }
20
+ function lineStyle(container, ctx, p1, p2) {
21
+ const options = container.actualOptions, connectOptions = options.interactivity.modes.connect;
22
+ if (!connectOptions) {
23
+ return;
24
+ }
25
+ return gradient(ctx, p1, p2, connectOptions.links.opacity);
26
+ }
27
+ function drawConnection(container, p1, p2) {
28
+ container.canvas.draw((ctx) => {
29
+ var _a;
30
+ const ls = lineStyle(container, ctx, p1, p2);
31
+ if (!ls) {
32
+ return;
33
+ }
34
+ const pos1 = p1.getPosition(), pos2 = p2.getPosition();
35
+ drawConnectLine(ctx, (_a = p1.retina.linksWidth) !== null && _a !== void 0 ? _a : 0, ls, pos1, pos2);
36
+ });
37
+ }
38
+ export class Connector extends ExternalInteractorBase {
39
+ constructor(container) {
40
+ super(container);
41
+ }
42
+ clear() {
43
+ }
44
+ init() {
45
+ const container = this.container, connect = container.actualOptions.interactivity.modes.connect;
46
+ if (!connect) {
47
+ return;
48
+ }
49
+ container.retina.connectModeDistance = connect.distance * container.retina.pixelRatio;
50
+ container.retina.connectModeRadius = connect.radius * container.retina.pixelRatio;
51
+ }
52
+ async interact() {
53
+ const container = this.container, options = container.actualOptions;
54
+ if (options.interactivity.events.onHover.enable && container.interactivity.status === "pointermove") {
55
+ const mousePos = container.interactivity.mouse.position;
56
+ if (!container.retina.connectModeDistance ||
57
+ container.retina.connectModeDistance < 0 ||
58
+ !container.retina.connectModeRadius ||
59
+ container.retina.connectModeRadius < 0 ||
60
+ !mousePos) {
61
+ return;
62
+ }
63
+ const distance = Math.abs(container.retina.connectModeRadius), query = container.particles.quadTree.queryCircle(mousePos, distance, (p) => this.isEnabled(p));
64
+ let i = 0;
65
+ for (const p1 of query) {
66
+ const pos1 = p1.getPosition();
67
+ for (const p2 of query.slice(i + 1)) {
68
+ const pos2 = p2.getPosition(), distMax = Math.abs(container.retina.connectModeDistance), xDiff = Math.abs(pos1.x - pos2.x), yDiff = Math.abs(pos1.y - pos2.y);
69
+ if (xDiff < distMax && yDiff < distMax) {
70
+ drawConnection(container, p1, p2);
71
+ }
72
+ }
73
+ ++i;
74
+ }
75
+ }
76
+ }
77
+ isEnabled(particle) {
78
+ var _a;
79
+ const container = this.container, mouse = container.interactivity.mouse, events = ((_a = particle === null || particle === void 0 ? void 0 : particle.interactivity) !== null && _a !== void 0 ? _a : container.actualOptions.interactivity).events;
80
+ if (!(events.onHover.enable && mouse.position)) {
81
+ return false;
82
+ }
83
+ return isInArray("connect", events.onHover.mode);
84
+ }
85
+ loadModeOptions(options, ...sources) {
86
+ if (!options.connect) {
87
+ options.connect = new Connect();
88
+ }
89
+ for (const source of sources) {
90
+ options.connect.load(source === null || source === void 0 ? void 0 : source.connect);
91
+ }
92
+ }
93
+ reset() {
94
+ }
95
+ }
@@ -0,0 +1,33 @@
1
+ import { ConnectLinks } from "./ConnectLinks";
2
+ export class Connect {
3
+ constructor() {
4
+ this.distance = 80;
5
+ this.links = new ConnectLinks();
6
+ this.radius = 60;
7
+ }
8
+ get lineLinked() {
9
+ return this.links;
10
+ }
11
+ set lineLinked(value) {
12
+ this.links = value;
13
+ }
14
+ get line_linked() {
15
+ return this.links;
16
+ }
17
+ set line_linked(value) {
18
+ this.links = value;
19
+ }
20
+ load(data) {
21
+ var _a, _b;
22
+ if (!data) {
23
+ return;
24
+ }
25
+ if (data.distance !== undefined) {
26
+ this.distance = data.distance;
27
+ }
28
+ this.links.load((_b = (_a = data.links) !== null && _a !== void 0 ? _a : data.lineLinked) !== null && _b !== void 0 ? _b : data.line_linked);
29
+ if (data.radius !== undefined) {
30
+ this.radius = data.radius;
31
+ }
32
+ }
33
+ }
@@ -0,0 +1,13 @@
1
+ export class ConnectLinks {
2
+ constructor() {
3
+ this.opacity = 0.5;
4
+ }
5
+ load(data) {
6
+ if (!data) {
7
+ return;
8
+ }
9
+ if (data.opacity !== undefined) {
10
+ this.opacity = data.opacity;
11
+ }
12
+ }
13
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { Connector } from "./Connector";
2
+ export async function loadExternalConnectInteraction(engine) {
3
+ await engine.addInteractor("externalConnect", (container) => new Connector(container));
4
+ }
5
+ export * from "./Options/Classes/Connect";
6
+ export * from "./Options/Classes/ConnectLinks";
7
+ export * from "./Options/Interfaces/IConnect";
8
+ export * from "./Options/Interfaces/IConnectLinks";
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Connector = void 0;
13
+ const engine_1 = require("@tsparticles/engine");
14
+ const Connect_1 = require("./Options/Classes/Connect");
15
+ function gradient(context, p1, p2, opacity) {
16
+ const gradStop = Math.floor(p2.getRadius() / p1.getRadius()), color1 = p1.getFillColor(), color2 = p2.getFillColor();
17
+ if (!color1 || !color2) {
18
+ return;
19
+ }
20
+ const sourcePos = p1.getPosition(), destPos = p2.getPosition(), midRgb = (0, engine_1.colorMix)(color1, color2, p1.getRadius(), p2.getRadius()), grad = context.createLinearGradient(sourcePos.x, sourcePos.y, destPos.x, destPos.y);
21
+ grad.addColorStop(0, (0, engine_1.getStyleFromHsl)(color1, opacity));
22
+ grad.addColorStop(gradStop > 1 ? 1 : gradStop, (0, engine_1.getStyleFromRgb)(midRgb, opacity));
23
+ grad.addColorStop(1, (0, engine_1.getStyleFromHsl)(color2, opacity));
24
+ return grad;
25
+ }
26
+ function drawConnectLine(context, width, lineStyle, begin, end) {
27
+ (0, engine_1.drawLine)(context, begin, end);
28
+ context.lineWidth = width;
29
+ context.strokeStyle = lineStyle;
30
+ context.stroke();
31
+ }
32
+ function lineStyle(container, ctx, p1, p2) {
33
+ const options = container.actualOptions, connectOptions = options.interactivity.modes.connect;
34
+ if (!connectOptions) {
35
+ return;
36
+ }
37
+ return gradient(ctx, p1, p2, connectOptions.links.opacity);
38
+ }
39
+ function drawConnection(container, p1, p2) {
40
+ container.canvas.draw((ctx) => {
41
+ var _a;
42
+ const ls = lineStyle(container, ctx, p1, p2);
43
+ if (!ls) {
44
+ return;
45
+ }
46
+ const pos1 = p1.getPosition(), pos2 = p2.getPosition();
47
+ drawConnectLine(ctx, (_a = p1.retina.linksWidth) !== null && _a !== void 0 ? _a : 0, ls, pos1, pos2);
48
+ });
49
+ }
50
+ class Connector extends engine_1.ExternalInteractorBase {
51
+ constructor(container) {
52
+ super(container);
53
+ }
54
+ clear() {
55
+ }
56
+ init() {
57
+ const container = this.container, connect = container.actualOptions.interactivity.modes.connect;
58
+ if (!connect) {
59
+ return;
60
+ }
61
+ container.retina.connectModeDistance = connect.distance * container.retina.pixelRatio;
62
+ container.retina.connectModeRadius = connect.radius * container.retina.pixelRatio;
63
+ }
64
+ interact() {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ const container = this.container, options = container.actualOptions;
67
+ if (options.interactivity.events.onHover.enable && container.interactivity.status === "pointermove") {
68
+ const mousePos = container.interactivity.mouse.position;
69
+ if (!container.retina.connectModeDistance ||
70
+ container.retina.connectModeDistance < 0 ||
71
+ !container.retina.connectModeRadius ||
72
+ container.retina.connectModeRadius < 0 ||
73
+ !mousePos) {
74
+ return;
75
+ }
76
+ const distance = Math.abs(container.retina.connectModeRadius), query = container.particles.quadTree.queryCircle(mousePos, distance, (p) => this.isEnabled(p));
77
+ let i = 0;
78
+ for (const p1 of query) {
79
+ const pos1 = p1.getPosition();
80
+ for (const p2 of query.slice(i + 1)) {
81
+ const pos2 = p2.getPosition(), distMax = Math.abs(container.retina.connectModeDistance), xDiff = Math.abs(pos1.x - pos2.x), yDiff = Math.abs(pos1.y - pos2.y);
82
+ if (xDiff < distMax && yDiff < distMax) {
83
+ drawConnection(container, p1, p2);
84
+ }
85
+ }
86
+ ++i;
87
+ }
88
+ }
89
+ });
90
+ }
91
+ isEnabled(particle) {
92
+ var _a;
93
+ const container = this.container, mouse = container.interactivity.mouse, events = ((_a = particle === null || particle === void 0 ? void 0 : particle.interactivity) !== null && _a !== void 0 ? _a : container.actualOptions.interactivity).events;
94
+ if (!(events.onHover.enable && mouse.position)) {
95
+ return false;
96
+ }
97
+ return (0, engine_1.isInArray)("connect", events.onHover.mode);
98
+ }
99
+ loadModeOptions(options, ...sources) {
100
+ if (!options.connect) {
101
+ options.connect = new Connect_1.Connect();
102
+ }
103
+ for (const source of sources) {
104
+ options.connect.load(source === null || source === void 0 ? void 0 : source.connect);
105
+ }
106
+ }
107
+ reset() {
108
+ }
109
+ }
110
+ exports.Connector = Connector;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Connect = void 0;
4
+ const ConnectLinks_1 = require("./ConnectLinks");
5
+ class Connect {
6
+ constructor() {
7
+ this.distance = 80;
8
+ this.links = new ConnectLinks_1.ConnectLinks();
9
+ this.radius = 60;
10
+ }
11
+ get lineLinked() {
12
+ return this.links;
13
+ }
14
+ set lineLinked(value) {
15
+ this.links = value;
16
+ }
17
+ get line_linked() {
18
+ return this.links;
19
+ }
20
+ set line_linked(value) {
21
+ this.links = value;
22
+ }
23
+ load(data) {
24
+ var _a, _b;
25
+ if (!data) {
26
+ return;
27
+ }
28
+ if (data.distance !== undefined) {
29
+ this.distance = data.distance;
30
+ }
31
+ this.links.load((_b = (_a = data.links) !== null && _a !== void 0 ? _a : data.lineLinked) !== null && _b !== void 0 ? _b : data.line_linked);
32
+ if (data.radius !== undefined) {
33
+ this.radius = data.radius;
34
+ }
35
+ }
36
+ }
37
+ exports.Connect = Connect;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConnectLinks = void 0;
4
+ class ConnectLinks {
5
+ constructor() {
6
+ this.opacity = 0.5;
7
+ }
8
+ load(data) {
9
+ if (!data) {
10
+ return;
11
+ }
12
+ if (data.opacity !== undefined) {
13
+ this.opacity = data.opacity;
14
+ }
15
+ }
16
+ }
17
+ exports.ConnectLinks = ConnectLinks;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/cjs/Types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/cjs/index.js ADDED
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.loadExternalConnectInteraction = void 0;
27
+ const Connector_1 = require("./Connector");
28
+ function loadExternalConnectInteraction(engine) {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ yield engine.addInteractor("externalConnect", (container) => new Connector_1.Connector(container));
31
+ });
32
+ }
33
+ exports.loadExternalConnectInteraction = loadExternalConnectInteraction;
34
+ __exportStar(require("./Options/Classes/Connect"), exports);
35
+ __exportStar(require("./Options/Classes/ConnectLinks"), exports);
36
+ __exportStar(require("./Options/Interfaces/IConnect"), exports);
37
+ __exportStar(require("./Options/Interfaces/IConnectLinks"), exports);
@@ -0,0 +1,95 @@
1
+ import { ExternalInteractorBase, colorMix, drawLine, getStyleFromHsl, getStyleFromRgb, isInArray, } from "@tsparticles/engine";
2
+ import { Connect } from "./Options/Classes/Connect";
3
+ function gradient(context, p1, p2, opacity) {
4
+ const gradStop = Math.floor(p2.getRadius() / p1.getRadius()), color1 = p1.getFillColor(), color2 = p2.getFillColor();
5
+ if (!color1 || !color2) {
6
+ return;
7
+ }
8
+ const sourcePos = p1.getPosition(), destPos = p2.getPosition(), midRgb = colorMix(color1, color2, p1.getRadius(), p2.getRadius()), grad = context.createLinearGradient(sourcePos.x, sourcePos.y, destPos.x, destPos.y);
9
+ grad.addColorStop(0, getStyleFromHsl(color1, opacity));
10
+ grad.addColorStop(gradStop > 1 ? 1 : gradStop, getStyleFromRgb(midRgb, opacity));
11
+ grad.addColorStop(1, getStyleFromHsl(color2, opacity));
12
+ return grad;
13
+ }
14
+ function drawConnectLine(context, width, lineStyle, begin, end) {
15
+ drawLine(context, begin, end);
16
+ context.lineWidth = width;
17
+ context.strokeStyle = lineStyle;
18
+ context.stroke();
19
+ }
20
+ function lineStyle(container, ctx, p1, p2) {
21
+ const options = container.actualOptions, connectOptions = options.interactivity.modes.connect;
22
+ if (!connectOptions) {
23
+ return;
24
+ }
25
+ return gradient(ctx, p1, p2, connectOptions.links.opacity);
26
+ }
27
+ function drawConnection(container, p1, p2) {
28
+ container.canvas.draw((ctx) => {
29
+ var _a;
30
+ const ls = lineStyle(container, ctx, p1, p2);
31
+ if (!ls) {
32
+ return;
33
+ }
34
+ const pos1 = p1.getPosition(), pos2 = p2.getPosition();
35
+ drawConnectLine(ctx, (_a = p1.retina.linksWidth) !== null && _a !== void 0 ? _a : 0, ls, pos1, pos2);
36
+ });
37
+ }
38
+ export class Connector extends ExternalInteractorBase {
39
+ constructor(container) {
40
+ super(container);
41
+ }
42
+ clear() {
43
+ }
44
+ init() {
45
+ const container = this.container, connect = container.actualOptions.interactivity.modes.connect;
46
+ if (!connect) {
47
+ return;
48
+ }
49
+ container.retina.connectModeDistance = connect.distance * container.retina.pixelRatio;
50
+ container.retina.connectModeRadius = connect.radius * container.retina.pixelRatio;
51
+ }
52
+ async interact() {
53
+ const container = this.container, options = container.actualOptions;
54
+ if (options.interactivity.events.onHover.enable && container.interactivity.status === "pointermove") {
55
+ const mousePos = container.interactivity.mouse.position;
56
+ if (!container.retina.connectModeDistance ||
57
+ container.retina.connectModeDistance < 0 ||
58
+ !container.retina.connectModeRadius ||
59
+ container.retina.connectModeRadius < 0 ||
60
+ !mousePos) {
61
+ return;
62
+ }
63
+ const distance = Math.abs(container.retina.connectModeRadius), query = container.particles.quadTree.queryCircle(mousePos, distance, (p) => this.isEnabled(p));
64
+ let i = 0;
65
+ for (const p1 of query) {
66
+ const pos1 = p1.getPosition();
67
+ for (const p2 of query.slice(i + 1)) {
68
+ const pos2 = p2.getPosition(), distMax = Math.abs(container.retina.connectModeDistance), xDiff = Math.abs(pos1.x - pos2.x), yDiff = Math.abs(pos1.y - pos2.y);
69
+ if (xDiff < distMax && yDiff < distMax) {
70
+ drawConnection(container, p1, p2);
71
+ }
72
+ }
73
+ ++i;
74
+ }
75
+ }
76
+ }
77
+ isEnabled(particle) {
78
+ var _a;
79
+ const container = this.container, mouse = container.interactivity.mouse, events = ((_a = particle === null || particle === void 0 ? void 0 : particle.interactivity) !== null && _a !== void 0 ? _a : container.actualOptions.interactivity).events;
80
+ if (!(events.onHover.enable && mouse.position)) {
81
+ return false;
82
+ }
83
+ return isInArray("connect", events.onHover.mode);
84
+ }
85
+ loadModeOptions(options, ...sources) {
86
+ if (!options.connect) {
87
+ options.connect = new Connect();
88
+ }
89
+ for (const source of sources) {
90
+ options.connect.load(source === null || source === void 0 ? void 0 : source.connect);
91
+ }
92
+ }
93
+ reset() {
94
+ }
95
+ }
@@ -0,0 +1,33 @@
1
+ import { ConnectLinks } from "./ConnectLinks";
2
+ export class Connect {
3
+ constructor() {
4
+ this.distance = 80;
5
+ this.links = new ConnectLinks();
6
+ this.radius = 60;
7
+ }
8
+ get lineLinked() {
9
+ return this.links;
10
+ }
11
+ set lineLinked(value) {
12
+ this.links = value;
13
+ }
14
+ get line_linked() {
15
+ return this.links;
16
+ }
17
+ set line_linked(value) {
18
+ this.links = value;
19
+ }
20
+ load(data) {
21
+ var _a, _b;
22
+ if (!data) {
23
+ return;
24
+ }
25
+ if (data.distance !== undefined) {
26
+ this.distance = data.distance;
27
+ }
28
+ this.links.load((_b = (_a = data.links) !== null && _a !== void 0 ? _a : data.lineLinked) !== null && _b !== void 0 ? _b : data.line_linked);
29
+ if (data.radius !== undefined) {
30
+ this.radius = data.radius;
31
+ }
32
+ }
33
+ }
@@ -0,0 +1,13 @@
1
+ export class ConnectLinks {
2
+ constructor() {
3
+ this.opacity = 0.5;
4
+ }
5
+ load(data) {
6
+ if (!data) {
7
+ return;
8
+ }
9
+ if (data.opacity !== undefined) {
10
+ this.opacity = data.opacity;
11
+ }
12
+ }
13
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/esm/Types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/esm/index.js ADDED
@@ -0,0 +1,8 @@
1
+ import { Connector } from "./Connector";
2
+ export async function loadExternalConnectInteraction(engine) {
3
+ await engine.addInteractor("externalConnect", (container) => new Connector(container));
4
+ }
5
+ export * from "./Options/Classes/Connect";
6
+ export * from "./Options/Classes/ConnectLinks";
7
+ export * from "./Options/Interfaces/IConnect";
8
+ export * from "./Options/Interfaces/IConnectLinks";