@tsparticles/interaction-particles-attract 4.0.0-alpha.8 → 4.0.0-beta.1

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 (35) hide show
  1. package/667.min.js +1 -0
  2. package/README.md +5 -0
  3. package/browser/Attractor.js +27 -6
  4. package/browser/Options/Classes/Attract.js +35 -0
  5. package/browser/index.js +5 -5
  6. package/cjs/Attractor.js +27 -6
  7. package/cjs/Options/Classes/Attract.js +35 -0
  8. package/cjs/Types.js +1 -0
  9. package/cjs/index.js +5 -5
  10. package/dist_browser_Attractor_js.js +12 -2
  11. package/esm/Attractor.js +27 -6
  12. package/esm/Options/Classes/Attract.js +35 -0
  13. package/esm/Options/Interfaces/IAttract.js +1 -0
  14. package/esm/Types.js +1 -0
  15. package/esm/index.js +5 -5
  16. package/package.json +3 -3
  17. package/report.html +84 -29
  18. package/tsparticles.interaction.particles.attract.js +31 -19
  19. package/tsparticles.interaction.particles.attract.min.js +2 -2
  20. package/types/Attractor.d.ts +6 -3
  21. package/types/Options/Classes/Attract.d.ts +9 -0
  22. package/types/Options/Interfaces/IAttract.d.ts +6 -0
  23. package/types/Types.d.ts +13 -0
  24. package/umd/Attractor.js +27 -6
  25. package/umd/Options/Classes/Attract.js +49 -0
  26. package/umd/Types.js +12 -0
  27. package/umd/index.js +5 -5
  28. package/63.min.js +0 -2
  29. package/63.min.js.LICENSE.txt +0 -1
  30. package/tsparticles.interaction.particles.attract.min.js.LICENSE.txt +0 -1
  31. package/types/AttractParticle.d.ts +0 -4
  32. /package/browser/{AttractParticle.js → Options/Interfaces/IAttract.js} +0 -0
  33. /package/{cjs/AttractParticle.js → browser/Types.js} +0 -0
  34. /package/{esm/AttractParticle.js → cjs/Options/Interfaces/IAttract.js} +0 -0
  35. /package/umd/{AttractParticle.js → Options/Interfaces/IAttract.js} +0 -0
package/667.min.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";(this.webpackChunk_tsparticles_interaction_particles_attract=this.webpackChunk_tsparticles_interaction_particles_attract||[]).push([[667],{667(t,e,a){a.d(e,{Attractor:()=>c});var i=a(303);class s{distance;enable;rotate;constructor(){this.distance=200,this.enable=!1,this.rotate={x:3e3,y:3e3}}load(t){if(!(0,i.isNull)(t)&&(void 0!==t.distance&&(this.distance=(0,i.setRangeValue)(t.distance)),void 0!==t.enable&&(this.enable=t.enable),t.rotate)){let e=t.rotate.x;void 0!==e&&(this.rotate.x=e);let a=t.rotate.y;void 0!==a&&(this.rotate.y=a)}}}var r=a(702);class c extends r.ParticlesInteractorBase{_maxDistance;constructor(t){super(t),this._maxDistance=0}get maxDistance(){return this._maxDistance}clear(){}init(){}interact(t){if(!t.options.attract?.enable)return;let e=this.container;if((0,i.isNull)(t.attractDistance)){let a=(0,i.getRangeValue)(t.options.attract.distance);a>this._maxDistance&&(this._maxDistance=a),t.attractDistance=a*e.retina.pixelRatio}let a=t.attractDistance,s=t.getPosition();for(let r of e.particles.grid.queryCircle(s,a)){if(t===r||!r.options.attract?.enable||r.destroyed||r.spawning)continue;let e=r.getPosition(),{dx:a,dy:c}=(0,i.getDistances)(s,e),n=t.options.attract.rotate,o=a/(1e3*n.x),l=c/(1e3*n.y),d=r.size.value/t.size.value,u=1/d;t.velocity.x-=o*d,t.velocity.y-=l*d,r.velocity.x+=o*u,r.velocity.y+=l*u}}isEnabled(t){return t.options.attract?.enable??!1}loadParticlesOptions(t,...e){for(let a of(t.attract??=new s,e))t.attract.load(a?.attract)}reset(){}}}}]);
package/README.md CHANGED
@@ -27,6 +27,7 @@ Once the scripts are loaded you can set up `tsParticles` and the interaction plu
27
27
 
28
28
  ```javascript
29
29
  (async () => {
30
+ await loadInteractivityPlugin(tsParticles);
30
31
  await loadParticlesAttractInteraction(tsParticles);
31
32
 
32
33
  await tsParticles.load({
@@ -56,9 +57,11 @@ Then you need to import it in the app, like this:
56
57
 
57
58
  ```javascript
58
59
  const { tsParticles } = require("@tsparticles/engine");
60
+ const { loadInteractivityPlugin } = require("@tsparticles/plugin-interactivity");
59
61
  const { loadParticlesAttractInteraction } = require("@tsparticles/interaction-particles-attract");
60
62
 
61
63
  (async () => {
64
+ await loadInteractivityPlugin(tsParticles);
62
65
  await loadParticlesAttractInteraction(tsParticles);
63
66
  })();
64
67
  ```
@@ -67,9 +70,11 @@ or
67
70
 
68
71
  ```javascript
69
72
  import { tsParticles } from "@tsparticles/engine";
73
+ import { loadInteractivityPlugin } from "@tsparticles/plugin-interactivity";
70
74
  import { loadParticlesAttractInteraction } from "@tsparticles/interaction-particles-attract";
71
75
 
72
76
  (async () => {
77
+ await loadInteractivityPlugin(tsParticles);
73
78
  await loadParticlesAttractInteraction(tsParticles);
74
79
  })();
75
80
  ```
@@ -1,23 +1,38 @@
1
- import { getDistances, getRangeValue } from "@tsparticles/engine";
1
+ import { getDistances, getRangeValue, isNull } from "@tsparticles/engine";
2
+ import { Attract } from "./Options/Classes/Attract.js";
2
3
  import { ParticlesInteractorBase } from "@tsparticles/plugin-interactivity";
3
4
  const attractFactor = 1000, identity = 1;
4
5
  export class Attractor extends ParticlesInteractorBase {
6
+ _maxDistance;
5
7
  constructor(container) {
6
8
  super(container);
9
+ this._maxDistance = 0;
10
+ }
11
+ get maxDistance() {
12
+ return this._maxDistance;
7
13
  }
8
14
  clear() {
9
15
  }
10
16
  init() {
11
17
  }
12
18
  interact(p1) {
19
+ if (!p1.options.attract?.enable) {
20
+ return;
21
+ }
13
22
  const container = this.container;
14
- p1.attractDistance ??= getRangeValue(p1.options.move.attract.distance) * container.retina.pixelRatio;
15
- const distance = p1.attractDistance, pos1 = p1.getPosition(), query = container.particles.quadTree.queryCircle(pos1, distance);
23
+ if (isNull(p1.attractDistance)) {
24
+ const attractDistance = getRangeValue(p1.options.attract.distance);
25
+ if (attractDistance > this._maxDistance) {
26
+ this._maxDistance = attractDistance;
27
+ }
28
+ p1.attractDistance = attractDistance * container.retina.pixelRatio;
29
+ }
30
+ const distance = p1.attractDistance, pos1 = p1.getPosition(), query = container.particles.grid.queryCircle(pos1, distance);
16
31
  for (const p2 of query) {
17
- if (p1 === p2 || !p2.options.move.attract.enable || p2.destroyed || p2.spawning) {
32
+ if (p1 === p2 || !p2.options.attract?.enable || p2.destroyed || p2.spawning) {
18
33
  continue;
19
34
  }
20
- const pos2 = p2.getPosition(), { dx, dy } = getDistances(pos1, pos2), rotate = p1.options.move.attract.rotate, ax = dx / (rotate.x * attractFactor), ay = dy / (rotate.y * attractFactor), p1Factor = p2.size.value / p1.size.value, p2Factor = identity / p1Factor;
35
+ const pos2 = p2.getPosition(), { dx, dy } = getDistances(pos1, pos2), rotate = p1.options.attract.rotate, ax = dx / (rotate.x * attractFactor), ay = dy / (rotate.y * attractFactor), p1Factor = p2.size.value / p1.size.value, p2Factor = identity / p1Factor;
21
36
  p1.velocity.x -= ax * p1Factor;
22
37
  p1.velocity.y -= ay * p1Factor;
23
38
  p2.velocity.x += ax * p2Factor;
@@ -25,7 +40,13 @@ export class Attractor extends ParticlesInteractorBase {
25
40
  }
26
41
  }
27
42
  isEnabled(particle) {
28
- return particle.options.move.attract.enable;
43
+ return particle.options.attract?.enable ?? false;
44
+ }
45
+ loadParticlesOptions(options, ...sources) {
46
+ options.attract ??= new Attract();
47
+ for (const source of sources) {
48
+ options.attract.load(source?.attract);
49
+ }
29
50
  }
30
51
  reset() {
31
52
  }
@@ -0,0 +1,35 @@
1
+ import { isNull, setRangeValue, } from "@tsparticles/engine";
2
+ export class Attract {
3
+ distance;
4
+ enable;
5
+ rotate;
6
+ constructor() {
7
+ this.distance = 200;
8
+ this.enable = false;
9
+ this.rotate = {
10
+ x: 3000,
11
+ y: 3000,
12
+ };
13
+ }
14
+ load(data) {
15
+ if (isNull(data)) {
16
+ return;
17
+ }
18
+ if (data.distance !== undefined) {
19
+ this.distance = setRangeValue(data.distance);
20
+ }
21
+ if (data.enable !== undefined) {
22
+ this.enable = data.enable;
23
+ }
24
+ if (data.rotate) {
25
+ const rotateX = data.rotate.x;
26
+ if (rotateX !== undefined) {
27
+ this.rotate.x = rotateX;
28
+ }
29
+ const rotateY = data.rotate.y;
30
+ if (rotateY !== undefined) {
31
+ this.rotate.y = rotateY;
32
+ }
33
+ }
34
+ }
35
+ }
package/browser/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  export async function loadParticlesAttractInteraction(engine) {
2
- engine.checkVersion("4.0.0-alpha.8");
3
- await engine.register(async (e) => {
4
- const { loadInteractivityPlugin } = await import("@tsparticles/plugin-interactivity");
5
- await loadInteractivityPlugin(e);
6
- e.addInteractor?.("particlesAttract", async (container) => {
2
+ engine.checkVersion("4.0.0-beta.1");
3
+ await engine.pluginManager.register(async (e) => {
4
+ const { ensureInteractivityPluginLoaded } = await import("@tsparticles/plugin-interactivity");
5
+ ensureInteractivityPluginLoaded(e);
6
+ e.pluginManager.addInteractor?.("particlesAttract", async (container) => {
7
7
  const { Attractor } = await import("./Attractor.js");
8
8
  return new Attractor(container);
9
9
  });
package/cjs/Attractor.js CHANGED
@@ -1,23 +1,38 @@
1
- import { getDistances, getRangeValue } from "@tsparticles/engine";
1
+ import { getDistances, getRangeValue, isNull } from "@tsparticles/engine";
2
+ import { Attract } from "./Options/Classes/Attract.js";
2
3
  import { ParticlesInteractorBase } from "@tsparticles/plugin-interactivity";
3
4
  const attractFactor = 1000, identity = 1;
4
5
  export class Attractor extends ParticlesInteractorBase {
6
+ _maxDistance;
5
7
  constructor(container) {
6
8
  super(container);
9
+ this._maxDistance = 0;
10
+ }
11
+ get maxDistance() {
12
+ return this._maxDistance;
7
13
  }
8
14
  clear() {
9
15
  }
10
16
  init() {
11
17
  }
12
18
  interact(p1) {
19
+ if (!p1.options.attract?.enable) {
20
+ return;
21
+ }
13
22
  const container = this.container;
14
- p1.attractDistance ??= getRangeValue(p1.options.move.attract.distance) * container.retina.pixelRatio;
15
- const distance = p1.attractDistance, pos1 = p1.getPosition(), query = container.particles.quadTree.queryCircle(pos1, distance);
23
+ if (isNull(p1.attractDistance)) {
24
+ const attractDistance = getRangeValue(p1.options.attract.distance);
25
+ if (attractDistance > this._maxDistance) {
26
+ this._maxDistance = attractDistance;
27
+ }
28
+ p1.attractDistance = attractDistance * container.retina.pixelRatio;
29
+ }
30
+ const distance = p1.attractDistance, pos1 = p1.getPosition(), query = container.particles.grid.queryCircle(pos1, distance);
16
31
  for (const p2 of query) {
17
- if (p1 === p2 || !p2.options.move.attract.enable || p2.destroyed || p2.spawning) {
32
+ if (p1 === p2 || !p2.options.attract?.enable || p2.destroyed || p2.spawning) {
18
33
  continue;
19
34
  }
20
- const pos2 = p2.getPosition(), { dx, dy } = getDistances(pos1, pos2), rotate = p1.options.move.attract.rotate, ax = dx / (rotate.x * attractFactor), ay = dy / (rotate.y * attractFactor), p1Factor = p2.size.value / p1.size.value, p2Factor = identity / p1Factor;
35
+ const pos2 = p2.getPosition(), { dx, dy } = getDistances(pos1, pos2), rotate = p1.options.attract.rotate, ax = dx / (rotate.x * attractFactor), ay = dy / (rotate.y * attractFactor), p1Factor = p2.size.value / p1.size.value, p2Factor = identity / p1Factor;
21
36
  p1.velocity.x -= ax * p1Factor;
22
37
  p1.velocity.y -= ay * p1Factor;
23
38
  p2.velocity.x += ax * p2Factor;
@@ -25,7 +40,13 @@ export class Attractor extends ParticlesInteractorBase {
25
40
  }
26
41
  }
27
42
  isEnabled(particle) {
28
- return particle.options.move.attract.enable;
43
+ return particle.options.attract?.enable ?? false;
44
+ }
45
+ loadParticlesOptions(options, ...sources) {
46
+ options.attract ??= new Attract();
47
+ for (const source of sources) {
48
+ options.attract.load(source?.attract);
49
+ }
29
50
  }
30
51
  reset() {
31
52
  }
@@ -0,0 +1,35 @@
1
+ import { isNull, setRangeValue, } from "@tsparticles/engine";
2
+ export class Attract {
3
+ distance;
4
+ enable;
5
+ rotate;
6
+ constructor() {
7
+ this.distance = 200;
8
+ this.enable = false;
9
+ this.rotate = {
10
+ x: 3000,
11
+ y: 3000,
12
+ };
13
+ }
14
+ load(data) {
15
+ if (isNull(data)) {
16
+ return;
17
+ }
18
+ if (data.distance !== undefined) {
19
+ this.distance = setRangeValue(data.distance);
20
+ }
21
+ if (data.enable !== undefined) {
22
+ this.enable = data.enable;
23
+ }
24
+ if (data.rotate) {
25
+ const rotateX = data.rotate.x;
26
+ if (rotateX !== undefined) {
27
+ this.rotate.x = rotateX;
28
+ }
29
+ const rotateY = data.rotate.y;
30
+ if (rotateY !== undefined) {
31
+ this.rotate.y = rotateY;
32
+ }
33
+ }
34
+ }
35
+ }
package/cjs/Types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/cjs/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  export async function loadParticlesAttractInteraction(engine) {
2
- engine.checkVersion("4.0.0-alpha.8");
3
- await engine.register(async (e) => {
4
- const { loadInteractivityPlugin } = await import("@tsparticles/plugin-interactivity");
5
- await loadInteractivityPlugin(e);
6
- e.addInteractor?.("particlesAttract", async (container) => {
2
+ engine.checkVersion("4.0.0-beta.1");
3
+ await engine.pluginManager.register(async (e) => {
4
+ const { ensureInteractivityPluginLoaded } = await import("@tsparticles/plugin-interactivity");
5
+ ensureInteractivityPluginLoaded(e);
6
+ e.pluginManager.addInteractor?.("particlesAttract", async (container) => {
7
7
  const { Attractor } = await import("./Attractor.js");
8
8
  return new Attractor(container);
9
9
  });
@@ -4,7 +4,7 @@
4
4
  * Demo / Generator : https://particles.js.org/
5
5
  * GitHub : https://www.github.com/matteobruni/tsparticles
6
6
  * How to use? : Check the GitHub README
7
- * v4.0.0-alpha.8
7
+ * v4.0.0-beta.1
8
8
  */
9
9
  "use strict";
10
10
  /*
@@ -23,7 +23,17 @@
23
23
  \***********************************/
24
24
  (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
25
25
 
26
- eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ Attractor: () => (/* binding */ Attractor)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n/* harmony import */ var _tsparticles_plugin_interactivity__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @tsparticles/plugin-interactivity */ \"@tsparticles/plugin-interactivity\");\n\n\nconst attractFactor = 1000,\n identity = 1;\nclass Attractor extends _tsparticles_plugin_interactivity__WEBPACK_IMPORTED_MODULE_1__.ParticlesInteractorBase {\n constructor(container) {\n super(container);\n }\n clear() {}\n init() {}\n interact(p1) {\n const container = this.container;\n p1.attractDistance ??= (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRangeValue)(p1.options.move.attract.distance) * container.retina.pixelRatio;\n const distance = p1.attractDistance,\n pos1 = p1.getPosition(),\n query = container.particles.quadTree.queryCircle(pos1, distance);\n for (const p2 of query) {\n if (p1 === p2 || !p2.options.move.attract.enable || p2.destroyed || p2.spawning) {\n continue;\n }\n const pos2 = p2.getPosition(),\n {\n dx,\n dy\n } = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getDistances)(pos1, pos2),\n rotate = p1.options.move.attract.rotate,\n ax = dx / (rotate.x * attractFactor),\n ay = dy / (rotate.y * attractFactor),\n p1Factor = p2.size.value / p1.size.value,\n p2Factor = identity / p1Factor;\n p1.velocity.x -= ax * p1Factor;\n p1.velocity.y -= ay * p1Factor;\n p2.velocity.x += ax * p2Factor;\n p2.velocity.y += ay * p2Factor;\n }\n }\n isEnabled(particle) {\n return particle.options.move.attract.enable;\n }\n reset() {}\n}\n\n//# sourceURL=webpack://@tsparticles/interaction-particles-attract/./dist/browser/Attractor.js?\n}");
26
+ eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ Attractor: () => (/* binding */ Attractor)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n/* harmony import */ var _Options_Classes_Attract_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./Options/Classes/Attract.js */ \"./dist/browser/Options/Classes/Attract.js\");\n/* harmony import */ var _tsparticles_plugin_interactivity__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @tsparticles/plugin-interactivity */ \"@tsparticles/plugin-interactivity\");\n\n\n\nconst attractFactor = 1000, identity = 1;\nclass Attractor extends _tsparticles_plugin_interactivity__WEBPACK_IMPORTED_MODULE_2__.ParticlesInteractorBase {\n _maxDistance;\n constructor(container){\n super(container);\n this._maxDistance = 0;\n }\n get maxDistance() {\n return this._maxDistance;\n }\n clear() {}\n init() {}\n interact(p1) {\n if (!p1.options.attract?.enable) {\n return;\n }\n const container = this.container;\n if ((0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.isNull)(p1.attractDistance)) {\n const attractDistance = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRangeValue)(p1.options.attract.distance);\n if (attractDistance > this._maxDistance) {\n this._maxDistance = attractDistance;\n }\n p1.attractDistance = attractDistance * container.retina.pixelRatio;\n }\n const distance = p1.attractDistance, pos1 = p1.getPosition(), query = container.particles.grid.queryCircle(pos1, distance);\n for (const p2 of query){\n if (p1 === p2 || !p2.options.attract?.enable || p2.destroyed || p2.spawning) {\n continue;\n }\n const pos2 = p2.getPosition(), { dx, dy } = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getDistances)(pos1, pos2), rotate = p1.options.attract.rotate, ax = dx / (rotate.x * attractFactor), ay = dy / (rotate.y * attractFactor), p1Factor = p2.size.value / p1.size.value, p2Factor = identity / p1Factor;\n p1.velocity.x -= ax * p1Factor;\n p1.velocity.y -= ay * p1Factor;\n p2.velocity.x += ax * p2Factor;\n p2.velocity.y += ay * p2Factor;\n }\n }\n isEnabled(particle) {\n return particle.options.attract?.enable ?? false;\n }\n loadParticlesOptions(options, ...sources) {\n options.attract ??= new _Options_Classes_Attract_js__WEBPACK_IMPORTED_MODULE_1__.Attract();\n for (const source of sources){\n options.attract.load(source?.attract);\n }\n }\n reset() {}\n}\n\n\n//# sourceURL=webpack://@tsparticles/interaction-particles-attract/./dist/browser/Attractor.js?\n}");
27
+
28
+ /***/ },
29
+
30
+ /***/ "./dist/browser/Options/Classes/Attract.js"
31
+ /*!*************************************************!*\
32
+ !*** ./dist/browser/Options/Classes/Attract.js ***!
33
+ \*************************************************/
34
+ (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
35
+
36
+ eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ Attract: () => (/* binding */ Attract)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n\nclass Attract {\n distance;\n enable;\n rotate;\n constructor(){\n this.distance = 200;\n this.enable = false;\n this.rotate = {\n x: 3000,\n y: 3000\n };\n }\n load(data) {\n if ((0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.isNull)(data)) {\n return;\n }\n if (data.distance !== undefined) {\n this.distance = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.setRangeValue)(data.distance);\n }\n if (data.enable !== undefined) {\n this.enable = data.enable;\n }\n if (data.rotate) {\n const rotateX = data.rotate.x;\n if (rotateX !== undefined) {\n this.rotate.x = rotateX;\n }\n const rotateY = data.rotate.y;\n if (rotateY !== undefined) {\n this.rotate.y = rotateY;\n }\n }\n }\n}\n\n\n//# sourceURL=webpack://@tsparticles/interaction-particles-attract/./dist/browser/Options/Classes/Attract.js?\n}");
27
37
 
28
38
  /***/ }
29
39
 
package/esm/Attractor.js CHANGED
@@ -1,23 +1,38 @@
1
- import { getDistances, getRangeValue } from "@tsparticles/engine";
1
+ import { getDistances, getRangeValue, isNull } from "@tsparticles/engine";
2
+ import { Attract } from "./Options/Classes/Attract.js";
2
3
  import { ParticlesInteractorBase } from "@tsparticles/plugin-interactivity";
3
4
  const attractFactor = 1000, identity = 1;
4
5
  export class Attractor extends ParticlesInteractorBase {
6
+ _maxDistance;
5
7
  constructor(container) {
6
8
  super(container);
9
+ this._maxDistance = 0;
10
+ }
11
+ get maxDistance() {
12
+ return this._maxDistance;
7
13
  }
8
14
  clear() {
9
15
  }
10
16
  init() {
11
17
  }
12
18
  interact(p1) {
19
+ if (!p1.options.attract?.enable) {
20
+ return;
21
+ }
13
22
  const container = this.container;
14
- p1.attractDistance ??= getRangeValue(p1.options.move.attract.distance) * container.retina.pixelRatio;
15
- const distance = p1.attractDistance, pos1 = p1.getPosition(), query = container.particles.quadTree.queryCircle(pos1, distance);
23
+ if (isNull(p1.attractDistance)) {
24
+ const attractDistance = getRangeValue(p1.options.attract.distance);
25
+ if (attractDistance > this._maxDistance) {
26
+ this._maxDistance = attractDistance;
27
+ }
28
+ p1.attractDistance = attractDistance * container.retina.pixelRatio;
29
+ }
30
+ const distance = p1.attractDistance, pos1 = p1.getPosition(), query = container.particles.grid.queryCircle(pos1, distance);
16
31
  for (const p2 of query) {
17
- if (p1 === p2 || !p2.options.move.attract.enable || p2.destroyed || p2.spawning) {
32
+ if (p1 === p2 || !p2.options.attract?.enable || p2.destroyed || p2.spawning) {
18
33
  continue;
19
34
  }
20
- const pos2 = p2.getPosition(), { dx, dy } = getDistances(pos1, pos2), rotate = p1.options.move.attract.rotate, ax = dx / (rotate.x * attractFactor), ay = dy / (rotate.y * attractFactor), p1Factor = p2.size.value / p1.size.value, p2Factor = identity / p1Factor;
35
+ const pos2 = p2.getPosition(), { dx, dy } = getDistances(pos1, pos2), rotate = p1.options.attract.rotate, ax = dx / (rotate.x * attractFactor), ay = dy / (rotate.y * attractFactor), p1Factor = p2.size.value / p1.size.value, p2Factor = identity / p1Factor;
21
36
  p1.velocity.x -= ax * p1Factor;
22
37
  p1.velocity.y -= ay * p1Factor;
23
38
  p2.velocity.x += ax * p2Factor;
@@ -25,7 +40,13 @@ export class Attractor extends ParticlesInteractorBase {
25
40
  }
26
41
  }
27
42
  isEnabled(particle) {
28
- return particle.options.move.attract.enable;
43
+ return particle.options.attract?.enable ?? false;
44
+ }
45
+ loadParticlesOptions(options, ...sources) {
46
+ options.attract ??= new Attract();
47
+ for (const source of sources) {
48
+ options.attract.load(source?.attract);
49
+ }
29
50
  }
30
51
  reset() {
31
52
  }
@@ -0,0 +1,35 @@
1
+ import { isNull, setRangeValue, } from "@tsparticles/engine";
2
+ export class Attract {
3
+ distance;
4
+ enable;
5
+ rotate;
6
+ constructor() {
7
+ this.distance = 200;
8
+ this.enable = false;
9
+ this.rotate = {
10
+ x: 3000,
11
+ y: 3000,
12
+ };
13
+ }
14
+ load(data) {
15
+ if (isNull(data)) {
16
+ return;
17
+ }
18
+ if (data.distance !== undefined) {
19
+ this.distance = setRangeValue(data.distance);
20
+ }
21
+ if (data.enable !== undefined) {
22
+ this.enable = data.enable;
23
+ }
24
+ if (data.rotate) {
25
+ const rotateX = data.rotate.x;
26
+ if (rotateX !== undefined) {
27
+ this.rotate.x = rotateX;
28
+ }
29
+ const rotateY = data.rotate.y;
30
+ if (rotateY !== undefined) {
31
+ this.rotate.y = rotateY;
32
+ }
33
+ }
34
+ }
35
+ }
@@ -0,0 +1 @@
1
+ export {};
package/esm/Types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/esm/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  export async function loadParticlesAttractInteraction(engine) {
2
- engine.checkVersion("4.0.0-alpha.8");
3
- await engine.register(async (e) => {
4
- const { loadInteractivityPlugin } = await import("@tsparticles/plugin-interactivity");
5
- await loadInteractivityPlugin(e);
6
- e.addInteractor?.("particlesAttract", async (container) => {
2
+ engine.checkVersion("4.0.0-beta.1");
3
+ await engine.pluginManager.register(async (e) => {
4
+ const { ensureInteractivityPluginLoaded } = await import("@tsparticles/plugin-interactivity");
5
+ ensureInteractivityPluginLoaded(e);
6
+ e.pluginManager.addInteractor?.("particlesAttract", async (container) => {
7
7
  const { Attractor } = await import("./Attractor.js");
8
8
  return new Attractor(container);
9
9
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsparticles/interaction-particles-attract",
3
- "version": "4.0.0-alpha.8",
3
+ "version": "4.0.0-beta.1",
4
4
  "description": "tsParticles attract particles interaction",
5
5
  "homepage": "https://particles.js.org",
6
6
  "repository": {
@@ -87,8 +87,8 @@
87
87
  "./package.json": "./package.json"
88
88
  },
89
89
  "dependencies": {
90
- "@tsparticles/engine": "4.0.0-alpha.8",
91
- "@tsparticles/plugin-interactivity": "4.0.0-alpha.8"
90
+ "@tsparticles/engine": "4.0.0-beta.1",
91
+ "@tsparticles/plugin-interactivity": "4.0.0-beta.1"
92
92
  },
93
93
  "publishConfig": {
94
94
  "access": "public"