@tsparticles/engine 4.0.0-beta.0 → 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 (228) hide show
  1. package/155.min.js +1 -0
  2. package/README.md +0 -21
  3. package/browser/Core/CanvasManager.js +303 -0
  4. package/browser/Core/Container.js +64 -34
  5. package/browser/Core/Engine.js +26 -138
  6. package/browser/Core/Interfaces/IPositionFromSizeParams.js +1 -1
  7. package/browser/Core/Particle.js +29 -28
  8. package/{cjs/Core/Particles.js → browser/Core/ParticlesManager.js} +28 -57
  9. package/browser/Core/RenderManager.js +303 -0
  10. package/browser/Core/Retina.js +3 -8
  11. package/browser/Core/Utils/PluginManager.js +145 -0
  12. package/browser/Options/Classes/Options.js +7 -7
  13. package/browser/Options/Classes/Particles/ParticlesOptions.js +5 -5
  14. package/browser/Types/CanvasContextType.js +1 -0
  15. package/browser/Utils/CanvasUtils.js +1 -1
  16. package/browser/Utils/ColorUtils.js +21 -21
  17. package/browser/Utils/LogUtils.js +1 -0
  18. package/browser/Utils/OptionsUtils.js +2 -2
  19. package/cjs/Core/CanvasManager.js +303 -0
  20. package/cjs/Core/Container.js +64 -34
  21. package/cjs/Core/Engine.js +26 -138
  22. package/cjs/Core/Interfaces/IPositionFromSizeParams.js +1 -1
  23. package/cjs/Core/Particle.js +29 -28
  24. package/{esm/Core/Particles.js → cjs/Core/ParticlesManager.js} +28 -57
  25. package/cjs/Core/RenderManager.js +303 -0
  26. package/cjs/Core/Retina.js +3 -8
  27. package/cjs/Core/Utils/PluginManager.js +145 -0
  28. package/cjs/Options/Classes/Options.js +7 -7
  29. package/cjs/Options/Classes/Particles/ParticlesOptions.js +5 -5
  30. package/cjs/Types/CanvasContextType.js +1 -0
  31. package/cjs/Utils/CanvasUtils.js +1 -1
  32. package/cjs/Utils/ColorUtils.js +21 -21
  33. package/cjs/Utils/LogUtils.js +1 -0
  34. package/cjs/Utils/OptionsUtils.js +2 -2
  35. package/dist_browser_Core_Container_js.js +24 -14
  36. package/esm/Core/CanvasManager.js +303 -0
  37. package/esm/Core/Container.js +64 -34
  38. package/esm/Core/Engine.js +26 -138
  39. package/esm/Core/Interfaces/IPositionFromSizeParams.js +1 -1
  40. package/esm/Core/Particle.js +29 -28
  41. package/{browser/Core/Particles.js → esm/Core/ParticlesManager.js} +28 -57
  42. package/esm/Core/RenderManager.js +303 -0
  43. package/esm/Core/Retina.js +3 -8
  44. package/esm/Core/Utils/PluginManager.js +145 -0
  45. package/esm/Options/Classes/Options.js +7 -7
  46. package/esm/Options/Classes/Particles/ParticlesOptions.js +5 -5
  47. package/esm/Types/CanvasContextType.js +1 -0
  48. package/esm/Utils/CanvasUtils.js +1 -1
  49. package/esm/Utils/ColorUtils.js +21 -21
  50. package/esm/Utils/LogUtils.js +1 -0
  51. package/esm/Utils/OptionsUtils.js +2 -2
  52. package/package.json +2 -3
  53. package/report.html +84 -29
  54. package/tsparticles.engine.js +18 -8
  55. package/tsparticles.engine.min.js +2 -2
  56. package/types/Core/CanvasManager.d.ts +39 -0
  57. package/types/Core/Container.d.ts +26 -7
  58. package/types/Core/Engine.d.ts +2 -54
  59. package/types/Core/Interfaces/IContainerPlugin.d.ts +8 -7
  60. package/types/Core/Interfaces/IDrawParticleParams.d.ts +2 -1
  61. package/types/Core/Interfaces/IParticleRetinaProps.d.ts +4 -4
  62. package/types/Core/Interfaces/IParticleUpdater.d.ts +2 -1
  63. package/types/Core/Interfaces/IShapeDrawData.d.ts +2 -1
  64. package/types/Core/Particle.d.ts +4 -4
  65. package/types/Core/{Particles.d.ts → ParticlesManager.d.ts} +4 -11
  66. package/types/Core/{Canvas.d.ts → RenderManager.d.ts} +12 -33
  67. package/types/Core/Retina.d.ts +0 -2
  68. package/types/Core/Utils/PluginManager.d.ts +62 -0
  69. package/types/Options/Classes/Options.d.ts +3 -3
  70. package/types/Options/Classes/Particles/ParticlesOptions.d.ts +3 -3
  71. package/types/Types/CanvasContextType.d.ts +1 -0
  72. package/types/Utils/CanvasUtils.d.ts +8 -5
  73. package/types/Utils/ColorUtils.d.ts +8 -8
  74. package/types/Utils/LogUtils.d.ts +1 -0
  75. package/types/Utils/OptionsUtils.d.ts +2 -2
  76. package/types/export-types.d.ts +4 -2
  77. package/164.min.js +0 -1
  78. package/browser/Core/Canvas.js +0 -570
  79. package/cjs/Core/Canvas.js +0 -570
  80. package/esm/Core/Canvas.js +0 -570
  81. package/umd/Core/Canvas.js +0 -584
  82. package/umd/Core/Container.js +0 -356
  83. package/umd/Core/Engine.js +0 -295
  84. package/umd/Core/Interfaces/Colors.js +0 -12
  85. package/umd/Core/Interfaces/IBounds.js +0 -12
  86. package/umd/Core/Interfaces/IBubbleParticleData.js +0 -12
  87. package/umd/Core/Interfaces/ICircleBouncer.js +0 -12
  88. package/umd/Core/Interfaces/IColorManager.js +0 -12
  89. package/umd/Core/Interfaces/IContainerPlugin.js +0 -12
  90. package/umd/Core/Interfaces/ICoordinates.js +0 -12
  91. package/umd/Core/Interfaces/IDelta.js +0 -12
  92. package/umd/Core/Interfaces/IDimension.js +0 -12
  93. package/umd/Core/Interfaces/IDistance.js +0 -12
  94. package/umd/Core/Interfaces/IDrawParticleParams.js +0 -12
  95. package/umd/Core/Interfaces/IEffectDrawer.js +0 -12
  96. package/umd/Core/Interfaces/ILoadParams.js +0 -12
  97. package/umd/Core/Interfaces/IPalette.js +0 -12
  98. package/umd/Core/Interfaces/IParticleColorStyle.js +0 -12
  99. package/umd/Core/Interfaces/IParticleHslAnimation.js +0 -12
  100. package/umd/Core/Interfaces/IParticleLife.js +0 -12
  101. package/umd/Core/Interfaces/IParticleOpacityData.js +0 -12
  102. package/umd/Core/Interfaces/IParticleRetinaProps.js +0 -12
  103. package/umd/Core/Interfaces/IParticleRoll.js +0 -12
  104. package/umd/Core/Interfaces/IParticleRotateData.js +0 -12
  105. package/umd/Core/Interfaces/IParticleTransformValues.js +0 -12
  106. package/umd/Core/Interfaces/IParticleUpdater.js +0 -12
  107. package/umd/Core/Interfaces/IParticleValueAnimation.js +0 -12
  108. package/umd/Core/Interfaces/IPlugin.js +0 -12
  109. package/umd/Core/Interfaces/IPositionFromSizeParams.js +0 -12
  110. package/umd/Core/Interfaces/IRangeValue.js +0 -12
  111. package/umd/Core/Interfaces/IShapeDrawData.js +0 -12
  112. package/umd/Core/Interfaces/IShapeDrawer.js +0 -12
  113. package/umd/Core/Interfaces/IShapeValues.js +0 -12
  114. package/umd/Core/Interfaces/ISlowParticleData.js +0 -12
  115. package/umd/Core/Particle.js +0 -432
  116. package/umd/Core/Particles.js +0 -407
  117. package/umd/Core/Retina.js +0 -52
  118. package/umd/Core/Utils/Constants.js +0 -23
  119. package/umd/Core/Utils/EventListeners.js +0 -102
  120. package/umd/Core/Utils/Ranges.js +0 -91
  121. package/umd/Core/Utils/SpatialHashGrid.js +0 -116
  122. package/umd/Core/Utils/Vectors.js +0 -126
  123. package/umd/Enums/AnimationStatus.js +0 -18
  124. package/umd/Enums/Directions/MoveDirection.js +0 -27
  125. package/umd/Enums/Directions/OutModeDirection.js +0 -20
  126. package/umd/Enums/Directions/RotateDirection.js +0 -19
  127. package/umd/Enums/Modes/AnimationMode.js +0 -20
  128. package/umd/Enums/Modes/LimitMode.js +0 -18
  129. package/umd/Enums/Modes/OutMode.js +0 -21
  130. package/umd/Enums/Modes/PixelMode.js +0 -18
  131. package/umd/Enums/RangeType.js +0 -18
  132. package/umd/Enums/Types/AlterType.js +0 -18
  133. package/umd/Enums/Types/DestroyType.js +0 -19
  134. package/umd/Enums/Types/EasingType.js +0 -58
  135. package/umd/Enums/Types/EventType.js +0 -28
  136. package/umd/Enums/Types/GradientType.js +0 -19
  137. package/umd/Enums/Types/ParticleOutType.js +0 -19
  138. package/umd/Enums/Types/StartValueType.js +0 -19
  139. package/umd/Options/Classes/AnimatableColor.js +0 -52
  140. package/umd/Options/Classes/AnimationOptions.js +0 -79
  141. package/umd/Options/Classes/Background/Background.js +0 -56
  142. package/umd/Options/Classes/ColorAnimation.js +0 -44
  143. package/umd/Options/Classes/FullScreen/FullScreen.js +0 -34
  144. package/umd/Options/Classes/HslAnimation.js +0 -30
  145. package/umd/Options/Classes/Options.js +0 -171
  146. package/umd/Options/Classes/OptionsColor.js +0 -42
  147. package/umd/Options/Classes/Particles/Bounce/ParticlesBounce.js +0 -31
  148. package/umd/Options/Classes/Particles/Bounce/ParticlesBounceFactor.js +0 -21
  149. package/umd/Options/Classes/Particles/Effect/Effect.js +0 -46
  150. package/umd/Options/Classes/Particles/Fill.js +0 -42
  151. package/umd/Options/Classes/Particles/Move/Move.js +0 -120
  152. package/umd/Options/Classes/Particles/Move/MoveAngle.js +0 -35
  153. package/umd/Options/Classes/Particles/Move/MoveCenter.js +0 -45
  154. package/umd/Options/Classes/Particles/Move/MoveGravity.js +0 -45
  155. package/umd/Options/Classes/Particles/Move/OutModes.js +0 -38
  156. package/umd/Options/Classes/Particles/Move/Path/MovePath.js +0 -46
  157. package/umd/Options/Classes/Particles/Move/Spin.js +0 -40
  158. package/umd/Options/Classes/Particles/Number/ParticlesDensity.js +0 -41
  159. package/umd/Options/Classes/Particles/Number/ParticlesNumber.js +0 -37
  160. package/umd/Options/Classes/Particles/Number/ParticlesNumberLimit.js +0 -35
  161. package/umd/Options/Classes/Particles/Opacity/Opacity.js +0 -35
  162. package/umd/Options/Classes/Particles/Opacity/OpacityAnimation.js +0 -34
  163. package/umd/Options/Classes/Particles/ParticlesOptions.js +0 -116
  164. package/umd/Options/Classes/Particles/Shape/Shape.js +0 -46
  165. package/umd/Options/Classes/Particles/Size/Size.js +0 -35
  166. package/umd/Options/Classes/Particles/Size/SizeAnimation.js +0 -34
  167. package/umd/Options/Classes/Particles/Stroke.js +0 -39
  168. package/umd/Options/Classes/Particles/ZIndex/ZIndex.js +0 -42
  169. package/umd/Options/Classes/ResizeEvent.js +0 -34
  170. package/umd/Options/Classes/ValueWithRandom.js +0 -56
  171. package/umd/Options/Interfaces/Background/IBackground.js +0 -12
  172. package/umd/Options/Interfaces/FullScreen/IFullScreen.js +0 -12
  173. package/umd/Options/Interfaces/IAnimatable.js +0 -12
  174. package/umd/Options/Interfaces/IAnimatableColor.js +0 -12
  175. package/umd/Options/Interfaces/IAnimation.js +0 -12
  176. package/umd/Options/Interfaces/IColorAnimation.js +0 -12
  177. package/umd/Options/Interfaces/IHslAnimation.js +0 -12
  178. package/umd/Options/Interfaces/IOptionLoader.js +0 -12
  179. package/umd/Options/Interfaces/IOptions.js +0 -12
  180. package/umd/Options/Interfaces/IOptionsColor.js +0 -12
  181. package/umd/Options/Interfaces/IResizeEvent.js +0 -12
  182. package/umd/Options/Interfaces/IValueWithRandom.js +0 -12
  183. package/umd/Options/Interfaces/Particles/Bounce/IParticlesBounce.js +0 -12
  184. package/umd/Options/Interfaces/Particles/Effect/IEffect.js +0 -12
  185. package/umd/Options/Interfaces/Particles/IFill.js +0 -12
  186. package/umd/Options/Interfaces/Particles/IParticlesOptions.js +0 -12
  187. package/umd/Options/Interfaces/Particles/IStroke.js +0 -12
  188. package/umd/Options/Interfaces/Particles/Move/IMove.js +0 -12
  189. package/umd/Options/Interfaces/Particles/Move/IMoveAngle.js +0 -12
  190. package/umd/Options/Interfaces/Particles/Move/IMoveCenter.js +0 -12
  191. package/umd/Options/Interfaces/Particles/Move/IMoveGravity.js +0 -12
  192. package/umd/Options/Interfaces/Particles/Move/IOutModes.js +0 -12
  193. package/umd/Options/Interfaces/Particles/Move/ISpin.js +0 -12
  194. package/umd/Options/Interfaces/Particles/Move/Path/IMovePath.js +0 -12
  195. package/umd/Options/Interfaces/Particles/Number/IParticlesDensity.js +0 -12
  196. package/umd/Options/Interfaces/Particles/Number/IParticlesNumber.js +0 -12
  197. package/umd/Options/Interfaces/Particles/Number/IParticlesNumberLimit.js +0 -12
  198. package/umd/Options/Interfaces/Particles/Opacity/IOpacity.js +0 -12
  199. package/umd/Options/Interfaces/Particles/Opacity/IOpacityAnimation.js +0 -12
  200. package/umd/Options/Interfaces/Particles/Shape/IShape.js +0 -12
  201. package/umd/Options/Interfaces/Particles/Size/ISize.js +0 -12
  202. package/umd/Options/Interfaces/Particles/Size/ISizeAnimation.js +0 -12
  203. package/umd/Options/Interfaces/Particles/ZIndex/IZIndex.js +0 -12
  204. package/umd/Types/CustomEventArgs.js +0 -12
  205. package/umd/Types/CustomEventListener.js +0 -12
  206. package/umd/Types/EasingFunction.js +0 -12
  207. package/umd/Types/EngineInitializers.js +0 -12
  208. package/umd/Types/ExportResult.js +0 -12
  209. package/umd/Types/ISourceOptions.js +0 -12
  210. package/umd/Types/ParticlesGroups.js +0 -12
  211. package/umd/Types/PathOptions.js +0 -12
  212. package/umd/Types/RangeValue.js +0 -12
  213. package/umd/Types/RecursivePartial.js +0 -12
  214. package/umd/Types/ShapeData.js +0 -12
  215. package/umd/Types/SingleOrMultiple.js +0 -12
  216. package/umd/Utils/CanvasUtils.js +0 -145
  217. package/umd/Utils/ColorUtils.js +0 -395
  218. package/umd/Utils/EventDispatcher.js +0 -63
  219. package/umd/Utils/LogUtils.js +0 -36
  220. package/umd/Utils/MathUtils.js +0 -203
  221. package/umd/Utils/OptionsUtils.js +0 -25
  222. package/umd/Utils/TypeUtils.js +0 -40
  223. package/umd/Utils/Utils.js +0 -484
  224. package/umd/bundle.js +0 -32
  225. package/umd/export-types.js +0 -12
  226. package/umd/exports.js +0 -82
  227. package/umd/index.js +0 -32
  228. package/umd/initEngine.js +0 -17
@@ -0,0 +1,303 @@
1
+ import { clear, drawParticle, drawParticlePlugin, paintBase, paintImage } from "../Utils/CanvasUtils.js";
2
+ import { defaultCompositeValue, defaultTransformValue, minimumSize, zIndexFactorOffset } from "./Utils/Constants.js";
3
+ import { getStyleFromHsl, rangeColorToHsl } from "../Utils/ColorUtils.js";
4
+ const fColorIndex = 0, sColorIndex = 1;
5
+ function setTransformValue(factor, newFactor, key) {
6
+ const newValue = newFactor[key];
7
+ if (newValue !== undefined) {
8
+ factor[key] = (factor[key] ?? defaultTransformValue) * newValue;
9
+ }
10
+ }
11
+ export class RenderManager {
12
+ _canvasClearPlugins;
13
+ _canvasManager;
14
+ _canvasPaintPlugins;
15
+ _clearDrawPlugins;
16
+ _colorPlugins;
17
+ _container;
18
+ _context;
19
+ _contextSettings;
20
+ _drawParticlePlugins;
21
+ _drawParticlesCleanupPlugins;
22
+ _drawParticlesSetupPlugins;
23
+ _drawPlugins;
24
+ _drawSettingsCleanupPlugins;
25
+ _drawSettingsSetupPlugins;
26
+ _pluginManager;
27
+ _postDrawUpdaters;
28
+ _preDrawUpdaters;
29
+ _reusableColorStyles = {};
30
+ _reusablePluginColors = [undefined, undefined];
31
+ _reusableTransform = {};
32
+ constructor(pluginManager, container, canvasManager) {
33
+ this._pluginManager = pluginManager;
34
+ this._container = container;
35
+ this._canvasManager = canvasManager;
36
+ this._context = null;
37
+ this._preDrawUpdaters = [];
38
+ this._postDrawUpdaters = [];
39
+ this._colorPlugins = [];
40
+ this._canvasClearPlugins = [];
41
+ this._canvasPaintPlugins = [];
42
+ this._clearDrawPlugins = [];
43
+ this._drawParticlePlugins = [];
44
+ this._drawParticlesCleanupPlugins = [];
45
+ this._drawParticlesSetupPlugins = [];
46
+ this._drawPlugins = [];
47
+ this._drawSettingsSetupPlugins = [];
48
+ this._drawSettingsCleanupPlugins = [];
49
+ }
50
+ get settings() {
51
+ return this._contextSettings;
52
+ }
53
+ canvasClear() {
54
+ if (!this._container.actualOptions.clear) {
55
+ return;
56
+ }
57
+ this.draw(ctx => {
58
+ clear(ctx, this._canvasManager.size);
59
+ });
60
+ }
61
+ clear() {
62
+ let pluginHandled = false;
63
+ for (const plugin of this._canvasClearPlugins) {
64
+ pluginHandled = plugin.canvasClear?.() ?? false;
65
+ if (pluginHandled) {
66
+ break;
67
+ }
68
+ }
69
+ if (pluginHandled) {
70
+ return;
71
+ }
72
+ this.canvasClear();
73
+ }
74
+ destroy() {
75
+ this.stop();
76
+ this._preDrawUpdaters = [];
77
+ this._postDrawUpdaters = [];
78
+ this._colorPlugins = [];
79
+ this._canvasClearPlugins = [];
80
+ this._canvasPaintPlugins = [];
81
+ this._clearDrawPlugins = [];
82
+ this._drawParticlePlugins = [];
83
+ this._drawParticlesCleanupPlugins = [];
84
+ this._drawParticlesSetupPlugins = [];
85
+ this._drawPlugins = [];
86
+ this._drawSettingsSetupPlugins = [];
87
+ this._drawSettingsCleanupPlugins = [];
88
+ }
89
+ draw(cb) {
90
+ const ctx = this._context;
91
+ if (!ctx) {
92
+ return;
93
+ }
94
+ return cb(ctx);
95
+ }
96
+ drawParticle(particle, delta) {
97
+ if (particle.spawning || particle.destroyed) {
98
+ return;
99
+ }
100
+ const radius = particle.getRadius();
101
+ if (radius <= minimumSize) {
102
+ return;
103
+ }
104
+ const pfColor = particle.getFillColor(), psColor = particle.getStrokeColor();
105
+ let [fColor, sColor] = this._getPluginParticleColors(particle);
106
+ fColor ??= pfColor;
107
+ sColor ??= psColor;
108
+ if (!fColor && !sColor) {
109
+ return;
110
+ }
111
+ const container = this._container, zIndexOptions = particle.options.zIndex, zIndexFactor = zIndexFactorOffset - particle.zIndexFactor, { fillOpacity, opacity, strokeOpacity } = particle.getOpacity(), transform = this._reusableTransform, colorStyles = this._reusableColorStyles, fill = fColor ? getStyleFromHsl(fColor, container.hdr, fillOpacity * opacity) : undefined, stroke = sColor ? getStyleFromHsl(sColor, container.hdr, strokeOpacity * opacity) : fill;
112
+ transform.a = transform.b = transform.c = transform.d = undefined;
113
+ colorStyles.fill = fill;
114
+ colorStyles.stroke = stroke;
115
+ this.draw((context) => {
116
+ for (const plugin of this._drawParticlesSetupPlugins) {
117
+ plugin.drawParticleSetup?.(context, particle, delta);
118
+ }
119
+ this._applyPreDrawUpdaters(context, particle, radius, opacity, colorStyles, transform);
120
+ drawParticle({
121
+ container,
122
+ context,
123
+ particle,
124
+ delta,
125
+ colorStyles,
126
+ radius: radius * zIndexFactor ** zIndexOptions.sizeRate,
127
+ opacity: opacity,
128
+ transform,
129
+ });
130
+ this._applyPostDrawUpdaters(particle);
131
+ for (const plugin of this._drawParticlesCleanupPlugins) {
132
+ plugin.drawParticleCleanup?.(context, particle, delta);
133
+ }
134
+ });
135
+ }
136
+ drawParticlePlugins(particle, delta) {
137
+ this.draw(ctx => {
138
+ for (const plugin of this._drawParticlePlugins) {
139
+ drawParticlePlugin(ctx, plugin, particle, delta);
140
+ }
141
+ });
142
+ }
143
+ drawParticles(delta) {
144
+ const { particles } = this._container;
145
+ this.clear();
146
+ particles.update(delta);
147
+ this.draw(ctx => {
148
+ for (const plugin of this._drawSettingsSetupPlugins) {
149
+ plugin.drawSettingsSetup?.(ctx, delta);
150
+ }
151
+ for (const plugin of this._drawPlugins) {
152
+ plugin.draw?.(ctx, delta);
153
+ }
154
+ particles.drawParticles(delta);
155
+ for (const plugin of this._clearDrawPlugins) {
156
+ plugin.clearDraw?.(ctx, delta);
157
+ }
158
+ for (const plugin of this._drawSettingsCleanupPlugins) {
159
+ plugin.drawSettingsCleanup?.(ctx, delta);
160
+ }
161
+ });
162
+ }
163
+ init() {
164
+ this.initUpdaters();
165
+ this.initPlugins();
166
+ this.paint();
167
+ }
168
+ initPlugins() {
169
+ this._colorPlugins = [];
170
+ this._canvasClearPlugins = [];
171
+ this._canvasPaintPlugins = [];
172
+ this._clearDrawPlugins = [];
173
+ this._drawParticlePlugins = [];
174
+ this._drawParticlesSetupPlugins = [];
175
+ this._drawParticlesCleanupPlugins = [];
176
+ this._drawPlugins = [];
177
+ this._drawSettingsSetupPlugins = [];
178
+ this._drawSettingsCleanupPlugins = [];
179
+ for (const plugin of this._container.plugins) {
180
+ if (plugin.particleFillColor ?? plugin.particleStrokeColor) {
181
+ this._colorPlugins.push(plugin);
182
+ }
183
+ if (plugin.canvasClear) {
184
+ this._canvasClearPlugins.push(plugin);
185
+ }
186
+ if (plugin.canvasPaint) {
187
+ this._canvasPaintPlugins.push(plugin);
188
+ }
189
+ if (plugin.drawParticle) {
190
+ this._drawParticlePlugins.push(plugin);
191
+ }
192
+ if (plugin.drawParticleSetup) {
193
+ this._drawParticlesSetupPlugins.push(plugin);
194
+ }
195
+ if (plugin.drawParticleCleanup) {
196
+ this._drawParticlesCleanupPlugins.push(plugin);
197
+ }
198
+ if (plugin.draw) {
199
+ this._drawPlugins.push(plugin);
200
+ }
201
+ if (plugin.drawSettingsSetup) {
202
+ this._drawSettingsSetupPlugins.push(plugin);
203
+ }
204
+ if (plugin.drawSettingsCleanup) {
205
+ this._drawSettingsCleanupPlugins.push(plugin);
206
+ }
207
+ if (plugin.clearDraw) {
208
+ this._clearDrawPlugins.push(plugin);
209
+ }
210
+ }
211
+ }
212
+ initUpdaters() {
213
+ this._preDrawUpdaters = [];
214
+ this._postDrawUpdaters = [];
215
+ for (const updater of this._container.particleUpdaters) {
216
+ if (updater.afterDraw) {
217
+ this._postDrawUpdaters.push(updater);
218
+ }
219
+ if (updater.getColorStyles ?? updater.getTransformValues ?? updater.beforeDraw) {
220
+ this._preDrawUpdaters.push(updater);
221
+ }
222
+ }
223
+ }
224
+ paint() {
225
+ let handled = false;
226
+ for (const plugin of this._canvasPaintPlugins) {
227
+ handled = plugin.canvasPaint?.() ?? false;
228
+ if (handled) {
229
+ break;
230
+ }
231
+ }
232
+ if (handled) {
233
+ return;
234
+ }
235
+ this.paintBase();
236
+ }
237
+ paintBase(baseColor) {
238
+ this.draw(ctx => {
239
+ paintBase(ctx, this._canvasManager.size, baseColor);
240
+ });
241
+ }
242
+ paintImage(image, opacity) {
243
+ this.draw(ctx => {
244
+ paintImage(ctx, this._canvasManager.size, image, opacity);
245
+ });
246
+ }
247
+ setContext(context) {
248
+ this._context = context;
249
+ if (this._context) {
250
+ this._context.globalCompositeOperation = defaultCompositeValue;
251
+ }
252
+ }
253
+ setContextSettings(settings) {
254
+ this._contextSettings = settings;
255
+ }
256
+ stop() {
257
+ this.draw(ctx => {
258
+ clear(ctx, this._canvasManager.size);
259
+ });
260
+ }
261
+ _applyPostDrawUpdaters = particle => {
262
+ for (const updater of this._postDrawUpdaters) {
263
+ updater.afterDraw?.(particle);
264
+ }
265
+ };
266
+ _applyPreDrawUpdaters = (ctx, particle, radius, zOpacity, colorStyles, transform) => {
267
+ for (const updater of this._preDrawUpdaters) {
268
+ if (updater.getColorStyles) {
269
+ const { fill, stroke } = updater.getColorStyles(particle, ctx, radius, zOpacity);
270
+ if (fill) {
271
+ colorStyles.fill = fill;
272
+ }
273
+ if (stroke) {
274
+ colorStyles.stroke = stroke;
275
+ }
276
+ }
277
+ if (updater.getTransformValues) {
278
+ const updaterTransform = updater.getTransformValues(particle);
279
+ for (const key in updaterTransform) {
280
+ setTransformValue(transform, updaterTransform, key);
281
+ }
282
+ }
283
+ updater.beforeDraw?.(particle);
284
+ }
285
+ };
286
+ _getPluginParticleColors = particle => {
287
+ let fColor, sColor;
288
+ for (const plugin of this._colorPlugins) {
289
+ if (!fColor && plugin.particleFillColor) {
290
+ fColor = rangeColorToHsl(this._pluginManager, plugin.particleFillColor(particle));
291
+ }
292
+ if (!sColor && plugin.particleStrokeColor) {
293
+ sColor = rangeColorToHsl(this._pluginManager, plugin.particleStrokeColor(particle));
294
+ }
295
+ if (fColor && sColor) {
296
+ break;
297
+ }
298
+ }
299
+ this._reusablePluginColors[fColorIndex] = fColor;
300
+ this._reusablePluginColors[sColorIndex] = sColor;
301
+ return this._reusablePluginColors;
302
+ };
303
+ }
@@ -2,10 +2,8 @@ import { defaultRatio, defaultReduceFactor } from "./Utils/Constants.js";
2
2
  import { getRangeValue } from "../Utils/MathUtils.js";
3
3
  export class Retina {
4
4
  container;
5
- maxSpeed;
6
5
  pixelRatio;
7
6
  reduceFactor;
8
- sizeAnimationSpeed;
9
7
  constructor(container) {
10
8
  this.container = container;
11
9
  this.pixelRatio = defaultRatio;
@@ -21,18 +19,15 @@ export class Retina {
21
19
  canvas.size.width = element.offsetWidth * ratio;
22
20
  canvas.size.height = element.offsetHeight * ratio;
23
21
  }
24
- const particles = options.particles, moveOptions = particles.move;
25
- this.maxSpeed = getRangeValue(moveOptions.gravity.maxSpeed) * ratio;
26
- this.sizeAnimationSpeed = getRangeValue(particles.size.animation.speed) * ratio;
27
22
  }
28
23
  initParticle(particle) {
29
24
  const options = particle.options, ratio = this.pixelRatio, moveOptions = options.move, moveDistance = moveOptions.distance, props = particle.retina;
25
+ props.maxSpeed = getRangeValue(moveOptions.gravity.maxSpeed) * ratio;
30
26
  props.moveDrift = getRangeValue(moveOptions.drift) * ratio;
31
27
  props.moveSpeed = getRangeValue(moveOptions.speed) * ratio;
32
28
  props.sizeAnimationSpeed = getRangeValue(options.size.animation.speed) * ratio;
33
29
  const maxDistance = props.maxDistance;
34
- maxDistance.horizontal = moveDistance.horizontal !== undefined ? moveDistance.horizontal * ratio : undefined;
35
- maxDistance.vertical = moveDistance.vertical !== undefined ? moveDistance.vertical * ratio : undefined;
36
- props.maxSpeed = getRangeValue(moveOptions.gravity.maxSpeed) * ratio;
30
+ maxDistance.horizontal = moveDistance.horizontal === undefined ? undefined : moveDistance.horizontal * ratio;
31
+ maxDistance.vertical = moveDistance.vertical === undefined ? undefined : moveDistance.vertical * ratio;
37
32
  }
38
33
  }
@@ -0,0 +1,145 @@
1
+ import { getItemMapFromInitializer, getItemsFromInitializer } from "../../Utils/Utils.js";
2
+ import { EventType } from "../../Enums/Types/EventType.js";
3
+ export class PluginManager {
4
+ colorManagers = new Map();
5
+ easingFunctions = new Map();
6
+ effectDrawers = new Map();
7
+ initializers = {
8
+ effects: new Map(),
9
+ shapes: new Map(),
10
+ updaters: new Map(),
11
+ };
12
+ palettes = new Map();
13
+ plugins = [];
14
+ presets = new Map();
15
+ shapeDrawers = new Map();
16
+ updaters = new Map();
17
+ _allLoadersSet = new Set();
18
+ _configs = new Map();
19
+ _engine;
20
+ _executedSet = new Set();
21
+ _initialized = false;
22
+ _isRunningLoaders = false;
23
+ _loadPromises = new Set();
24
+ constructor(engine) {
25
+ this._engine = engine;
26
+ }
27
+ get configs() {
28
+ const res = {};
29
+ for (const [name, config] of this._configs) {
30
+ res[name] = config;
31
+ }
32
+ return res;
33
+ }
34
+ addColorManager(name, manager) {
35
+ this.colorManagers.set(name, manager);
36
+ }
37
+ addConfig(config) {
38
+ const key = config.key ?? config.name ?? "default";
39
+ this._configs.set(key, config);
40
+ this._engine.dispatchEvent(EventType.configAdded, { data: { name: key, config } });
41
+ }
42
+ addEasing(name, easing) {
43
+ if (this.easingFunctions.get(name)) {
44
+ return;
45
+ }
46
+ this.easingFunctions.set(name, easing);
47
+ }
48
+ addEffect(effect, drawer) {
49
+ this.initializers.effects.set(effect, drawer);
50
+ }
51
+ addPalette(name, palette) {
52
+ this.palettes.set(name, palette);
53
+ }
54
+ addParticleUpdater(name, updaterInitializer) {
55
+ this.initializers.updaters.set(name, updaterInitializer);
56
+ }
57
+ addPlugin(plugin) {
58
+ if (this.getPlugin(plugin.id)) {
59
+ return;
60
+ }
61
+ this.plugins.push(plugin);
62
+ }
63
+ addPreset(preset, options, override = false) {
64
+ if (!(override || !this.getPreset(preset))) {
65
+ return;
66
+ }
67
+ this.presets.set(preset, options);
68
+ }
69
+ addShape(shapes, drawer) {
70
+ for (const shape of shapes) {
71
+ this.initializers.shapes.set(shape, drawer);
72
+ }
73
+ }
74
+ clearPlugins(container) {
75
+ this.effectDrawers.delete(container);
76
+ this.shapeDrawers.delete(container);
77
+ this.updaters.delete(container);
78
+ }
79
+ getEasing(name) {
80
+ return this.easingFunctions.get(name) ?? ((value) => value);
81
+ }
82
+ getEffectDrawers(container, force = false) {
83
+ return getItemMapFromInitializer(container, this.effectDrawers, this.initializers.effects, force);
84
+ }
85
+ getPalette(name) {
86
+ return this.palettes.get(name);
87
+ }
88
+ getPlugin(plugin) {
89
+ return this.plugins.find(t => t.id === plugin);
90
+ }
91
+ getPreset(preset) {
92
+ return this.presets.get(preset);
93
+ }
94
+ async getShapeDrawers(container, force = false) {
95
+ return getItemMapFromInitializer(container, this.shapeDrawers, this.initializers.shapes, force);
96
+ }
97
+ async getUpdaters(container, force = false) {
98
+ return getItemsFromInitializer(container, this.updaters, this.initializers.updaters, force);
99
+ }
100
+ async init() {
101
+ if (this._initialized || this._isRunningLoaders) {
102
+ return;
103
+ }
104
+ this._isRunningLoaders = true;
105
+ this._executedSet = new Set();
106
+ this._allLoadersSet = new Set(this._loadPromises);
107
+ try {
108
+ for (const loader of this._allLoadersSet) {
109
+ await this._runLoader(loader, this._executedSet, this._allLoadersSet);
110
+ }
111
+ }
112
+ finally {
113
+ this._loadPromises.clear();
114
+ this._isRunningLoaders = false;
115
+ this._initialized = true;
116
+ }
117
+ }
118
+ loadParticlesOptions(container, options, ...sourceOptions) {
119
+ const updaters = this.updaters.get(container);
120
+ if (!updaters) {
121
+ return;
122
+ }
123
+ updaters.forEach(updater => updater.loadOptions?.(options, ...sourceOptions));
124
+ }
125
+ async register(...loaders) {
126
+ if (this._initialized) {
127
+ throw new Error("Register plugins can only be done before calling tsParticles.load()");
128
+ }
129
+ for (const loader of loaders) {
130
+ if (this._isRunningLoaders) {
131
+ await this._runLoader(loader, this._executedSet, this._allLoadersSet);
132
+ }
133
+ else {
134
+ this._loadPromises.add(loader);
135
+ }
136
+ }
137
+ }
138
+ async _runLoader(loader, executed, allLoaders) {
139
+ if (executed.has(loader))
140
+ return;
141
+ executed.add(loader);
142
+ allLoaders.add(loader);
143
+ await loader(this._engine);
144
+ }
145
+ }
@@ -28,9 +28,9 @@ export class Options {
28
28
  style;
29
29
  zLayers;
30
30
  _container;
31
- _engine;
32
- constructor(engine, container) {
33
- this._engine = engine;
31
+ _pluginManager;
32
+ constructor(pluginManager, container) {
33
+ this._pluginManager = pluginManager;
34
34
  this._container = container;
35
35
  this.autoPlay = true;
36
36
  this.background = new Background();
@@ -42,7 +42,7 @@ export class Options {
42
42
  this.duration = 0;
43
43
  this.fpsLimit = 120;
44
44
  this.hdr = true;
45
- this.particles = loadParticlesOptions(this._engine, this._container);
45
+ this.particles = loadParticlesOptions(this._pluginManager, this._container);
46
46
  this.pauseOnBlur = true;
47
47
  this.pauseOnOutsideViewport = true;
48
48
  this.resize = new ResizeEvent();
@@ -116,12 +116,12 @@ export class Options {
116
116
  if (data.smooth !== undefined) {
117
117
  this.smooth = data.smooth;
118
118
  }
119
- this._engine.plugins.forEach(plugin => {
119
+ this._pluginManager.plugins.forEach(plugin => {
120
120
  plugin.loadOptions(this._container, this, data);
121
121
  });
122
122
  }
123
123
  _importPalette = palette => {
124
- const paletteData = this._engine.getPalette(palette);
124
+ const paletteData = this._pluginManager.getPalette(palette);
125
125
  if (!paletteData) {
126
126
  return;
127
127
  }
@@ -152,6 +152,6 @@ export class Options {
152
152
  });
153
153
  };
154
154
  _importPreset = preset => {
155
- this.load(this._engine.getPreset(preset));
155
+ this.load(this._pluginManager.getPreset(preset));
156
156
  };
157
157
  }
@@ -24,9 +24,9 @@ export class ParticlesOptions {
24
24
  stroke;
25
25
  zIndex;
26
26
  _container;
27
- _engine;
28
- constructor(engine, container) {
29
- this._engine = engine;
27
+ _pluginManager;
28
+ constructor(pluginManager, container) {
29
+ this._pluginManager = pluginManager;
30
30
  this._container = container;
31
31
  this.bounce = new ParticlesBounce();
32
32
  this.effect = new Effect();
@@ -84,12 +84,12 @@ export class ParticlesOptions {
84
84
  });
85
85
  }
86
86
  if (this._container) {
87
- for (const plugin of this._engine.plugins) {
87
+ for (const plugin of this._pluginManager.plugins) {
88
88
  if (plugin.loadParticlesOptions) {
89
89
  plugin.loadParticlesOptions(this._container, this, data);
90
90
  }
91
91
  }
92
- const updaters = this._engine.updaters.get(this._container);
92
+ const updaters = this._pluginManager.updaters.get(this._container);
93
93
  if (updaters) {
94
94
  for (const updater of updaters) {
95
95
  if (updater.loadOptions) {
@@ -0,0 +1 @@
1
+ export {};
@@ -16,7 +16,7 @@ export function clear(context, dimension) {
16
16
  context.clearRect(originPoint.x, originPoint.y, dimension.width, dimension.height);
17
17
  }
18
18
  export function drawParticle(data) {
19
- const { container, context, particle, delta, colorStyles, radius, opacity, transform } = data, { effectDrawers, shapeDrawers } = container.particles, pos = particle.getPosition(), transformData = particle.getTransformData(transform), drawScale = defaultZoom, drawPosition = {
19
+ const { container, context, particle, delta, colorStyles, radius, opacity, transform } = data, { effectDrawers, shapeDrawers } = container, pos = particle.getPosition(), transformData = particle.getTransformData(transform), drawScale = defaultZoom, drawPosition = {
20
20
  x: pos.x,
21
21
  y: pos.y,
22
22
  };
@@ -17,35 +17,35 @@ function getCachedStyle(key, generator) {
17
17
  }
18
18
  return cached;
19
19
  }
20
- function stringToRgba(engine, input) {
20
+ function stringToRgba(pluginManager, input) {
21
21
  if (!input) {
22
22
  return;
23
23
  }
24
- for (const manager of engine.colorManagers.values()) {
24
+ for (const manager of pluginManager.colorManagers.values()) {
25
25
  if (manager.accepts(input)) {
26
26
  return manager.parseString(input);
27
27
  }
28
28
  }
29
29
  return undefined;
30
30
  }
31
- export function rangeColorToRgb(engine, input, index, useIndex = true) {
31
+ export function rangeColorToRgb(pluginManager, input, index, useIndex = true) {
32
32
  if (!input) {
33
33
  return;
34
34
  }
35
35
  const color = isString(input) ? { value: input } : input;
36
36
  if (isString(color.value)) {
37
- return colorToRgb(engine, color.value, index, useIndex);
37
+ return colorToRgb(pluginManager, color.value, index, useIndex);
38
38
  }
39
39
  if (isArray(color.value)) {
40
40
  const value = itemFromArray(color.value, index, useIndex);
41
41
  if (!value) {
42
42
  return;
43
43
  }
44
- return rangeColorToRgb(engine, {
44
+ return rangeColorToRgb(pluginManager, {
45
45
  value,
46
46
  });
47
47
  }
48
- for (const manager of engine.colorManagers.values()) {
48
+ for (const manager of pluginManager.colorManagers.values()) {
49
49
  const res = manager.handleRangeColor(color);
50
50
  if (res) {
51
51
  return res;
@@ -53,24 +53,24 @@ export function rangeColorToRgb(engine, input, index, useIndex = true) {
53
53
  }
54
54
  return undefined;
55
55
  }
56
- export function colorToRgb(engine, input, index, useIndex = true) {
56
+ export function colorToRgb(pluginManager, input, index, useIndex = true) {
57
57
  if (!input) {
58
58
  return;
59
59
  }
60
60
  const color = isString(input) ? { value: input } : input;
61
61
  if (isString(color.value)) {
62
- return color.value === randomColorValue ? getRandomRgbColor() : stringToRgb(engine, color.value);
62
+ return color.value === randomColorValue ? getRandomRgbColor() : stringToRgb(pluginManager, color.value);
63
63
  }
64
64
  if (isArray(color.value)) {
65
65
  const value = itemFromArray(color.value, index, useIndex);
66
66
  if (!value) {
67
67
  return;
68
68
  }
69
- return colorToRgb(engine, {
69
+ return colorToRgb(pluginManager, {
70
70
  value,
71
71
  });
72
72
  }
73
- for (const manager of engine.colorManagers.values()) {
73
+ for (const manager of pluginManager.colorManagers.values()) {
74
74
  const res = manager.handleColor(color);
75
75
  if (res) {
76
76
  return res;
@@ -78,12 +78,12 @@ export function colorToRgb(engine, input, index, useIndex = true) {
78
78
  }
79
79
  return undefined;
80
80
  }
81
- export function colorToHsl(engine, color, index, useIndex = true) {
82
- const rgb = colorToRgb(engine, color, index, useIndex);
81
+ export function colorToHsl(pluginManager, color, index, useIndex = true) {
82
+ const rgb = colorToRgb(pluginManager, color, index, useIndex);
83
83
  return rgb ? rgbToHsl(rgb) : undefined;
84
84
  }
85
- export function rangeColorToHsl(engine, color, index, useIndex = true) {
86
- const rgb = rangeColorToRgb(engine, color, index, useIndex);
85
+ export function rangeColorToHsl(pluginManager, color, index, useIndex = true) {
86
+ const rgb = rangeColorToRgb(pluginManager, color, index, useIndex);
87
87
  return rgb ? rgbToHsl(rgb) : undefined;
88
88
  }
89
89
  export function rgbToHsl(color) {
@@ -115,11 +115,11 @@ export function rgbToHsl(color) {
115
115
  }
116
116
  return res;
117
117
  }
118
- export function stringToAlpha(engine, input) {
119
- return stringToRgba(engine, input)?.a;
118
+ export function stringToAlpha(pluginManager, input) {
119
+ return stringToRgba(pluginManager, input)?.a;
120
120
  }
121
- export function stringToRgb(engine, input) {
122
- return stringToRgba(engine, input);
121
+ export function stringToRgb(pluginManager, input) {
122
+ return stringToRgba(pluginManager, input);
123
123
  }
124
124
  export function hslToRgb(hsl) {
125
125
  const h = ((hsl.h % hMax) + hMax) % hMax, s = Math.max(sMin, Math.min(sMax, hsl.s)), l = Math.max(lMin, Math.min(lMax, hsl.l)), hNormalized = h / hMax, sNormalized = s / sMax, lNormalized = l / lMax;
@@ -223,11 +223,11 @@ export function getLinkColor(p1, p2, linkColor) {
223
223
  }
224
224
  return undefined;
225
225
  }
226
- export function getLinkRandomColor(engine, optColor, blink, consent) {
226
+ export function getLinkRandomColor(pluginManager, optColor, blink, consent) {
227
227
  const color = isString(optColor) ? optColor : optColor.value;
228
228
  if (color === randomColorValue) {
229
229
  if (consent) {
230
- return rangeColorToRgb(engine, {
230
+ return rangeColorToRgb(pluginManager, {
231
231
  value: color,
232
232
  });
233
233
  }
@@ -240,7 +240,7 @@ export function getLinkRandomColor(engine, optColor, blink, consent) {
240
240
  return midColorValue;
241
241
  }
242
242
  else {
243
- return rangeColorToRgb(engine, {
243
+ return rangeColorToRgb(pluginManager, {
244
244
  value: color,
245
245
  });
246
246
  }