@tsparticles/engine 4.0.0-beta.0 → 4.0.0-beta.1
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.
- package/155.min.js +1 -0
- package/README.md +0 -21
- package/browser/Core/CanvasManager.js +303 -0
- package/browser/Core/Container.js +61 -34
- package/browser/Core/Engine.js +26 -138
- package/browser/Core/Particle.js +29 -28
- package/{cjs/Core/Particles.js → browser/Core/ParticlesManager.js} +27 -57
- package/browser/Core/RenderManager.js +303 -0
- package/browser/Core/Retina.js +3 -8
- package/browser/Core/Utils/PluginManager.js +145 -0
- package/browser/Options/Classes/Options.js +7 -7
- package/browser/Options/Classes/Particles/ParticlesOptions.js +5 -5
- package/browser/Types/CanvasContextType.js +1 -0
- package/browser/Utils/CanvasUtils.js +1 -1
- package/browser/Utils/ColorUtils.js +21 -21
- package/browser/Utils/LogUtils.js +1 -0
- package/browser/Utils/OptionsUtils.js +2 -2
- package/cjs/Core/CanvasManager.js +303 -0
- package/cjs/Core/Container.js +61 -34
- package/cjs/Core/Engine.js +26 -138
- package/cjs/Core/Particle.js +29 -28
- package/{esm/Core/Particles.js → cjs/Core/ParticlesManager.js} +27 -57
- package/cjs/Core/RenderManager.js +303 -0
- package/cjs/Core/Retina.js +3 -8
- package/cjs/Core/Utils/PluginManager.js +145 -0
- package/cjs/Options/Classes/Options.js +7 -7
- package/cjs/Options/Classes/Particles/ParticlesOptions.js +5 -5
- package/cjs/Types/CanvasContextType.js +1 -0
- package/cjs/Utils/CanvasUtils.js +1 -1
- package/cjs/Utils/ColorUtils.js +21 -21
- package/cjs/Utils/LogUtils.js +1 -0
- package/cjs/Utils/OptionsUtils.js +2 -2
- package/dist_browser_Core_Container_js.js +24 -14
- package/esm/Core/CanvasManager.js +303 -0
- package/esm/Core/Container.js +61 -34
- package/esm/Core/Engine.js +26 -138
- package/esm/Core/Particle.js +29 -28
- package/{browser/Core/Particles.js → esm/Core/ParticlesManager.js} +27 -57
- package/esm/Core/RenderManager.js +303 -0
- package/esm/Core/Retina.js +3 -8
- package/esm/Core/Utils/PluginManager.js +145 -0
- package/esm/Options/Classes/Options.js +7 -7
- package/esm/Options/Classes/Particles/ParticlesOptions.js +5 -5
- package/esm/Types/CanvasContextType.js +1 -0
- package/esm/Utils/CanvasUtils.js +1 -1
- package/esm/Utils/ColorUtils.js +21 -21
- package/esm/Utils/LogUtils.js +1 -0
- package/esm/Utils/OptionsUtils.js +2 -2
- package/package.json +1 -1
- package/report.html +84 -29
- package/tsparticles.engine.js +18 -8
- package/tsparticles.engine.min.js +2 -2
- package/types/Core/CanvasManager.d.ts +39 -0
- package/types/Core/Container.d.ts +26 -7
- package/types/Core/Engine.d.ts +2 -54
- package/types/Core/Interfaces/IContainerPlugin.d.ts +8 -7
- package/types/Core/Interfaces/IDrawParticleParams.d.ts +2 -1
- package/types/Core/Interfaces/IParticleRetinaProps.d.ts +4 -4
- package/types/Core/Interfaces/IParticleUpdater.d.ts +2 -1
- package/types/Core/Interfaces/IShapeDrawData.d.ts +2 -1
- package/types/Core/Particle.d.ts +4 -4
- package/types/Core/{Particles.d.ts → ParticlesManager.d.ts} +4 -11
- package/types/Core/{Canvas.d.ts → RenderManager.d.ts} +12 -33
- package/types/Core/Retina.d.ts +0 -2
- package/types/Core/Utils/PluginManager.d.ts +62 -0
- package/types/Options/Classes/Options.d.ts +3 -3
- package/types/Options/Classes/Particles/ParticlesOptions.d.ts +3 -3
- package/types/Types/CanvasContextType.d.ts +1 -0
- package/types/Utils/CanvasUtils.d.ts +8 -5
- package/types/Utils/ColorUtils.d.ts +8 -8
- package/types/Utils/LogUtils.d.ts +1 -0
- package/types/Utils/OptionsUtils.d.ts +2 -2
- package/types/export-types.d.ts +4 -2
- package/umd/Core/CanvasManager.js +317 -0
- package/umd/Core/Container.js +61 -34
- package/umd/Core/Engine.js +25 -137
- package/umd/Core/Particle.js +29 -28
- package/umd/Core/{Particles.js → ParticlesManager.js} +29 -59
- package/umd/Core/RenderManager.js +317 -0
- package/umd/Core/Retina.js +3 -8
- package/umd/Core/Utils/PluginManager.js +159 -0
- package/umd/Options/Classes/Options.js +7 -7
- package/umd/Options/Classes/Particles/ParticlesOptions.js +5 -5
- package/umd/Types/CanvasContextType.js +12 -0
- package/umd/Utils/CanvasUtils.js +1 -1
- package/umd/Utils/ColorUtils.js +21 -21
- package/umd/Utils/LogUtils.js +1 -0
- package/umd/Utils/OptionsUtils.js +2 -2
- package/164.min.js +0 -1
- package/browser/Core/Canvas.js +0 -570
- package/cjs/Core/Canvas.js +0 -570
- package/esm/Core/Canvas.js +0 -570
- package/umd/Core/Canvas.js +0 -584
|
@@ -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
|
+
}
|
package/browser/Core/Retina.js
CHANGED
|
@@ -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
|
|
35
|
-
maxDistance.vertical = moveDistance.vertical
|
|
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
|
-
|
|
32
|
-
constructor(
|
|
33
|
-
this.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
28
|
-
constructor(
|
|
29
|
-
this.
|
|
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.
|
|
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.
|
|
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
|
|
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(
|
|
20
|
+
function stringToRgba(pluginManager, input) {
|
|
21
21
|
if (!input) {
|
|
22
22
|
return;
|
|
23
23
|
}
|
|
24
|
-
for (const manager of
|
|
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(
|
|
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(
|
|
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(
|
|
44
|
+
return rangeColorToRgb(pluginManager, {
|
|
45
45
|
value,
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
|
-
for (const manager of
|
|
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(
|
|
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(
|
|
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(
|
|
69
|
+
return colorToRgb(pluginManager, {
|
|
70
70
|
value,
|
|
71
71
|
});
|
|
72
72
|
}
|
|
73
|
-
for (const manager of
|
|
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(
|
|
82
|
-
const rgb = colorToRgb(
|
|
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(
|
|
86
|
-
const rgb = rangeColorToRgb(
|
|
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(
|
|
119
|
-
return stringToRgba(
|
|
118
|
+
export function stringToAlpha(pluginManager, input) {
|
|
119
|
+
return stringToRgba(pluginManager, input)?.a;
|
|
120
120
|
}
|
|
121
|
-
export function stringToRgb(
|
|
122
|
-
return stringToRgba(
|
|
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(
|
|
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(
|
|
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(
|
|
243
|
+
return rangeColorToRgb(pluginManager, {
|
|
244
244
|
value: color,
|
|
245
245
|
});
|
|
246
246
|
}
|