@tsparticles/engine 4.0.0-beta.0 → 4.0.0-beta.10

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 (242) hide show
  1. package/67.min.js +1 -0
  2. package/README.md +53 -965
  3. package/browser/Core/CanvasManager.js +303 -0
  4. package/browser/Core/Container.js +61 -34
  5. package/browser/Core/Engine.js +26 -138
  6. package/browser/Core/Particle.js +29 -28
  7. package/{cjs/Core/Particles.js → browser/Core/ParticlesManager.js} +27 -57
  8. package/browser/Core/RenderManager.js +303 -0
  9. package/browser/Core/Retina.js +3 -8
  10. package/browser/Core/Utils/PluginManager.js +145 -0
  11. package/browser/Options/Classes/Options.js +8 -21
  12. package/browser/Options/Classes/Particles/Paint.js +20 -0
  13. package/browser/Options/Classes/Particles/ParticlesOptions.js +65 -27
  14. package/browser/Options/Interfaces/Particles/IPaint.js +1 -0
  15. package/browser/Types/CanvasContextType.js +1 -0
  16. package/browser/Utils/CanvasUtils.js +4 -4
  17. package/browser/Utils/ColorUtils.js +21 -21
  18. package/browser/Utils/LogUtils.js +1 -0
  19. package/browser/Utils/OptionsUtils.js +2 -2
  20. package/browser/Utils/Utils.js +16 -10
  21. package/browser/exports.js +1 -0
  22. package/cjs/Core/CanvasManager.js +303 -0
  23. package/cjs/Core/Container.js +61 -34
  24. package/cjs/Core/Engine.js +26 -138
  25. package/cjs/Core/Particle.js +29 -28
  26. package/{esm/Core/Particles.js → cjs/Core/ParticlesManager.js} +27 -57
  27. package/cjs/Core/RenderManager.js +303 -0
  28. package/cjs/Core/Retina.js +3 -8
  29. package/cjs/Core/Utils/PluginManager.js +145 -0
  30. package/cjs/Options/Classes/Options.js +8 -21
  31. package/cjs/Options/Classes/Particles/Paint.js +20 -0
  32. package/cjs/Options/Classes/Particles/ParticlesOptions.js +65 -27
  33. package/cjs/Options/Interfaces/Particles/IPaint.js +1 -0
  34. package/cjs/Types/CanvasContextType.js +1 -0
  35. package/cjs/Utils/CanvasUtils.js +4 -4
  36. package/cjs/Utils/ColorUtils.js +21 -21
  37. package/cjs/Utils/LogUtils.js +1 -0
  38. package/cjs/Utils/OptionsUtils.js +2 -2
  39. package/cjs/Utils/Utils.js +16 -10
  40. package/cjs/exports.js +1 -0
  41. package/dist_browser_Core_Container_js.js +24 -14
  42. package/esm/Core/CanvasManager.js +303 -0
  43. package/esm/Core/Container.js +61 -34
  44. package/esm/Core/Engine.js +26 -138
  45. package/esm/Core/Particle.js +29 -28
  46. package/{browser/Core/Particles.js → esm/Core/ParticlesManager.js} +27 -57
  47. package/esm/Core/RenderManager.js +303 -0
  48. package/esm/Core/Retina.js +3 -8
  49. package/esm/Core/Utils/PluginManager.js +145 -0
  50. package/esm/Options/Classes/Options.js +8 -21
  51. package/esm/Options/Classes/Particles/Paint.js +20 -0
  52. package/esm/Options/Classes/Particles/ParticlesOptions.js +65 -27
  53. package/esm/Options/Interfaces/Particles/IPaint.js +1 -0
  54. package/esm/Types/CanvasContextType.js +1 -0
  55. package/esm/Utils/CanvasUtils.js +4 -4
  56. package/esm/Utils/ColorUtils.js +21 -21
  57. package/esm/Utils/LogUtils.js +1 -0
  58. package/esm/Utils/OptionsUtils.js +2 -2
  59. package/esm/Utils/Utils.js +16 -10
  60. package/esm/exports.js +1 -0
  61. package/package.json +2 -3
  62. package/report.html +84 -29
  63. package/tsparticles.engine.js +31 -11
  64. package/tsparticles.engine.min.js +2 -2
  65. package/types/Core/CanvasManager.d.ts +39 -0
  66. package/types/Core/Container.d.ts +26 -7
  67. package/types/Core/Engine.d.ts +2 -54
  68. package/types/Core/Interfaces/IContainerPlugin.d.ts +8 -7
  69. package/types/Core/Interfaces/IDrawParticleParams.d.ts +2 -1
  70. package/types/Core/Interfaces/IPalette.d.ts +15 -2
  71. package/types/Core/Interfaces/IParticleRetinaProps.d.ts +4 -4
  72. package/types/Core/Interfaces/IParticleUpdater.d.ts +2 -1
  73. package/types/Core/Interfaces/IShapeDrawData.d.ts +2 -1
  74. package/types/Core/Particle.d.ts +4 -4
  75. package/types/Core/{Particles.d.ts → ParticlesManager.d.ts} +4 -11
  76. package/types/Core/{Canvas.d.ts → RenderManager.d.ts} +12 -33
  77. package/types/Core/Retina.d.ts +0 -2
  78. package/types/Core/Utils/PluginManager.d.ts +62 -0
  79. package/types/Options/Classes/Options.d.ts +3 -3
  80. package/types/Options/Classes/Particles/Paint.d.ts +10 -0
  81. package/types/Options/Classes/Particles/ParticlesOptions.d.ts +7 -7
  82. package/types/Options/Interfaces/Particles/IPaint.d.ts +6 -0
  83. package/types/Options/Interfaces/Particles/IParticlesOptions.d.ts +3 -4
  84. package/types/Types/CanvasContextType.d.ts +1 -0
  85. package/types/Utils/CanvasUtils.d.ts +8 -5
  86. package/types/Utils/ColorUtils.d.ts +8 -8
  87. package/types/Utils/LogUtils.d.ts +1 -0
  88. package/types/Utils/OptionsUtils.d.ts +2 -2
  89. package/types/export-types.d.ts +5 -2
  90. package/types/exports.d.ts +1 -0
  91. package/164.min.js +0 -1
  92. package/browser/Core/Canvas.js +0 -570
  93. package/cjs/Core/Canvas.js +0 -570
  94. package/esm/Core/Canvas.js +0 -570
  95. package/umd/Core/Canvas.js +0 -584
  96. package/umd/Core/Container.js +0 -356
  97. package/umd/Core/Engine.js +0 -295
  98. package/umd/Core/Interfaces/Colors.js +0 -12
  99. package/umd/Core/Interfaces/IBounds.js +0 -12
  100. package/umd/Core/Interfaces/IBubbleParticleData.js +0 -12
  101. package/umd/Core/Interfaces/ICircleBouncer.js +0 -12
  102. package/umd/Core/Interfaces/IColorManager.js +0 -12
  103. package/umd/Core/Interfaces/IContainerPlugin.js +0 -12
  104. package/umd/Core/Interfaces/ICoordinates.js +0 -12
  105. package/umd/Core/Interfaces/IDelta.js +0 -12
  106. package/umd/Core/Interfaces/IDimension.js +0 -12
  107. package/umd/Core/Interfaces/IDistance.js +0 -12
  108. package/umd/Core/Interfaces/IDrawParticleParams.js +0 -12
  109. package/umd/Core/Interfaces/IEffectDrawer.js +0 -12
  110. package/umd/Core/Interfaces/ILoadParams.js +0 -12
  111. package/umd/Core/Interfaces/IPalette.js +0 -12
  112. package/umd/Core/Interfaces/IParticleColorStyle.js +0 -12
  113. package/umd/Core/Interfaces/IParticleHslAnimation.js +0 -12
  114. package/umd/Core/Interfaces/IParticleLife.js +0 -12
  115. package/umd/Core/Interfaces/IParticleOpacityData.js +0 -12
  116. package/umd/Core/Interfaces/IParticleRetinaProps.js +0 -12
  117. package/umd/Core/Interfaces/IParticleRoll.js +0 -12
  118. package/umd/Core/Interfaces/IParticleRotateData.js +0 -12
  119. package/umd/Core/Interfaces/IParticleTransformValues.js +0 -12
  120. package/umd/Core/Interfaces/IParticleUpdater.js +0 -12
  121. package/umd/Core/Interfaces/IParticleValueAnimation.js +0 -12
  122. package/umd/Core/Interfaces/IPlugin.js +0 -12
  123. package/umd/Core/Interfaces/IPositionFromSizeParams.js +0 -12
  124. package/umd/Core/Interfaces/IRangeValue.js +0 -12
  125. package/umd/Core/Interfaces/IShapeDrawData.js +0 -12
  126. package/umd/Core/Interfaces/IShapeDrawer.js +0 -12
  127. package/umd/Core/Interfaces/IShapeValues.js +0 -12
  128. package/umd/Core/Interfaces/ISlowParticleData.js +0 -12
  129. package/umd/Core/Particle.js +0 -432
  130. package/umd/Core/Particles.js +0 -407
  131. package/umd/Core/Retina.js +0 -52
  132. package/umd/Core/Utils/Constants.js +0 -23
  133. package/umd/Core/Utils/EventListeners.js +0 -102
  134. package/umd/Core/Utils/Ranges.js +0 -91
  135. package/umd/Core/Utils/SpatialHashGrid.js +0 -116
  136. package/umd/Core/Utils/Vectors.js +0 -126
  137. package/umd/Enums/AnimationStatus.js +0 -18
  138. package/umd/Enums/Directions/MoveDirection.js +0 -27
  139. package/umd/Enums/Directions/OutModeDirection.js +0 -20
  140. package/umd/Enums/Directions/RotateDirection.js +0 -19
  141. package/umd/Enums/Modes/AnimationMode.js +0 -20
  142. package/umd/Enums/Modes/LimitMode.js +0 -18
  143. package/umd/Enums/Modes/OutMode.js +0 -21
  144. package/umd/Enums/Modes/PixelMode.js +0 -18
  145. package/umd/Enums/RangeType.js +0 -18
  146. package/umd/Enums/Types/AlterType.js +0 -18
  147. package/umd/Enums/Types/DestroyType.js +0 -19
  148. package/umd/Enums/Types/EasingType.js +0 -58
  149. package/umd/Enums/Types/EventType.js +0 -28
  150. package/umd/Enums/Types/GradientType.js +0 -19
  151. package/umd/Enums/Types/ParticleOutType.js +0 -19
  152. package/umd/Enums/Types/StartValueType.js +0 -19
  153. package/umd/Options/Classes/AnimatableColor.js +0 -52
  154. package/umd/Options/Classes/AnimationOptions.js +0 -79
  155. package/umd/Options/Classes/Background/Background.js +0 -56
  156. package/umd/Options/Classes/ColorAnimation.js +0 -44
  157. package/umd/Options/Classes/FullScreen/FullScreen.js +0 -34
  158. package/umd/Options/Classes/HslAnimation.js +0 -30
  159. package/umd/Options/Classes/Options.js +0 -171
  160. package/umd/Options/Classes/OptionsColor.js +0 -42
  161. package/umd/Options/Classes/Particles/Bounce/ParticlesBounce.js +0 -31
  162. package/umd/Options/Classes/Particles/Bounce/ParticlesBounceFactor.js +0 -21
  163. package/umd/Options/Classes/Particles/Effect/Effect.js +0 -46
  164. package/umd/Options/Classes/Particles/Fill.js +0 -42
  165. package/umd/Options/Classes/Particles/Move/Move.js +0 -120
  166. package/umd/Options/Classes/Particles/Move/MoveAngle.js +0 -35
  167. package/umd/Options/Classes/Particles/Move/MoveCenter.js +0 -45
  168. package/umd/Options/Classes/Particles/Move/MoveGravity.js +0 -45
  169. package/umd/Options/Classes/Particles/Move/OutModes.js +0 -38
  170. package/umd/Options/Classes/Particles/Move/Path/MovePath.js +0 -46
  171. package/umd/Options/Classes/Particles/Move/Spin.js +0 -40
  172. package/umd/Options/Classes/Particles/Number/ParticlesDensity.js +0 -41
  173. package/umd/Options/Classes/Particles/Number/ParticlesNumber.js +0 -37
  174. package/umd/Options/Classes/Particles/Number/ParticlesNumberLimit.js +0 -35
  175. package/umd/Options/Classes/Particles/Opacity/Opacity.js +0 -35
  176. package/umd/Options/Classes/Particles/Opacity/OpacityAnimation.js +0 -34
  177. package/umd/Options/Classes/Particles/ParticlesOptions.js +0 -116
  178. package/umd/Options/Classes/Particles/Shape/Shape.js +0 -46
  179. package/umd/Options/Classes/Particles/Size/Size.js +0 -35
  180. package/umd/Options/Classes/Particles/Size/SizeAnimation.js +0 -34
  181. package/umd/Options/Classes/Particles/Stroke.js +0 -39
  182. package/umd/Options/Classes/Particles/ZIndex/ZIndex.js +0 -42
  183. package/umd/Options/Classes/ResizeEvent.js +0 -34
  184. package/umd/Options/Classes/ValueWithRandom.js +0 -56
  185. package/umd/Options/Interfaces/Background/IBackground.js +0 -12
  186. package/umd/Options/Interfaces/FullScreen/IFullScreen.js +0 -12
  187. package/umd/Options/Interfaces/IAnimatable.js +0 -12
  188. package/umd/Options/Interfaces/IAnimatableColor.js +0 -12
  189. package/umd/Options/Interfaces/IAnimation.js +0 -12
  190. package/umd/Options/Interfaces/IColorAnimation.js +0 -12
  191. package/umd/Options/Interfaces/IHslAnimation.js +0 -12
  192. package/umd/Options/Interfaces/IOptionLoader.js +0 -12
  193. package/umd/Options/Interfaces/IOptions.js +0 -12
  194. package/umd/Options/Interfaces/IOptionsColor.js +0 -12
  195. package/umd/Options/Interfaces/IResizeEvent.js +0 -12
  196. package/umd/Options/Interfaces/IValueWithRandom.js +0 -12
  197. package/umd/Options/Interfaces/Particles/Bounce/IParticlesBounce.js +0 -12
  198. package/umd/Options/Interfaces/Particles/Effect/IEffect.js +0 -12
  199. package/umd/Options/Interfaces/Particles/IFill.js +0 -12
  200. package/umd/Options/Interfaces/Particles/IParticlesOptions.js +0 -12
  201. package/umd/Options/Interfaces/Particles/IStroke.js +0 -12
  202. package/umd/Options/Interfaces/Particles/Move/IMove.js +0 -12
  203. package/umd/Options/Interfaces/Particles/Move/IMoveAngle.js +0 -12
  204. package/umd/Options/Interfaces/Particles/Move/IMoveCenter.js +0 -12
  205. package/umd/Options/Interfaces/Particles/Move/IMoveGravity.js +0 -12
  206. package/umd/Options/Interfaces/Particles/Move/IOutModes.js +0 -12
  207. package/umd/Options/Interfaces/Particles/Move/ISpin.js +0 -12
  208. package/umd/Options/Interfaces/Particles/Move/Path/IMovePath.js +0 -12
  209. package/umd/Options/Interfaces/Particles/Number/IParticlesDensity.js +0 -12
  210. package/umd/Options/Interfaces/Particles/Number/IParticlesNumber.js +0 -12
  211. package/umd/Options/Interfaces/Particles/Number/IParticlesNumberLimit.js +0 -12
  212. package/umd/Options/Interfaces/Particles/Opacity/IOpacity.js +0 -12
  213. package/umd/Options/Interfaces/Particles/Opacity/IOpacityAnimation.js +0 -12
  214. package/umd/Options/Interfaces/Particles/Shape/IShape.js +0 -12
  215. package/umd/Options/Interfaces/Particles/Size/ISize.js +0 -12
  216. package/umd/Options/Interfaces/Particles/Size/ISizeAnimation.js +0 -12
  217. package/umd/Options/Interfaces/Particles/ZIndex/IZIndex.js +0 -12
  218. package/umd/Types/CustomEventArgs.js +0 -12
  219. package/umd/Types/CustomEventListener.js +0 -12
  220. package/umd/Types/EasingFunction.js +0 -12
  221. package/umd/Types/EngineInitializers.js +0 -12
  222. package/umd/Types/ExportResult.js +0 -12
  223. package/umd/Types/ISourceOptions.js +0 -12
  224. package/umd/Types/ParticlesGroups.js +0 -12
  225. package/umd/Types/PathOptions.js +0 -12
  226. package/umd/Types/RangeValue.js +0 -12
  227. package/umd/Types/RecursivePartial.js +0 -12
  228. package/umd/Types/ShapeData.js +0 -12
  229. package/umd/Types/SingleOrMultiple.js +0 -12
  230. package/umd/Utils/CanvasUtils.js +0 -145
  231. package/umd/Utils/ColorUtils.js +0 -395
  232. package/umd/Utils/EventDispatcher.js +0 -63
  233. package/umd/Utils/LogUtils.js +0 -36
  234. package/umd/Utils/MathUtils.js +0 -203
  235. package/umd/Utils/OptionsUtils.js +0 -25
  236. package/umd/Utils/TypeUtils.js +0 -40
  237. package/umd/Utils/Utils.js +0 -484
  238. package/umd/bundle.js +0 -32
  239. package/umd/export-types.js +0 -12
  240. package/umd/exports.js +0 -82
  241. package/umd/index.js +0 -32
  242. 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
  }
@@ -134,24 +134,11 @@ export class Options {
134
134
  mode: paletteData.blendMode,
135
135
  },
136
136
  particles: {
137
- fill: {
138
- color: paletteData.fill
139
- ? {
140
- value: paletteData.colors,
141
- }
142
- : undefined,
143
- enable: paletteData.fill,
144
- },
145
- stroke: !paletteData.fill
146
- ? paletteData.colors.map(color => ({
147
- color: { value: color },
148
- width: 1,
149
- }))
150
- : undefined,
137
+ palette,
151
138
  },
152
139
  });
153
140
  };
154
141
  _importPreset = preset => {
155
- this.load(this._engine.getPreset(preset));
142
+ this.load(this._pluginManager.getPreset(preset));
156
143
  };
157
144
  }
@@ -0,0 +1,20 @@
1
+ import { Fill } from "./Fill.js";
2
+ import { Stroke } from "./Stroke.js";
3
+ import { isNull } from "../../../Utils/TypeUtils.js";
4
+ export class Paint {
5
+ fill;
6
+ stroke;
7
+ load(data) {
8
+ if (isNull(data)) {
9
+ return;
10
+ }
11
+ if (data.fill !== undefined) {
12
+ this.fill ??= new Fill();
13
+ this.fill.load(data.fill);
14
+ }
15
+ if (data.stroke !== undefined) {
16
+ this.stroke ??= new Stroke();
17
+ this.stroke.load(data.stroke);
18
+ }
19
+ }
20
+ }