@tsparticles/plugin-interactivity 4.0.0-alpha.14

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 (168) hide show
  1. package/19.min.js +1 -0
  2. package/74.min.js +1 -0
  3. package/LICENSE +21 -0
  4. package/README.md +74 -0
  5. package/browser/DivType.js +5 -0
  6. package/browser/ExternalInteractorBase.js +8 -0
  7. package/browser/IExternalInteractor.js +1 -0
  8. package/browser/IInteractivityData.js +1 -0
  9. package/browser/IInteractor.js +1 -0
  10. package/browser/IMouseData.js +1 -0
  11. package/browser/IParticlesInteractor.js +1 -0
  12. package/browser/InteractionManager.js +212 -0
  13. package/browser/InteractivityConstants.js +1 -0
  14. package/browser/InteractivityDetect.js +6 -0
  15. package/browser/InteractivityEventListeners.js +244 -0
  16. package/browser/InteractivityPlugin.js +49 -0
  17. package/browser/InteractivityPluginInstance.js +55 -0
  18. package/browser/InteractorType.js +5 -0
  19. package/browser/Options/Classes/Events/ClickEvent.js +20 -0
  20. package/browser/Options/Classes/Events/DivEvent.js +31 -0
  21. package/browser/Options/Classes/Events/Events.js +29 -0
  22. package/browser/Options/Classes/Events/HoverEvent.js +20 -0
  23. package/browser/Options/Classes/Interactivity.js +25 -0
  24. package/browser/Options/Classes/Modes/Modes.js +27 -0
  25. package/browser/Options/Interfaces/Events/IClickEvent.js +1 -0
  26. package/browser/Options/Interfaces/Events/IDivEvent.js +1 -0
  27. package/browser/Options/Interfaces/Events/IEvents.js +1 -0
  28. package/browser/Options/Interfaces/Events/IHoverEvent.js +1 -0
  29. package/browser/Options/Interfaces/IInteractivity.js +1 -0
  30. package/browser/Options/Interfaces/Modes/IModeDiv.js +1 -0
  31. package/browser/Options/Interfaces/Modes/IModes.js +1 -0
  32. package/browser/ParticlesInteractorBase.js +8 -0
  33. package/browser/index.js +38 -0
  34. package/browser/package.json +1 -0
  35. package/browser/types.js +1 -0
  36. package/browser/utils.js +32 -0
  37. package/cjs/DivType.js +5 -0
  38. package/cjs/ExternalInteractorBase.js +8 -0
  39. package/cjs/IExternalInteractor.js +1 -0
  40. package/cjs/IInteractivityData.js +1 -0
  41. package/cjs/IInteractor.js +1 -0
  42. package/cjs/IMouseData.js +1 -0
  43. package/cjs/IParticlesInteractor.js +1 -0
  44. package/cjs/InteractionManager.js +212 -0
  45. package/cjs/InteractivityConstants.js +1 -0
  46. package/cjs/InteractivityDetect.js +6 -0
  47. package/cjs/InteractivityEventListeners.js +244 -0
  48. package/cjs/InteractivityPlugin.js +49 -0
  49. package/cjs/InteractivityPluginInstance.js +55 -0
  50. package/cjs/InteractorType.js +5 -0
  51. package/cjs/Options/Classes/Events/ClickEvent.js +20 -0
  52. package/cjs/Options/Classes/Events/DivEvent.js +31 -0
  53. package/cjs/Options/Classes/Events/Events.js +29 -0
  54. package/cjs/Options/Classes/Events/HoverEvent.js +20 -0
  55. package/cjs/Options/Classes/Interactivity.js +25 -0
  56. package/cjs/Options/Classes/Modes/Modes.js +27 -0
  57. package/cjs/Options/Interfaces/Events/IClickEvent.js +1 -0
  58. package/cjs/Options/Interfaces/Events/IDivEvent.js +1 -0
  59. package/cjs/Options/Interfaces/Events/IEvents.js +1 -0
  60. package/cjs/Options/Interfaces/Events/IHoverEvent.js +1 -0
  61. package/cjs/Options/Interfaces/IInteractivity.js +1 -0
  62. package/cjs/Options/Interfaces/Modes/IModeDiv.js +1 -0
  63. package/cjs/Options/Interfaces/Modes/IModes.js +1 -0
  64. package/cjs/ParticlesInteractorBase.js +8 -0
  65. package/cjs/index.js +38 -0
  66. package/cjs/package.json +1 -0
  67. package/cjs/types.js +1 -0
  68. package/cjs/utils.js +32 -0
  69. package/dist_browser_InteractivityPluginInstance_js.js +50 -0
  70. package/dist_browser_InteractivityPlugin_js.js +70 -0
  71. package/esm/DivType.js +5 -0
  72. package/esm/ExternalInteractorBase.js +8 -0
  73. package/esm/IExternalInteractor.js +1 -0
  74. package/esm/IInteractivityData.js +1 -0
  75. package/esm/IInteractor.js +1 -0
  76. package/esm/IMouseData.js +1 -0
  77. package/esm/IParticlesInteractor.js +1 -0
  78. package/esm/InteractionManager.js +212 -0
  79. package/esm/InteractivityConstants.js +1 -0
  80. package/esm/InteractivityDetect.js +6 -0
  81. package/esm/InteractivityEventListeners.js +244 -0
  82. package/esm/InteractivityPlugin.js +49 -0
  83. package/esm/InteractivityPluginInstance.js +55 -0
  84. package/esm/InteractorType.js +5 -0
  85. package/esm/Options/Classes/Events/ClickEvent.js +20 -0
  86. package/esm/Options/Classes/Events/DivEvent.js +31 -0
  87. package/esm/Options/Classes/Events/Events.js +29 -0
  88. package/esm/Options/Classes/Events/HoverEvent.js +20 -0
  89. package/esm/Options/Classes/Interactivity.js +25 -0
  90. package/esm/Options/Classes/Modes/Modes.js +27 -0
  91. package/esm/Options/Interfaces/Events/IClickEvent.js +1 -0
  92. package/esm/Options/Interfaces/Events/IDivEvent.js +1 -0
  93. package/esm/Options/Interfaces/Events/IEvents.js +1 -0
  94. package/esm/Options/Interfaces/Events/IHoverEvent.js +1 -0
  95. package/esm/Options/Interfaces/IInteractivity.js +1 -0
  96. package/esm/Options/Interfaces/Modes/IModeDiv.js +1 -0
  97. package/esm/Options/Interfaces/Modes/IModes.js +1 -0
  98. package/esm/ParticlesInteractorBase.js +8 -0
  99. package/esm/index.js +38 -0
  100. package/esm/package.json +1 -0
  101. package/esm/types.js +1 -0
  102. package/esm/utils.js +32 -0
  103. package/package.json +95 -0
  104. package/report.html +40 -0
  105. package/tsparticles.plugin.interactivity.js +407 -0
  106. package/tsparticles.plugin.interactivity.min.js +2 -0
  107. package/types/DivType.d.ts +4 -0
  108. package/types/ExternalInteractorBase.d.ts +15 -0
  109. package/types/IExternalInteractor.d.ts +11 -0
  110. package/types/IInteractivityData.d.ts +6 -0
  111. package/types/IInteractor.d.ts +12 -0
  112. package/types/IMouseData.d.ts +9 -0
  113. package/types/IParticlesInteractor.d.ts +7 -0
  114. package/types/InteractionManager.d.ts +27 -0
  115. package/types/InteractivityConstants.d.ts +1 -0
  116. package/types/InteractivityDetect.d.ts +5 -0
  117. package/types/InteractivityEventListeners.d.ts +25 -0
  118. package/types/InteractivityPlugin.d.ts +11 -0
  119. package/types/InteractivityPluginInstance.d.ts +20 -0
  120. package/types/InteractorType.d.ts +4 -0
  121. package/types/Options/Classes/Events/ClickEvent.d.ts +8 -0
  122. package/types/Options/Classes/Events/DivEvent.d.ts +11 -0
  123. package/types/Options/Classes/Events/Events.d.ts +12 -0
  124. package/types/Options/Classes/Events/HoverEvent.d.ts +8 -0
  125. package/types/Options/Classes/Interactivity.d.ts +14 -0
  126. package/types/Options/Classes/Modes/Modes.d.ts +10 -0
  127. package/types/Options/Interfaces/Events/IClickEvent.d.ts +5 -0
  128. package/types/Options/Interfaces/Events/IDivEvent.d.ts +8 -0
  129. package/types/Options/Interfaces/Events/IEvents.d.ts +9 -0
  130. package/types/Options/Interfaces/Events/IHoverEvent.d.ts +5 -0
  131. package/types/Options/Interfaces/IInteractivity.d.ts +9 -0
  132. package/types/Options/Interfaces/Modes/IModeDiv.d.ts +4 -0
  133. package/types/Options/Interfaces/Modes/IModes.d.ts +1 -0
  134. package/types/ParticlesInteractorBase.d.ts +15 -0
  135. package/types/index.d.ts +19 -0
  136. package/types/types.d.ts +34 -0
  137. package/types/utils.d.ts +7 -0
  138. package/umd/DivType.js +18 -0
  139. package/umd/ExternalInteractorBase.js +22 -0
  140. package/umd/IExternalInteractor.js +12 -0
  141. package/umd/IInteractivityData.js +12 -0
  142. package/umd/IInteractor.js +12 -0
  143. package/umd/IMouseData.js +12 -0
  144. package/umd/IParticlesInteractor.js +12 -0
  145. package/umd/InteractionManager.js +226 -0
  146. package/umd/InteractivityConstants.js +14 -0
  147. package/umd/InteractivityDetect.js +19 -0
  148. package/umd/InteractivityEventListeners.js +258 -0
  149. package/umd/InteractivityPlugin.js +97 -0
  150. package/umd/InteractivityPluginInstance.js +69 -0
  151. package/umd/InteractorType.js +18 -0
  152. package/umd/Options/Classes/Events/ClickEvent.js +34 -0
  153. package/umd/Options/Classes/Events/DivEvent.js +45 -0
  154. package/umd/Options/Classes/Events/Events.js +43 -0
  155. package/umd/Options/Classes/Events/HoverEvent.js +34 -0
  156. package/umd/Options/Classes/Interactivity.js +39 -0
  157. package/umd/Options/Classes/Modes/Modes.js +41 -0
  158. package/umd/Options/Interfaces/Events/IClickEvent.js +12 -0
  159. package/umd/Options/Interfaces/Events/IDivEvent.js +12 -0
  160. package/umd/Options/Interfaces/Events/IEvents.js +12 -0
  161. package/umd/Options/Interfaces/Events/IHoverEvent.js +12 -0
  162. package/umd/Options/Interfaces/IInteractivity.js +12 -0
  163. package/umd/Options/Interfaces/Modes/IModeDiv.js +12 -0
  164. package/umd/Options/Interfaces/Modes/IModes.js +12 -0
  165. package/umd/ParticlesInteractorBase.js +22 -0
  166. package/umd/index.js +88 -0
  167. package/umd/types.js +12 -0
  168. package/umd/utils.js +48 -0
@@ -0,0 +1,244 @@
1
+ import { double, executeOnSingleOrMultiple, lengthOffset, manageListener, touchDelay, visibilityChangeEvent, } from "@tsparticles/engine";
2
+ import { mouseDownEvent, mouseLeaveEvent, mouseMoveEvent, mouseOutEvent, mouseUpEvent, touchCancelEvent, touchEndEvent, touchMoveEvent, touchStartEvent, } from "./InteractivityConstants.js";
3
+ import { InteractivityDetect } from "./InteractivityDetect.js";
4
+ export class InteractivityEventListeners {
5
+ _canPush = true;
6
+ _clickPositionPlugins;
7
+ _container;
8
+ _handlers;
9
+ _interactionManager;
10
+ _touches;
11
+ constructor(container, interactionManager) {
12
+ this._container = container;
13
+ this._clickPositionPlugins = [];
14
+ this._interactionManager = interactionManager;
15
+ this._touches = new Map();
16
+ this._handlers = {
17
+ mouseDown: () => {
18
+ this._mouseDown();
19
+ },
20
+ mouseLeave: () => {
21
+ this._mouseTouchFinish();
22
+ },
23
+ mouseMove: (e) => {
24
+ this._mouseTouchMove(e);
25
+ },
26
+ mouseUp: (e) => {
27
+ this._mouseTouchClick(e);
28
+ },
29
+ touchStart: (e) => {
30
+ this._touchStart(e);
31
+ },
32
+ touchMove: (e) => {
33
+ this._mouseTouchMove(e);
34
+ },
35
+ touchEnd: (e) => {
36
+ this._touchEnd(e);
37
+ },
38
+ touchCancel: (e) => {
39
+ this._touchEnd(e);
40
+ },
41
+ touchEndClick: (e) => {
42
+ this._touchEndClick(e);
43
+ },
44
+ visibilityChange: () => {
45
+ this._handleVisibilityChange();
46
+ },
47
+ };
48
+ }
49
+ addListeners() {
50
+ this._manageListeners(true);
51
+ }
52
+ init() {
53
+ this._clickPositionPlugins.length = 0;
54
+ for (const plugin of this._container.plugins.filter(p => !!p.clickPositionValid)) {
55
+ this._clickPositionPlugins.push(plugin);
56
+ }
57
+ }
58
+ removeListeners() {
59
+ this._manageListeners(false);
60
+ }
61
+ _doMouseTouchClick = e => {
62
+ const container = this._container, interactionManager = this._interactionManager, options = container.actualOptions;
63
+ if (this._canPush) {
64
+ const mouseInteractivity = interactionManager.interactivityData.mouse, mousePos = mouseInteractivity.position;
65
+ if (!mousePos) {
66
+ return;
67
+ }
68
+ mouseInteractivity.clickPosition = { ...mousePos };
69
+ mouseInteractivity.clickTime = performance.now();
70
+ const onClick = options.interactivity?.events.onClick;
71
+ if (!onClick?.mode) {
72
+ return;
73
+ }
74
+ executeOnSingleOrMultiple(onClick.mode, mode => {
75
+ interactionManager.handleClickMode(mode);
76
+ });
77
+ }
78
+ if (e.type === "touchend") {
79
+ setTimeout(() => {
80
+ this._mouseTouchFinish();
81
+ }, touchDelay);
82
+ }
83
+ };
84
+ _handleVisibilityChange = () => {
85
+ this._mouseTouchFinish();
86
+ };
87
+ _manageInteractivityListeners = (mouseLeaveTmpEvent, add) => {
88
+ const handlers = this._handlers, container = this._container, interactionManager = this._interactionManager, options = container.actualOptions, interactivityEl = interactionManager.interactivityData.element;
89
+ if (!interactivityEl) {
90
+ return;
91
+ }
92
+ const html = interactivityEl, canvas = container.canvas;
93
+ canvas.setPointerEvents(html === canvas.element ? "initial" : "none");
94
+ if (!(options.interactivity?.events.onHover.enable || options.interactivity?.events.onClick.enable)) {
95
+ return;
96
+ }
97
+ manageListener(interactivityEl, mouseMoveEvent, handlers.mouseMove, add);
98
+ manageListener(interactivityEl, touchStartEvent, handlers.touchStart, add);
99
+ manageListener(interactivityEl, touchMoveEvent, handlers.touchMove, add);
100
+ if (options.interactivity.events.onClick.enable) {
101
+ manageListener(interactivityEl, touchEndEvent, handlers.touchEndClick, add);
102
+ manageListener(interactivityEl, mouseUpEvent, handlers.mouseUp, add);
103
+ manageListener(interactivityEl, mouseDownEvent, handlers.mouseDown, add);
104
+ }
105
+ else {
106
+ manageListener(interactivityEl, touchEndEvent, handlers.touchEnd, add);
107
+ }
108
+ manageListener(interactivityEl, mouseLeaveTmpEvent, handlers.mouseLeave, add);
109
+ manageListener(interactivityEl, touchCancelEvent, handlers.touchCancel, add);
110
+ };
111
+ _manageListeners = add => {
112
+ const handlers = this._handlers, container = this._container, interactionManager = this._interactionManager, options = container.actualOptions, detectType = options.interactivity?.detectsOn, canvasEl = container.canvas.element;
113
+ let mouseLeaveTmpEvent = mouseLeaveEvent;
114
+ if (detectType === InteractivityDetect.window) {
115
+ interactionManager.interactivityData.element = globalThis;
116
+ mouseLeaveTmpEvent = mouseOutEvent;
117
+ }
118
+ else if (detectType === InteractivityDetect.parent && canvasEl) {
119
+ interactionManager.interactivityData.element = canvasEl.parentElement ?? canvasEl.parentNode;
120
+ }
121
+ else {
122
+ interactionManager.interactivityData.element = canvasEl;
123
+ }
124
+ this._manageInteractivityListeners(mouseLeaveTmpEvent, add);
125
+ manageListener(document, visibilityChangeEvent, handlers.visibilityChange, add, false);
126
+ };
127
+ _mouseDown = () => {
128
+ const { interactivityData } = this._interactionManager, { mouse } = interactivityData;
129
+ mouse.clicking = true;
130
+ mouse.downPosition = mouse.position;
131
+ };
132
+ _mouseTouchClick = e => {
133
+ const container = this._container, interactionManager = this._interactionManager, options = container.actualOptions, { mouse } = interactionManager.interactivityData;
134
+ mouse.inside = true;
135
+ let handled = false;
136
+ const mousePosition = mouse.position;
137
+ if (!mousePosition || !options.interactivity?.events.onClick.enable) {
138
+ return;
139
+ }
140
+ for (const plugin of this._clickPositionPlugins) {
141
+ handled = plugin.clickPositionValid?.(mousePosition) ?? false;
142
+ if (handled) {
143
+ break;
144
+ }
145
+ }
146
+ if (!handled) {
147
+ this._doMouseTouchClick(e);
148
+ }
149
+ mouse.clicking = false;
150
+ };
151
+ _mouseTouchFinish = () => {
152
+ const { interactivityData } = this._interactionManager, { mouse } = interactivityData;
153
+ delete mouse.position;
154
+ delete mouse.clickPosition;
155
+ delete mouse.downPosition;
156
+ interactivityData.status = mouseLeaveEvent;
157
+ mouse.inside = false;
158
+ mouse.clicking = false;
159
+ };
160
+ _mouseTouchMove = e => {
161
+ const container = this._container, interactionManager = this._interactionManager, options = container.actualOptions, interactivity = interactionManager.interactivityData, canvasEl = container.canvas.element;
162
+ if (!interactivity.element) {
163
+ return;
164
+ }
165
+ interactivity.mouse.inside = true;
166
+ let pos;
167
+ if (e.type.startsWith("pointer")) {
168
+ this._canPush = true;
169
+ const mouseEvent = e;
170
+ if (interactivity.element === globalThis) {
171
+ if (canvasEl) {
172
+ const clientRect = canvasEl.getBoundingClientRect();
173
+ pos = {
174
+ x: mouseEvent.clientX - clientRect.left,
175
+ y: mouseEvent.clientY - clientRect.top,
176
+ };
177
+ }
178
+ }
179
+ else if (options.interactivity?.detectsOn === InteractivityDetect.parent) {
180
+ const source = mouseEvent.target, target = mouseEvent.currentTarget;
181
+ if (canvasEl) {
182
+ const sourceRect = source.getBoundingClientRect(), targetRect = target.getBoundingClientRect(), canvasRect = canvasEl.getBoundingClientRect();
183
+ pos = {
184
+ x: mouseEvent.offsetX + double * sourceRect.left - (targetRect.left + canvasRect.left),
185
+ y: mouseEvent.offsetY + double * sourceRect.top - (targetRect.top + canvasRect.top),
186
+ };
187
+ }
188
+ else {
189
+ pos = {
190
+ x: mouseEvent.offsetX,
191
+ y: mouseEvent.offsetY,
192
+ };
193
+ }
194
+ }
195
+ else if (mouseEvent.target === canvasEl) {
196
+ pos = {
197
+ x: mouseEvent.offsetX,
198
+ y: mouseEvent.offsetY,
199
+ };
200
+ }
201
+ }
202
+ else {
203
+ this._canPush = e.type !== "touchmove";
204
+ if (canvasEl) {
205
+ const touchEvent = e, lastTouch = touchEvent.touches[touchEvent.touches.length - lengthOffset], canvasRect = canvasEl.getBoundingClientRect();
206
+ if (!lastTouch) {
207
+ return;
208
+ }
209
+ pos = {
210
+ x: lastTouch.clientX - canvasRect.left,
211
+ y: lastTouch.clientY - canvasRect.top,
212
+ };
213
+ }
214
+ }
215
+ const pxRatio = container.retina.pixelRatio;
216
+ if (pos) {
217
+ pos.x *= pxRatio;
218
+ pos.y *= pxRatio;
219
+ }
220
+ interactivity.mouse.position = pos;
221
+ interactivity.status = mouseMoveEvent;
222
+ };
223
+ _touchEnd = e => {
224
+ const evt = e, touches = Array.from(evt.changedTouches);
225
+ for (const touch of touches) {
226
+ this._touches.delete(touch.identifier);
227
+ }
228
+ this._mouseTouchFinish();
229
+ };
230
+ _touchEndClick = e => {
231
+ const evt = e, touches = Array.from(evt.changedTouches);
232
+ for (const touch of touches) {
233
+ this._touches.delete(touch.identifier);
234
+ }
235
+ this._mouseTouchClick(e);
236
+ };
237
+ _touchStart = e => {
238
+ const evt = e, touches = Array.from(evt.changedTouches);
239
+ for (const touch of touches) {
240
+ this._touches.set(touch.identifier, performance.now());
241
+ }
242
+ this._mouseTouchMove(e);
243
+ };
244
+ }
@@ -0,0 +1,49 @@
1
+ import { deepExtend, } from "@tsparticles/engine";
2
+ import { Interactivity } from "./Options/Classes/Interactivity.js";
3
+ export class InteractivityPlugin {
4
+ id = "interactivity";
5
+ _engine;
6
+ constructor(engine) {
7
+ this._engine = engine;
8
+ }
9
+ async getPlugin(container) {
10
+ const { InteractivityPluginInstance } = await import("./InteractivityPluginInstance.js");
11
+ return new InteractivityPluginInstance(this._engine, container);
12
+ }
13
+ loadOptions(container, options, source) {
14
+ if (!this.needsPlugin()) {
15
+ return;
16
+ }
17
+ let interactivityOptions = options.interactivity;
18
+ if (!interactivityOptions?.load) {
19
+ options.interactivity = interactivityOptions = new Interactivity(this._engine, container);
20
+ }
21
+ interactivityOptions.load(source?.interactivity);
22
+ const interactors = this._engine.interactors?.get(container);
23
+ if (!interactors) {
24
+ return;
25
+ }
26
+ for (const interactor of interactors) {
27
+ if (interactor.loadOptions) {
28
+ interactor.loadOptions(options, source);
29
+ }
30
+ }
31
+ }
32
+ loadParticlesOptions(container, options, source) {
33
+ if (source?.interactivity) {
34
+ options.interactivity = deepExtend({}, source.interactivity);
35
+ }
36
+ const interactors = this._engine.interactors?.get(container);
37
+ if (!interactors) {
38
+ return;
39
+ }
40
+ for (const interactor of interactors) {
41
+ if (interactor.loadParticlesOptions) {
42
+ interactor.loadParticlesOptions(options, source);
43
+ }
44
+ }
45
+ }
46
+ needsPlugin() {
47
+ return true;
48
+ }
49
+ }
@@ -0,0 +1,55 @@
1
+ import { InteractionManager } from "./InteractionManager.js";
2
+ import { Interactivity } from "./Options/Classes/Interactivity.js";
3
+ export class InteractivityPluginInstance {
4
+ interactionManager;
5
+ _container;
6
+ _engine;
7
+ constructor(engine, container) {
8
+ this._container = container;
9
+ this._engine = engine;
10
+ this.interactionManager = new InteractionManager(engine, container);
11
+ this._container.addClickHandler = (callback) => {
12
+ this.interactionManager.addClickHandler(callback);
13
+ };
14
+ }
15
+ addClickHandler(callback) {
16
+ this.interactionManager.addClickHandler(callback);
17
+ }
18
+ clearClickHandlers() {
19
+ this.interactionManager.clearClickHandlers();
20
+ }
21
+ destroy() {
22
+ this.clearClickHandlers();
23
+ this._engine.interactors?.delete(this._container);
24
+ }
25
+ particleCreated(particle) {
26
+ const interactivityParticle = particle, interactivity = new Interactivity(this._engine, this._container);
27
+ interactivity.load(this._container.actualOptions.interactivity);
28
+ interactivity.load(interactivityParticle.options.interactivity);
29
+ interactivityParticle.interactivity = interactivity;
30
+ }
31
+ particleReset(particle) {
32
+ this.interactionManager.reset(particle);
33
+ }
34
+ postParticleUpdate(particle, delta) {
35
+ this.interactionManager.particlesInteract(particle, delta);
36
+ }
37
+ postUpdate(delta) {
38
+ this.interactionManager.externalInteract(delta);
39
+ }
40
+ async preInit() {
41
+ await this.interactionManager.init();
42
+ }
43
+ async redrawInit() {
44
+ await this.interactionManager.init();
45
+ }
46
+ start() {
47
+ this.interactionManager.addListeners();
48
+ this.interactionManager.startObserving();
49
+ return Promise.resolve();
50
+ }
51
+ stop() {
52
+ this.interactionManager.removeListeners();
53
+ this.interactionManager.stopObserving();
54
+ }
55
+ }
@@ -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,20 @@
1
+ import { isNull } from "@tsparticles/engine";
2
+ export class ClickEvent {
3
+ enable;
4
+ mode;
5
+ constructor() {
6
+ this.enable = false;
7
+ this.mode = [];
8
+ }
9
+ load(data) {
10
+ if (isNull(data)) {
11
+ return;
12
+ }
13
+ if (data.enable !== undefined) {
14
+ this.enable = data.enable;
15
+ }
16
+ if (data.mode !== undefined) {
17
+ this.mode = data.mode;
18
+ }
19
+ }
20
+ }
@@ -0,0 +1,31 @@
1
+ import { isNull } from "@tsparticles/engine";
2
+ import { DivType } from "../../../DivType.js";
3
+ export class DivEvent {
4
+ enable;
5
+ mode;
6
+ selectors;
7
+ type;
8
+ constructor() {
9
+ this.selectors = [];
10
+ this.enable = false;
11
+ this.mode = [];
12
+ this.type = DivType.circle;
13
+ }
14
+ load(data) {
15
+ if (isNull(data)) {
16
+ return;
17
+ }
18
+ if (data.selectors !== undefined) {
19
+ this.selectors = data.selectors;
20
+ }
21
+ if (data.enable !== undefined) {
22
+ this.enable = data.enable;
23
+ }
24
+ if (data.mode !== undefined) {
25
+ this.mode = data.mode;
26
+ }
27
+ if (data.type !== undefined) {
28
+ this.type = data.type;
29
+ }
30
+ }
31
+ }
@@ -0,0 +1,29 @@
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
+ onClick;
7
+ onDiv;
8
+ onHover;
9
+ constructor() {
10
+ this.onClick = new ClickEvent();
11
+ this.onDiv = new DivEvent();
12
+ this.onHover = new HoverEvent();
13
+ }
14
+ load(data) {
15
+ if (isNull(data)) {
16
+ return;
17
+ }
18
+ this.onClick.load(data.onClick);
19
+ const onDiv = data.onDiv;
20
+ if (onDiv !== undefined) {
21
+ this.onDiv = executeOnSingleOrMultiple(onDiv, t => {
22
+ const tmp = new DivEvent();
23
+ tmp.load(t);
24
+ return tmp;
25
+ });
26
+ }
27
+ this.onHover.load(data.onHover);
28
+ }
29
+ }
@@ -0,0 +1,20 @@
1
+ import { isNull } from "@tsparticles/engine";
2
+ export class HoverEvent {
3
+ enable;
4
+ mode;
5
+ constructor() {
6
+ this.enable = false;
7
+ this.mode = [];
8
+ }
9
+ load(data) {
10
+ if (isNull(data)) {
11
+ return;
12
+ }
13
+ if (data.enable !== undefined) {
14
+ this.enable = data.enable;
15
+ }
16
+ if (data.mode !== undefined) {
17
+ this.mode = data.mode;
18
+ }
19
+ }
20
+ }
@@ -0,0 +1,25 @@
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
+ detectsOn;
7
+ events;
8
+ modes;
9
+ constructor(engine, container) {
10
+ this.detectsOn = InteractivityDetect.window;
11
+ this.events = new Events();
12
+ this.modes = new Modes(engine, container);
13
+ }
14
+ load(data) {
15
+ if (isNull(data)) {
16
+ return;
17
+ }
18
+ const detectsOn = data.detectsOn;
19
+ if (detectsOn !== undefined) {
20
+ this.detectsOn = detectsOn;
21
+ }
22
+ this.events.load(data.events);
23
+ this.modes.load(data.modes);
24
+ }
25
+ }
@@ -0,0 +1,27 @@
1
+ import { isNull } from "@tsparticles/engine";
2
+ export class Modes {
3
+ _container;
4
+ _engine;
5
+ constructor(engine, container) {
6
+ this._engine = engine;
7
+ this._container = container;
8
+ }
9
+ load(data) {
10
+ if (isNull(data)) {
11
+ return;
12
+ }
13
+ if (!this._container) {
14
+ return;
15
+ }
16
+ const interactors = this._engine.interactors?.get(this._container);
17
+ if (!interactors) {
18
+ return;
19
+ }
20
+ for (const interactor of interactors) {
21
+ if (!interactor.loadModeOptions) {
22
+ continue;
23
+ }
24
+ interactor.loadModeOptions(this, data);
25
+ }
26
+ }
27
+ }
@@ -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,8 @@
1
+ import { InteractorType } from "./InteractorType.js";
2
+ export class ParticlesInteractorBase {
3
+ type = InteractorType.particles;
4
+ container;
5
+ constructor(container) {
6
+ this.container = container;
7
+ }
8
+ }
package/cjs/index.js ADDED
@@ -0,0 +1,38 @@
1
+ import { getItemsFromInitializer } from "@tsparticles/engine";
2
+ export async function loadInteractivityPlugin(engine) {
3
+ engine.checkVersion("4.0.0-alpha.14");
4
+ await engine.register(async (e) => {
5
+ const interactivityEngine = e, { InteractivityPlugin } = await import("./InteractivityPlugin.js");
6
+ interactivityEngine.addPlugin(new InteractivityPlugin(interactivityEngine));
7
+ interactivityEngine.initializers.interactors ??= new Map();
8
+ interactivityEngine.interactors ??= new Map();
9
+ interactivityEngine.addInteractor = (name, interactorInitializer) => {
10
+ interactivityEngine.initializers.interactors ??= new Map();
11
+ interactivityEngine.initializers.interactors.set(name, interactorInitializer);
12
+ };
13
+ interactivityEngine.getInteractors = async (container, force = false) => {
14
+ interactivityEngine.interactors ??= new Map();
15
+ interactivityEngine.initializers.interactors ??= new Map();
16
+ return getItemsFromInitializer(container, interactivityEngine.interactors, interactivityEngine.initializers.interactors, 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": "commonjs" }
package/cjs/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/cjs/utils.js ADDED
@@ -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
+ }