@tsparticles/updater-gradient 3.0.0-alpha.0 → 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
@@ -4,7 +4,7 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "@tsparticles/engine", "./Options/Classes/AnimatableGradient"], factory);
7
+ define(["require", "exports", "@tsparticles/engine", "./Options/Classes/AnimatableGradient", "./Utils"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
@@ -12,112 +12,9 @@
12
12
  exports.GradientUpdater = void 0;
13
13
  const engine_1 = require("@tsparticles/engine");
14
14
  const AnimatableGradient_1 = require("./Options/Classes/AnimatableGradient");
15
- function updateColorOpacity(delta, value) {
16
- var _a, _b, _c;
17
- if (!value.enable) {
18
- return;
19
- }
20
- const decay = (_a = value.decay) !== null && _a !== void 0 ? _a : 1;
21
- switch (value.status) {
22
- case "increasing":
23
- if (value.value >= value.max) {
24
- value.status = "decreasing";
25
- }
26
- else {
27
- value.value += ((_b = value.velocity) !== null && _b !== void 0 ? _b : 0) * delta.factor;
28
- }
29
- break;
30
- case "decreasing":
31
- if (value.value <= value.min) {
32
- value.status = "increasing";
33
- }
34
- else {
35
- value.value -= ((_c = value.velocity) !== null && _c !== void 0 ? _c : 0) * delta.factor;
36
- }
37
- break;
38
- }
39
- if (value.velocity && decay !== 1) {
40
- value.velocity *= decay;
41
- }
42
- }
43
- function updateColorValue(delta, value, max, decrease) {
44
- var _a, _b;
45
- const colorValue = value;
46
- if (!colorValue || !colorValue.enable) {
47
- return;
48
- }
49
- const velocity = ((_a = value.velocity) !== null && _a !== void 0 ? _a : 0) * delta.factor, decay = (_b = value.decay) !== null && _b !== void 0 ? _b : 1;
50
- if (!decrease || colorValue.status === "increasing") {
51
- colorValue.value += velocity;
52
- if (decrease && colorValue.value > max) {
53
- colorValue.status = "decreasing";
54
- colorValue.value -= colorValue.value % max;
55
- }
56
- }
57
- else {
58
- colorValue.value -= velocity;
59
- if (colorValue.value < 0) {
60
- colorValue.status = "increasing";
61
- colorValue.value += colorValue.value;
62
- }
63
- }
64
- if (colorValue.value > max) {
65
- colorValue.value %= max;
66
- }
67
- if (value.velocity && decay !== 1) {
68
- value.velocity *= decay;
69
- }
70
- }
71
- function updateAngle(delta, angle) {
72
- var _a, _b;
73
- 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;
74
- if (!angle.enable) {
75
- return;
76
- }
77
- switch (angle.status) {
78
- case "increasing":
79
- angle.value += speed;
80
- if (angle.value > max) {
81
- angle.value -= max;
82
- }
83
- break;
84
- case "decreasing":
85
- default:
86
- angle.value -= speed;
87
- if (angle.value < 0) {
88
- angle.value += max;
89
- }
90
- break;
91
- }
92
- if (angle.velocity && decay !== 1) {
93
- angle.velocity *= decay;
94
- }
95
- }
96
- function updateGradient(particle, delta) {
97
- var _a, _b, _c;
98
- const gradient = particle.gradient;
99
- if (!gradient) {
100
- return;
101
- }
102
- updateAngle(delta, gradient.angle);
103
- for (const color of gradient.colors) {
104
- if (((_a = particle.color) === null || _a === void 0 ? void 0 : _a.h) !== undefined) {
105
- updateColorValue(delta, color.value.h, 360, false);
106
- }
107
- if (((_b = particle.color) === null || _b === void 0 ? void 0 : _b.s) !== undefined) {
108
- updateColorValue(delta, color.value.s, 100, true);
109
- }
110
- if (((_c = particle.color) === null || _c === void 0 ? void 0 : _c.l) !== undefined) {
111
- updateColorValue(delta, color.value.l, 100, true);
112
- }
113
- if (color.opacity) {
114
- updateColorOpacity(delta, color.opacity);
115
- }
116
- }
117
- }
15
+ const Utils_1 = require("./Utils");
118
16
  class GradientUpdater {
119
17
  getColorStyles(particle, context, radius, opacity) {
120
- var _a, _b;
121
18
  const gradient = particle.gradient;
122
19
  if (!gradient) {
123
20
  return {};
@@ -125,12 +22,12 @@
125
22
  const gradientAngle = gradient.angle.value, fillGradient = gradient.type === "radial"
126
23
  ? context.createRadialGradient(0, 0, 0, 0, 0, radius)
127
24
  : context.createLinearGradient(Math.cos(gradientAngle) * -radius, Math.sin(gradientAngle) * -radius, Math.cos(gradientAngle) * radius, Math.sin(gradientAngle) * radius);
128
- for (const color of gradient.colors) {
129
- fillGradient.addColorStop(color.stop, (0, engine_1.getStyleFromHsl)({
130
- h: color.value.h.value,
131
- s: color.value.s.value,
132
- l: color.value.l.value,
133
- }, (_b = (_a = color.opacity) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : opacity));
25
+ for (const { stop, value, opacity: cOpacity } of gradient.colors) {
26
+ fillGradient.addColorStop(stop, (0, engine_1.getStyleFromHsl)({
27
+ h: value.h.value,
28
+ s: value.s.value,
29
+ l: value.l.value,
30
+ }, cOpacity?.value ?? opacity));
134
31
  }
135
32
  return { fill: fillGradient };
136
33
  }
@@ -139,20 +36,22 @@
139
36
  if (!gradient) {
140
37
  return;
141
38
  }
39
+ const { angle } = gradient;
142
40
  particle.gradient = {
143
41
  angle: {
144
- value: gradient.angle.value,
145
- enable: gradient.angle.animation.enable,
146
- velocity: ((0, engine_1.getRangeValue)(gradient.angle.animation.speed) / 360) * particle.container.retina.reduceFactor,
147
- decay: 1 - (0, engine_1.getRangeValue)(gradient.angle.animation.decay),
42
+ value: (0, engine_1.getRangeValue)(angle.value),
43
+ enable: angle.animation.enable,
44
+ velocity: ((0, engine_1.getRangeValue)(angle.animation.speed) / 360) * particle.container.retina.reduceFactor,
45
+ decay: 1 - (0, engine_1.getRangeValue)(angle.animation.decay),
46
+ delayTime: (0, engine_1.getRangeValue)(angle.animation.delay) * 1000,
47
+ time: 0,
148
48
  },
149
49
  type: gradient.type,
150
50
  colors: [],
151
51
  };
152
52
  let rotateDirection = gradient.angle.direction;
153
53
  if (rotateDirection === "random") {
154
- const index = Math.floor((0, engine_1.getRandom)() * 2);
155
- rotateDirection = index > 0 ? "counter-clockwise" : "clockwise";
54
+ rotateDirection = (0, engine_1.getRandom)() > 0.5 ? "counter-clockwise" : "clockwise";
156
55
  }
157
56
  switch (rotateDirection) {
158
57
  case "counter-clockwise":
@@ -182,28 +81,31 @@
182
81
  velocity: ((0, engine_1.getRangeValue)(grColor.opacity.animation.speed) / 100) *
183
82
  particle.container.retina.reduceFactor,
184
83
  decay: 1 - (0, engine_1.getRangeValue)(grColor.opacity.animation.decay),
84
+ delayTime: (0, engine_1.getRangeValue)(grColor.opacity.animation.delay) * 1000,
85
+ time: 0,
185
86
  }
186
87
  : undefined,
187
88
  };
188
- if (grColor.opacity && addColor.opacity) {
89
+ const { opacity: addOpacity } = addColor;
90
+ if (grColor.opacity && addOpacity) {
189
91
  const opacityRange = grColor.opacity.value;
190
- addColor.opacity.min = (0, engine_1.getRangeMin)(opacityRange);
191
- addColor.opacity.max = (0, engine_1.getRangeMax)(opacityRange);
92
+ addOpacity.min = (0, engine_1.getRangeMin)(opacityRange);
93
+ addOpacity.max = (0, engine_1.getRangeMax)(opacityRange);
192
94
  const opacityAnimation = grColor.opacity.animation;
193
95
  switch (opacityAnimation.startValue) {
194
96
  case "min":
195
- addColor.opacity.value = addColor.opacity.min;
196
- addColor.opacity.status = "increasing";
197
- break;
198
- case "random":
199
- addColor.opacity.value = (0, engine_1.randomInRange)(addColor.opacity);
200
- addColor.opacity.status =
201
- (0, engine_1.getRandom)() >= 0.5 ? "increasing" : "decreasing";
97
+ addOpacity.value = addOpacity.min;
98
+ addOpacity.status = "increasing";
202
99
  break;
203
100
  case "max":
101
+ addOpacity.value = addOpacity.max;
102
+ addOpacity.status = "decreasing";
103
+ break;
104
+ case "random":
204
105
  default:
205
- addColor.opacity.value = addColor.opacity.max;
206
- addColor.opacity.status = "decreasing";
106
+ addOpacity.value = (0, engine_1.randomInRange)(addOpacity);
107
+ addOpacity.status =
108
+ (0, engine_1.getRandom)() >= 0.5 ? "increasing" : "decreasing";
207
109
  break;
208
110
  }
209
111
  }
@@ -211,15 +113,15 @@
211
113
  }
212
114
  }
213
115
  isEnabled(particle) {
214
- var _a, _b, _c;
215
116
  return (!particle.destroyed &&
216
117
  !particle.spawning &&
217
- (((_a = particle.gradient) === null || _a === void 0 ? void 0 : _a.angle.enable) ||
218
- ((_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)));
118
+ (particle.gradient?.angle.enable ||
119
+ (particle.gradient?.colors.some((c) => c.value.h.enable || c.value.s.enable || c.value.l.enable) ??
120
+ false)));
219
121
  }
220
122
  loadOptions(options, ...sources) {
221
123
  for (const source of sources) {
222
- if (!(source === null || source === void 0 ? void 0 : source.gradient)) {
124
+ if (!source?.gradient) {
223
125
  continue;
224
126
  }
225
127
  const gradientToLoad = source.gradient;
@@ -234,7 +136,7 @@
234
136
  }
235
137
  }
236
138
  update(particle, delta) {
237
- updateGradient(particle, delta);
139
+ (0, Utils_1.updateGradient)(particle, delta);
238
140
  }
239
141
  }
240
142
  exports.GradientUpdater = GradientUpdater;
@@ -27,7 +27,7 @@
27
27
  this.value = engine_1.AnimatableColor.create(this.value, data.value);
28
28
  if (data.opacity !== undefined) {
29
29
  this.opacity = new GradientColorOpacity_1.GradientColorOpacity();
30
- if (typeof data.opacity === "number") {
30
+ if ((0, engine_1.isNumber)(data.opacity)) {
31
31
  this.opacity.value = data.opacity;
32
32
  }
33
33
  else {
@@ -4,12 +4,13 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./GradientAngleAnimation"], factory);
7
+ define(["require", "exports", "@tsparticles/engine", "./GradientAngleAnimation"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.GradientAngle = void 0;
13
+ const engine_1 = require("@tsparticles/engine");
13
14
  const GradientAngleAnimation_1 = require("./GradientAngleAnimation");
14
15
  class GradientAngle {
15
16
  constructor() {
@@ -23,7 +24,7 @@
23
24
  }
24
25
  this.animation.load(data.animation);
25
26
  if (data.value !== undefined) {
26
- this.value = data.value;
27
+ this.value = (0, engine_1.setRangeValue)(data.value);
27
28
  }
28
29
  if (data.direction !== undefined) {
29
30
  this.direction = data.direction;
@@ -17,6 +17,7 @@
17
17
  this.enable = false;
18
18
  this.speed = 0;
19
19
  this.decay = 0;
20
+ this.delay = 0;
20
21
  this.sync = false;
21
22
  }
22
23
  load(data) {
@@ -35,6 +36,9 @@
35
36
  if (data.decay !== undefined) {
36
37
  this.decay = (0, engine_1.setRangeValue)(data.decay);
37
38
  }
39
+ if (data.delay !== undefined) {
40
+ this.delay = (0, engine_1.setRangeValue)(data.delay);
41
+ }
38
42
  if (data.sync !== undefined) {
39
43
  this.sync = data.sync;
40
44
  }
@@ -4,14 +4,14 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./GradientColorOpacityAnimation", "@tsparticles/engine"], factory);
7
+ define(["require", "exports", "@tsparticles/engine", "./GradientColorOpacityAnimation"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.GradientColorOpacity = void 0;
13
- const GradientColorOpacityAnimation_1 = require("./GradientColorOpacityAnimation");
14
13
  const engine_1 = require("@tsparticles/engine");
14
+ const GradientColorOpacityAnimation_1 = require("./GradientColorOpacityAnimation");
15
15
  class GradientColorOpacity {
16
16
  constructor() {
17
17
  this.value = 0;
@@ -17,6 +17,7 @@
17
17
  this.enable = false;
18
18
  this.speed = 0;
19
19
  this.decay = 0;
20
+ this.delay = 0;
20
21
  this.sync = false;
21
22
  this.startValue = "random";
22
23
  }
@@ -39,6 +40,12 @@
39
40
  if (data.startValue !== undefined) {
40
41
  this.startValue = data.startValue;
41
42
  }
43
+ if (data.decay !== undefined) {
44
+ this.decay = (0, engine_1.setRangeValue)(data.decay);
45
+ }
46
+ if (data.delay !== undefined) {
47
+ this.delay = (0, engine_1.setRangeValue)(data.delay);
48
+ }
42
49
  }
43
50
  }
44
51
  exports.GradientColorOpacityAnimation = GradientColorOpacityAnimation;
package/umd/Types.js ADDED
@@ -0,0 +1,12 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ });
package/umd/Utils.js ADDED
@@ -0,0 +1,121 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.updateGradient = void 0;
13
+ function updateColorOpacity(delta, value) {
14
+ if (!value.enable) {
15
+ return;
16
+ }
17
+ const decay = value.decay ?? 1;
18
+ switch (value.status) {
19
+ case "increasing":
20
+ if (value.value >= value.max) {
21
+ value.status = "decreasing";
22
+ }
23
+ else {
24
+ value.value += (value.velocity ?? 0) * delta.factor;
25
+ }
26
+ break;
27
+ case "decreasing":
28
+ if (value.value <= value.min) {
29
+ value.status = "increasing";
30
+ }
31
+ else {
32
+ value.value -= (value.velocity ?? 0) * delta.factor;
33
+ }
34
+ break;
35
+ }
36
+ if (value.velocity && decay !== 1) {
37
+ value.velocity *= decay;
38
+ }
39
+ }
40
+ function updateColorValue(delta, colorValue, max, decrease) {
41
+ if (!colorValue || !colorValue.enable) {
42
+ return;
43
+ }
44
+ if (!colorValue.time) {
45
+ colorValue.time = 0;
46
+ }
47
+ if ((colorValue.delayTime ?? 0) > 0 && colorValue.time < (colorValue.delayTime ?? 0)) {
48
+ colorValue.time += delta.value;
49
+ }
50
+ if ((colorValue.delayTime ?? 0) > 0 && colorValue.time < (colorValue.delayTime ?? 0)) {
51
+ return;
52
+ }
53
+ const velocity = (colorValue.velocity ?? 0) * delta.factor, decay = colorValue.decay ?? 1;
54
+ if (!decrease || colorValue.status === "increasing") {
55
+ colorValue.value += velocity;
56
+ if (decrease && colorValue.value > max) {
57
+ colorValue.status = "decreasing";
58
+ colorValue.value -= colorValue.value % max;
59
+ }
60
+ }
61
+ else {
62
+ colorValue.value -= velocity;
63
+ if (colorValue.value < 0) {
64
+ colorValue.status = "increasing";
65
+ colorValue.value += colorValue.value;
66
+ }
67
+ }
68
+ if (colorValue.value > max) {
69
+ colorValue.value %= max;
70
+ }
71
+ if (colorValue.velocity && decay !== 1) {
72
+ colorValue.velocity *= decay;
73
+ }
74
+ }
75
+ function updateAngle(delta, angle) {
76
+ const speed = (angle.velocity ?? 0) * delta.factor, max = 2 * Math.PI, decay = angle.decay ?? 1;
77
+ if (!angle.enable) {
78
+ return;
79
+ }
80
+ switch (angle.status) {
81
+ case "increasing":
82
+ angle.value += speed;
83
+ if (angle.value > max) {
84
+ angle.value -= max;
85
+ }
86
+ break;
87
+ case "decreasing":
88
+ default:
89
+ angle.value -= speed;
90
+ if (angle.value < 0) {
91
+ angle.value += max;
92
+ }
93
+ break;
94
+ }
95
+ if (angle.velocity && decay !== 1) {
96
+ angle.velocity *= decay;
97
+ }
98
+ }
99
+ function updateGradient(particle, delta) {
100
+ const { gradient } = particle;
101
+ if (!gradient) {
102
+ return;
103
+ }
104
+ updateAngle(delta, gradient.angle);
105
+ for (const color of gradient.colors) {
106
+ if (particle.color?.h !== undefined) {
107
+ updateColorValue(delta, color.value.h, 360, false);
108
+ }
109
+ if (particle.color?.s !== undefined) {
110
+ updateColorValue(delta, color.value.s, 100, true);
111
+ }
112
+ if (particle.color?.l !== undefined) {
113
+ updateColorValue(delta, color.value.l, 100, true);
114
+ }
115
+ if (color.opacity) {
116
+ updateColorOpacity(delta, color.opacity);
117
+ }
118
+ }
119
+ }
120
+ exports.updateGradient = updateGradient;
121
+ });
package/umd/index.js CHANGED
@@ -11,8 +11,8 @@
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.loadGradientUpdater = void 0;
13
13
  const GradientUpdater_1 = require("./GradientUpdater");
14
- function loadGradientUpdater(engine) {
15
- engine.addParticleUpdater("gradient", () => new GradientUpdater_1.GradientUpdater());
14
+ async function loadGradientUpdater(engine, refresh = true) {
15
+ await engine.addParticleUpdater("gradient", () => new GradientUpdater_1.GradientUpdater(), refresh);
16
16
  }
17
17
  exports.loadGradientUpdater = loadGradientUpdater;
18
18
  });