@tsparticles/engine 3.0.3 → 3.2.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 (156) hide show
  1. package/373.min.js +2 -0
  2. package/373.min.js.LICENSE.txt +1 -0
  3. package/438.min.js +2 -0
  4. package/438.min.js.LICENSE.txt +1 -0
  5. package/README.md +337 -216
  6. package/browser/Core/Canvas.js +102 -49
  7. package/browser/Core/Container.js +53 -41
  8. package/browser/Core/Engine.js +47 -32
  9. package/browser/Core/Particle.js +46 -48
  10. package/browser/Core/Particles.js +70 -57
  11. package/browser/Core/Retina.js +5 -4
  12. package/browser/Core/Utils/Circle.js +4 -3
  13. package/browser/Core/Utils/Constants.js +3 -0
  14. package/browser/Core/Utils/EventListeners.js +19 -16
  15. package/browser/Core/Utils/ExternalInteractorBase.js +1 -1
  16. package/browser/Core/Utils/InteractionManager.js +17 -8
  17. package/browser/Core/Utils/ParticlesInteractorBase.js +1 -1
  18. package/browser/Core/Utils/QuadTree.js +5 -3
  19. package/browser/Core/Utils/Vector.js +7 -2
  20. package/browser/Core/Utils/Vector3d.js +14 -9
  21. package/browser/Options/Classes/BackgroundMask/BackgroundMask.js +1 -1
  22. package/browser/Options/Classes/BackgroundMask/BackgroundMaskCover.js +3 -2
  23. package/browser/Options/Classes/ManualParticle.js +3 -2
  24. package/browser/Options/Classes/Options.js +3 -0
  25. package/browser/Utils/CanvasUtils.js +50 -40
  26. package/browser/Utils/ColorUtils.js +124 -45
  27. package/browser/Utils/EventDispatcher.js +6 -5
  28. package/browser/Utils/HslColorManager.js +5 -5
  29. package/browser/Utils/NumberUtils.js +35 -23
  30. package/browser/Utils/RgbColorManager.js +5 -5
  31. package/browser/Utils/Utils.js +102 -19
  32. package/cjs/Core/Canvas.js +102 -49
  33. package/cjs/Core/Container.js +53 -41
  34. package/cjs/Core/Engine.js +70 -32
  35. package/cjs/Core/Particle.js +45 -47
  36. package/cjs/Core/Particles.js +93 -57
  37. package/cjs/Core/Retina.js +5 -4
  38. package/cjs/Core/Utils/Circle.js +4 -3
  39. package/cjs/Core/Utils/Constants.js +4 -1
  40. package/cjs/Core/Utils/EventListeners.js +18 -15
  41. package/cjs/Core/Utils/ExternalInteractorBase.js +1 -1
  42. package/cjs/Core/Utils/InteractionManager.js +17 -8
  43. package/cjs/Core/Utils/ParticlesInteractorBase.js +1 -1
  44. package/cjs/Core/Utils/QuadTree.js +5 -3
  45. package/cjs/Core/Utils/Vector.js +7 -2
  46. package/cjs/Core/Utils/Vector3d.js +14 -9
  47. package/cjs/Options/Classes/BackgroundMask/BackgroundMask.js +1 -1
  48. package/cjs/Options/Classes/BackgroundMask/BackgroundMaskCover.js +3 -2
  49. package/cjs/Options/Classes/ManualParticle.js +3 -2
  50. package/cjs/Options/Classes/Options.js +3 -0
  51. package/cjs/Utils/CanvasUtils.js +50 -40
  52. package/cjs/Utils/ColorUtils.js +126 -45
  53. package/cjs/Utils/EventDispatcher.js +6 -5
  54. package/cjs/Utils/HslColorManager.js +5 -5
  55. package/cjs/Utils/NumberUtils.js +37 -24
  56. package/cjs/Utils/RgbColorManager.js +5 -5
  57. package/cjs/Utils/Utils.js +103 -19
  58. package/dist_browser_Core_Container_js.js +92 -0
  59. package/dist_browser_Core_Particle_js.js +32 -0
  60. package/esm/Core/Canvas.js +102 -49
  61. package/esm/Core/Container.js +53 -41
  62. package/esm/Core/Engine.js +47 -32
  63. package/esm/Core/Particle.js +46 -48
  64. package/esm/Core/Particles.js +70 -57
  65. package/esm/Core/Retina.js +5 -4
  66. package/esm/Core/Utils/Circle.js +4 -3
  67. package/esm/Core/Utils/Constants.js +3 -0
  68. package/esm/Core/Utils/EventListeners.js +19 -16
  69. package/esm/Core/Utils/ExternalInteractorBase.js +1 -1
  70. package/esm/Core/Utils/InteractionManager.js +17 -8
  71. package/esm/Core/Utils/ParticlesInteractorBase.js +1 -1
  72. package/esm/Core/Utils/QuadTree.js +5 -3
  73. package/esm/Core/Utils/Vector.js +7 -2
  74. package/esm/Core/Utils/Vector3d.js +14 -9
  75. package/esm/Options/Classes/BackgroundMask/BackgroundMask.js +1 -1
  76. package/esm/Options/Classes/BackgroundMask/BackgroundMaskCover.js +3 -2
  77. package/esm/Options/Classes/ManualParticle.js +3 -2
  78. package/esm/Options/Classes/Options.js +3 -0
  79. package/esm/Utils/CanvasUtils.js +50 -40
  80. package/esm/Utils/ColorUtils.js +124 -45
  81. package/esm/Utils/EventDispatcher.js +6 -5
  82. package/esm/Utils/HslColorManager.js +5 -5
  83. package/esm/Utils/NumberUtils.js +35 -23
  84. package/esm/Utils/RgbColorManager.js +5 -5
  85. package/esm/Utils/Utils.js +102 -19
  86. package/package.json +1 -1
  87. package/report.html +3 -3
  88. package/tsparticles.engine.js +894 -5461
  89. package/tsparticles.engine.min.js +1 -1
  90. package/tsparticles.engine.min.js.LICENSE.txt +1 -1
  91. package/types/Core/Canvas.d.ts +5 -3
  92. package/types/Core/Container.d.ts +1 -1
  93. package/types/Core/Engine.d.ts +13 -8
  94. package/types/Core/Interfaces/IContainerPlugin.d.ts +3 -3
  95. package/types/Core/Interfaces/IEffectDrawer.d.ts +3 -3
  96. package/types/Core/Interfaces/IMovePathGenerator.d.ts +2 -2
  97. package/types/Core/Interfaces/IParticleHslAnimation.d.ts +4 -4
  98. package/types/Core/Interfaces/IParticleMover.d.ts +2 -2
  99. package/types/Core/Interfaces/IParticleUpdater.d.ts +2 -2
  100. package/types/Core/Interfaces/IParticleValueAnimation.d.ts +4 -0
  101. package/types/Core/Interfaces/IPlugin.d.ts +1 -1
  102. package/types/Core/Interfaces/IShapeDrawData.d.ts +2 -2
  103. package/types/Core/Interfaces/IShapeDrawer.d.ts +4 -4
  104. package/types/Core/Particle.d.ts +3 -3
  105. package/types/Core/Particles.d.ts +12 -8
  106. package/types/Core/Utils/Constants.d.ts +3 -0
  107. package/types/Core/Utils/ExternalInteractorBase.d.ts +1 -1
  108. package/types/Core/Utils/InteractionManager.d.ts +3 -3
  109. package/types/Core/Utils/ParticlesInteractorBase.d.ts +1 -1
  110. package/types/Core/Utils/Point.d.ts +1 -1
  111. package/types/Enums/Modes/OutMode.d.ts +0 -3
  112. package/types/Options/Classes/BackgroundMask/BackgroundMaskCover.d.ts +2 -1
  113. package/types/Options/Classes/Options.d.ts +1 -0
  114. package/types/Options/Classes/Particles/Move/Move.d.ts +1 -2
  115. package/types/Options/Classes/Particles/Move/OutModes.d.ts +5 -6
  116. package/types/Options/Interfaces/BackgroundMask/IBackgroundMask.d.ts +2 -1
  117. package/types/Options/Interfaces/BackgroundMask/IBackgroundMaskCover.d.ts +2 -1
  118. package/types/Options/Interfaces/IOptions.d.ts +1 -0
  119. package/types/Options/Interfaces/Interactivity/Modes/IModes.d.ts +1 -3
  120. package/types/Options/Interfaces/Particles/Move/IMove.d.ts +2 -2
  121. package/types/Options/Interfaces/Particles/Move/IOutModes.d.ts +6 -6
  122. package/types/Types/CustomEventArgs.d.ts +2 -2
  123. package/types/Types/ExportResult.d.ts +2 -2
  124. package/types/Types/ParticlesGroups.d.ts +1 -3
  125. package/types/Types/PathOptions.d.ts +1 -3
  126. package/types/Types/ShapeData.d.ts +1 -3
  127. package/types/Utils/CanvasUtils.d.ts +9 -8
  128. package/types/Utils/ColorUtils.d.ts +5 -0
  129. package/types/Utils/NumberUtils.d.ts +2 -2
  130. package/types/Utils/Utils.d.ts +9 -6
  131. package/umd/Core/Canvas.js +102 -49
  132. package/umd/Core/Container.js +54 -42
  133. package/umd/Core/Engine.js +72 -33
  134. package/umd/Core/Particle.js +46 -48
  135. package/umd/Core/Particles.js +95 -58
  136. package/umd/Core/Retina.js +5 -4
  137. package/umd/Core/Utils/Circle.js +4 -3
  138. package/umd/Core/Utils/Constants.js +4 -1
  139. package/umd/Core/Utils/EventListeners.js +18 -15
  140. package/umd/Core/Utils/ExternalInteractorBase.js +1 -1
  141. package/umd/Core/Utils/InteractionManager.js +17 -8
  142. package/umd/Core/Utils/ParticlesInteractorBase.js +1 -1
  143. package/umd/Core/Utils/QuadTree.js +5 -3
  144. package/umd/Core/Utils/Vector.js +7 -2
  145. package/umd/Core/Utils/Vector3d.js +14 -9
  146. package/umd/Options/Classes/BackgroundMask/BackgroundMask.js +1 -1
  147. package/umd/Options/Classes/BackgroundMask/BackgroundMaskCover.js +3 -2
  148. package/umd/Options/Classes/ManualParticle.js +3 -2
  149. package/umd/Options/Classes/Options.js +3 -0
  150. package/umd/Utils/CanvasUtils.js +50 -40
  151. package/umd/Utils/ColorUtils.js +127 -46
  152. package/umd/Utils/EventDispatcher.js +6 -5
  153. package/umd/Utils/HslColorManager.js +5 -5
  154. package/umd/Utils/NumberUtils.js +38 -25
  155. package/umd/Utils/RgbColorManager.js +5 -5
  156. package/umd/Utils/Utils.js +104 -20
@@ -1,22 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Container = void 0;
4
+ const Constants_js_1 = require("./Utils/Constants.js");
4
5
  const Utils_js_1 = require("../Utils/Utils.js");
5
6
  const Canvas_js_1 = require("./Canvas.js");
6
7
  const EventListeners_js_1 = require("./Utils/EventListeners.js");
7
8
  const Options_js_1 = require("../Options/Classes/Options.js");
8
9
  const Particles_js_1 = require("./Particles.js");
9
10
  const Retina_js_1 = require("./Retina.js");
10
- const Constants_js_1 = require("./Utils/Constants.js");
11
11
  const NumberUtils_js_1 = require("../Utils/NumberUtils.js");
12
12
  const OptionsUtils_js_1 = require("../Utils/OptionsUtils.js");
13
13
  function guardCheck(container) {
14
14
  return container && !container.destroyed;
15
15
  }
16
- function initDelta(value, fpsLimit = 60, smooth = false) {
16
+ const defaultFps = 60;
17
+ function initDelta(value, fpsLimit = defaultFps, smooth = false) {
17
18
  return {
18
19
  value,
19
- factor: smooth ? 60 / fpsLimit : (60 * value) / 1000,
20
+ factor: smooth ? defaultFps / fpsLimit : (defaultFps * value) / Constants_js_1.millisecondsToSeconds,
20
21
  };
21
22
  }
22
23
  function loadContainerOptions(engine, container, ...sourceOptionsArr) {
@@ -34,14 +35,19 @@ class Container {
34
35
  if (entry.target !== this.interactivity.element) {
35
36
  continue;
36
37
  }
37
- (entry.isIntersecting ? this.play : this.pause)();
38
+ if (entry.isIntersecting) {
39
+ this.play();
40
+ }
41
+ else {
42
+ this.pause();
43
+ }
38
44
  }
39
45
  };
40
46
  this._nextFrame = async (timestamp) => {
41
47
  try {
42
48
  if (!this._smooth &&
43
49
  this._lastFrameTime !== undefined &&
44
- timestamp < this._lastFrameTime + 1000 / this.fpsLimit) {
50
+ timestamp < this._lastFrameTime + Constants_js_1.millisecondsToSeconds / this.fpsLimit) {
45
51
  this.draw(false);
46
52
  return;
47
53
  }
@@ -49,7 +55,7 @@ class Container {
49
55
  const delta = initDelta(timestamp - this._lastFrameTime, this.fpsLimit, this._smooth);
50
56
  this.addLifeTime(delta.value);
51
57
  this._lastFrameTime = timestamp;
52
- if (delta.value > 1000) {
58
+ if (delta.value > Constants_js_1.millisecondsToSeconds) {
53
59
  this.draw(false);
54
60
  return;
55
61
  }
@@ -58,7 +64,7 @@ class Container {
58
64
  this.destroy();
59
65
  return;
60
66
  }
61
- if (this.getAnimationStatus()) {
67
+ if (this.animationStatus) {
62
68
  this.draw(false);
63
69
  }
64
70
  }
@@ -101,6 +107,9 @@ class Container {
101
107
  this._intersectionObserver = (0, Utils_js_1.safeIntersectionObserver)((entries) => this._intersectionManager(entries));
102
108
  this._engine.dispatchEvent("containerBuilt", { container: this });
103
109
  }
110
+ get animationStatus() {
111
+ return !this._paused && !this.pageHidden && guardCheck(this);
112
+ }
104
113
  get options() {
105
114
  return this._options;
106
115
  }
@@ -132,8 +141,8 @@ class Container {
132
141
  const mouseEvent = e, pos = {
133
142
  x: mouseEvent.offsetX || mouseEvent.clientX,
134
143
  y: mouseEvent.offsetY || mouseEvent.clientY,
135
- };
136
- clickOrTouchHandler(e, pos, 1);
144
+ }, radius = 1;
145
+ clickOrTouchHandler(e, pos, radius);
137
146
  };
138
147
  const touchStartHandler = () => {
139
148
  if (!guardCheck(this)) {
@@ -154,16 +163,17 @@ class Container {
154
163
  }
155
164
  if (touched && !touchMoved) {
156
165
  const touchEvent = e;
157
- let lastTouch = touchEvent.touches[touchEvent.touches.length - 1];
166
+ const lengthOffset = 1;
167
+ let lastTouch = touchEvent.touches[touchEvent.touches.length - lengthOffset];
158
168
  if (!lastTouch) {
159
- lastTouch = touchEvent.changedTouches[touchEvent.changedTouches.length - 1];
169
+ lastTouch = touchEvent.changedTouches[touchEvent.changedTouches.length - lengthOffset];
160
170
  if (!lastTouch) {
161
171
  return;
162
172
  }
163
173
  }
164
- const element = this.canvas.element, canvasRect = element ? element.getBoundingClientRect() : undefined, pos = {
165
- x: lastTouch.clientX - (canvasRect ? canvasRect.left : 0),
166
- y: lastTouch.clientY - (canvasRect ? canvasRect.top : 0),
174
+ const element = this.canvas.element, canvasRect = element ? element.getBoundingClientRect() : undefined, minCoordinate = 0, pos = {
175
+ x: lastTouch.clientX - (canvasRect ? canvasRect.left : minCoordinate),
176
+ y: lastTouch.clientY - (canvasRect ? canvasRect.top : minCoordinate),
167
177
  };
168
178
  clickOrTouchHandler(e, pos, Math.max(lastTouch.radiusX, lastTouch.radiusY));
169
179
  }
@@ -205,10 +215,10 @@ class Container {
205
215
  this.particles.destroy();
206
216
  this.canvas.destroy();
207
217
  for (const [, effectDrawer] of this.effectDrawers) {
208
- effectDrawer.destroy && effectDrawer.destroy(this);
218
+ effectDrawer.destroy?.(this);
209
219
  }
210
220
  for (const [, shapeDrawer] of this.shapeDrawers) {
211
- shapeDrawer.destroy && shapeDrawer.destroy(this);
221
+ shapeDrawer.destroy?.(this);
212
222
  }
213
223
  for (const key of this.effectDrawers.keys()) {
214
224
  this.effectDrawers.delete(key);
@@ -218,9 +228,10 @@ class Container {
218
228
  }
219
229
  this._engine.clearPlugins(this);
220
230
  this.destroyed = true;
221
- const mainArr = this._engine.dom(), idx = mainArr.findIndex((t) => t === this);
222
- if (idx >= 0) {
223
- mainArr.splice(idx, 1);
231
+ const mainArr = this._engine.dom(), idx = mainArr.findIndex((t) => t === this), minIndex = 0;
232
+ if (idx >= minIndex) {
233
+ const deleteCount = 1;
234
+ mainArr.splice(idx, deleteCount);
224
235
  }
225
236
  this._engine.dispatchEvent("containerDestroyed", { container: this });
226
237
  }
@@ -229,13 +240,14 @@ class Container {
229
240
  return;
230
241
  }
231
242
  let refreshTime = force;
232
- this._drawAnimationFrame = requestAnimationFrame(async (timestamp) => {
243
+ const frame = async (timestamp) => {
233
244
  if (refreshTime) {
234
245
  this._lastFrameTime = undefined;
235
246
  refreshTime = false;
236
247
  }
237
248
  await this._nextFrame(timestamp);
238
- });
249
+ };
250
+ this._drawAnimationFrame = requestAnimationFrame((timestamp) => void frame(timestamp));
239
251
  }
240
252
  async export(type, options = {}) {
241
253
  for (const [, plugin] of this.plugins) {
@@ -250,16 +262,13 @@ class Container {
250
262
  }
251
263
  (0, Utils_js_1.getLogger)().error(`${Constants_js_1.errorPrefix} - Export plugin with type ${type} not found`);
252
264
  }
253
- getAnimationStatus() {
254
- return !this._paused && !this.pageHidden && guardCheck(this);
255
- }
256
265
  handleClickMode(mode) {
257
266
  if (!guardCheck(this)) {
258
267
  return;
259
268
  }
260
269
  this.particles.handleClickMode(mode);
261
270
  for (const [, plugin] of this.plugins) {
262
- plugin.handleClickMode && plugin.handleClickMode(mode);
271
+ plugin.handleClickMode?.(mode);
263
272
  }
264
273
  }
265
274
  async init() {
@@ -280,9 +289,10 @@ class Container {
280
289
  this.shapeDrawers.set(type, drawer);
281
290
  }
282
291
  }
292
+ await this.particles.initPlugins();
283
293
  this._options = loadContainerOptions(this._engine, this, this._initialSourceOptions, this.sourceOptions);
284
294
  this.actualOptions = loadContainerOptions(this._engine, this, this._options);
285
- const availablePlugins = this._engine.getAvailablePlugins(this);
295
+ const availablePlugins = await this._engine.getAvailablePlugins(this);
286
296
  for (const [id, plugin] of availablePlugins) {
287
297
  this.plugins.set(id, plugin);
288
298
  }
@@ -292,25 +302,26 @@ class Container {
292
302
  this.canvas.initBackground();
293
303
  this.canvas.resize();
294
304
  this.zLayers = this.actualOptions.zLayers;
295
- this._duration = (0, NumberUtils_js_1.getRangeValue)(this.actualOptions.duration) * 1000;
296
- this._delay = (0, NumberUtils_js_1.getRangeValue)(this.actualOptions.delay) * 1000;
305
+ this._duration = (0, NumberUtils_js_1.getRangeValue)(this.actualOptions.duration) * Constants_js_1.millisecondsToSeconds;
306
+ this._delay = (0, NumberUtils_js_1.getRangeValue)(this.actualOptions.delay) * Constants_js_1.millisecondsToSeconds;
297
307
  this._lifeTime = 0;
298
- this.fpsLimit = this.actualOptions.fpsLimit > 0 ? this.actualOptions.fpsLimit : 120;
308
+ const defaultFpsLimit = 120, minFpsLimit = 0;
309
+ this.fpsLimit = this.actualOptions.fpsLimit > minFpsLimit ? this.actualOptions.fpsLimit : defaultFpsLimit;
299
310
  this._smooth = this.actualOptions.smooth;
300
311
  for (const [, drawer] of this.effectDrawers) {
301
- drawer.init && (await drawer.init(this));
312
+ await drawer.init?.(this);
302
313
  }
303
314
  for (const [, drawer] of this.shapeDrawers) {
304
- drawer.init && (await drawer.init(this));
315
+ await drawer.init?.(this);
305
316
  }
306
317
  for (const [, plugin] of this.plugins) {
307
- plugin.init && (await plugin.init());
318
+ await plugin.init?.();
308
319
  }
309
320
  this._engine.dispatchEvent("containerInit", { container: this });
310
- this.particles.init();
311
- this.particles.setDensity();
321
+ await this.particles.init();
322
+ await this.particles.setDensity();
312
323
  for (const [, plugin] of this.plugins) {
313
- plugin.particlesSetup && plugin.particlesSetup();
324
+ plugin.particlesSetup?.();
314
325
  }
315
326
  this._engine.dispatchEvent("particlesSetup", { container: this });
316
327
  }
@@ -333,7 +344,7 @@ class Container {
333
344
  return;
334
345
  }
335
346
  for (const [, plugin] of this.plugins) {
336
- plugin.pause && plugin.pause();
347
+ plugin.pause?.();
337
348
  }
338
349
  if (!this.pageHidden) {
339
350
  this._paused = true;
@@ -360,7 +371,7 @@ class Container {
360
371
  }
361
372
  }
362
373
  this._engine.dispatchEvent("containerPlay", { container: this });
363
- this.draw(needsUpdate || false);
374
+ this.draw(needsUpdate ?? false);
364
375
  }
365
376
  async refresh() {
366
377
  if (!guardCheck(this)) {
@@ -385,18 +396,19 @@ class Container {
385
396
  await this.init();
386
397
  this.started = true;
387
398
  await new Promise((resolve) => {
388
- this._delayTimeout = setTimeout(async () => {
399
+ const start = async () => {
389
400
  this._eventListeners.addListeners();
390
401
  if (this.interactivity.element instanceof HTMLElement && this._intersectionObserver) {
391
402
  this._intersectionObserver.observe(this.interactivity.element);
392
403
  }
393
404
  for (const [, plugin] of this.plugins) {
394
- plugin.start && (await plugin.start());
405
+ await plugin.start?.();
395
406
  }
396
407
  this._engine.dispatchEvent("containerStarted", { container: this });
397
408
  this.play();
398
409
  resolve();
399
- }, this._delay);
410
+ };
411
+ this._delayTimeout = setTimeout(() => void start(), this._delay);
400
412
  });
401
413
  }
402
414
  stop() {
@@ -417,7 +429,7 @@ class Container {
417
429
  this._intersectionObserver.unobserve(this.interactivity.element);
418
430
  }
419
431
  for (const [, plugin] of this.plugins) {
420
- plugin.stop && plugin.stop();
432
+ plugin.stop?.();
421
433
  }
422
434
  for (const key of this.plugins.keys()) {
423
435
  this.plugins.delete(key);
@@ -1,15 +1,37 @@
1
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  exports.Engine = void 0;
4
27
  const Constants_js_1 = require("./Utils/Constants.js");
5
28
  const Utils_js_1 = require("../Utils/Utils.js");
6
- const Container_js_1 = require("./Container.js");
7
29
  const EventDispatcher_js_1 = require("../Utils/EventDispatcher.js");
8
30
  const NumberUtils_js_1 = require("../Utils/NumberUtils.js");
9
- function getItemsFromInitializer(container, map, initializers, force = false) {
31
+ async function getItemsFromInitializer(container, map, initializers, force = false) {
10
32
  let res = map.get(container);
11
33
  if (!res || force) {
12
- res = [...initializers.values()].map((t) => t(container));
34
+ res = await Promise.all([...initializers.values()].map((t) => t(container)));
13
35
  map.set(container, res);
14
36
  }
15
37
  return res;
@@ -21,7 +43,7 @@ async function getDataFromUrl(data) {
21
43
  }
22
44
  const response = await fetch(url);
23
45
  if (response.ok) {
24
- return response.json();
46
+ return (await response.json());
25
47
  }
26
48
  (0, Utils_js_1.getLogger)().error(`${Constants_js_1.errorPrefix} ${response.status} while retrieving config file`);
27
49
  return data.fallback;
@@ -54,16 +76,18 @@ class Engine {
54
76
  return res;
55
77
  }
56
78
  get version() {
57
- return "3.0.3";
79
+ return "3.2.0";
58
80
  }
59
81
  addConfig(config) {
60
- const name = config.name ?? "default";
61
- this._configs.set(name, config);
62
- this._eventDispatcher.dispatchEvent("configAdded", { data: { name, config } });
82
+ const key = config.key ?? config.name ?? "default";
83
+ this._configs.set(key, config);
84
+ this._eventDispatcher.dispatchEvent("configAdded", { data: { name: key, config } });
63
85
  }
64
86
  async addEffect(effect, drawer, refresh = true) {
65
87
  (0, Utils_js_1.executeOnSingleOrMultiple)(effect, (type) => {
66
- !this.getEffectDrawer(type) && this.effectDrawers.set(type, drawer);
88
+ if (!this.getEffectDrawer(type)) {
89
+ this.effectDrawers.set(type, drawer);
90
+ }
67
91
  });
68
92
  await this.refresh(refresh);
69
93
  }
@@ -83,20 +107,28 @@ class Engine {
83
107
  await this.refresh(refresh);
84
108
  }
85
109
  async addPathGenerator(name, generator, refresh = true) {
86
- !this.getPathGenerator(name) && this.pathGenerators.set(name, generator);
110
+ if (!this.getPathGenerator(name)) {
111
+ this.pathGenerators.set(name, generator);
112
+ }
87
113
  await this.refresh(refresh);
88
114
  }
89
115
  async addPlugin(plugin, refresh = true) {
90
- !this.getPlugin(plugin.id) && this.plugins.push(plugin);
116
+ if (!this.getPlugin(plugin.id)) {
117
+ this.plugins.push(plugin);
118
+ }
91
119
  await this.refresh(refresh);
92
120
  }
93
121
  async addPreset(preset, options, override = false, refresh = true) {
94
- (override || !this.getPreset(preset)) && this.presets.set(preset, options);
122
+ if (override || !this.getPreset(preset)) {
123
+ this.presets.set(preset, options);
124
+ }
95
125
  await this.refresh(refresh);
96
126
  }
97
127
  async addShape(shape, drawer, refresh = true) {
98
128
  (0, Utils_js_1.executeOnSingleOrMultiple)(shape, (type) => {
99
- !this.getShapeDrawer(type) && this.shapeDrawers.set(type, drawer);
129
+ if (!this.getShapeDrawer(type)) {
130
+ this.shapeDrawers.set(type, drawer);
131
+ }
100
132
  });
101
133
  await this.refresh(refresh);
102
134
  }
@@ -114,26 +146,29 @@ class Engine {
114
146
  domItem(index) {
115
147
  const dom = this.dom(), item = dom[index];
116
148
  if (!item || item.destroyed) {
117
- dom.splice(index, 1);
149
+ const deleteCount = 1;
150
+ dom.splice(index, deleteCount);
118
151
  return;
119
152
  }
120
153
  return item;
121
154
  }
122
- getAvailablePlugins(container) {
155
+ async getAvailablePlugins(container) {
123
156
  const res = new Map();
124
157
  for (const plugin of this.plugins) {
125
- plugin.needsPlugin(container.actualOptions) && res.set(plugin.id, plugin.getPlugin(container));
158
+ if (plugin.needsPlugin(container.actualOptions)) {
159
+ res.set(plugin.id, await plugin.getPlugin(container));
160
+ }
126
161
  }
127
162
  return res;
128
163
  }
129
164
  getEffectDrawer(type) {
130
165
  return this.effectDrawers.get(type);
131
166
  }
132
- getInteractors(container, force = false) {
133
- return getItemsFromInitializer(container, this.interactors, this._initializers.interactors, force);
167
+ async getInteractors(container, force = false) {
168
+ return await getItemsFromInitializer(container, this.interactors, this._initializers.interactors, force);
134
169
  }
135
- getMovers(container, force = false) {
136
- return getItemsFromInitializer(container, this.movers, this._initializers.movers, force);
170
+ async getMovers(container, force = false) {
171
+ return await getItemsFromInitializer(container, this.movers, this._initializers.movers, force);
137
172
  }
138
173
  getPathGenerator(type) {
139
174
  return this.pathGenerators.get(type);
@@ -153,8 +188,8 @@ class Engine {
153
188
  getSupportedShapes() {
154
189
  return this.shapeDrawers.keys();
155
190
  }
156
- getUpdaters(container, force = false) {
157
- return getItemsFromInitializer(container, this.updaters, this._initializers.updaters, force);
191
+ async getUpdaters(container, force = false) {
192
+ return await getItemsFromInitializer(container, this.updaters, this._initializers.updaters, force);
158
193
  }
159
194
  init() {
160
195
  if (this._initialized) {
@@ -163,19 +198,20 @@ class Engine {
163
198
  this._initialized = true;
164
199
  }
165
200
  async load(params) {
166
- const id = params.id ?? params.element?.id ?? `tsparticles${Math.floor((0, NumberUtils_js_1.getRandom)() * 10000)}`, { index, url } = params, options = url ? await getDataFromUrl({ fallback: params.options, url, index }) : params.options;
201
+ const randomFactor = 10000, id = params.id ?? params.element?.id ?? `tsparticles${Math.floor((0, NumberUtils_js_1.getRandom)() * randomFactor)}`, { index, url } = params, options = url ? await getDataFromUrl({ fallback: params.options, url, index }) : params.options;
167
202
  let domContainer = params.element ?? document.getElementById(id);
168
203
  if (!domContainer) {
169
204
  domContainer = document.createElement("div");
170
205
  domContainer.id = id;
171
206
  document.body.append(domContainer);
172
207
  }
173
- const currentOptions = (0, Utils_js_1.itemFromSingleOrMultiple)(options, index), dom = this.dom(), oldIndex = dom.findIndex((v) => v.id.description === id);
174
- if (oldIndex >= 0) {
208
+ const currentOptions = (0, Utils_js_1.itemFromSingleOrMultiple)(options, index), dom = this.dom(), oldIndex = dom.findIndex((v) => v.id.description === id), minIndex = 0;
209
+ if (oldIndex >= minIndex) {
175
210
  const old = this.domItem(oldIndex);
176
211
  if (old && !old.destroyed) {
177
212
  old.destroy();
178
- dom.splice(oldIndex, 1);
213
+ const deleteCount = 1;
214
+ dom.splice(oldIndex, deleteCount);
179
215
  }
180
216
  }
181
217
  let canvasEl;
@@ -186,7 +222,8 @@ class Engine {
186
222
  else {
187
223
  const existingCanvases = domContainer.getElementsByTagName("canvas");
188
224
  if (existingCanvases.length) {
189
- canvasEl = existingCanvases[0];
225
+ const firstIndex = 0;
226
+ canvasEl = existingCanvases[firstIndex];
190
227
  canvasEl.dataset[Constants_js_1.generatedAttribute] = "false";
191
228
  }
192
229
  else {
@@ -201,9 +238,10 @@ class Engine {
201
238
  if (!canvasEl.style.height) {
202
239
  canvasEl.style.height = "100%";
203
240
  }
204
- const newItem = new Container_js_1.Container(this, id, currentOptions);
205
- if (oldIndex >= 0) {
206
- dom.splice(oldIndex, 0, newItem);
241
+ const { Container } = await Promise.resolve().then(() => __importStar(require("./Container.js"))), newItem = new Container(this, id, currentOptions);
242
+ if (oldIndex >= minIndex) {
243
+ const deleteCount = 0;
244
+ dom.splice(oldIndex, deleteCount, newItem);
207
245
  }
208
246
  else {
209
247
  dom.push(newItem);
@@ -223,14 +261,14 @@ class Engine {
223
261
  return;
224
262
  }
225
263
  for (const updater of updaters) {
226
- updater.loadOptions && updater.loadOptions(options, ...sourceOptions);
264
+ updater.loadOptions?.(options, ...sourceOptions);
227
265
  }
228
266
  }
229
267
  async refresh(refresh = true) {
230
268
  if (!refresh) {
231
269
  return;
232
270
  }
233
- this.dom().forEach((t) => t.refresh());
271
+ await Promise.all(this.dom().map((t) => t.refresh()));
234
272
  }
235
273
  removeEventListener(type, listener) {
236
274
  this._eventDispatcher.removeEventListener(type, listener);