@tsparticles/updater-gradient 3.0.0-alpha.1 → 3.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +16 -12
  2. package/browser/GradientUpdater.js +34 -132
  3. package/browser/Options/Classes/AnimatableGradientColor.js +2 -2
  4. package/browser/Options/Classes/GradientAngle.js +2 -1
  5. package/browser/Options/Classes/GradientAngleAnimation.js +5 -1
  6. package/browser/Options/Classes/GradientColorOpacity.js +1 -1
  7. package/browser/Options/Classes/GradientColorOpacityAnimation.js +8 -1
  8. package/browser/Types.js +1 -0
  9. package/browser/Utils.js +107 -0
  10. package/browser/index.js +2 -2
  11. package/cjs/GradientUpdater.js +35 -133
  12. package/cjs/Options/Classes/AnimatableGradientColor.js +1 -1
  13. package/cjs/Options/Classes/GradientAngle.js +2 -1
  14. package/cjs/Options/Classes/GradientAngleAnimation.js +4 -0
  15. package/cjs/Options/Classes/GradientColorOpacity.js +1 -1
  16. package/cjs/Options/Classes/GradientColorOpacityAnimation.js +7 -0
  17. package/cjs/Types.js +2 -0
  18. package/cjs/Utils.js +111 -0
  19. package/cjs/index.js +2 -2
  20. package/esm/GradientUpdater.js +34 -132
  21. package/esm/Options/Classes/AnimatableGradientColor.js +2 -2
  22. package/esm/Options/Classes/GradientAngle.js +2 -1
  23. package/esm/Options/Classes/GradientAngleAnimation.js +5 -1
  24. package/esm/Options/Classes/GradientColorOpacity.js +1 -1
  25. package/esm/Options/Classes/GradientColorOpacityAnimation.js +8 -1
  26. package/esm/Types.js +1 -0
  27. package/esm/Utils.js +107 -0
  28. package/esm/index.js +2 -2
  29. package/package.json +10 -5
  30. package/report.html +4 -4
  31. package/tsparticles.updater.gradient.js +87 -56
  32. package/tsparticles.updater.gradient.min.js +1 -1
  33. package/tsparticles.updater.gradient.min.js.LICENSE.txt +1 -8
  34. package/types/GradientUpdater.d.ts +2 -25
  35. package/types/Options/Classes/AnimatableGradient.d.ts +1 -2
  36. package/types/Options/Classes/AnimatableGradientColor.d.ts +1 -2
  37. package/types/Options/Classes/GradientAngle.d.ts +2 -3
  38. package/types/Options/Classes/GradientAngleAnimation.d.ts +2 -1
  39. package/types/Options/Classes/GradientColorOpacity.d.ts +1 -1
  40. package/types/Options/Classes/GradientColorOpacityAnimation.d.ts +2 -2
  41. package/types/Options/Interfaces/Gradients.d.ts +1 -1
  42. package/types/Types.d.ts +23 -0
  43. package/types/Utils.d.ts +3 -0
  44. package/types/index.d.ts +1 -1
  45. package/umd/GradientUpdater.js +36 -134
  46. package/umd/Options/Classes/AnimatableGradientColor.js +1 -1
  47. package/umd/Options/Classes/GradientAngle.js +3 -2
  48. package/umd/Options/Classes/GradientAngleAnimation.js +4 -0
  49. package/umd/Options/Classes/GradientColorOpacity.js +2 -2
  50. package/umd/Options/Classes/GradientColorOpacityAnimation.js +7 -0
  51. package/umd/Types.js +12 -0
  52. package/umd/Utils.js +121 -0
  53. package/umd/index.js +2 -2
@@ -3,112 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GradientUpdater = void 0;
4
4
  const engine_1 = require("@tsparticles/engine");
5
5
  const AnimatableGradient_1 = require("./Options/Classes/AnimatableGradient");
6
- function updateColorOpacity(delta, value) {
7
- var _a, _b, _c;
8
- if (!value.enable) {
9
- return;
10
- }
11
- const decay = (_a = value.decay) !== null && _a !== void 0 ? _a : 1;
12
- switch (value.status) {
13
- case "increasing":
14
- if (value.value >= value.max) {
15
- value.status = "decreasing";
16
- }
17
- else {
18
- value.value += ((_b = value.velocity) !== null && _b !== void 0 ? _b : 0) * delta.factor;
19
- }
20
- break;
21
- case "decreasing":
22
- if (value.value <= value.min) {
23
- value.status = "increasing";
24
- }
25
- else {
26
- value.value -= ((_c = value.velocity) !== null && _c !== void 0 ? _c : 0) * delta.factor;
27
- }
28
- break;
29
- }
30
- if (value.velocity && decay !== 1) {
31
- value.velocity *= decay;
32
- }
33
- }
34
- function updateColorValue(delta, value, max, decrease) {
35
- var _a, _b;
36
- const colorValue = value;
37
- if (!colorValue || !colorValue.enable) {
38
- return;
39
- }
40
- const velocity = ((_a = value.velocity) !== null && _a !== void 0 ? _a : 0) * delta.factor, decay = (_b = value.decay) !== null && _b !== void 0 ? _b : 1;
41
- if (!decrease || colorValue.status === "increasing") {
42
- colorValue.value += velocity;
43
- if (decrease && colorValue.value > max) {
44
- colorValue.status = "decreasing";
45
- colorValue.value -= colorValue.value % max;
46
- }
47
- }
48
- else {
49
- colorValue.value -= velocity;
50
- if (colorValue.value < 0) {
51
- colorValue.status = "increasing";
52
- colorValue.value += colorValue.value;
53
- }
54
- }
55
- if (colorValue.value > max) {
56
- colorValue.value %= max;
57
- }
58
- if (value.velocity && decay !== 1) {
59
- value.velocity *= decay;
60
- }
61
- }
62
- function updateAngle(delta, angle) {
63
- var _a, _b;
64
- const speed = ((_a = angle.velocity) !== null && _a !== void 0 ? _a : 0) * delta.factor, max = 2 * Math.PI, decay = (_b = angle.decay) !== null && _b !== void 0 ? _b : 1;
65
- if (!angle.enable) {
66
- return;
67
- }
68
- switch (angle.status) {
69
- case "increasing":
70
- angle.value += speed;
71
- if (angle.value > max) {
72
- angle.value -= max;
73
- }
74
- break;
75
- case "decreasing":
76
- default:
77
- angle.value -= speed;
78
- if (angle.value < 0) {
79
- angle.value += max;
80
- }
81
- break;
82
- }
83
- if (angle.velocity && decay !== 1) {
84
- angle.velocity *= decay;
85
- }
86
- }
87
- function updateGradient(particle, delta) {
88
- var _a, _b, _c;
89
- const gradient = particle.gradient;
90
- if (!gradient) {
91
- return;
92
- }
93
- updateAngle(delta, gradient.angle);
94
- for (const color of gradient.colors) {
95
- if (((_a = particle.color) === null || _a === void 0 ? void 0 : _a.h) !== undefined) {
96
- updateColorValue(delta, color.value.h, 360, false);
97
- }
98
- if (((_b = particle.color) === null || _b === void 0 ? void 0 : _b.s) !== undefined) {
99
- updateColorValue(delta, color.value.s, 100, true);
100
- }
101
- if (((_c = particle.color) === null || _c === void 0 ? void 0 : _c.l) !== undefined) {
102
- updateColorValue(delta, color.value.l, 100, true);
103
- }
104
- if (color.opacity) {
105
- updateColorOpacity(delta, color.opacity);
106
- }
107
- }
108
- }
6
+ const Utils_1 = require("./Utils");
109
7
  class GradientUpdater {
110
8
  getColorStyles(particle, context, radius, opacity) {
111
- var _a, _b;
112
9
  const gradient = particle.gradient;
113
10
  if (!gradient) {
114
11
  return {};
@@ -116,12 +13,12 @@ class GradientUpdater {
116
13
  const gradientAngle = gradient.angle.value, fillGradient = gradient.type === "radial"
117
14
  ? context.createRadialGradient(0, 0, 0, 0, 0, radius)
118
15
  : context.createLinearGradient(Math.cos(gradientAngle) * -radius, Math.sin(gradientAngle) * -radius, Math.cos(gradientAngle) * radius, Math.sin(gradientAngle) * radius);
119
- for (const color of gradient.colors) {
120
- fillGradient.addColorStop(color.stop, (0, engine_1.getStyleFromHsl)({
121
- h: color.value.h.value,
122
- s: color.value.s.value,
123
- l: color.value.l.value,
124
- }, (_b = (_a = color.opacity) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : opacity));
16
+ for (const { stop, value, opacity: cOpacity } of gradient.colors) {
17
+ fillGradient.addColorStop(stop, (0, engine_1.getStyleFromHsl)({
18
+ h: value.h.value,
19
+ s: value.s.value,
20
+ l: value.l.value,
21
+ }, cOpacity?.value ?? opacity));
125
22
  }
126
23
  return { fill: fillGradient };
127
24
  }
@@ -130,20 +27,22 @@ class GradientUpdater {
130
27
  if (!gradient) {
131
28
  return;
132
29
  }
30
+ const { angle } = gradient;
133
31
  particle.gradient = {
134
32
  angle: {
135
- value: gradient.angle.value,
136
- enable: gradient.angle.animation.enable,
137
- velocity: ((0, engine_1.getRangeValue)(gradient.angle.animation.speed) / 360) * particle.container.retina.reduceFactor,
138
- decay: 1 - (0, engine_1.getRangeValue)(gradient.angle.animation.decay),
33
+ value: (0, engine_1.getRangeValue)(angle.value),
34
+ enable: angle.animation.enable,
35
+ velocity: ((0, engine_1.getRangeValue)(angle.animation.speed) / 360) * particle.container.retina.reduceFactor,
36
+ decay: 1 - (0, engine_1.getRangeValue)(angle.animation.decay),
37
+ delayTime: (0, engine_1.getRangeValue)(angle.animation.delay) * 1000,
38
+ time: 0,
139
39
  },
140
40
  type: gradient.type,
141
41
  colors: [],
142
42
  };
143
43
  let rotateDirection = gradient.angle.direction;
144
44
  if (rotateDirection === "random") {
145
- const index = Math.floor((0, engine_1.getRandom)() * 2);
146
- rotateDirection = index > 0 ? "counter-clockwise" : "clockwise";
45
+ rotateDirection = (0, engine_1.getRandom)() > 0.5 ? "counter-clockwise" : "clockwise";
147
46
  }
148
47
  switch (rotateDirection) {
149
48
  case "counter-clockwise":
@@ -173,28 +72,31 @@ class GradientUpdater {
173
72
  velocity: ((0, engine_1.getRangeValue)(grColor.opacity.animation.speed) / 100) *
174
73
  particle.container.retina.reduceFactor,
175
74
  decay: 1 - (0, engine_1.getRangeValue)(grColor.opacity.animation.decay),
75
+ delayTime: (0, engine_1.getRangeValue)(grColor.opacity.animation.delay) * 1000,
76
+ time: 0,
176
77
  }
177
78
  : undefined,
178
79
  };
179
- if (grColor.opacity && addColor.opacity) {
80
+ const { opacity: addOpacity } = addColor;
81
+ if (grColor.opacity && addOpacity) {
180
82
  const opacityRange = grColor.opacity.value;
181
- addColor.opacity.min = (0, engine_1.getRangeMin)(opacityRange);
182
- addColor.opacity.max = (0, engine_1.getRangeMax)(opacityRange);
83
+ addOpacity.min = (0, engine_1.getRangeMin)(opacityRange);
84
+ addOpacity.max = (0, engine_1.getRangeMax)(opacityRange);
183
85
  const opacityAnimation = grColor.opacity.animation;
184
86
  switch (opacityAnimation.startValue) {
185
87
  case "min":
186
- addColor.opacity.value = addColor.opacity.min;
187
- addColor.opacity.status = "increasing";
188
- break;
189
- case "random":
190
- addColor.opacity.value = (0, engine_1.randomInRange)(addColor.opacity);
191
- addColor.opacity.status =
192
- (0, engine_1.getRandom)() >= 0.5 ? "increasing" : "decreasing";
88
+ addOpacity.value = addOpacity.min;
89
+ addOpacity.status = "increasing";
193
90
  break;
194
91
  case "max":
92
+ addOpacity.value = addOpacity.max;
93
+ addOpacity.status = "decreasing";
94
+ break;
95
+ case "random":
195
96
  default:
196
- addColor.opacity.value = addColor.opacity.max;
197
- addColor.opacity.status = "decreasing";
97
+ addOpacity.value = (0, engine_1.randomInRange)(addOpacity);
98
+ addOpacity.status =
99
+ (0, engine_1.getRandom)() >= 0.5 ? "increasing" : "decreasing";
198
100
  break;
199
101
  }
200
102
  }
@@ -202,15 +104,15 @@ class GradientUpdater {
202
104
  }
203
105
  }
204
106
  isEnabled(particle) {
205
- var _a, _b, _c;
206
107
  return (!particle.destroyed &&
207
108
  !particle.spawning &&
208
- (((_a = particle.gradient) === null || _a === void 0 ? void 0 : _a.angle.enable) ||
209
- ((_c = (_b = particle.gradient) === null || _b === void 0 ? void 0 : _b.colors.some((c) => c.value.h.enable || c.value.s.enable || c.value.l.enable)) !== null && _c !== void 0 ? _c : false)));
109
+ (particle.gradient?.angle.enable ||
110
+ (particle.gradient?.colors.some((c) => c.value.h.enable || c.value.s.enable || c.value.l.enable) ??
111
+ false)));
210
112
  }
211
113
  loadOptions(options, ...sources) {
212
114
  for (const source of sources) {
213
- if (!(source === null || source === void 0 ? void 0 : source.gradient)) {
115
+ if (!source?.gradient) {
214
116
  continue;
215
117
  }
216
118
  const gradientToLoad = source.gradient;
@@ -225,7 +127,7 @@ class GradientUpdater {
225
127
  }
226
128
  }
227
129
  update(particle, delta) {
228
- updateGradient(particle, delta);
130
+ (0, Utils_1.updateGradient)(particle, delta);
229
131
  }
230
132
  }
231
133
  exports.GradientUpdater = GradientUpdater;
@@ -18,7 +18,7 @@ class AnimatableGradientColor {
18
18
  this.value = engine_1.AnimatableColor.create(this.value, data.value);
19
19
  if (data.opacity !== undefined) {
20
20
  this.opacity = new GradientColorOpacity_1.GradientColorOpacity();
21
- if (typeof data.opacity === "number") {
21
+ if ((0, engine_1.isNumber)(data.opacity)) {
22
22
  this.opacity.value = data.opacity;
23
23
  }
24
24
  else {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GradientAngle = void 0;
4
+ const engine_1 = require("@tsparticles/engine");
4
5
  const GradientAngleAnimation_1 = require("./GradientAngleAnimation");
5
6
  class GradientAngle {
6
7
  constructor() {
@@ -14,7 +15,7 @@ class GradientAngle {
14
15
  }
15
16
  this.animation.load(data.animation);
16
17
  if (data.value !== undefined) {
17
- this.value = data.value;
18
+ this.value = (0, engine_1.setRangeValue)(data.value);
18
19
  }
19
20
  if (data.direction !== undefined) {
20
21
  this.direction = data.direction;
@@ -8,6 +8,7 @@ class GradientAngleAnimation {
8
8
  this.enable = false;
9
9
  this.speed = 0;
10
10
  this.decay = 0;
11
+ this.delay = 0;
11
12
  this.sync = false;
12
13
  }
13
14
  load(data) {
@@ -26,6 +27,9 @@ class GradientAngleAnimation {
26
27
  if (data.decay !== undefined) {
27
28
  this.decay = (0, engine_1.setRangeValue)(data.decay);
28
29
  }
30
+ if (data.delay !== undefined) {
31
+ this.delay = (0, engine_1.setRangeValue)(data.delay);
32
+ }
29
33
  if (data.sync !== undefined) {
30
34
  this.sync = data.sync;
31
35
  }
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GradientColorOpacity = void 0;
4
- const GradientColorOpacityAnimation_1 = require("./GradientColorOpacityAnimation");
5
4
  const engine_1 = require("@tsparticles/engine");
5
+ const GradientColorOpacityAnimation_1 = require("./GradientColorOpacityAnimation");
6
6
  class GradientColorOpacity {
7
7
  constructor() {
8
8
  this.value = 0;
@@ -8,6 +8,7 @@ class GradientColorOpacityAnimation {
8
8
  this.enable = false;
9
9
  this.speed = 0;
10
10
  this.decay = 0;
11
+ this.delay = 0;
11
12
  this.sync = false;
12
13
  this.startValue = "random";
13
14
  }
@@ -30,6 +31,12 @@ class GradientColorOpacityAnimation {
30
31
  if (data.startValue !== undefined) {
31
32
  this.startValue = data.startValue;
32
33
  }
34
+ if (data.decay !== undefined) {
35
+ this.decay = (0, engine_1.setRangeValue)(data.decay);
36
+ }
37
+ if (data.delay !== undefined) {
38
+ this.delay = (0, engine_1.setRangeValue)(data.delay);
39
+ }
33
40
  }
34
41
  }
35
42
  exports.GradientColorOpacityAnimation = GradientColorOpacityAnimation;
package/cjs/Types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/cjs/Utils.js ADDED
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.updateGradient = void 0;
4
+ function updateColorOpacity(delta, value) {
5
+ if (!value.enable) {
6
+ return;
7
+ }
8
+ const decay = value.decay ?? 1;
9
+ switch (value.status) {
10
+ case "increasing":
11
+ if (value.value >= value.max) {
12
+ value.status = "decreasing";
13
+ }
14
+ else {
15
+ value.value += (value.velocity ?? 0) * delta.factor;
16
+ }
17
+ break;
18
+ case "decreasing":
19
+ if (value.value <= value.min) {
20
+ value.status = "increasing";
21
+ }
22
+ else {
23
+ value.value -= (value.velocity ?? 0) * delta.factor;
24
+ }
25
+ break;
26
+ }
27
+ if (value.velocity && decay !== 1) {
28
+ value.velocity *= decay;
29
+ }
30
+ }
31
+ function updateColorValue(delta, colorValue, max, decrease) {
32
+ if (!colorValue || !colorValue.enable) {
33
+ return;
34
+ }
35
+ if (!colorValue.time) {
36
+ colorValue.time = 0;
37
+ }
38
+ if ((colorValue.delayTime ?? 0) > 0 && colorValue.time < (colorValue.delayTime ?? 0)) {
39
+ colorValue.time += delta.value;
40
+ }
41
+ if ((colorValue.delayTime ?? 0) > 0 && colorValue.time < (colorValue.delayTime ?? 0)) {
42
+ return;
43
+ }
44
+ const velocity = (colorValue.velocity ?? 0) * delta.factor, decay = colorValue.decay ?? 1;
45
+ if (!decrease || colorValue.status === "increasing") {
46
+ colorValue.value += velocity;
47
+ if (decrease && colorValue.value > max) {
48
+ colorValue.status = "decreasing";
49
+ colorValue.value -= colorValue.value % max;
50
+ }
51
+ }
52
+ else {
53
+ colorValue.value -= velocity;
54
+ if (colorValue.value < 0) {
55
+ colorValue.status = "increasing";
56
+ colorValue.value += colorValue.value;
57
+ }
58
+ }
59
+ if (colorValue.value > max) {
60
+ colorValue.value %= max;
61
+ }
62
+ if (colorValue.velocity && decay !== 1) {
63
+ colorValue.velocity *= decay;
64
+ }
65
+ }
66
+ function updateAngle(delta, angle) {
67
+ const speed = (angle.velocity ?? 0) * delta.factor, max = 2 * Math.PI, decay = angle.decay ?? 1;
68
+ if (!angle.enable) {
69
+ return;
70
+ }
71
+ switch (angle.status) {
72
+ case "increasing":
73
+ angle.value += speed;
74
+ if (angle.value > max) {
75
+ angle.value -= max;
76
+ }
77
+ break;
78
+ case "decreasing":
79
+ default:
80
+ angle.value -= speed;
81
+ if (angle.value < 0) {
82
+ angle.value += max;
83
+ }
84
+ break;
85
+ }
86
+ if (angle.velocity && decay !== 1) {
87
+ angle.velocity *= decay;
88
+ }
89
+ }
90
+ function updateGradient(particle, delta) {
91
+ const { gradient } = particle;
92
+ if (!gradient) {
93
+ return;
94
+ }
95
+ updateAngle(delta, gradient.angle);
96
+ for (const color of gradient.colors) {
97
+ if (particle.color?.h !== undefined) {
98
+ updateColorValue(delta, color.value.h, 360, false);
99
+ }
100
+ if (particle.color?.s !== undefined) {
101
+ updateColorValue(delta, color.value.s, 100, true);
102
+ }
103
+ if (particle.color?.l !== undefined) {
104
+ updateColorValue(delta, color.value.l, 100, true);
105
+ }
106
+ if (color.opacity) {
107
+ updateColorOpacity(delta, color.opacity);
108
+ }
109
+ }
110
+ }
111
+ exports.updateGradient = updateGradient;
package/cjs/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.loadGradientUpdater = void 0;
4
4
  const GradientUpdater_1 = require("./GradientUpdater");
5
- function loadGradientUpdater(engine) {
6
- engine.addParticleUpdater("gradient", () => new GradientUpdater_1.GradientUpdater());
5
+ async function loadGradientUpdater(engine, refresh = true) {
6
+ await engine.addParticleUpdater("gradient", () => new GradientUpdater_1.GradientUpdater(), refresh);
7
7
  }
8
8
  exports.loadGradientUpdater = loadGradientUpdater;
@@ -1,111 +1,8 @@
1
1
  import { executeOnSingleOrMultiple, getHslAnimationFromHsl, getRandom, getRangeMax, getRangeMin, getRangeValue, getStyleFromHsl, itemFromSingleOrMultiple, randomInRange, rangeColorToHsl, } from "@tsparticles/engine";
2
2
  import { AnimatableGradient } from "./Options/Classes/AnimatableGradient";
3
- function updateColorOpacity(delta, value) {
4
- var _a, _b, _c;
5
- if (!value.enable) {
6
- return;
7
- }
8
- const decay = (_a = value.decay) !== null && _a !== void 0 ? _a : 1;
9
- switch (value.status) {
10
- case "increasing":
11
- if (value.value >= value.max) {
12
- value.status = "decreasing";
13
- }
14
- else {
15
- value.value += ((_b = value.velocity) !== null && _b !== void 0 ? _b : 0) * delta.factor;
16
- }
17
- break;
18
- case "decreasing":
19
- if (value.value <= value.min) {
20
- value.status = "increasing";
21
- }
22
- else {
23
- value.value -= ((_c = value.velocity) !== null && _c !== void 0 ? _c : 0) * delta.factor;
24
- }
25
- break;
26
- }
27
- if (value.velocity && decay !== 1) {
28
- value.velocity *= decay;
29
- }
30
- }
31
- function updateColorValue(delta, value, max, decrease) {
32
- var _a, _b;
33
- const colorValue = value;
34
- if (!colorValue || !colorValue.enable) {
35
- return;
36
- }
37
- const velocity = ((_a = value.velocity) !== null && _a !== void 0 ? _a : 0) * delta.factor, decay = (_b = value.decay) !== null && _b !== void 0 ? _b : 1;
38
- if (!decrease || colorValue.status === "increasing") {
39
- colorValue.value += velocity;
40
- if (decrease && colorValue.value > max) {
41
- colorValue.status = "decreasing";
42
- colorValue.value -= colorValue.value % max;
43
- }
44
- }
45
- else {
46
- colorValue.value -= velocity;
47
- if (colorValue.value < 0) {
48
- colorValue.status = "increasing";
49
- colorValue.value += colorValue.value;
50
- }
51
- }
52
- if (colorValue.value > max) {
53
- colorValue.value %= max;
54
- }
55
- if (value.velocity && decay !== 1) {
56
- value.velocity *= decay;
57
- }
58
- }
59
- function updateAngle(delta, angle) {
60
- var _a, _b;
61
- const speed = ((_a = angle.velocity) !== null && _a !== void 0 ? _a : 0) * delta.factor, max = 2 * Math.PI, decay = (_b = angle.decay) !== null && _b !== void 0 ? _b : 1;
62
- if (!angle.enable) {
63
- return;
64
- }
65
- switch (angle.status) {
66
- case "increasing":
67
- angle.value += speed;
68
- if (angle.value > max) {
69
- angle.value -= max;
70
- }
71
- break;
72
- case "decreasing":
73
- default:
74
- angle.value -= speed;
75
- if (angle.value < 0) {
76
- angle.value += max;
77
- }
78
- break;
79
- }
80
- if (angle.velocity && decay !== 1) {
81
- angle.velocity *= decay;
82
- }
83
- }
84
- function updateGradient(particle, delta) {
85
- var _a, _b, _c;
86
- const gradient = particle.gradient;
87
- if (!gradient) {
88
- return;
89
- }
90
- updateAngle(delta, gradient.angle);
91
- for (const color of gradient.colors) {
92
- if (((_a = particle.color) === null || _a === void 0 ? void 0 : _a.h) !== undefined) {
93
- updateColorValue(delta, color.value.h, 360, false);
94
- }
95
- if (((_b = particle.color) === null || _b === void 0 ? void 0 : _b.s) !== undefined) {
96
- updateColorValue(delta, color.value.s, 100, true);
97
- }
98
- if (((_c = particle.color) === null || _c === void 0 ? void 0 : _c.l) !== undefined) {
99
- updateColorValue(delta, color.value.l, 100, true);
100
- }
101
- if (color.opacity) {
102
- updateColorOpacity(delta, color.opacity);
103
- }
104
- }
105
- }
3
+ import { updateGradient } from "./Utils";
106
4
  export class GradientUpdater {
107
5
  getColorStyles(particle, context, radius, opacity) {
108
- var _a, _b;
109
6
  const gradient = particle.gradient;
110
7
  if (!gradient) {
111
8
  return {};
@@ -113,12 +10,12 @@ export class GradientUpdater {
113
10
  const gradientAngle = gradient.angle.value, fillGradient = gradient.type === "radial"
114
11
  ? context.createRadialGradient(0, 0, 0, 0, 0, radius)
115
12
  : context.createLinearGradient(Math.cos(gradientAngle) * -radius, Math.sin(gradientAngle) * -radius, Math.cos(gradientAngle) * radius, Math.sin(gradientAngle) * radius);
116
- for (const color of gradient.colors) {
117
- fillGradient.addColorStop(color.stop, getStyleFromHsl({
118
- h: color.value.h.value,
119
- s: color.value.s.value,
120
- l: color.value.l.value,
121
- }, (_b = (_a = color.opacity) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : opacity));
13
+ for (const { stop, value, opacity: cOpacity } of gradient.colors) {
14
+ fillGradient.addColorStop(stop, getStyleFromHsl({
15
+ h: value.h.value,
16
+ s: value.s.value,
17
+ l: value.l.value,
18
+ }, cOpacity?.value ?? opacity));
122
19
  }
123
20
  return { fill: fillGradient };
124
21
  }
@@ -127,20 +24,22 @@ export class GradientUpdater {
127
24
  if (!gradient) {
128
25
  return;
129
26
  }
27
+ const { angle } = gradient;
130
28
  particle.gradient = {
131
29
  angle: {
132
- value: gradient.angle.value,
133
- enable: gradient.angle.animation.enable,
134
- velocity: (getRangeValue(gradient.angle.animation.speed) / 360) * particle.container.retina.reduceFactor,
135
- decay: 1 - getRangeValue(gradient.angle.animation.decay),
30
+ value: getRangeValue(angle.value),
31
+ enable: angle.animation.enable,
32
+ velocity: (getRangeValue(angle.animation.speed) / 360) * particle.container.retina.reduceFactor,
33
+ decay: 1 - getRangeValue(angle.animation.decay),
34
+ delayTime: getRangeValue(angle.animation.delay) * 1000,
35
+ time: 0,
136
36
  },
137
37
  type: gradient.type,
138
38
  colors: [],
139
39
  };
140
40
  let rotateDirection = gradient.angle.direction;
141
41
  if (rotateDirection === "random") {
142
- const index = Math.floor(getRandom() * 2);
143
- rotateDirection = index > 0 ? "counter-clockwise" : "clockwise";
42
+ rotateDirection = getRandom() > 0.5 ? "counter-clockwise" : "clockwise";
144
43
  }
145
44
  switch (rotateDirection) {
146
45
  case "counter-clockwise":
@@ -170,28 +69,31 @@ export class GradientUpdater {
170
69
  velocity: (getRangeValue(grColor.opacity.animation.speed) / 100) *
171
70
  particle.container.retina.reduceFactor,
172
71
  decay: 1 - getRangeValue(grColor.opacity.animation.decay),
72
+ delayTime: getRangeValue(grColor.opacity.animation.delay) * 1000,
73
+ time: 0,
173
74
  }
174
75
  : undefined,
175
76
  };
176
- if (grColor.opacity && addColor.opacity) {
77
+ const { opacity: addOpacity } = addColor;
78
+ if (grColor.opacity && addOpacity) {
177
79
  const opacityRange = grColor.opacity.value;
178
- addColor.opacity.min = getRangeMin(opacityRange);
179
- addColor.opacity.max = getRangeMax(opacityRange);
80
+ addOpacity.min = getRangeMin(opacityRange);
81
+ addOpacity.max = getRangeMax(opacityRange);
180
82
  const opacityAnimation = grColor.opacity.animation;
181
83
  switch (opacityAnimation.startValue) {
182
84
  case "min":
183
- addColor.opacity.value = addColor.opacity.min;
184
- addColor.opacity.status = "increasing";
185
- break;
186
- case "random":
187
- addColor.opacity.value = randomInRange(addColor.opacity);
188
- addColor.opacity.status =
189
- getRandom() >= 0.5 ? "increasing" : "decreasing";
85
+ addOpacity.value = addOpacity.min;
86
+ addOpacity.status = "increasing";
190
87
  break;
191
88
  case "max":
89
+ addOpacity.value = addOpacity.max;
90
+ addOpacity.status = "decreasing";
91
+ break;
92
+ case "random":
192
93
  default:
193
- addColor.opacity.value = addColor.opacity.max;
194
- addColor.opacity.status = "decreasing";
94
+ addOpacity.value = randomInRange(addOpacity);
95
+ addOpacity.status =
96
+ getRandom() >= 0.5 ? "increasing" : "decreasing";
195
97
  break;
196
98
  }
197
99
  }
@@ -199,15 +101,15 @@ export class GradientUpdater {
199
101
  }
200
102
  }
201
103
  isEnabled(particle) {
202
- var _a, _b, _c;
203
104
  return (!particle.destroyed &&
204
105
  !particle.spawning &&
205
- (((_a = particle.gradient) === null || _a === void 0 ? void 0 : _a.angle.enable) ||
206
- ((_c = (_b = particle.gradient) === null || _b === void 0 ? void 0 : _b.colors.some((c) => c.value.h.enable || c.value.s.enable || c.value.l.enable)) !== null && _c !== void 0 ? _c : false)));
106
+ (particle.gradient?.angle.enable ||
107
+ (particle.gradient?.colors.some((c) => c.value.h.enable || c.value.s.enable || c.value.l.enable) ??
108
+ false)));
207
109
  }
208
110
  loadOptions(options, ...sources) {
209
111
  for (const source of sources) {
210
- if (!(source === null || source === void 0 ? void 0 : source.gradient)) {
112
+ if (!source?.gradient) {
211
113
  continue;
212
114
  }
213
115
  const gradientToLoad = source.gradient;
@@ -1,4 +1,4 @@
1
- import { AnimatableColor } from "@tsparticles/engine";
1
+ import { AnimatableColor, isNumber } from "@tsparticles/engine";
2
2
  import { GradientColorOpacity } from "./GradientColorOpacity";
3
3
  export class AnimatableGradientColor {
4
4
  constructor() {
@@ -15,7 +15,7 @@ export class AnimatableGradientColor {
15
15
  this.value = AnimatableColor.create(this.value, data.value);
16
16
  if (data.opacity !== undefined) {
17
17
  this.opacity = new GradientColorOpacity();
18
- if (typeof data.opacity === "number") {
18
+ if (isNumber(data.opacity)) {
19
19
  this.opacity.value = data.opacity;
20
20
  }
21
21
  else {