@tsparticles/plugin-interactivity 4.0.0-alpha.4

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 (171) hide show
  1. package/284.min.js +2 -0
  2. package/284.min.js.LICENSE.txt +1 -0
  3. package/721.min.js +2 -0
  4. package/721.min.js.LICENSE.txt +1 -0
  5. package/LICENSE +21 -0
  6. package/README.md +74 -0
  7. package/browser/DivType.js +5 -0
  8. package/browser/ExternalInteractorBase.js +7 -0
  9. package/browser/IExternalInteractor.js +1 -0
  10. package/browser/IInteractivityData.js +1 -0
  11. package/browser/IInteractor.js +1 -0
  12. package/browser/IMouseData.js +1 -0
  13. package/browser/IParticlesInteractor.js +1 -0
  14. package/browser/InteractionManager.js +202 -0
  15. package/browser/InteractivityConstants.js +1 -0
  16. package/browser/InteractivityDetect.js +6 -0
  17. package/browser/InteractivityEventListeners.js +235 -0
  18. package/browser/InteractivityPlugin.js +48 -0
  19. package/browser/InteractivityPluginInstance.js +52 -0
  20. package/browser/InteractorType.js +5 -0
  21. package/browser/Options/Classes/Events/ClickEvent.js +18 -0
  22. package/browser/Options/Classes/Events/DivEvent.js +27 -0
  23. package/browser/Options/Classes/Events/Events.js +26 -0
  24. package/browser/Options/Classes/Events/HoverEvent.js +18 -0
  25. package/browser/Options/Classes/Interactivity.js +22 -0
  26. package/browser/Options/Classes/Modes/Modes.js +25 -0
  27. package/browser/Options/Interfaces/Events/IClickEvent.js +1 -0
  28. package/browser/Options/Interfaces/Events/IDivEvent.js +1 -0
  29. package/browser/Options/Interfaces/Events/IEvents.js +1 -0
  30. package/browser/Options/Interfaces/Events/IHoverEvent.js +1 -0
  31. package/browser/Options/Interfaces/IInteractivity.js +1 -0
  32. package/browser/Options/Interfaces/Modes/IModeDiv.js +1 -0
  33. package/browser/Options/Interfaces/Modes/IModes.js +1 -0
  34. package/browser/ParticlesInteractorBase.js +7 -0
  35. package/browser/index.js +38 -0
  36. package/browser/package.json +1 -0
  37. package/browser/types.js +1 -0
  38. package/browser/utils.js +32 -0
  39. package/cjs/DivType.js +5 -0
  40. package/cjs/ExternalInteractorBase.js +7 -0
  41. package/cjs/IExternalInteractor.js +1 -0
  42. package/cjs/IInteractivityData.js +1 -0
  43. package/cjs/IInteractor.js +1 -0
  44. package/cjs/IMouseData.js +1 -0
  45. package/cjs/IParticlesInteractor.js +1 -0
  46. package/cjs/InteractionManager.js +202 -0
  47. package/cjs/InteractivityConstants.js +1 -0
  48. package/cjs/InteractivityDetect.js +6 -0
  49. package/cjs/InteractivityEventListeners.js +235 -0
  50. package/cjs/InteractivityPlugin.js +48 -0
  51. package/cjs/InteractivityPluginInstance.js +52 -0
  52. package/cjs/InteractorType.js +5 -0
  53. package/cjs/Options/Classes/Events/ClickEvent.js +18 -0
  54. package/cjs/Options/Classes/Events/DivEvent.js +27 -0
  55. package/cjs/Options/Classes/Events/Events.js +26 -0
  56. package/cjs/Options/Classes/Events/HoverEvent.js +18 -0
  57. package/cjs/Options/Classes/Interactivity.js +22 -0
  58. package/cjs/Options/Classes/Modes/Modes.js +25 -0
  59. package/cjs/Options/Interfaces/Events/IClickEvent.js +1 -0
  60. package/cjs/Options/Interfaces/Events/IDivEvent.js +1 -0
  61. package/cjs/Options/Interfaces/Events/IEvents.js +1 -0
  62. package/cjs/Options/Interfaces/Events/IHoverEvent.js +1 -0
  63. package/cjs/Options/Interfaces/IInteractivity.js +1 -0
  64. package/cjs/Options/Interfaces/Modes/IModeDiv.js +1 -0
  65. package/cjs/Options/Interfaces/Modes/IModes.js +1 -0
  66. package/cjs/ParticlesInteractorBase.js +7 -0
  67. package/cjs/index.js +38 -0
  68. package/cjs/package.json +1 -0
  69. package/cjs/types.js +1 -0
  70. package/cjs/utils.js +32 -0
  71. package/dist_browser_InteractivityPluginInstance_js.js +50 -0
  72. package/dist_browser_InteractivityPlugin_js.js +70 -0
  73. package/esm/DivType.js +5 -0
  74. package/esm/ExternalInteractorBase.js +7 -0
  75. package/esm/IExternalInteractor.js +1 -0
  76. package/esm/IInteractivityData.js +1 -0
  77. package/esm/IInteractor.js +1 -0
  78. package/esm/IMouseData.js +1 -0
  79. package/esm/IParticlesInteractor.js +1 -0
  80. package/esm/InteractionManager.js +202 -0
  81. package/esm/InteractivityConstants.js +1 -0
  82. package/esm/InteractivityDetect.js +6 -0
  83. package/esm/InteractivityEventListeners.js +235 -0
  84. package/esm/InteractivityPlugin.js +48 -0
  85. package/esm/InteractivityPluginInstance.js +52 -0
  86. package/esm/InteractorType.js +5 -0
  87. package/esm/Options/Classes/Events/ClickEvent.js +18 -0
  88. package/esm/Options/Classes/Events/DivEvent.js +27 -0
  89. package/esm/Options/Classes/Events/Events.js +26 -0
  90. package/esm/Options/Classes/Events/HoverEvent.js +18 -0
  91. package/esm/Options/Classes/Interactivity.js +22 -0
  92. package/esm/Options/Classes/Modes/Modes.js +25 -0
  93. package/esm/Options/Interfaces/Events/IClickEvent.js +1 -0
  94. package/esm/Options/Interfaces/Events/IDivEvent.js +1 -0
  95. package/esm/Options/Interfaces/Events/IEvents.js +1 -0
  96. package/esm/Options/Interfaces/Events/IHoverEvent.js +1 -0
  97. package/esm/Options/Interfaces/IInteractivity.js +1 -0
  98. package/esm/Options/Interfaces/Modes/IModeDiv.js +1 -0
  99. package/esm/Options/Interfaces/Modes/IModes.js +1 -0
  100. package/esm/ParticlesInteractorBase.js +7 -0
  101. package/esm/index.js +38 -0
  102. package/esm/package.json +1 -0
  103. package/esm/types.js +1 -0
  104. package/esm/utils.js +32 -0
  105. package/package.json +95 -0
  106. package/report.html +40 -0
  107. package/tsparticles.plugin.interactivity.js +395 -0
  108. package/tsparticles.plugin.interactivity.min.js +2 -0
  109. package/tsparticles.plugin.interactivity.min.js.LICENSE.txt +1 -0
  110. package/types/DivType.d.ts +4 -0
  111. package/types/ExternalInteractorBase.d.ts +15 -0
  112. package/types/IExternalInteractor.d.ts +11 -0
  113. package/types/IInteractivityData.d.ts +6 -0
  114. package/types/IInteractor.d.ts +12 -0
  115. package/types/IMouseData.d.ts +9 -0
  116. package/types/IParticlesInteractor.d.ts +7 -0
  117. package/types/InteractionManager.d.ts +27 -0
  118. package/types/InteractivityConstants.d.ts +1 -0
  119. package/types/InteractivityDetect.d.ts +5 -0
  120. package/types/InteractivityEventListeners.d.ts +23 -0
  121. package/types/InteractivityPlugin.d.ts +11 -0
  122. package/types/InteractivityPluginInstance.d.ts +20 -0
  123. package/types/InteractorType.d.ts +4 -0
  124. package/types/Options/Classes/Events/ClickEvent.d.ts +8 -0
  125. package/types/Options/Classes/Events/DivEvent.d.ts +11 -0
  126. package/types/Options/Classes/Events/Events.d.ts +12 -0
  127. package/types/Options/Classes/Events/HoverEvent.d.ts +8 -0
  128. package/types/Options/Classes/Interactivity.d.ts +14 -0
  129. package/types/Options/Classes/Modes/Modes.d.ts +10 -0
  130. package/types/Options/Interfaces/Events/IClickEvent.d.ts +5 -0
  131. package/types/Options/Interfaces/Events/IDivEvent.d.ts +8 -0
  132. package/types/Options/Interfaces/Events/IEvents.d.ts +9 -0
  133. package/types/Options/Interfaces/Events/IHoverEvent.d.ts +5 -0
  134. package/types/Options/Interfaces/IInteractivity.d.ts +9 -0
  135. package/types/Options/Interfaces/Modes/IModeDiv.d.ts +4 -0
  136. package/types/Options/Interfaces/Modes/IModes.d.ts +1 -0
  137. package/types/ParticlesInteractorBase.d.ts +15 -0
  138. package/types/index.d.ts +19 -0
  139. package/types/types.d.ts +32 -0
  140. package/types/utils.d.ts +7 -0
  141. package/umd/DivType.js +18 -0
  142. package/umd/ExternalInteractorBase.js +21 -0
  143. package/umd/IExternalInteractor.js +12 -0
  144. package/umd/IInteractivityData.js +12 -0
  145. package/umd/IInteractor.js +12 -0
  146. package/umd/IMouseData.js +12 -0
  147. package/umd/IParticlesInteractor.js +12 -0
  148. package/umd/InteractionManager.js +216 -0
  149. package/umd/InteractivityConstants.js +14 -0
  150. package/umd/InteractivityDetect.js +19 -0
  151. package/umd/InteractivityEventListeners.js +249 -0
  152. package/umd/InteractivityPlugin.js +96 -0
  153. package/umd/InteractivityPluginInstance.js +66 -0
  154. package/umd/InteractorType.js +18 -0
  155. package/umd/Options/Classes/Events/ClickEvent.js +32 -0
  156. package/umd/Options/Classes/Events/DivEvent.js +41 -0
  157. package/umd/Options/Classes/Events/Events.js +40 -0
  158. package/umd/Options/Classes/Events/HoverEvent.js +32 -0
  159. package/umd/Options/Classes/Interactivity.js +36 -0
  160. package/umd/Options/Classes/Modes/Modes.js +39 -0
  161. package/umd/Options/Interfaces/Events/IClickEvent.js +12 -0
  162. package/umd/Options/Interfaces/Events/IDivEvent.js +12 -0
  163. package/umd/Options/Interfaces/Events/IEvents.js +12 -0
  164. package/umd/Options/Interfaces/Events/IHoverEvent.js +12 -0
  165. package/umd/Options/Interfaces/IInteractivity.js +12 -0
  166. package/umd/Options/Interfaces/Modes/IModeDiv.js +12 -0
  167. package/umd/Options/Interfaces/Modes/IModes.js +12 -0
  168. package/umd/ParticlesInteractorBase.js +21 -0
  169. package/umd/index.js +88 -0
  170. package/umd/types.js +12 -0
  171. package/umd/utils.js +48 -0
@@ -0,0 +1,52 @@
1
+ import { InteractionManager } from "./InteractionManager.js";
2
+ import { Interactivity } from "./Options/Classes/Interactivity.js";
3
+ export class InteractivityPluginInstance {
4
+ constructor(engine, container) {
5
+ this._container = container;
6
+ this._engine = engine;
7
+ this.interactionManager = new InteractionManager(engine, container);
8
+ this._container.addClickHandler = (callback) => {
9
+ this.interactionManager.addClickHandler(callback);
10
+ };
11
+ }
12
+ addClickHandler(callback) {
13
+ this.interactionManager.addClickHandler(callback);
14
+ }
15
+ clearClickHandlers() {
16
+ this.interactionManager.clearClickHandlers();
17
+ }
18
+ destroy() {
19
+ this.clearClickHandlers();
20
+ this._engine.interactors?.delete(this._container);
21
+ }
22
+ particleCreated(particle) {
23
+ const interactivityParticle = particle, interactivity = new Interactivity(this._engine, this._container);
24
+ interactivity.load(this._container.actualOptions.interactivity);
25
+ interactivity.load(interactivityParticle.options.interactivity);
26
+ interactivityParticle.interactivity = interactivity;
27
+ }
28
+ particleReset(particle) {
29
+ this.interactionManager.reset(particle);
30
+ }
31
+ postParticleUpdate(particle, delta) {
32
+ this.interactionManager.particlesInteract(particle, delta);
33
+ }
34
+ postUpdate(delta) {
35
+ this.interactionManager.externalInteract(delta);
36
+ }
37
+ async preInit() {
38
+ await this.interactionManager.init();
39
+ }
40
+ async redrawInit() {
41
+ await this.interactionManager.init();
42
+ }
43
+ start() {
44
+ this.interactionManager.addListeners();
45
+ this.interactionManager.startObserving();
46
+ return Promise.resolve();
47
+ }
48
+ stop() {
49
+ this.interactionManager.removeListeners();
50
+ this.interactionManager.stopObserving();
51
+ }
52
+ }
@@ -0,0 +1,5 @@
1
+ export var InteractorType;
2
+ (function (InteractorType) {
3
+ InteractorType["external"] = "external";
4
+ InteractorType["particles"] = "particles";
5
+ })(InteractorType || (InteractorType = {}));
@@ -0,0 +1,18 @@
1
+ import { isNull } from "@tsparticles/engine";
2
+ export class ClickEvent {
3
+ constructor() {
4
+ this.enable = false;
5
+ this.mode = [];
6
+ }
7
+ load(data) {
8
+ if (isNull(data)) {
9
+ return;
10
+ }
11
+ if (data.enable !== undefined) {
12
+ this.enable = data.enable;
13
+ }
14
+ if (data.mode !== undefined) {
15
+ this.mode = data.mode;
16
+ }
17
+ }
18
+ }
@@ -0,0 +1,27 @@
1
+ import { isNull } from "@tsparticles/engine";
2
+ import { DivType } from "../../../DivType.js";
3
+ export class DivEvent {
4
+ constructor() {
5
+ this.selectors = [];
6
+ this.enable = false;
7
+ this.mode = [];
8
+ this.type = DivType.circle;
9
+ }
10
+ load(data) {
11
+ if (isNull(data)) {
12
+ return;
13
+ }
14
+ if (data.selectors !== undefined) {
15
+ this.selectors = data.selectors;
16
+ }
17
+ if (data.enable !== undefined) {
18
+ this.enable = data.enable;
19
+ }
20
+ if (data.mode !== undefined) {
21
+ this.mode = data.mode;
22
+ }
23
+ if (data.type !== undefined) {
24
+ this.type = data.type;
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,26 @@
1
+ import { executeOnSingleOrMultiple, isNull, } from "@tsparticles/engine";
2
+ import { ClickEvent } from "./ClickEvent.js";
3
+ import { DivEvent } from "./DivEvent.js";
4
+ import { HoverEvent } from "./HoverEvent.js";
5
+ export class Events {
6
+ constructor() {
7
+ this.onClick = new ClickEvent();
8
+ this.onDiv = new DivEvent();
9
+ this.onHover = new HoverEvent();
10
+ }
11
+ load(data) {
12
+ if (isNull(data)) {
13
+ return;
14
+ }
15
+ this.onClick.load(data.onClick);
16
+ const onDiv = data.onDiv;
17
+ if (onDiv !== undefined) {
18
+ this.onDiv = executeOnSingleOrMultiple(onDiv, t => {
19
+ const tmp = new DivEvent();
20
+ tmp.load(t);
21
+ return tmp;
22
+ });
23
+ }
24
+ this.onHover.load(data.onHover);
25
+ }
26
+ }
@@ -0,0 +1,18 @@
1
+ import { isNull } from "@tsparticles/engine";
2
+ export class HoverEvent {
3
+ constructor() {
4
+ this.enable = false;
5
+ this.mode = [];
6
+ }
7
+ load(data) {
8
+ if (isNull(data)) {
9
+ return;
10
+ }
11
+ if (data.enable !== undefined) {
12
+ this.enable = data.enable;
13
+ }
14
+ if (data.mode !== undefined) {
15
+ this.mode = data.mode;
16
+ }
17
+ }
18
+ }
@@ -0,0 +1,22 @@
1
+ import { isNull } from "@tsparticles/engine";
2
+ import { Events } from "./Events/Events.js";
3
+ import { InteractivityDetect } from "../../InteractivityDetect.js";
4
+ import { Modes } from "./Modes/Modes.js";
5
+ export class Interactivity {
6
+ constructor(engine, container) {
7
+ this.detectsOn = InteractivityDetect.window;
8
+ this.events = new Events();
9
+ this.modes = new Modes(engine, container);
10
+ }
11
+ load(data) {
12
+ if (isNull(data)) {
13
+ return;
14
+ }
15
+ const detectsOn = data.detectsOn;
16
+ if (detectsOn !== undefined) {
17
+ this.detectsOn = detectsOn;
18
+ }
19
+ this.events.load(data.events);
20
+ this.modes.load(data.modes);
21
+ }
22
+ }
@@ -0,0 +1,25 @@
1
+ import { isNull } from "@tsparticles/engine";
2
+ export class Modes {
3
+ constructor(engine, container) {
4
+ this._engine = engine;
5
+ this._container = container;
6
+ }
7
+ load(data) {
8
+ if (isNull(data)) {
9
+ return;
10
+ }
11
+ if (!this._container) {
12
+ return;
13
+ }
14
+ const interactors = this._engine.interactors?.get(this._container);
15
+ if (!interactors) {
16
+ return;
17
+ }
18
+ for (const interactor of interactors) {
19
+ if (!interactor.loadModeOptions) {
20
+ continue;
21
+ }
22
+ interactor.loadModeOptions(this, data);
23
+ }
24
+ }
25
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import { InteractorType } from "./InteractorType.js";
2
+ export class ParticlesInteractorBase {
3
+ constructor(container) {
4
+ this.type = InteractorType.particles;
5
+ this.container = container;
6
+ }
7
+ }
@@ -0,0 +1,38 @@
1
+ import { getItemsFromInitializer } from "@tsparticles/engine";
2
+ export function loadInteractivityPlugin(engine) {
3
+ engine.checkVersion("4.0.0-alpha.4");
4
+ engine.register(async (e) => {
5
+ const interactivityEngine = e, { InteractivityPlugin } = await import("./InteractivityPlugin.js");
6
+ interactivityEngine.addPlugin(new InteractivityPlugin(interactivityEngine));
7
+ interactivityEngine.interactorsInitializers ??= new Map();
8
+ interactivityEngine.interactors ??= new Map();
9
+ interactivityEngine.addInteractor = (name, interactorInitializer) => {
10
+ interactivityEngine.interactorsInitializers ??= new Map();
11
+ interactivityEngine.interactorsInitializers.set(name, interactorInitializer);
12
+ };
13
+ interactivityEngine.getInteractors = async (container, force = false) => {
14
+ interactivityEngine.interactors ??= new Map();
15
+ interactivityEngine.interactorsInitializers ??= new Map();
16
+ return getItemsFromInitializer(container, interactivityEngine.interactors, interactivityEngine.interactorsInitializers, force);
17
+ };
18
+ interactivityEngine.setOnClickHandler = (callback) => {
19
+ const { items } = interactivityEngine;
20
+ if (!items.length) {
21
+ throw new Error("Click handlers can only be set after calling tsParticles.load()");
22
+ }
23
+ items.forEach(item => {
24
+ const interactivityContainer = item;
25
+ interactivityContainer.addClickHandler?.(callback);
26
+ });
27
+ };
28
+ });
29
+ }
30
+ export * from "./DivType.js";
31
+ export * from "./ExternalInteractorBase.js";
32
+ export * from "./InteractivityConstants.js";
33
+ export * from "./InteractivityDetect.js";
34
+ export * from "./InteractorType.js";
35
+ export * from "./ParticlesInteractorBase.js";
36
+ export * from "./utils.js";
37
+ export * from "./Options/Classes/Events/DivEvent.js";
38
+ export * from "./Options/Classes/Modes/Modes.js";
@@ -0,0 +1 @@
1
+ { "type": "module" }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ import { executeOnSingleOrMultiple, findItemFromSingleOrMultiple, isArray, isInArray, } from "@tsparticles/engine";
2
+ function checkSelector(element, selectors) {
3
+ const res = executeOnSingleOrMultiple(selectors, selector => {
4
+ return element.matches(selector);
5
+ });
6
+ return isArray(res) ? res.some(t => t) : res;
7
+ }
8
+ export function isDivModeEnabled(mode, divs) {
9
+ return !!findItemFromSingleOrMultiple(divs, t => t.enable && isInArray(mode, t.mode));
10
+ }
11
+ export function divModeExecute(mode, divs, callback) {
12
+ executeOnSingleOrMultiple(divs, div => {
13
+ const divMode = div.mode, divEnabled = div.enable;
14
+ if (divEnabled && isInArray(mode, divMode)) {
15
+ singleDivModeExecute(div, callback);
16
+ }
17
+ });
18
+ }
19
+ export function singleDivModeExecute(div, callback) {
20
+ const selectors = div.selectors;
21
+ executeOnSingleOrMultiple(selectors, selector => {
22
+ callback(selector, div);
23
+ });
24
+ }
25
+ export function divMode(divs, element) {
26
+ if (!element || !divs) {
27
+ return;
28
+ }
29
+ return findItemFromSingleOrMultiple(divs, div => {
30
+ return checkSelector(element, div.selectors);
31
+ });
32
+ }
package/cjs/DivType.js ADDED
@@ -0,0 +1,5 @@
1
+ export var DivType;
2
+ (function (DivType) {
3
+ DivType["circle"] = "circle";
4
+ DivType["rectangle"] = "rectangle";
5
+ })(DivType || (DivType = {}));
@@ -0,0 +1,7 @@
1
+ import { InteractorType } from "./InteractorType.js";
2
+ export class ExternalInteractorBase {
3
+ constructor(container) {
4
+ this.type = InteractorType.external;
5
+ this.container = container;
6
+ }
7
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,202 @@
1
+ import { clickRadius, minCoordinate, safeIntersectionObserver, touchEndLengthOffset, } from "@tsparticles/engine";
2
+ import { clickEvent, touchCancelEvent, touchEndEvent, touchMoveEvent, touchStartEvent, } from "./InteractivityConstants.js";
3
+ import { InteractivityEventListeners } from "./InteractivityEventListeners.js";
4
+ import { InteractorType } from "./InteractorType.js";
5
+ export class InteractionManager {
6
+ constructor(engine, container) {
7
+ this.container = container;
8
+ this._intersectionManager = entries => {
9
+ const { container } = this;
10
+ if (container.destroyed || !container.actualOptions.pauseOnOutsideViewport) {
11
+ return;
12
+ }
13
+ for (const entry of entries) {
14
+ if (entry.target !== this.interactivityData.element) {
15
+ continue;
16
+ }
17
+ if (entry.isIntersecting) {
18
+ container.play();
19
+ }
20
+ else {
21
+ container.pause();
22
+ }
23
+ }
24
+ };
25
+ this._engine = engine;
26
+ this._interactors = [];
27
+ this._externalInteractors = [];
28
+ this._particleInteractors = [];
29
+ this._clickHandlers = new Map();
30
+ this._eventListeners = new InteractivityEventListeners(container, this);
31
+ this.interactivityData = {
32
+ mouse: {
33
+ clicking: false,
34
+ inside: false,
35
+ },
36
+ };
37
+ this._intersectionObserver = safeIntersectionObserver(entries => {
38
+ this._intersectionManager(entries);
39
+ });
40
+ }
41
+ addClickHandler(callback) {
42
+ const { container, interactivityData } = this;
43
+ if (container.destroyed) {
44
+ return;
45
+ }
46
+ const el = interactivityData.element;
47
+ if (!el) {
48
+ return;
49
+ }
50
+ const clickOrTouchHandler = (e, pos, radius) => {
51
+ if (container.destroyed) {
52
+ return;
53
+ }
54
+ const pxRatio = container.retina.pixelRatio, posRetina = {
55
+ x: pos.x * pxRatio,
56
+ y: pos.y * pxRatio,
57
+ }, particles = container.particles.quadTree.queryCircle(posRetina, radius * pxRatio);
58
+ callback(e, particles);
59
+ }, clickHandler = (e) => {
60
+ if (container.destroyed) {
61
+ return;
62
+ }
63
+ const mouseEvent = e, pos = {
64
+ x: mouseEvent.offsetX,
65
+ y: mouseEvent.offsetY,
66
+ };
67
+ clickOrTouchHandler(e, pos, clickRadius);
68
+ }, touchStartHandler = () => {
69
+ if (container.destroyed) {
70
+ return;
71
+ }
72
+ touched = true;
73
+ touchMoved = false;
74
+ }, touchMoveHandler = () => {
75
+ if (container.destroyed) {
76
+ return;
77
+ }
78
+ touchMoved = true;
79
+ }, touchEndHandler = (e) => {
80
+ if (container.destroyed) {
81
+ return;
82
+ }
83
+ if (touched && !touchMoved) {
84
+ const touchEvent = e, lastTouch = touchEvent.touches[touchEvent.touches.length - touchEndLengthOffset];
85
+ if (!lastTouch) {
86
+ return;
87
+ }
88
+ const element = container.canvas.element, canvasRect = element ? element.getBoundingClientRect() : undefined, pos = {
89
+ x: lastTouch.clientX - (canvasRect ? canvasRect.left : minCoordinate),
90
+ y: lastTouch.clientY - (canvasRect ? canvasRect.top : minCoordinate),
91
+ };
92
+ clickOrTouchHandler(e, pos, Math.max(lastTouch.radiusX, lastTouch.radiusY));
93
+ }
94
+ touched = false;
95
+ touchMoved = false;
96
+ }, touchCancelHandler = () => {
97
+ if (container.destroyed) {
98
+ return;
99
+ }
100
+ touched = false;
101
+ touchMoved = false;
102
+ };
103
+ let touched = false, touchMoved = false;
104
+ this._clickHandlers.set(clickEvent, clickHandler);
105
+ this._clickHandlers.set(touchStartEvent, touchStartHandler);
106
+ this._clickHandlers.set(touchMoveEvent, touchMoveHandler);
107
+ this._clickHandlers.set(touchEndEvent, touchEndHandler);
108
+ this._clickHandlers.set(touchCancelEvent, touchCancelHandler);
109
+ for (const [key, handler] of this._clickHandlers) {
110
+ el.addEventListener(key, handler);
111
+ }
112
+ }
113
+ addListeners() {
114
+ this._eventListeners.addListeners();
115
+ }
116
+ clearClickHandlers() {
117
+ const { container, interactivityData } = this;
118
+ if (container.destroyed) {
119
+ return;
120
+ }
121
+ for (const [key, handler] of this._clickHandlers) {
122
+ interactivityData.element?.removeEventListener(key, handler);
123
+ }
124
+ this._clickHandlers.clear();
125
+ }
126
+ externalInteract(delta) {
127
+ for (const interactor of this._externalInteractors) {
128
+ const { interactivityData } = this;
129
+ if (interactor.isEnabled(interactivityData)) {
130
+ interactor.interact(interactivityData, delta);
131
+ }
132
+ }
133
+ }
134
+ handleClickMode(mode) {
135
+ if (this.container.destroyed) {
136
+ return;
137
+ }
138
+ const { interactivityData } = this;
139
+ for (const interactor of this._externalInteractors) {
140
+ interactor.handleClickMode?.(mode, interactivityData);
141
+ }
142
+ }
143
+ async init() {
144
+ const interactors = await this._engine.getInteractors?.(this.container, true);
145
+ if (!interactors) {
146
+ return;
147
+ }
148
+ this._interactors = interactors;
149
+ this._externalInteractors = [];
150
+ this._particleInteractors = [];
151
+ for (const interactor of this._interactors) {
152
+ switch (interactor.type) {
153
+ case InteractorType.external:
154
+ this._externalInteractors.push(interactor);
155
+ break;
156
+ case InteractorType.particles:
157
+ this._particleInteractors.push(interactor);
158
+ break;
159
+ }
160
+ interactor.init();
161
+ }
162
+ }
163
+ particlesInteract(particle, delta) {
164
+ const { interactivityData } = this;
165
+ for (const interactor of this._externalInteractors) {
166
+ interactor.clear(particle, delta);
167
+ }
168
+ for (const interactor of this._particleInteractors) {
169
+ if (interactor.isEnabled(particle, interactivityData)) {
170
+ interactor.interact(particle, interactivityData, delta);
171
+ }
172
+ }
173
+ }
174
+ removeListeners() {
175
+ this._eventListeners.removeListeners();
176
+ }
177
+ reset(particle) {
178
+ const { interactivityData } = this;
179
+ for (const interactor of this._externalInteractors) {
180
+ if (interactor.isEnabled(interactivityData)) {
181
+ interactor.reset(interactivityData, particle);
182
+ }
183
+ }
184
+ for (const interactor of this._particleInteractors) {
185
+ if (interactor.isEnabled(particle, interactivityData)) {
186
+ interactor.reset(interactivityData, particle);
187
+ }
188
+ }
189
+ }
190
+ startObserving() {
191
+ const { interactivityData } = this;
192
+ if (interactivityData.element instanceof HTMLElement && this._intersectionObserver) {
193
+ this._intersectionObserver.observe(interactivityData.element);
194
+ }
195
+ }
196
+ stopObserving() {
197
+ const { interactivityData } = this;
198
+ if (interactivityData.element instanceof HTMLElement && this._intersectionObserver) {
199
+ this._intersectionObserver.unobserve(interactivityData.element);
200
+ }
201
+ }
202
+ }
@@ -0,0 +1 @@
1
+ export const clickEvent = "click", mouseDownEvent = "pointerdown", mouseUpEvent = "pointerup", mouseLeaveEvent = "pointerleave", mouseOutEvent = "pointerout", mouseMoveEvent = "pointermove", touchStartEvent = "touchstart", touchEndEvent = "touchend", touchMoveEvent = "touchmove", touchCancelEvent = "touchcancel";
@@ -0,0 +1,6 @@
1
+ export var InteractivityDetect;
2
+ (function (InteractivityDetect) {
3
+ InteractivityDetect["canvas"] = "canvas";
4
+ InteractivityDetect["parent"] = "parent";
5
+ InteractivityDetect["window"] = "window";
6
+ })(InteractivityDetect || (InteractivityDetect = {}));