@tsparticles/interaction-external-repulse 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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +70 -0
  3. package/browser/Options/Classes/Repulse.js +16 -0
  4. package/browser/Options/Classes/RepulseBase.js +33 -0
  5. package/browser/Options/Classes/RepulseDiv.js +26 -0
  6. package/browser/Options/Classes/RepulseOptions.js +1 -0
  7. package/browser/Options/Interfaces/IRepulse.js +1 -0
  8. package/browser/Options/Interfaces/IRepulseBase.js +1 -0
  9. package/browser/Options/Interfaces/IRepulseDiv.js +1 -0
  10. package/browser/Repulser.js +159 -0
  11. package/browser/Types.js +1 -0
  12. package/browser/index.js +10 -0
  13. package/cjs/Options/Classes/Repulse.js +20 -0
  14. package/cjs/Options/Classes/RepulseBase.js +37 -0
  15. package/cjs/Options/Classes/RepulseDiv.js +30 -0
  16. package/cjs/Options/Classes/RepulseOptions.js +2 -0
  17. package/cjs/Options/Interfaces/IRepulse.js +2 -0
  18. package/cjs/Options/Interfaces/IRepulseBase.js +2 -0
  19. package/cjs/Options/Interfaces/IRepulseDiv.js +2 -0
  20. package/cjs/Repulser.js +174 -0
  21. package/cjs/Types.js +2 -0
  22. package/cjs/index.js +39 -0
  23. package/esm/Options/Classes/Repulse.js +16 -0
  24. package/esm/Options/Classes/RepulseBase.js +33 -0
  25. package/esm/Options/Classes/RepulseDiv.js +26 -0
  26. package/esm/Options/Classes/RepulseOptions.js +1 -0
  27. package/esm/Options/Interfaces/IRepulse.js +1 -0
  28. package/esm/Options/Interfaces/IRepulseBase.js +1 -0
  29. package/esm/Options/Interfaces/IRepulseDiv.js +1 -0
  30. package/esm/Repulser.js +159 -0
  31. package/esm/Types.js +1 -0
  32. package/esm/index.js +10 -0
  33. package/package.json +82 -0
  34. package/report.html +39 -0
  35. package/tsparticles.interaction.external.repulse.js +401 -0
  36. package/tsparticles.interaction.external.repulse.min.js +2 -0
  37. package/tsparticles.interaction.external.repulse.min.js.LICENSE.txt +8 -0
  38. package/types/Options/Classes/Repulse.d.ts +8 -0
  39. package/types/Options/Classes/RepulseBase.d.ts +13 -0
  40. package/types/Options/Classes/RepulseDiv.d.ts +10 -0
  41. package/types/Options/Classes/RepulseOptions.d.ts +7 -0
  42. package/types/Options/Interfaces/IRepulse.d.ts +6 -0
  43. package/types/Options/Interfaces/IRepulseBase.d.ts +9 -0
  44. package/types/Options/Interfaces/IRepulseDiv.d.ts +4 -0
  45. package/types/Repulser.d.ts +18 -0
  46. package/types/Types.d.ts +24 -0
  47. package/types/index.d.ts +8 -0
  48. package/umd/Options/Classes/Repulse.js +30 -0
  49. package/umd/Options/Classes/RepulseBase.js +47 -0
  50. package/umd/Options/Classes/RepulseDiv.js +40 -0
  51. package/umd/Options/Classes/RepulseOptions.js +12 -0
  52. package/umd/Options/Interfaces/IRepulse.js +12 -0
  53. package/umd/Options/Interfaces/IRepulseBase.js +12 -0
  54. package/umd/Options/Interfaces/IRepulseDiv.js +12 -0
  55. package/umd/Repulser.js +173 -0
  56. package/umd/Types.js +12 -0
  57. package/umd/index.js +38 -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 Repulse Interaction
4
+
5
+ [![jsDelivr](https://data.jsdelivr.com/v1/package/npm/tsparticles-interaction-external-repulse/badge)](https://www.jsdelivr.com/package/npm/tsparticles-interaction-external-repulse)
6
+ [![npmjs](https://badge.fury.io/js/tsparticles-interaction-external-repulse.svg)](https://www.npmjs.com/package/tsparticles-interaction-external-repulse)
7
+ [![npmjs](https://img.shields.io/npm/dt/tsparticles-interaction-external-repulse)](https://www.npmjs.com/package/tsparticles-interaction-external-repulse) [![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 repulse 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.repulse.min.js` file will export the function to load the interaction
19
+ plugin:
20
+
21
+ ```javascript
22
+ loadExternalRepulseInteraction;
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
+ loadExternalRepulseInteraction(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-repulse
46
+ ```
47
+
48
+ or
49
+
50
+ ```shell
51
+ $ yarn add tsparticles-interaction-external-repulse
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 { loadExternalRepulseInteraction } = require("tsparticles-interaction-external-repulse");
59
+
60
+ loadExternalRepulseInteraction(tsParticles);
61
+ ```
62
+
63
+ or
64
+
65
+ ```javascript
66
+ import { tsParticles } from "tsparticles-engine";
67
+ import { loadExternalRepulseInteraction } from "tsparticles-interaction-external-repulse";
68
+
69
+ loadExternalRepulseInteraction(tsParticles);
70
+ ```
@@ -0,0 +1,16 @@
1
+ import { RepulseBase } from "./RepulseBase";
2
+ import { RepulseDiv } from "./RepulseDiv";
3
+ import { executeOnSingleOrMultiple } from "@tsparticles/engine";
4
+ export class Repulse extends RepulseBase {
5
+ load(data) {
6
+ super.load(data);
7
+ if (!data) {
8
+ return;
9
+ }
10
+ this.divs = executeOnSingleOrMultiple(data.divs, (div) => {
11
+ const tmp = new RepulseDiv();
12
+ tmp.load(div);
13
+ return tmp;
14
+ });
15
+ }
16
+ }
@@ -0,0 +1,33 @@
1
+ export class RepulseBase {
2
+ constructor() {
3
+ this.distance = 200;
4
+ this.duration = 0.4;
5
+ this.factor = 100;
6
+ this.speed = 1;
7
+ this.maxSpeed = 50;
8
+ this.easing = "ease-out-quad";
9
+ }
10
+ load(data) {
11
+ if (!data) {
12
+ return;
13
+ }
14
+ if (data.distance !== undefined) {
15
+ this.distance = data.distance;
16
+ }
17
+ if (data.duration !== undefined) {
18
+ this.duration = data.duration;
19
+ }
20
+ if (data.easing !== undefined) {
21
+ this.easing = data.easing;
22
+ }
23
+ if (data.factor !== undefined) {
24
+ this.factor = data.factor;
25
+ }
26
+ if (data.speed !== undefined) {
27
+ this.speed = data.speed;
28
+ }
29
+ if (data.maxSpeed !== undefined) {
30
+ this.maxSpeed = data.maxSpeed;
31
+ }
32
+ }
33
+ }
@@ -0,0 +1,26 @@
1
+ import { RepulseBase } from "./RepulseBase";
2
+ import { executeOnSingleOrMultiple } from "@tsparticles/engine";
3
+ export class RepulseDiv extends RepulseBase {
4
+ constructor() {
5
+ super();
6
+ this.selectors = [];
7
+ }
8
+ get ids() {
9
+ return executeOnSingleOrMultiple(this.selectors, (t) => t.replace("#", ""));
10
+ }
11
+ set ids(value) {
12
+ this.selectors = executeOnSingleOrMultiple(value, (t) => `#${t}`);
13
+ }
14
+ load(data) {
15
+ super.load(data);
16
+ if (!data) {
17
+ return;
18
+ }
19
+ if (data.ids !== undefined) {
20
+ this.ids = data.ids;
21
+ }
22
+ if (data.selectors !== undefined) {
23
+ this.selectors = data.selectors;
24
+ }
25
+ }
26
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,159 @@
1
+ import { Circle, ExternalInteractorBase, Rectangle, Vector, clamp, divMode, divModeExecute, getDistances, getEasing, isDivModeEnabled, isInArray, mouseMoveEvent, } from "@tsparticles/engine";
2
+ import { Repulse } from "./Options/Classes/Repulse";
3
+ export class Repulser extends ExternalInteractorBase {
4
+ constructor(engine, container) {
5
+ super(container);
6
+ this._engine = engine;
7
+ if (!container.repulse) {
8
+ container.repulse = { particles: [] };
9
+ }
10
+ this.handleClickMode = (mode) => {
11
+ const options = this.container.actualOptions, repulse = options.interactivity.modes.repulse;
12
+ if (!repulse || mode !== "repulse") {
13
+ return;
14
+ }
15
+ if (!container.repulse) {
16
+ container.repulse = { particles: [] };
17
+ }
18
+ container.repulse.clicking = true;
19
+ container.repulse.count = 0;
20
+ for (const particle of container.repulse.particles) {
21
+ if (!this.isEnabled(particle)) {
22
+ continue;
23
+ }
24
+ particle.velocity.setTo(particle.initialVelocity);
25
+ }
26
+ container.repulse.particles = [];
27
+ container.repulse.finish = false;
28
+ setTimeout(() => {
29
+ if (!container.destroyed) {
30
+ if (!container.repulse) {
31
+ container.repulse = { particles: [] };
32
+ }
33
+ container.repulse.clicking = false;
34
+ }
35
+ }, repulse.duration * 1000);
36
+ };
37
+ }
38
+ clear() {
39
+ }
40
+ init() {
41
+ const container = this.container, repulse = container.actualOptions.interactivity.modes.repulse;
42
+ if (!repulse) {
43
+ return;
44
+ }
45
+ container.retina.repulseModeDistance = repulse.distance * container.retina.pixelRatio;
46
+ }
47
+ async interact() {
48
+ const container = this.container, options = container.actualOptions, mouseMoveStatus = container.interactivity.status === mouseMoveEvent, events = options.interactivity.events, hoverEnabled = events.onHover.enable, hoverMode = events.onHover.mode, clickEnabled = events.onClick.enable, clickMode = events.onClick.mode, divs = events.onDiv;
49
+ if (mouseMoveStatus && hoverEnabled && isInArray("repulse", hoverMode)) {
50
+ this.hoverRepulse();
51
+ }
52
+ else if (clickEnabled && isInArray("repulse", clickMode)) {
53
+ this.clickRepulse();
54
+ }
55
+ else {
56
+ divModeExecute("repulse", divs, (selector, div) => this.singleSelectorRepulse(selector, div));
57
+ }
58
+ }
59
+ isEnabled(particle) {
60
+ var _a;
61
+ const container = this.container, options = container.actualOptions, mouse = container.interactivity.mouse, events = ((_a = particle === null || particle === void 0 ? void 0 : particle.interactivity) !== null && _a !== void 0 ? _a : options.interactivity).events, divs = events.onDiv, divRepulse = isDivModeEnabled("repulse", divs);
62
+ if (!(divRepulse || (events.onHover.enable && mouse.position) || (events.onClick.enable && mouse.clickPosition))) {
63
+ return false;
64
+ }
65
+ const hoverMode = events.onHover.mode, clickMode = events.onClick.mode;
66
+ return isInArray("repulse", hoverMode) || isInArray("repulse", clickMode) || divRepulse;
67
+ }
68
+ loadModeOptions(options, ...sources) {
69
+ if (!options.repulse) {
70
+ options.repulse = new Repulse();
71
+ }
72
+ for (const source of sources) {
73
+ options.repulse.load(source === null || source === void 0 ? void 0 : source.repulse);
74
+ }
75
+ }
76
+ reset() {
77
+ }
78
+ clickRepulse() {
79
+ const container = this.container, repulse = container.actualOptions.interactivity.modes.repulse;
80
+ if (!repulse) {
81
+ return;
82
+ }
83
+ if (!container.repulse) {
84
+ container.repulse = { particles: [] };
85
+ }
86
+ if (!container.repulse.finish) {
87
+ if (!container.repulse.count) {
88
+ container.repulse.count = 0;
89
+ }
90
+ container.repulse.count++;
91
+ if (container.repulse.count === container.particles.count) {
92
+ container.repulse.finish = true;
93
+ }
94
+ }
95
+ if (container.repulse.clicking) {
96
+ const repulseDistance = container.retina.repulseModeDistance;
97
+ if (!repulseDistance || repulseDistance < 0) {
98
+ return;
99
+ }
100
+ const repulseRadius = Math.pow(repulseDistance / 6, 3), mouseClickPos = container.interactivity.mouse.clickPosition;
101
+ if (mouseClickPos === undefined) {
102
+ return;
103
+ }
104
+ const range = new Circle(mouseClickPos.x, mouseClickPos.y, repulseRadius), query = container.particles.quadTree.query(range, (p) => this.isEnabled(p));
105
+ for (const particle of query) {
106
+ const { dx, dy, distance } = getDistances(mouseClickPos, particle.position), d = distance ** 2, velocity = repulse.speed, force = (-repulseRadius * velocity) / d;
107
+ if (d <= repulseRadius) {
108
+ container.repulse.particles.push(particle);
109
+ const vect = Vector.create(dx, dy);
110
+ vect.length = force;
111
+ particle.velocity.setTo(vect);
112
+ }
113
+ }
114
+ }
115
+ else if (container.repulse.clicking === false) {
116
+ for (const particle of container.repulse.particles) {
117
+ particle.velocity.setTo(particle.initialVelocity);
118
+ }
119
+ container.repulse.particles = [];
120
+ }
121
+ }
122
+ hoverRepulse() {
123
+ const container = this.container, mousePos = container.interactivity.mouse.position, repulseRadius = container.retina.repulseModeDistance;
124
+ if (!repulseRadius || repulseRadius < 0 || !mousePos) {
125
+ return;
126
+ }
127
+ this.processRepulse(mousePos, repulseRadius, new Circle(mousePos.x, mousePos.y, repulseRadius));
128
+ }
129
+ processRepulse(position, repulseRadius, area, divRepulse) {
130
+ var _a;
131
+ const container = this.container, query = container.particles.quadTree.query(area, (p) => this.isEnabled(p)), repulseOptions = container.actualOptions.interactivity.modes.repulse;
132
+ if (!repulseOptions) {
133
+ return;
134
+ }
135
+ for (const particle of query) {
136
+ const { dx, dy, distance } = getDistances(particle.position, position), velocity = ((_a = divRepulse === null || divRepulse === void 0 ? void 0 : divRepulse.speed) !== null && _a !== void 0 ? _a : repulseOptions.speed) * repulseOptions.factor, repulseFactor = clamp(getEasing(repulseOptions.easing)(1 - distance / repulseRadius) * velocity, 0, repulseOptions.maxSpeed), normVec = Vector.create(distance === 0 ? velocity : (dx / distance) * repulseFactor, distance === 0 ? velocity : (dy / distance) * repulseFactor);
137
+ particle.position.addTo(normVec);
138
+ }
139
+ }
140
+ singleSelectorRepulse(selector, div) {
141
+ const container = this.container, repulse = container.actualOptions.interactivity.modes.repulse;
142
+ if (!repulse) {
143
+ return;
144
+ }
145
+ const query = document.querySelectorAll(selector);
146
+ if (!query.length) {
147
+ return;
148
+ }
149
+ query.forEach((item) => {
150
+ const elem = item, pxRatio = container.retina.pixelRatio, pos = {
151
+ x: (elem.offsetLeft + elem.offsetWidth / 2) * pxRatio,
152
+ y: (elem.offsetTop + elem.offsetHeight / 2) * pxRatio,
153
+ }, repulseRadius = (elem.offsetWidth / 2) * pxRatio, area = div.type === "circle"
154
+ ? new Circle(pos.x, pos.y, repulseRadius)
155
+ : new Rectangle(elem.offsetLeft * pxRatio, elem.offsetTop * pxRatio, elem.offsetWidth * pxRatio, elem.offsetHeight * pxRatio), divs = repulse.divs, divRepulse = divMode(divs, elem);
156
+ this.processRepulse(pos, repulseRadius, area, divRepulse);
157
+ });
158
+ }
159
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import { Repulser } from "./Repulser";
2
+ export async function loadExternalRepulseInteraction(engine) {
3
+ await engine.addInteractor("externalRepulse", (container) => new Repulser(engine, container));
4
+ }
5
+ export * from "./Options/Classes/RepulseBase";
6
+ export * from "./Options/Classes/RepulseDiv";
7
+ export * from "./Options/Classes/Repulse";
8
+ export * from "./Options/Interfaces/IRepulseBase";
9
+ export * from "./Options/Interfaces/IRepulseDiv";
10
+ export * from "./Options/Interfaces/IRepulse";
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Repulse = void 0;
4
+ const RepulseBase_1 = require("./RepulseBase");
5
+ const RepulseDiv_1 = require("./RepulseDiv");
6
+ const engine_1 = require("@tsparticles/engine");
7
+ class Repulse extends RepulseBase_1.RepulseBase {
8
+ load(data) {
9
+ super.load(data);
10
+ if (!data) {
11
+ return;
12
+ }
13
+ this.divs = (0, engine_1.executeOnSingleOrMultiple)(data.divs, (div) => {
14
+ const tmp = new RepulseDiv_1.RepulseDiv();
15
+ tmp.load(div);
16
+ return tmp;
17
+ });
18
+ }
19
+ }
20
+ exports.Repulse = Repulse;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RepulseBase = void 0;
4
+ class RepulseBase {
5
+ constructor() {
6
+ this.distance = 200;
7
+ this.duration = 0.4;
8
+ this.factor = 100;
9
+ this.speed = 1;
10
+ this.maxSpeed = 50;
11
+ this.easing = "ease-out-quad";
12
+ }
13
+ load(data) {
14
+ if (!data) {
15
+ return;
16
+ }
17
+ if (data.distance !== undefined) {
18
+ this.distance = data.distance;
19
+ }
20
+ if (data.duration !== undefined) {
21
+ this.duration = data.duration;
22
+ }
23
+ if (data.easing !== undefined) {
24
+ this.easing = data.easing;
25
+ }
26
+ if (data.factor !== undefined) {
27
+ this.factor = data.factor;
28
+ }
29
+ if (data.speed !== undefined) {
30
+ this.speed = data.speed;
31
+ }
32
+ if (data.maxSpeed !== undefined) {
33
+ this.maxSpeed = data.maxSpeed;
34
+ }
35
+ }
36
+ }
37
+ exports.RepulseBase = RepulseBase;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RepulseDiv = void 0;
4
+ const RepulseBase_1 = require("./RepulseBase");
5
+ const engine_1 = require("@tsparticles/engine");
6
+ class RepulseDiv extends RepulseBase_1.RepulseBase {
7
+ constructor() {
8
+ super();
9
+ this.selectors = [];
10
+ }
11
+ get ids() {
12
+ return (0, engine_1.executeOnSingleOrMultiple)(this.selectors, (t) => t.replace("#", ""));
13
+ }
14
+ set ids(value) {
15
+ this.selectors = (0, engine_1.executeOnSingleOrMultiple)(value, (t) => `#${t}`);
16
+ }
17
+ load(data) {
18
+ super.load(data);
19
+ if (!data) {
20
+ return;
21
+ }
22
+ if (data.ids !== undefined) {
23
+ this.ids = data.ids;
24
+ }
25
+ if (data.selectors !== undefined) {
26
+ this.selectors = data.selectors;
27
+ }
28
+ }
29
+ }
30
+ exports.RepulseDiv = RepulseDiv;
@@ -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 });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,174 @@
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.Repulser = void 0;
13
+ const engine_1 = require("@tsparticles/engine");
14
+ const Repulse_1 = require("./Options/Classes/Repulse");
15
+ class Repulser extends engine_1.ExternalInteractorBase {
16
+ constructor(engine, container) {
17
+ super(container);
18
+ this._engine = engine;
19
+ if (!container.repulse) {
20
+ container.repulse = { particles: [] };
21
+ }
22
+ this.handleClickMode = (mode) => {
23
+ const options = this.container.actualOptions, repulse = options.interactivity.modes.repulse;
24
+ if (!repulse || mode !== "repulse") {
25
+ return;
26
+ }
27
+ if (!container.repulse) {
28
+ container.repulse = { particles: [] };
29
+ }
30
+ container.repulse.clicking = true;
31
+ container.repulse.count = 0;
32
+ for (const particle of container.repulse.particles) {
33
+ if (!this.isEnabled(particle)) {
34
+ continue;
35
+ }
36
+ particle.velocity.setTo(particle.initialVelocity);
37
+ }
38
+ container.repulse.particles = [];
39
+ container.repulse.finish = false;
40
+ setTimeout(() => {
41
+ if (!container.destroyed) {
42
+ if (!container.repulse) {
43
+ container.repulse = { particles: [] };
44
+ }
45
+ container.repulse.clicking = false;
46
+ }
47
+ }, repulse.duration * 1000);
48
+ };
49
+ }
50
+ clear() {
51
+ }
52
+ init() {
53
+ const container = this.container, repulse = container.actualOptions.interactivity.modes.repulse;
54
+ if (!repulse) {
55
+ return;
56
+ }
57
+ container.retina.repulseModeDistance = repulse.distance * container.retina.pixelRatio;
58
+ }
59
+ interact() {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ const container = this.container, options = container.actualOptions, mouseMoveStatus = container.interactivity.status === engine_1.mouseMoveEvent, events = options.interactivity.events, hoverEnabled = events.onHover.enable, hoverMode = events.onHover.mode, clickEnabled = events.onClick.enable, clickMode = events.onClick.mode, divs = events.onDiv;
62
+ if (mouseMoveStatus && hoverEnabled && (0, engine_1.isInArray)("repulse", hoverMode)) {
63
+ this.hoverRepulse();
64
+ }
65
+ else if (clickEnabled && (0, engine_1.isInArray)("repulse", clickMode)) {
66
+ this.clickRepulse();
67
+ }
68
+ else {
69
+ (0, engine_1.divModeExecute)("repulse", divs, (selector, div) => this.singleSelectorRepulse(selector, div));
70
+ }
71
+ });
72
+ }
73
+ isEnabled(particle) {
74
+ var _a;
75
+ const container = this.container, options = container.actualOptions, mouse = container.interactivity.mouse, events = ((_a = particle === null || particle === void 0 ? void 0 : particle.interactivity) !== null && _a !== void 0 ? _a : options.interactivity).events, divs = events.onDiv, divRepulse = (0, engine_1.isDivModeEnabled)("repulse", divs);
76
+ if (!(divRepulse || (events.onHover.enable && mouse.position) || (events.onClick.enable && mouse.clickPosition))) {
77
+ return false;
78
+ }
79
+ const hoverMode = events.onHover.mode, clickMode = events.onClick.mode;
80
+ return (0, engine_1.isInArray)("repulse", hoverMode) || (0, engine_1.isInArray)("repulse", clickMode) || divRepulse;
81
+ }
82
+ loadModeOptions(options, ...sources) {
83
+ if (!options.repulse) {
84
+ options.repulse = new Repulse_1.Repulse();
85
+ }
86
+ for (const source of sources) {
87
+ options.repulse.load(source === null || source === void 0 ? void 0 : source.repulse);
88
+ }
89
+ }
90
+ reset() {
91
+ }
92
+ clickRepulse() {
93
+ const container = this.container, repulse = container.actualOptions.interactivity.modes.repulse;
94
+ if (!repulse) {
95
+ return;
96
+ }
97
+ if (!container.repulse) {
98
+ container.repulse = { particles: [] };
99
+ }
100
+ if (!container.repulse.finish) {
101
+ if (!container.repulse.count) {
102
+ container.repulse.count = 0;
103
+ }
104
+ container.repulse.count++;
105
+ if (container.repulse.count === container.particles.count) {
106
+ container.repulse.finish = true;
107
+ }
108
+ }
109
+ if (container.repulse.clicking) {
110
+ const repulseDistance = container.retina.repulseModeDistance;
111
+ if (!repulseDistance || repulseDistance < 0) {
112
+ return;
113
+ }
114
+ const repulseRadius = Math.pow(repulseDistance / 6, 3), mouseClickPos = container.interactivity.mouse.clickPosition;
115
+ if (mouseClickPos === undefined) {
116
+ return;
117
+ }
118
+ const range = new engine_1.Circle(mouseClickPos.x, mouseClickPos.y, repulseRadius), query = container.particles.quadTree.query(range, (p) => this.isEnabled(p));
119
+ for (const particle of query) {
120
+ const { dx, dy, distance } = (0, engine_1.getDistances)(mouseClickPos, particle.position), d = Math.pow(distance, 2), velocity = repulse.speed, force = (-repulseRadius * velocity) / d;
121
+ if (d <= repulseRadius) {
122
+ container.repulse.particles.push(particle);
123
+ const vect = engine_1.Vector.create(dx, dy);
124
+ vect.length = force;
125
+ particle.velocity.setTo(vect);
126
+ }
127
+ }
128
+ }
129
+ else if (container.repulse.clicking === false) {
130
+ for (const particle of container.repulse.particles) {
131
+ particle.velocity.setTo(particle.initialVelocity);
132
+ }
133
+ container.repulse.particles = [];
134
+ }
135
+ }
136
+ hoverRepulse() {
137
+ const container = this.container, mousePos = container.interactivity.mouse.position, repulseRadius = container.retina.repulseModeDistance;
138
+ if (!repulseRadius || repulseRadius < 0 || !mousePos) {
139
+ return;
140
+ }
141
+ this.processRepulse(mousePos, repulseRadius, new engine_1.Circle(mousePos.x, mousePos.y, repulseRadius));
142
+ }
143
+ processRepulse(position, repulseRadius, area, divRepulse) {
144
+ var _a;
145
+ const container = this.container, query = container.particles.quadTree.query(area, (p) => this.isEnabled(p)), repulseOptions = container.actualOptions.interactivity.modes.repulse;
146
+ if (!repulseOptions) {
147
+ return;
148
+ }
149
+ for (const particle of query) {
150
+ const { dx, dy, distance } = (0, engine_1.getDistances)(particle.position, position), velocity = ((_a = divRepulse === null || divRepulse === void 0 ? void 0 : divRepulse.speed) !== null && _a !== void 0 ? _a : repulseOptions.speed) * repulseOptions.factor, repulseFactor = (0, engine_1.clamp)((0, engine_1.getEasing)(repulseOptions.easing)(1 - distance / repulseRadius) * velocity, 0, repulseOptions.maxSpeed), normVec = engine_1.Vector.create(distance === 0 ? velocity : (dx / distance) * repulseFactor, distance === 0 ? velocity : (dy / distance) * repulseFactor);
151
+ particle.position.addTo(normVec);
152
+ }
153
+ }
154
+ singleSelectorRepulse(selector, div) {
155
+ const container = this.container, repulse = container.actualOptions.interactivity.modes.repulse;
156
+ if (!repulse) {
157
+ return;
158
+ }
159
+ const query = document.querySelectorAll(selector);
160
+ if (!query.length) {
161
+ return;
162
+ }
163
+ query.forEach((item) => {
164
+ const elem = item, pxRatio = container.retina.pixelRatio, pos = {
165
+ x: (elem.offsetLeft + elem.offsetWidth / 2) * pxRatio,
166
+ y: (elem.offsetTop + elem.offsetHeight / 2) * pxRatio,
167
+ }, repulseRadius = (elem.offsetWidth / 2) * pxRatio, area = div.type === "circle"
168
+ ? new engine_1.Circle(pos.x, pos.y, repulseRadius)
169
+ : new engine_1.Rectangle(elem.offsetLeft * pxRatio, elem.offsetTop * pxRatio, elem.offsetWidth * pxRatio, elem.offsetHeight * pxRatio), divs = repulse.divs, divRepulse = (0, engine_1.divMode)(divs, elem);
170
+ this.processRepulse(pos, repulseRadius, area, divRepulse);
171
+ });
172
+ }
173
+ }
174
+ exports.Repulser = Repulser;
package/cjs/Types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });