@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/cjs/index.js ADDED
@@ -0,0 +1,39 @@
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.loadExternalRepulseInteraction = void 0;
27
+ const Repulser_1 = require("./Repulser");
28
+ function loadExternalRepulseInteraction(engine) {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ yield engine.addInteractor("externalRepulse", (container) => new Repulser_1.Repulser(engine, container));
31
+ });
32
+ }
33
+ exports.loadExternalRepulseInteraction = loadExternalRepulseInteraction;
34
+ __exportStar(require("./Options/Classes/RepulseBase"), exports);
35
+ __exportStar(require("./Options/Classes/RepulseDiv"), exports);
36
+ __exportStar(require("./Options/Classes/Repulse"), exports);
37
+ __exportStar(require("./Options/Interfaces/IRepulseBase"), exports);
38
+ __exportStar(require("./Options/Interfaces/IRepulseDiv"), exports);
39
+ __exportStar(require("./Options/Interfaces/IRepulse"), exports);
@@ -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
+ }
package/esm/Types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/esm/index.js ADDED
@@ -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";
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "@tsparticles/interaction-external-repulse",
3
+ "version": "3.0.0-alpha.0",
4
+ "description": "tsParticles repulse external interaction",
5
+ "homepage": "https://particles.js.org",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/matteobruni/tsparticles.git",
9
+ "directory": "interactions/external/repulse"
10
+ },
11
+ "keywords": [
12
+ "front-end",
13
+ "frontend",
14
+ "tsparticles",
15
+ "particles.js",
16
+ "particlesjs",
17
+ "particles",
18
+ "particle",
19
+ "canvas",
20
+ "jsparticles",
21
+ "xparticles",
22
+ "particles-js",
23
+ "particles-bg",
24
+ "particles-bg-vue",
25
+ "particles-ts",
26
+ "particles.ts",
27
+ "react-particles-js",
28
+ "react-particles.js",
29
+ "react-particles",
30
+ "react",
31
+ "reactjs",
32
+ "vue-particles",
33
+ "ngx-particles",
34
+ "angular-particles",
35
+ "particleground",
36
+ "vue",
37
+ "vuejs",
38
+ "preact",
39
+ "preactjs",
40
+ "jquery",
41
+ "angularjs",
42
+ "angular",
43
+ "typescript",
44
+ "javascript",
45
+ "animation",
46
+ "web",
47
+ "html5",
48
+ "web-design",
49
+ "webdesign",
50
+ "css",
51
+ "html",
52
+ "css3",
53
+ "animated",
54
+ "background",
55
+ "confetti",
56
+ "canvas",
57
+ "fireworks",
58
+ "fireworks-js",
59
+ "confetti-js",
60
+ "confettijs",
61
+ "fireworksjs",
62
+ "canvas-confetti",
63
+ "@tsparticles/plugin",
64
+ "@tsparticles/interaction"
65
+ ],
66
+ "author": "Matteo Bruni <matteo.bruni@me.com>",
67
+ "license": "MIT",
68
+ "bugs": {
69
+ "url": "https://github.com/matteobruni/tsparticles/issues"
70
+ },
71
+ "main": "cjs/index.js",
72
+ "jsdelivr": "tsparticles.interaction.external.repulse.min.js",
73
+ "unpkg": "tsparticles.interaction.external.repulse.min.js",
74
+ "module": "esm/index.js",
75
+ "types": "types/index.d.ts",
76
+ "publishConfig": {
77
+ "access": "public"
78
+ },
79
+ "dependencies": {
80
+ "@tsparticles/engine": "^3.0.0-alpha.0"
81
+ }
82
+ }