@tsparticles/updater-gradient 3.0.0-alpha.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 (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +70 -0
  3. package/browser/GradientUpdater.js +227 -0
  4. package/browser/Options/Classes/AnimatableGradient.js +25 -0
  5. package/browser/Options/Classes/AnimatableGradientColor.js +26 -0
  6. package/browser/Options/Classes/GradientAngle.js +20 -0
  7. package/browser/Options/Classes/GradientAngleAnimation.js +30 -0
  8. package/browser/Options/Classes/GradientColorOpacity.js +17 -0
  9. package/browser/Options/Classes/GradientColorOpacityAnimation.js +31 -0
  10. package/browser/Options/Interfaces/Gradients.js +1 -0
  11. package/browser/Options/Interfaces/IAnimatableGradient.js +1 -0
  12. package/browser/Options/Interfaces/IOptionsGradient.js +1 -0
  13. package/browser/index.js +4 -0
  14. package/cjs/GradientUpdater.js +231 -0
  15. package/cjs/Options/Classes/AnimatableGradient.js +29 -0
  16. package/cjs/Options/Classes/AnimatableGradientColor.js +30 -0
  17. package/cjs/Options/Classes/GradientAngle.js +24 -0
  18. package/cjs/Options/Classes/GradientAngleAnimation.js +34 -0
  19. package/cjs/Options/Classes/GradientColorOpacity.js +21 -0
  20. package/cjs/Options/Classes/GradientColorOpacityAnimation.js +35 -0
  21. package/cjs/Options/Interfaces/Gradients.js +2 -0
  22. package/cjs/Options/Interfaces/IAnimatableGradient.js +2 -0
  23. package/cjs/Options/Interfaces/IOptionsGradient.js +2 -0
  24. package/cjs/index.js +8 -0
  25. package/esm/GradientUpdater.js +227 -0
  26. package/esm/Options/Classes/AnimatableGradient.js +25 -0
  27. package/esm/Options/Classes/AnimatableGradientColor.js +26 -0
  28. package/esm/Options/Classes/GradientAngle.js +20 -0
  29. package/esm/Options/Classes/GradientAngleAnimation.js +30 -0
  30. package/esm/Options/Classes/GradientColorOpacity.js +17 -0
  31. package/esm/Options/Classes/GradientColorOpacityAnimation.js +31 -0
  32. package/esm/Options/Interfaces/Gradients.js +1 -0
  33. package/esm/Options/Interfaces/IAnimatableGradient.js +1 -0
  34. package/esm/Options/Interfaces/IOptionsGradient.js +1 -0
  35. package/esm/index.js +4 -0
  36. package/package.json +92 -0
  37. package/report.html +39 -0
  38. package/tsparticles.updater.gradient.js +486 -0
  39. package/tsparticles.updater.gradient.min.js +2 -0
  40. package/tsparticles.updater.gradient.min.js.LICENSE.txt +8 -0
  41. package/types/GradientUpdater.d.ts +32 -0
  42. package/types/Options/Classes/AnimatableGradient.d.ts +12 -0
  43. package/types/Options/Classes/AnimatableGradientColor.d.ts +11 -0
  44. package/types/Options/Classes/GradientAngle.d.ts +11 -0
  45. package/types/Options/Classes/GradientAngleAnimation.d.ts +10 -0
  46. package/types/Options/Classes/GradientColorOpacity.d.ts +10 -0
  47. package/types/Options/Classes/GradientColorOpacityAnimation.d.ts +13 -0
  48. package/types/Options/Interfaces/Gradients.d.ts +18 -0
  49. package/types/Options/Interfaces/IAnimatableGradient.d.ts +7 -0
  50. package/types/Options/Interfaces/IOptionsGradient.d.ts +10 -0
  51. package/types/index.d.ts +2 -0
  52. package/umd/GradientUpdater.js +241 -0
  53. package/umd/Options/Classes/AnimatableGradient.js +39 -0
  54. package/umd/Options/Classes/AnimatableGradientColor.js +40 -0
  55. package/umd/Options/Classes/GradientAngle.js +34 -0
  56. package/umd/Options/Classes/GradientAngleAnimation.js +44 -0
  57. package/umd/Options/Classes/GradientColorOpacity.js +31 -0
  58. package/umd/Options/Classes/GradientColorOpacityAnimation.js +45 -0
  59. package/umd/Options/Interfaces/Gradients.js +12 -0
  60. package/umd/Options/Interfaces/IAnimatableGradient.js +12 -0
  61. package/umd/Options/Interfaces/IOptionsGradient.js +12 -0
  62. package/umd/index.js +18 -0
@@ -0,0 +1,231 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GradientUpdater = void 0;
4
+ const engine_1 = require("@tsparticles/engine");
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
+ }
109
+ class GradientUpdater {
110
+ getColorStyles(particle, context, radius, opacity) {
111
+ var _a, _b;
112
+ const gradient = particle.gradient;
113
+ if (!gradient) {
114
+ return {};
115
+ }
116
+ const gradientAngle = gradient.angle.value, fillGradient = gradient.type === "radial"
117
+ ? context.createRadialGradient(0, 0, 0, 0, 0, radius)
118
+ : 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));
125
+ }
126
+ return { fill: fillGradient };
127
+ }
128
+ init(particle) {
129
+ const gradient = (0, engine_1.itemFromSingleOrMultiple)(particle.options.gradient);
130
+ if (!gradient) {
131
+ return;
132
+ }
133
+ particle.gradient = {
134
+ 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),
139
+ },
140
+ type: gradient.type,
141
+ colors: [],
142
+ };
143
+ let rotateDirection = gradient.angle.direction;
144
+ if (rotateDirection === "random") {
145
+ const index = Math.floor((0, engine_1.getRandom)() * 2);
146
+ rotateDirection = index > 0 ? "counter-clockwise" : "clockwise";
147
+ }
148
+ switch (rotateDirection) {
149
+ case "counter-clockwise":
150
+ case "counterClockwise":
151
+ particle.gradient.angle.status = "decreasing";
152
+ break;
153
+ case "clockwise":
154
+ particle.gradient.angle.status = "increasing";
155
+ break;
156
+ }
157
+ const reduceDuplicates = particle.options.reduceDuplicates;
158
+ for (const grColor of gradient.colors) {
159
+ const grHslColor = (0, engine_1.rangeColorToHsl)(grColor.value, particle.id, reduceDuplicates);
160
+ if (!grHslColor) {
161
+ continue;
162
+ }
163
+ const grHslAnimation = (0, engine_1.getHslAnimationFromHsl)(grHslColor, grColor.value.animation, particle.container.retina.reduceFactor), addColor = {
164
+ stop: grColor.stop,
165
+ value: grHslAnimation,
166
+ opacity: grColor.opacity
167
+ ? {
168
+ enable: grColor.opacity.animation.enable,
169
+ max: (0, engine_1.getRangeMax)(grColor.opacity.value),
170
+ min: (0, engine_1.getRangeMin)(grColor.opacity.value),
171
+ status: "increasing",
172
+ value: (0, engine_1.getRangeValue)(grColor.opacity.value),
173
+ velocity: ((0, engine_1.getRangeValue)(grColor.opacity.animation.speed) / 100) *
174
+ particle.container.retina.reduceFactor,
175
+ decay: 1 - (0, engine_1.getRangeValue)(grColor.opacity.animation.decay),
176
+ }
177
+ : undefined,
178
+ };
179
+ if (grColor.opacity && addColor.opacity) {
180
+ const opacityRange = grColor.opacity.value;
181
+ addColor.opacity.min = (0, engine_1.getRangeMin)(opacityRange);
182
+ addColor.opacity.max = (0, engine_1.getRangeMax)(opacityRange);
183
+ const opacityAnimation = grColor.opacity.animation;
184
+ switch (opacityAnimation.startValue) {
185
+ 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";
193
+ break;
194
+ case "max":
195
+ default:
196
+ addColor.opacity.value = addColor.opacity.max;
197
+ addColor.opacity.status = "decreasing";
198
+ break;
199
+ }
200
+ }
201
+ particle.gradient.colors.push(addColor);
202
+ }
203
+ }
204
+ isEnabled(particle) {
205
+ var _a, _b, _c;
206
+ return (!particle.destroyed &&
207
+ !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)));
210
+ }
211
+ loadOptions(options, ...sources) {
212
+ for (const source of sources) {
213
+ if (!(source === null || source === void 0 ? void 0 : source.gradient)) {
214
+ continue;
215
+ }
216
+ const gradientToLoad = source.gradient;
217
+ if (!gradientToLoad) {
218
+ continue;
219
+ }
220
+ options.gradient = (0, engine_1.executeOnSingleOrMultiple)(gradientToLoad, (gradient) => {
221
+ const tmp = new AnimatableGradient_1.AnimatableGradient();
222
+ tmp.load(gradient);
223
+ return tmp;
224
+ });
225
+ }
226
+ }
227
+ update(particle, delta) {
228
+ updateGradient(particle, delta);
229
+ }
230
+ }
231
+ exports.GradientUpdater = GradientUpdater;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AnimatableGradient = void 0;
4
+ const AnimatableGradientColor_1 = require("./AnimatableGradientColor");
5
+ const GradientAngle_1 = require("./GradientAngle");
6
+ class AnimatableGradient {
7
+ constructor() {
8
+ this.angle = new GradientAngle_1.GradientAngle();
9
+ this.colors = [];
10
+ this.type = "random";
11
+ }
12
+ load(data) {
13
+ if (!data) {
14
+ return;
15
+ }
16
+ this.angle.load(data.angle);
17
+ if (data.colors !== undefined) {
18
+ this.colors = data.colors.map((s) => {
19
+ const tmp = new AnimatableGradientColor_1.AnimatableGradientColor();
20
+ tmp.load(s);
21
+ return tmp;
22
+ });
23
+ }
24
+ if (data.type !== undefined) {
25
+ this.type = data.type;
26
+ }
27
+ }
28
+ }
29
+ exports.AnimatableGradient = AnimatableGradient;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AnimatableGradientColor = void 0;
4
+ const engine_1 = require("@tsparticles/engine");
5
+ const GradientColorOpacity_1 = require("./GradientColorOpacity");
6
+ class AnimatableGradientColor {
7
+ constructor() {
8
+ this.stop = 0;
9
+ this.value = new engine_1.AnimatableColor();
10
+ }
11
+ load(data) {
12
+ if (!data) {
13
+ return;
14
+ }
15
+ if (data.stop !== undefined) {
16
+ this.stop = data.stop;
17
+ }
18
+ this.value = engine_1.AnimatableColor.create(this.value, data.value);
19
+ if (data.opacity !== undefined) {
20
+ this.opacity = new GradientColorOpacity_1.GradientColorOpacity();
21
+ if (typeof data.opacity === "number") {
22
+ this.opacity.value = data.opacity;
23
+ }
24
+ else {
25
+ this.opacity.load(data.opacity);
26
+ }
27
+ }
28
+ }
29
+ }
30
+ exports.AnimatableGradientColor = AnimatableGradientColor;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GradientAngle = void 0;
4
+ const GradientAngleAnimation_1 = require("./GradientAngleAnimation");
5
+ class GradientAngle {
6
+ constructor() {
7
+ this.value = 0;
8
+ this.animation = new GradientAngleAnimation_1.GradientAngleAnimation();
9
+ this.direction = "clockwise";
10
+ }
11
+ load(data) {
12
+ if (!data) {
13
+ return;
14
+ }
15
+ this.animation.load(data.animation);
16
+ if (data.value !== undefined) {
17
+ this.value = data.value;
18
+ }
19
+ if (data.direction !== undefined) {
20
+ this.direction = data.direction;
21
+ }
22
+ }
23
+ }
24
+ exports.GradientAngle = GradientAngle;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GradientAngleAnimation = void 0;
4
+ const engine_1 = require("@tsparticles/engine");
5
+ class GradientAngleAnimation {
6
+ constructor() {
7
+ this.count = 0;
8
+ this.enable = false;
9
+ this.speed = 0;
10
+ this.decay = 0;
11
+ this.sync = false;
12
+ }
13
+ load(data) {
14
+ if (!data) {
15
+ return;
16
+ }
17
+ if (data.count !== undefined) {
18
+ this.count = (0, engine_1.setRangeValue)(data.count);
19
+ }
20
+ if (data.enable !== undefined) {
21
+ this.enable = data.enable;
22
+ }
23
+ if (data.speed !== undefined) {
24
+ this.speed = (0, engine_1.setRangeValue)(data.speed);
25
+ }
26
+ if (data.decay !== undefined) {
27
+ this.decay = (0, engine_1.setRangeValue)(data.decay);
28
+ }
29
+ if (data.sync !== undefined) {
30
+ this.sync = data.sync;
31
+ }
32
+ }
33
+ }
34
+ exports.GradientAngleAnimation = GradientAngleAnimation;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GradientColorOpacity = void 0;
4
+ const GradientColorOpacityAnimation_1 = require("./GradientColorOpacityAnimation");
5
+ const engine_1 = require("@tsparticles/engine");
6
+ class GradientColorOpacity {
7
+ constructor() {
8
+ this.value = 0;
9
+ this.animation = new GradientColorOpacityAnimation_1.GradientColorOpacityAnimation();
10
+ }
11
+ load(data) {
12
+ if (!data) {
13
+ return;
14
+ }
15
+ this.animation.load(data.animation);
16
+ if (data.value !== undefined) {
17
+ this.value = (0, engine_1.setRangeValue)(data.value);
18
+ }
19
+ }
20
+ }
21
+ exports.GradientColorOpacity = GradientColorOpacity;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GradientColorOpacityAnimation = void 0;
4
+ const engine_1 = require("@tsparticles/engine");
5
+ class GradientColorOpacityAnimation {
6
+ constructor() {
7
+ this.count = 0;
8
+ this.enable = false;
9
+ this.speed = 0;
10
+ this.decay = 0;
11
+ this.sync = false;
12
+ this.startValue = "random";
13
+ }
14
+ load(data) {
15
+ if (!data) {
16
+ return;
17
+ }
18
+ if (data.count !== undefined) {
19
+ this.count = (0, engine_1.setRangeValue)(data.count);
20
+ }
21
+ if (data.enable !== undefined) {
22
+ this.enable = data.enable;
23
+ }
24
+ if (data.speed !== undefined) {
25
+ this.speed = (0, engine_1.setRangeValue)(data.speed);
26
+ }
27
+ if (data.sync !== undefined) {
28
+ this.sync = data.sync;
29
+ }
30
+ if (data.startValue !== undefined) {
31
+ this.startValue = data.startValue;
32
+ }
33
+ }
34
+ }
35
+ exports.GradientColorOpacityAnimation = GradientColorOpacityAnimation;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/cjs/index.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadGradientUpdater = void 0;
4
+ const GradientUpdater_1 = require("./GradientUpdater");
5
+ function loadGradientUpdater(engine) {
6
+ engine.addParticleUpdater("gradient", () => new GradientUpdater_1.GradientUpdater());
7
+ }
8
+ exports.loadGradientUpdater = loadGradientUpdater;
@@ -0,0 +1,227 @@
1
+ import { executeOnSingleOrMultiple, getHslAnimationFromHsl, getRandom, getRangeMax, getRangeMin, getRangeValue, getStyleFromHsl, itemFromSingleOrMultiple, randomInRange, rangeColorToHsl, } from "@tsparticles/engine";
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
+ }
106
+ export class GradientUpdater {
107
+ getColorStyles(particle, context, radius, opacity) {
108
+ var _a, _b;
109
+ const gradient = particle.gradient;
110
+ if (!gradient) {
111
+ return {};
112
+ }
113
+ const gradientAngle = gradient.angle.value, fillGradient = gradient.type === "radial"
114
+ ? context.createRadialGradient(0, 0, 0, 0, 0, radius)
115
+ : 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));
122
+ }
123
+ return { fill: fillGradient };
124
+ }
125
+ init(particle) {
126
+ const gradient = itemFromSingleOrMultiple(particle.options.gradient);
127
+ if (!gradient) {
128
+ return;
129
+ }
130
+ particle.gradient = {
131
+ 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),
136
+ },
137
+ type: gradient.type,
138
+ colors: [],
139
+ };
140
+ let rotateDirection = gradient.angle.direction;
141
+ if (rotateDirection === "random") {
142
+ const index = Math.floor(getRandom() * 2);
143
+ rotateDirection = index > 0 ? "counter-clockwise" : "clockwise";
144
+ }
145
+ switch (rotateDirection) {
146
+ case "counter-clockwise":
147
+ case "counterClockwise":
148
+ particle.gradient.angle.status = "decreasing";
149
+ break;
150
+ case "clockwise":
151
+ particle.gradient.angle.status = "increasing";
152
+ break;
153
+ }
154
+ const reduceDuplicates = particle.options.reduceDuplicates;
155
+ for (const grColor of gradient.colors) {
156
+ const grHslColor = rangeColorToHsl(grColor.value, particle.id, reduceDuplicates);
157
+ if (!grHslColor) {
158
+ continue;
159
+ }
160
+ const grHslAnimation = getHslAnimationFromHsl(grHslColor, grColor.value.animation, particle.container.retina.reduceFactor), addColor = {
161
+ stop: grColor.stop,
162
+ value: grHslAnimation,
163
+ opacity: grColor.opacity
164
+ ? {
165
+ enable: grColor.opacity.animation.enable,
166
+ max: getRangeMax(grColor.opacity.value),
167
+ min: getRangeMin(grColor.opacity.value),
168
+ status: "increasing",
169
+ value: getRangeValue(grColor.opacity.value),
170
+ velocity: (getRangeValue(grColor.opacity.animation.speed) / 100) *
171
+ particle.container.retina.reduceFactor,
172
+ decay: 1 - getRangeValue(grColor.opacity.animation.decay),
173
+ }
174
+ : undefined,
175
+ };
176
+ if (grColor.opacity && addColor.opacity) {
177
+ const opacityRange = grColor.opacity.value;
178
+ addColor.opacity.min = getRangeMin(opacityRange);
179
+ addColor.opacity.max = getRangeMax(opacityRange);
180
+ const opacityAnimation = grColor.opacity.animation;
181
+ switch (opacityAnimation.startValue) {
182
+ 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";
190
+ break;
191
+ case "max":
192
+ default:
193
+ addColor.opacity.value = addColor.opacity.max;
194
+ addColor.opacity.status = "decreasing";
195
+ break;
196
+ }
197
+ }
198
+ particle.gradient.colors.push(addColor);
199
+ }
200
+ }
201
+ isEnabled(particle) {
202
+ var _a, _b, _c;
203
+ return (!particle.destroyed &&
204
+ !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)));
207
+ }
208
+ loadOptions(options, ...sources) {
209
+ for (const source of sources) {
210
+ if (!(source === null || source === void 0 ? void 0 : source.gradient)) {
211
+ continue;
212
+ }
213
+ const gradientToLoad = source.gradient;
214
+ if (!gradientToLoad) {
215
+ continue;
216
+ }
217
+ options.gradient = executeOnSingleOrMultiple(gradientToLoad, (gradient) => {
218
+ const tmp = new AnimatableGradient();
219
+ tmp.load(gradient);
220
+ return tmp;
221
+ });
222
+ }
223
+ }
224
+ update(particle, delta) {
225
+ updateGradient(particle, delta);
226
+ }
227
+ }
@@ -0,0 +1,25 @@
1
+ import { AnimatableGradientColor } from "./AnimatableGradientColor";
2
+ import { GradientAngle } from "./GradientAngle";
3
+ export class AnimatableGradient {
4
+ constructor() {
5
+ this.angle = new GradientAngle();
6
+ this.colors = [];
7
+ this.type = "random";
8
+ }
9
+ load(data) {
10
+ if (!data) {
11
+ return;
12
+ }
13
+ this.angle.load(data.angle);
14
+ if (data.colors !== undefined) {
15
+ this.colors = data.colors.map((s) => {
16
+ const tmp = new AnimatableGradientColor();
17
+ tmp.load(s);
18
+ return tmp;
19
+ });
20
+ }
21
+ if (data.type !== undefined) {
22
+ this.type = data.type;
23
+ }
24
+ }
25
+ }