@tsparticles/engine 4.0.0-beta.1 → 4.0.0-beta.2

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/browser/Core/Container.js +3 -0
  2. package/browser/Core/Engine.js +1 -1
  3. package/browser/Core/Interfaces/IPositionFromSizeParams.js +1 -1
  4. package/browser/Core/ParticlesManager.js +1 -0
  5. package/cjs/Core/Container.js +3 -0
  6. package/cjs/Core/Engine.js +1 -1
  7. package/cjs/Core/Interfaces/IPositionFromSizeParams.js +1 -1
  8. package/cjs/Core/ParticlesManager.js +1 -0
  9. package/dist_browser_Core_Container_js.js +3 -3
  10. package/esm/Core/Container.js +3 -0
  11. package/esm/Core/Engine.js +1 -1
  12. package/esm/Core/Interfaces/IPositionFromSizeParams.js +1 -1
  13. package/esm/Core/ParticlesManager.js +1 -0
  14. package/package.json +2 -3
  15. package/report.html +1 -1
  16. package/tsparticles.engine.js +2 -2
  17. package/tsparticles.engine.min.js +1 -1
  18. package/umd/Core/CanvasManager.js +0 -317
  19. package/umd/Core/Container.js +0 -383
  20. package/umd/Core/Engine.js +0 -183
  21. package/umd/Core/Interfaces/Colors.js +0 -12
  22. package/umd/Core/Interfaces/IBounds.js +0 -12
  23. package/umd/Core/Interfaces/IBubbleParticleData.js +0 -12
  24. package/umd/Core/Interfaces/ICircleBouncer.js +0 -12
  25. package/umd/Core/Interfaces/IColorManager.js +0 -12
  26. package/umd/Core/Interfaces/IContainerPlugin.js +0 -12
  27. package/umd/Core/Interfaces/ICoordinates.js +0 -12
  28. package/umd/Core/Interfaces/IDelta.js +0 -12
  29. package/umd/Core/Interfaces/IDimension.js +0 -12
  30. package/umd/Core/Interfaces/IDistance.js +0 -12
  31. package/umd/Core/Interfaces/IDrawParticleParams.js +0 -12
  32. package/umd/Core/Interfaces/IEffectDrawer.js +0 -12
  33. package/umd/Core/Interfaces/ILoadParams.js +0 -12
  34. package/umd/Core/Interfaces/IPalette.js +0 -12
  35. package/umd/Core/Interfaces/IParticleColorStyle.js +0 -12
  36. package/umd/Core/Interfaces/IParticleHslAnimation.js +0 -12
  37. package/umd/Core/Interfaces/IParticleLife.js +0 -12
  38. package/umd/Core/Interfaces/IParticleOpacityData.js +0 -12
  39. package/umd/Core/Interfaces/IParticleRetinaProps.js +0 -12
  40. package/umd/Core/Interfaces/IParticleRoll.js +0 -12
  41. package/umd/Core/Interfaces/IParticleRotateData.js +0 -12
  42. package/umd/Core/Interfaces/IParticleTransformValues.js +0 -12
  43. package/umd/Core/Interfaces/IParticleUpdater.js +0 -12
  44. package/umd/Core/Interfaces/IParticleValueAnimation.js +0 -12
  45. package/umd/Core/Interfaces/IPlugin.js +0 -12
  46. package/umd/Core/Interfaces/IPositionFromSizeParams.js +0 -12
  47. package/umd/Core/Interfaces/IRangeValue.js +0 -12
  48. package/umd/Core/Interfaces/IShapeDrawData.js +0 -12
  49. package/umd/Core/Interfaces/IShapeDrawer.js +0 -12
  50. package/umd/Core/Interfaces/IShapeValues.js +0 -12
  51. package/umd/Core/Interfaces/ISlowParticleData.js +0 -12
  52. package/umd/Core/Particle.js +0 -433
  53. package/umd/Core/ParticlesManager.js +0 -377
  54. package/umd/Core/RenderManager.js +0 -317
  55. package/umd/Core/Retina.js +0 -47
  56. package/umd/Core/Utils/Constants.js +0 -23
  57. package/umd/Core/Utils/EventListeners.js +0 -102
  58. package/umd/Core/Utils/PluginManager.js +0 -159
  59. package/umd/Core/Utils/Ranges.js +0 -91
  60. package/umd/Core/Utils/SpatialHashGrid.js +0 -116
  61. package/umd/Core/Utils/Vectors.js +0 -126
  62. package/umd/Enums/AnimationStatus.js +0 -18
  63. package/umd/Enums/Directions/MoveDirection.js +0 -27
  64. package/umd/Enums/Directions/OutModeDirection.js +0 -20
  65. package/umd/Enums/Directions/RotateDirection.js +0 -19
  66. package/umd/Enums/Modes/AnimationMode.js +0 -20
  67. package/umd/Enums/Modes/LimitMode.js +0 -18
  68. package/umd/Enums/Modes/OutMode.js +0 -21
  69. package/umd/Enums/Modes/PixelMode.js +0 -18
  70. package/umd/Enums/RangeType.js +0 -18
  71. package/umd/Enums/Types/AlterType.js +0 -18
  72. package/umd/Enums/Types/DestroyType.js +0 -19
  73. package/umd/Enums/Types/EasingType.js +0 -58
  74. package/umd/Enums/Types/EventType.js +0 -28
  75. package/umd/Enums/Types/GradientType.js +0 -19
  76. package/umd/Enums/Types/ParticleOutType.js +0 -19
  77. package/umd/Enums/Types/StartValueType.js +0 -19
  78. package/umd/Options/Classes/AnimatableColor.js +0 -52
  79. package/umd/Options/Classes/AnimationOptions.js +0 -79
  80. package/umd/Options/Classes/Background/Background.js +0 -56
  81. package/umd/Options/Classes/ColorAnimation.js +0 -44
  82. package/umd/Options/Classes/FullScreen/FullScreen.js +0 -34
  83. package/umd/Options/Classes/HslAnimation.js +0 -30
  84. package/umd/Options/Classes/Options.js +0 -171
  85. package/umd/Options/Classes/OptionsColor.js +0 -42
  86. package/umd/Options/Classes/Particles/Bounce/ParticlesBounce.js +0 -31
  87. package/umd/Options/Classes/Particles/Bounce/ParticlesBounceFactor.js +0 -21
  88. package/umd/Options/Classes/Particles/Effect/Effect.js +0 -46
  89. package/umd/Options/Classes/Particles/Fill.js +0 -42
  90. package/umd/Options/Classes/Particles/Move/Move.js +0 -120
  91. package/umd/Options/Classes/Particles/Move/MoveAngle.js +0 -35
  92. package/umd/Options/Classes/Particles/Move/MoveCenter.js +0 -45
  93. package/umd/Options/Classes/Particles/Move/MoveGravity.js +0 -45
  94. package/umd/Options/Classes/Particles/Move/OutModes.js +0 -38
  95. package/umd/Options/Classes/Particles/Move/Path/MovePath.js +0 -46
  96. package/umd/Options/Classes/Particles/Move/Spin.js +0 -40
  97. package/umd/Options/Classes/Particles/Number/ParticlesDensity.js +0 -41
  98. package/umd/Options/Classes/Particles/Number/ParticlesNumber.js +0 -37
  99. package/umd/Options/Classes/Particles/Number/ParticlesNumberLimit.js +0 -35
  100. package/umd/Options/Classes/Particles/Opacity/Opacity.js +0 -35
  101. package/umd/Options/Classes/Particles/Opacity/OpacityAnimation.js +0 -34
  102. package/umd/Options/Classes/Particles/ParticlesOptions.js +0 -116
  103. package/umd/Options/Classes/Particles/Shape/Shape.js +0 -46
  104. package/umd/Options/Classes/Particles/Size/Size.js +0 -35
  105. package/umd/Options/Classes/Particles/Size/SizeAnimation.js +0 -34
  106. package/umd/Options/Classes/Particles/Stroke.js +0 -39
  107. package/umd/Options/Classes/Particles/ZIndex/ZIndex.js +0 -42
  108. package/umd/Options/Classes/ResizeEvent.js +0 -34
  109. package/umd/Options/Classes/ValueWithRandom.js +0 -56
  110. package/umd/Options/Interfaces/Background/IBackground.js +0 -12
  111. package/umd/Options/Interfaces/FullScreen/IFullScreen.js +0 -12
  112. package/umd/Options/Interfaces/IAnimatable.js +0 -12
  113. package/umd/Options/Interfaces/IAnimatableColor.js +0 -12
  114. package/umd/Options/Interfaces/IAnimation.js +0 -12
  115. package/umd/Options/Interfaces/IColorAnimation.js +0 -12
  116. package/umd/Options/Interfaces/IHslAnimation.js +0 -12
  117. package/umd/Options/Interfaces/IOptionLoader.js +0 -12
  118. package/umd/Options/Interfaces/IOptions.js +0 -12
  119. package/umd/Options/Interfaces/IOptionsColor.js +0 -12
  120. package/umd/Options/Interfaces/IResizeEvent.js +0 -12
  121. package/umd/Options/Interfaces/IValueWithRandom.js +0 -12
  122. package/umd/Options/Interfaces/Particles/Bounce/IParticlesBounce.js +0 -12
  123. package/umd/Options/Interfaces/Particles/Effect/IEffect.js +0 -12
  124. package/umd/Options/Interfaces/Particles/IFill.js +0 -12
  125. package/umd/Options/Interfaces/Particles/IParticlesOptions.js +0 -12
  126. package/umd/Options/Interfaces/Particles/IStroke.js +0 -12
  127. package/umd/Options/Interfaces/Particles/Move/IMove.js +0 -12
  128. package/umd/Options/Interfaces/Particles/Move/IMoveAngle.js +0 -12
  129. package/umd/Options/Interfaces/Particles/Move/IMoveCenter.js +0 -12
  130. package/umd/Options/Interfaces/Particles/Move/IMoveGravity.js +0 -12
  131. package/umd/Options/Interfaces/Particles/Move/IOutModes.js +0 -12
  132. package/umd/Options/Interfaces/Particles/Move/ISpin.js +0 -12
  133. package/umd/Options/Interfaces/Particles/Move/Path/IMovePath.js +0 -12
  134. package/umd/Options/Interfaces/Particles/Number/IParticlesDensity.js +0 -12
  135. package/umd/Options/Interfaces/Particles/Number/IParticlesNumber.js +0 -12
  136. package/umd/Options/Interfaces/Particles/Number/IParticlesNumberLimit.js +0 -12
  137. package/umd/Options/Interfaces/Particles/Opacity/IOpacity.js +0 -12
  138. package/umd/Options/Interfaces/Particles/Opacity/IOpacityAnimation.js +0 -12
  139. package/umd/Options/Interfaces/Particles/Shape/IShape.js +0 -12
  140. package/umd/Options/Interfaces/Particles/Size/ISize.js +0 -12
  141. package/umd/Options/Interfaces/Particles/Size/ISizeAnimation.js +0 -12
  142. package/umd/Options/Interfaces/Particles/ZIndex/IZIndex.js +0 -12
  143. package/umd/Types/CanvasContextType.js +0 -12
  144. package/umd/Types/CustomEventArgs.js +0 -12
  145. package/umd/Types/CustomEventListener.js +0 -12
  146. package/umd/Types/EasingFunction.js +0 -12
  147. package/umd/Types/EngineInitializers.js +0 -12
  148. package/umd/Types/ExportResult.js +0 -12
  149. package/umd/Types/ISourceOptions.js +0 -12
  150. package/umd/Types/ParticlesGroups.js +0 -12
  151. package/umd/Types/PathOptions.js +0 -12
  152. package/umd/Types/RangeValue.js +0 -12
  153. package/umd/Types/RecursivePartial.js +0 -12
  154. package/umd/Types/ShapeData.js +0 -12
  155. package/umd/Types/SingleOrMultiple.js +0 -12
  156. package/umd/Utils/CanvasUtils.js +0 -145
  157. package/umd/Utils/ColorUtils.js +0 -395
  158. package/umd/Utils/EventDispatcher.js +0 -63
  159. package/umd/Utils/LogUtils.js +0 -37
  160. package/umd/Utils/MathUtils.js +0 -203
  161. package/umd/Utils/OptionsUtils.js +0 -25
  162. package/umd/Utils/TypeUtils.js +0 -40
  163. package/umd/Utils/Utils.js +0 -484
  164. package/umd/bundle.js +0 -32
  165. package/umd/export-types.js +0 -12
  166. package/umd/exports.js +0 -82
  167. package/umd/index.js +0 -32
  168. package/umd/initEngine.js +0 -17
@@ -1,317 +0,0 @@
1
- (function (factory) {
2
- if (typeof module === "object" && typeof module.exports === "object") {
3
- var v = factory(require, exports);
4
- if (v !== undefined) module.exports = v;
5
- }
6
- else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "../Utils/Utils.js", "./Utils/Constants.js", "../Utils/ColorUtils.js", "./RenderManager.js"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.CanvasManager = void 0;
13
- const Utils_js_1 = require("../Utils/Utils.js");
14
- const Constants_js_1 = require("./Utils/Constants.js");
15
- const ColorUtils_js_1 = require("../Utils/ColorUtils.js");
16
- const RenderManager_js_1 = require("./RenderManager.js");
17
- function setStyle(canvas, style, important = false) {
18
- if (!style) {
19
- return;
20
- }
21
- const element = canvas, elementStyle = element.style, keys = new Set();
22
- for (let i = 0; i < elementStyle.length; i++) {
23
- const key = elementStyle.item(i);
24
- if (!key) {
25
- continue;
26
- }
27
- keys.add(key);
28
- }
29
- for (let i = 0; i < style.length; i++) {
30
- const key = style.item(i);
31
- if (!key) {
32
- continue;
33
- }
34
- keys.add(key);
35
- }
36
- for (const key of keys) {
37
- const value = style.getPropertyValue(key);
38
- if (value) {
39
- elementStyle.setProperty(key, value, important ? "important" : "");
40
- }
41
- else {
42
- elementStyle.removeProperty(key);
43
- }
44
- }
45
- }
46
- class CanvasManager {
47
- element;
48
- render;
49
- size;
50
- zoom = Constants_js_1.defaultZoom;
51
- _container;
52
- _generated;
53
- _mutationObserver;
54
- _originalStyle;
55
- _pluginManager;
56
- _pointerEvents;
57
- _resizePlugins;
58
- _standardSize;
59
- _zoomCenter;
60
- constructor(pluginManager, container) {
61
- this._pluginManager = pluginManager;
62
- this._container = container;
63
- this.render = new RenderManager_js_1.RenderManager(pluginManager, container, this);
64
- this._standardSize = {
65
- height: 0,
66
- width: 0,
67
- };
68
- const pxRatio = container.retina.pixelRatio, stdSize = this._standardSize;
69
- this.size = {
70
- height: stdSize.height * pxRatio,
71
- width: stdSize.width * pxRatio,
72
- };
73
- this._generated = false;
74
- this._resizePlugins = [];
75
- this._pointerEvents = "none";
76
- }
77
- get _fullScreen() {
78
- return this._container.actualOptions.fullScreen.enable;
79
- }
80
- destroy() {
81
- this.stop();
82
- if (this._generated) {
83
- const element = this.element;
84
- element?.remove();
85
- this.element = undefined;
86
- }
87
- else {
88
- this._resetOriginalStyle();
89
- }
90
- this.render.destroy();
91
- this._resizePlugins = [];
92
- }
93
- getZoomCenter() {
94
- const pxRatio = this._container.retina.pixelRatio, { width, height } = this.size;
95
- if (this._zoomCenter) {
96
- return this._zoomCenter;
97
- }
98
- return {
99
- x: (width * Constants_js_1.half) / pxRatio,
100
- y: (height * Constants_js_1.half) / pxRatio,
101
- };
102
- }
103
- init() {
104
- this._safeMutationObserver(obs => {
105
- obs.disconnect();
106
- });
107
- this._mutationObserver = (0, Utils_js_1.safeMutationObserver)(records => {
108
- for (const record of records) {
109
- if (record.type === "attributes" && record.attributeName === "style") {
110
- this._repairStyle();
111
- }
112
- }
113
- });
114
- this.resize();
115
- this._initStyle();
116
- this.initBackground();
117
- this._safeMutationObserver(obs => {
118
- if (!this.element || !(this.element instanceof Node)) {
119
- return;
120
- }
121
- obs.observe(this.element, { attributes: true });
122
- });
123
- this.initPlugins();
124
- this.render.init();
125
- }
126
- initBackground() {
127
- const { _container } = this, options = _container.actualOptions, background = options.background, element = this.element;
128
- if (!element) {
129
- return;
130
- }
131
- const elementStyle = element.style, color = (0, ColorUtils_js_1.rangeColorToRgb)(this._pluginManager, background.color);
132
- if (color) {
133
- elementStyle.backgroundColor = (0, ColorUtils_js_1.getStyleFromRgb)(color, _container.hdr, background.opacity);
134
- }
135
- else {
136
- elementStyle.backgroundColor = "";
137
- }
138
- elementStyle.backgroundImage = background.image || "";
139
- elementStyle.backgroundPosition = background.position || "";
140
- elementStyle.backgroundRepeat = background.repeat || "";
141
- elementStyle.backgroundSize = background.size || "";
142
- }
143
- initPlugins() {
144
- this._resizePlugins = [];
145
- for (const plugin of this._container.plugins) {
146
- if (plugin.resize) {
147
- this._resizePlugins.push(plugin);
148
- }
149
- }
150
- }
151
- loadCanvas(canvas) {
152
- if (this._generated && this.element) {
153
- this.element.remove();
154
- }
155
- const container = this._container;
156
- this._generated =
157
- Constants_js_1.generatedAttribute in canvas.dataset ? canvas.dataset[Constants_js_1.generatedAttribute] === "true" : this._generated;
158
- this.element = canvas;
159
- this.element.ariaHidden = "true";
160
- this._originalStyle = (0, Utils_js_1.cloneStyle)(this.element.style);
161
- const standardSize = this._standardSize;
162
- standardSize.height = canvas.offsetHeight;
163
- standardSize.width = canvas.offsetWidth;
164
- const pxRatio = this._container.retina.pixelRatio, retinaSize = this.size;
165
- canvas.height = retinaSize.height = standardSize.height * pxRatio;
166
- canvas.width = retinaSize.width = standardSize.width * pxRatio;
167
- const canSupportHdrQuery = (0, Utils_js_1.safeMatchMedia)("(color-gamut: p3)");
168
- this.render.setContextSettings({
169
- alpha: true,
170
- colorSpace: canSupportHdrQuery?.matches && container.hdr ? "display-p3" : "srgb",
171
- desynchronized: true,
172
- willReadFrequently: false,
173
- });
174
- this.render.setContext(this.element.getContext("2d", this.render.settings));
175
- this._safeMutationObserver(obs => {
176
- obs.disconnect();
177
- });
178
- container.retina.init();
179
- this.initBackground();
180
- this._safeMutationObserver(obs => {
181
- if (!this.element || !(this.element instanceof Node)) {
182
- return;
183
- }
184
- obs.observe(this.element, { attributes: true });
185
- });
186
- }
187
- resize() {
188
- if (!this.element) {
189
- return false;
190
- }
191
- const container = this._container, currentSize = container.canvas._standardSize, newSize = {
192
- width: this.element.offsetWidth,
193
- height: this.element.offsetHeight,
194
- }, pxRatio = container.retina.pixelRatio, retinaSize = {
195
- width: newSize.width * pxRatio,
196
- height: newSize.height * pxRatio,
197
- };
198
- if (newSize.height === currentSize.height &&
199
- newSize.width === currentSize.width &&
200
- retinaSize.height === this.element.height &&
201
- retinaSize.width === this.element.width) {
202
- return false;
203
- }
204
- const oldSize = { ...currentSize };
205
- currentSize.height = newSize.height;
206
- currentSize.width = newSize.width;
207
- const canvasSize = this.size;
208
- this.element.width = canvasSize.width = retinaSize.width;
209
- this.element.height = canvasSize.height = retinaSize.height;
210
- if (this._container.started) {
211
- container.particles.setResizeFactor({
212
- width: currentSize.width / oldSize.width,
213
- height: currentSize.height / oldSize.height,
214
- });
215
- }
216
- return true;
217
- }
218
- setPointerEvents(type) {
219
- const element = this.element;
220
- if (!element) {
221
- return;
222
- }
223
- this._pointerEvents = type;
224
- this._repairStyle();
225
- }
226
- setZoom(zoomLevel, center) {
227
- this.zoom = zoomLevel;
228
- this._zoomCenter = center;
229
- }
230
- stop() {
231
- this._safeMutationObserver(obs => {
232
- obs.disconnect();
233
- });
234
- this._mutationObserver = undefined;
235
- this.render.stop();
236
- }
237
- async windowResize() {
238
- if (!this.element || !this.resize()) {
239
- return;
240
- }
241
- const container = this._container, needsRefresh = container.updateActualOptions();
242
- container.particles.setDensity();
243
- this._applyResizePlugins();
244
- if (needsRefresh) {
245
- await container.refresh();
246
- }
247
- }
248
- _applyResizePlugins = () => {
249
- for (const plugin of this._resizePlugins) {
250
- plugin.resize?.();
251
- }
252
- };
253
- _initStyle = () => {
254
- const element = this.element, options = this._container.actualOptions;
255
- if (!element) {
256
- return;
257
- }
258
- if (this._fullScreen) {
259
- this._setFullScreenStyle();
260
- }
261
- else {
262
- this._resetOriginalStyle();
263
- }
264
- for (const key in options.style) {
265
- if (!key || !(key in options.style)) {
266
- continue;
267
- }
268
- const value = options.style[key];
269
- if (!value) {
270
- continue;
271
- }
272
- element.style.setProperty(key, value, "important");
273
- }
274
- };
275
- _repairStyle = () => {
276
- const element = this.element;
277
- if (!element) {
278
- return;
279
- }
280
- this._safeMutationObserver(observer => {
281
- observer.disconnect();
282
- });
283
- this._initStyle();
284
- this.initBackground();
285
- const pointerEvents = this._pointerEvents;
286
- element.style.pointerEvents = pointerEvents;
287
- element.setAttribute("pointer-events", pointerEvents);
288
- this._safeMutationObserver(observer => {
289
- if (!(element instanceof Node)) {
290
- return;
291
- }
292
- observer.observe(element, { attributes: true });
293
- });
294
- };
295
- _resetOriginalStyle = () => {
296
- const element = this.element, originalStyle = this._originalStyle;
297
- if (!element || !originalStyle) {
298
- return;
299
- }
300
- setStyle(element, originalStyle, true);
301
- };
302
- _safeMutationObserver = callback => {
303
- if (!this._mutationObserver) {
304
- return;
305
- }
306
- callback(this._mutationObserver);
307
- };
308
- _setFullScreenStyle = () => {
309
- const element = this.element;
310
- if (!element) {
311
- return;
312
- }
313
- setStyle(element, (0, Utils_js_1.getFullScreenStyle)(this._container.actualOptions.fullScreen.zIndex), true);
314
- };
315
- }
316
- exports.CanvasManager = CanvasManager;
317
- });
@@ -1,383 +0,0 @@
1
- (function (factory) {
2
- if (typeof module === "object" && typeof module.exports === "object") {
3
- var v = factory(require, exports);
4
- if (v !== undefined) module.exports = v;
5
- }
6
- else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "../Utils/MathUtils.js", "./Utils/Constants.js", "./CanvasManager.js", "./Utils/EventListeners.js", "../Enums/Types/EventType.js", "../Options/Classes/Options.js", "./ParticlesManager.js", "./Retina.js", "../Utils/LogUtils.js", "../Utils/OptionsUtils.js"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Container = void 0;
13
- const MathUtils_js_1 = require("../Utils/MathUtils.js");
14
- const Constants_js_1 = require("./Utils/Constants.js");
15
- const CanvasManager_js_1 = require("./CanvasManager.js");
16
- const EventListeners_js_1 = require("./Utils/EventListeners.js");
17
- const EventType_js_1 = require("../Enums/Types/EventType.js");
18
- const Options_js_1 = require("../Options/Classes/Options.js");
19
- const ParticlesManager_js_1 = require("./ParticlesManager.js");
20
- const Retina_js_1 = require("./Retina.js");
21
- const LogUtils_js_1 = require("../Utils/LogUtils.js");
22
- const OptionsUtils_js_1 = require("../Utils/OptionsUtils.js");
23
- function guardCheck(container) {
24
- return !container.destroyed;
25
- }
26
- function updateDelta(delta, value, fpsLimit = Constants_js_1.defaultFps, smooth = false) {
27
- delta.value = value;
28
- delta.factor = smooth ? Constants_js_1.defaultFps / fpsLimit : (Constants_js_1.defaultFps * value) / Constants_js_1.millisecondsToSeconds;
29
- }
30
- function loadContainerOptions(pluginManager, container, ...sourceOptionsArr) {
31
- const options = new Options_js_1.Options(pluginManager, container);
32
- (0, OptionsUtils_js_1.loadOptions)(options, ...sourceOptionsArr);
33
- return options;
34
- }
35
- class Container {
36
- actualOptions;
37
- canvas;
38
- destroyed;
39
- effectDrawers;
40
- fpsLimit;
41
- hdr;
42
- id;
43
- pageHidden;
44
- particleCreatedPlugins;
45
- particleDestroyedPlugins;
46
- particlePositionPlugins;
47
- particleUpdaters;
48
- particles;
49
- plugins;
50
- retina;
51
- shapeDrawers;
52
- started;
53
- zLayers;
54
- _delay;
55
- _delayTimeout;
56
- _delta = { value: 0, factor: 0 };
57
- _dispatchCallback;
58
- _drawAnimationFrame;
59
- _duration;
60
- _eventListeners;
61
- _firstStart;
62
- _initialSourceOptions;
63
- _lastFrameTime;
64
- _lifeTime;
65
- _onDestroy;
66
- _options;
67
- _paused;
68
- _pluginManager;
69
- _smooth;
70
- _sourceOptions;
71
- constructor(params) {
72
- const { dispatchCallback, pluginManager, id, onDestroy, sourceOptions } = params;
73
- this._pluginManager = pluginManager;
74
- this._dispatchCallback = dispatchCallback;
75
- this._onDestroy = onDestroy;
76
- this.id = Symbol(id);
77
- this.fpsLimit = 120;
78
- this.hdr = false;
79
- this._smooth = false;
80
- this._delay = 0;
81
- this._duration = 0;
82
- this._lifeTime = 0;
83
- this._firstStart = true;
84
- this.started = false;
85
- this.destroyed = false;
86
- this._paused = true;
87
- this._lastFrameTime = 0;
88
- this.zLayers = 100;
89
- this.pageHidden = false;
90
- this._sourceOptions = sourceOptions;
91
- this._initialSourceOptions = sourceOptions;
92
- this.effectDrawers = new Map();
93
- this.shapeDrawers = new Map();
94
- this.particleUpdaters = [];
95
- this.retina = new Retina_js_1.Retina(this);
96
- this.canvas = new CanvasManager_js_1.CanvasManager(this._pluginManager, this);
97
- this.particles = new ParticlesManager_js_1.ParticlesManager(this._pluginManager, this);
98
- this.plugins = [];
99
- this.particleDestroyedPlugins = [];
100
- this.particleCreatedPlugins = [];
101
- this.particlePositionPlugins = [];
102
- this._options = loadContainerOptions(this._pluginManager, this);
103
- this.actualOptions = loadContainerOptions(this._pluginManager, this);
104
- this._eventListeners = new EventListeners_js_1.EventListeners(this);
105
- this.dispatchEvent(EventType_js_1.EventType.containerBuilt);
106
- }
107
- get animationStatus() {
108
- return !this._paused && !this.pageHidden && guardCheck(this);
109
- }
110
- get options() {
111
- return this._options;
112
- }
113
- get sourceOptions() {
114
- return this._sourceOptions;
115
- }
116
- addLifeTime(value) {
117
- this._lifeTime += value;
118
- }
119
- alive() {
120
- return !this._duration || this._lifeTime <= this._duration;
121
- }
122
- destroy(remove = true) {
123
- if (!guardCheck(this)) {
124
- return;
125
- }
126
- this.stop();
127
- this.particles.destroy();
128
- this.canvas.destroy();
129
- for (const [, effectDrawer] of this.effectDrawers) {
130
- effectDrawer.destroy?.(this);
131
- }
132
- for (const [, shapeDrawer] of this.shapeDrawers) {
133
- shapeDrawer.destroy?.(this);
134
- }
135
- for (const plugin of this.plugins) {
136
- plugin.destroy?.();
137
- }
138
- this.effectDrawers = new Map();
139
- this.shapeDrawers = new Map();
140
- this.particleUpdaters = [];
141
- this.plugins.length = 0;
142
- this._pluginManager.clearPlugins(this);
143
- this.destroyed = true;
144
- this._onDestroy(remove);
145
- this.dispatchEvent(EventType_js_1.EventType.containerDestroyed);
146
- }
147
- dispatchEvent(type, data) {
148
- this._dispatchCallback(type, {
149
- container: this,
150
- data,
151
- });
152
- }
153
- draw(force) {
154
- if (!guardCheck(this)) {
155
- return;
156
- }
157
- let refreshTime = force;
158
- this._drawAnimationFrame = (0, MathUtils_js_1.animate)((timestamp) => {
159
- if (refreshTime) {
160
- this._lastFrameTime = undefined;
161
- refreshTime = false;
162
- }
163
- this._nextFrame(timestamp);
164
- });
165
- }
166
- async export(type, options = {}) {
167
- for (const plugin of this.plugins) {
168
- if (!plugin.export) {
169
- continue;
170
- }
171
- const res = await plugin.export(type, options);
172
- if (!res.supported) {
173
- continue;
174
- }
175
- return res.blob;
176
- }
177
- (0, LogUtils_js_1.getLogger)().error(`Export plugin with type ${type} not found`);
178
- return undefined;
179
- }
180
- async init() {
181
- if (!guardCheck(this)) {
182
- return;
183
- }
184
- const allContainerPlugins = new Map();
185
- for (const plugin of this._pluginManager.plugins) {
186
- const containerPlugin = await plugin.getPlugin(this);
187
- if (containerPlugin.preInit) {
188
- await containerPlugin.preInit();
189
- }
190
- allContainerPlugins.set(plugin, containerPlugin);
191
- }
192
- await this.initDrawersAndUpdaters();
193
- this._options = loadContainerOptions(this._pluginManager, this, this._initialSourceOptions, this.sourceOptions);
194
- this.actualOptions = loadContainerOptions(this._pluginManager, this, this._options);
195
- this.plugins.length = 0;
196
- this.particleDestroyedPlugins.length = 0;
197
- this.particleCreatedPlugins.length = 0;
198
- this.particlePositionPlugins.length = 0;
199
- for (const [plugin, containerPlugin] of allContainerPlugins) {
200
- if (plugin.needsPlugin(this.actualOptions)) {
201
- this.plugins.push(containerPlugin);
202
- if (containerPlugin.particleCreated) {
203
- this.particleCreatedPlugins.push(containerPlugin);
204
- }
205
- if (containerPlugin.particleDestroyed) {
206
- this.particleDestroyedPlugins.push(containerPlugin);
207
- }
208
- if (containerPlugin.particlePosition) {
209
- this.particlePositionPlugins.push(containerPlugin);
210
- }
211
- }
212
- }
213
- this.retina.init();
214
- this.canvas.init();
215
- this.updateActualOptions();
216
- this.canvas.initBackground();
217
- this.canvas.resize();
218
- const { delay, duration, fpsLimit, hdr, smooth, zLayers } = this.actualOptions;
219
- this.hdr = hdr;
220
- this.zLayers = zLayers;
221
- this._duration = (0, MathUtils_js_1.getRangeValue)(duration) * Constants_js_1.millisecondsToSeconds;
222
- this._delay = (0, MathUtils_js_1.getRangeValue)(delay) * Constants_js_1.millisecondsToSeconds;
223
- this._lifeTime = 0;
224
- this.fpsLimit = fpsLimit > Constants_js_1.minFpsLimit ? fpsLimit : Constants_js_1.defaultFpsLimit;
225
- this._smooth = smooth;
226
- for (const plugin of this.plugins) {
227
- await plugin.init?.();
228
- }
229
- await this.particles.init();
230
- this.dispatchEvent(EventType_js_1.EventType.containerInit);
231
- this.particles.setDensity();
232
- for (const plugin of this.plugins) {
233
- plugin.particlesSetup?.();
234
- }
235
- this.dispatchEvent(EventType_js_1.EventType.particlesSetup);
236
- }
237
- async initDrawersAndUpdaters() {
238
- const pluginManager = this._pluginManager;
239
- this.effectDrawers = await pluginManager.getEffectDrawers(this, true);
240
- this.shapeDrawers = await pluginManager.getShapeDrawers(this, true);
241
- this.particleUpdaters = await pluginManager.getUpdaters(this, true);
242
- }
243
- pause() {
244
- if (!guardCheck(this)) {
245
- return;
246
- }
247
- if (this._drawAnimationFrame !== undefined) {
248
- (0, MathUtils_js_1.cancelAnimation)(this._drawAnimationFrame);
249
- delete this._drawAnimationFrame;
250
- }
251
- if (this._paused) {
252
- return;
253
- }
254
- for (const plugin of this.plugins) {
255
- plugin.pause?.();
256
- }
257
- if (!this.pageHidden) {
258
- this._paused = true;
259
- }
260
- this.dispatchEvent(EventType_js_1.EventType.containerPaused);
261
- }
262
- play(force) {
263
- if (!guardCheck(this)) {
264
- return;
265
- }
266
- const needsUpdate = this._paused || force;
267
- if (this._firstStart && !this.actualOptions.autoPlay) {
268
- this._firstStart = false;
269
- return;
270
- }
271
- if (this._paused) {
272
- this._paused = false;
273
- }
274
- if (needsUpdate) {
275
- for (const plugin of this.plugins) {
276
- if (plugin.play) {
277
- plugin.play();
278
- }
279
- }
280
- }
281
- this.dispatchEvent(EventType_js_1.EventType.containerPlay);
282
- this.draw(needsUpdate ?? false);
283
- }
284
- async refresh() {
285
- if (!guardCheck(this)) {
286
- return;
287
- }
288
- this.stop();
289
- return this.start();
290
- }
291
- async reset(sourceOptions) {
292
- if (!guardCheck(this)) {
293
- return;
294
- }
295
- this._initialSourceOptions = sourceOptions;
296
- this._sourceOptions = sourceOptions;
297
- this._options = loadContainerOptions(this._pluginManager, this, this._initialSourceOptions, this.sourceOptions);
298
- this.actualOptions = loadContainerOptions(this._pluginManager, this, this._options);
299
- return this.refresh();
300
- }
301
- async start() {
302
- if (!guardCheck(this) || this.started) {
303
- return;
304
- }
305
- await this.init();
306
- this.started = true;
307
- await new Promise(resolve => {
308
- const start = async () => {
309
- this._eventListeners.addListeners();
310
- for (const plugin of this.plugins) {
311
- await plugin.start?.();
312
- }
313
- this.dispatchEvent(EventType_js_1.EventType.containerStarted);
314
- this.play();
315
- resolve();
316
- };
317
- this._delayTimeout = setTimeout(() => void start(), this._delay);
318
- });
319
- }
320
- stop() {
321
- if (!guardCheck(this) || !this.started) {
322
- return;
323
- }
324
- if (this._delayTimeout) {
325
- clearTimeout(this._delayTimeout);
326
- delete this._delayTimeout;
327
- }
328
- this._firstStart = true;
329
- this.started = false;
330
- this._eventListeners.removeListeners();
331
- this.pause();
332
- this.particles.clear();
333
- this.canvas.stop();
334
- for (const plugin of this.plugins) {
335
- plugin.stop?.();
336
- }
337
- this.particleCreatedPlugins.length = 0;
338
- this.particleDestroyedPlugins.length = 0;
339
- this.particlePositionPlugins.length = 0;
340
- this._sourceOptions = this._options;
341
- this.dispatchEvent(EventType_js_1.EventType.containerStopped);
342
- }
343
- updateActualOptions() {
344
- let refresh = false;
345
- for (const plugin of this.plugins) {
346
- if (plugin.updateActualOptions) {
347
- refresh = plugin.updateActualOptions() || refresh;
348
- }
349
- }
350
- return refresh;
351
- }
352
- _nextFrame = (timestamp) => {
353
- try {
354
- if (!this._smooth &&
355
- this._lastFrameTime !== undefined &&
356
- timestamp < this._lastFrameTime + Constants_js_1.millisecondsToSeconds / this.fpsLimit) {
357
- this.draw(false);
358
- return;
359
- }
360
- this._lastFrameTime ??= timestamp;
361
- updateDelta(this._delta, timestamp - this._lastFrameTime, this.fpsLimit, this._smooth);
362
- this.addLifeTime(this._delta.value);
363
- this._lastFrameTime = timestamp;
364
- if (this._delta.value > Constants_js_1.millisecondsToSeconds) {
365
- this.draw(false);
366
- return;
367
- }
368
- this.canvas.render.drawParticles(this._delta);
369
- if (!this.alive()) {
370
- this.destroy();
371
- return;
372
- }
373
- if (this.animationStatus) {
374
- this.draw(false);
375
- }
376
- }
377
- catch (e) {
378
- (0, LogUtils_js_1.getLogger)().error("error in animation loop", e);
379
- }
380
- };
381
- }
382
- exports.Container = Container;
383
- });