@tsparticles/engine 3.0.2 → 3.1.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 (124) hide show
  1. package/README.md +283 -163
  2. package/browser/Core/Canvas.js +19 -19
  3. package/browser/Core/Container.js +45 -34
  4. package/browser/Core/Engine.js +36 -20
  5. package/browser/Core/Particle.js +35 -36
  6. package/browser/Core/Particles.js +30 -24
  7. package/browser/Core/Retina.js +5 -4
  8. package/browser/Core/Utils/Circle.js +4 -3
  9. package/browser/Core/Utils/Constants.js +3 -0
  10. package/browser/Core/Utils/EventListeners.js +18 -15
  11. package/browser/Core/Utils/ExternalInteractorBase.js +1 -1
  12. package/browser/Core/Utils/InteractionManager.js +14 -6
  13. package/browser/Core/Utils/ParticlesInteractorBase.js +1 -1
  14. package/browser/Core/Utils/QuadTree.js +5 -3
  15. package/browser/Core/Utils/Vector.js +7 -2
  16. package/browser/Core/Utils/Vector3d.js +14 -9
  17. package/browser/Options/Classes/ManualParticle.js +3 -2
  18. package/browser/Options/Classes/Options.js +3 -0
  19. package/browser/Utils/CanvasUtils.js +36 -26
  20. package/browser/Utils/ColorUtils.js +124 -45
  21. package/browser/Utils/EventDispatcher.js +6 -5
  22. package/browser/Utils/HslColorManager.js +5 -5
  23. package/browser/Utils/NumberUtils.js +35 -23
  24. package/browser/Utils/RgbColorManager.js +5 -5
  25. package/browser/Utils/Utils.js +102 -19
  26. package/cjs/Core/Canvas.js +19 -19
  27. package/cjs/Core/Container.js +45 -34
  28. package/cjs/Core/Engine.js +36 -20
  29. package/cjs/Core/Particle.js +34 -35
  30. package/cjs/Core/Particles.js +30 -24
  31. package/cjs/Core/Retina.js +5 -4
  32. package/cjs/Core/Utils/Circle.js +4 -3
  33. package/cjs/Core/Utils/Constants.js +4 -1
  34. package/cjs/Core/Utils/EventListeners.js +17 -14
  35. package/cjs/Core/Utils/ExternalInteractorBase.js +1 -1
  36. package/cjs/Core/Utils/InteractionManager.js +14 -6
  37. package/cjs/Core/Utils/ParticlesInteractorBase.js +1 -1
  38. package/cjs/Core/Utils/QuadTree.js +5 -3
  39. package/cjs/Core/Utils/Vector.js +7 -2
  40. package/cjs/Core/Utils/Vector3d.js +14 -9
  41. package/cjs/Options/Classes/ManualParticle.js +3 -2
  42. package/cjs/Options/Classes/Options.js +3 -0
  43. package/cjs/Utils/CanvasUtils.js +36 -26
  44. package/cjs/Utils/ColorUtils.js +126 -45
  45. package/cjs/Utils/EventDispatcher.js +6 -5
  46. package/cjs/Utils/HslColorManager.js +5 -5
  47. package/cjs/Utils/NumberUtils.js +37 -24
  48. package/cjs/Utils/RgbColorManager.js +5 -5
  49. package/cjs/Utils/Utils.js +103 -19
  50. package/esm/Core/Canvas.js +19 -19
  51. package/esm/Core/Container.js +45 -34
  52. package/esm/Core/Engine.js +36 -20
  53. package/esm/Core/Particle.js +35 -36
  54. package/esm/Core/Particles.js +30 -24
  55. package/esm/Core/Retina.js +5 -4
  56. package/esm/Core/Utils/Circle.js +4 -3
  57. package/esm/Core/Utils/Constants.js +3 -0
  58. package/esm/Core/Utils/EventListeners.js +18 -15
  59. package/esm/Core/Utils/ExternalInteractorBase.js +1 -1
  60. package/esm/Core/Utils/InteractionManager.js +14 -6
  61. package/esm/Core/Utils/ParticlesInteractorBase.js +1 -1
  62. package/esm/Core/Utils/QuadTree.js +5 -3
  63. package/esm/Core/Utils/Vector.js +7 -2
  64. package/esm/Core/Utils/Vector3d.js +14 -9
  65. package/esm/Options/Classes/ManualParticle.js +3 -2
  66. package/esm/Options/Classes/Options.js +3 -0
  67. package/esm/Utils/CanvasUtils.js +36 -26
  68. package/esm/Utils/ColorUtils.js +124 -45
  69. package/esm/Utils/EventDispatcher.js +6 -5
  70. package/esm/Utils/HslColorManager.js +5 -5
  71. package/esm/Utils/NumberUtils.js +35 -23
  72. package/esm/Utils/RgbColorManager.js +5 -5
  73. package/esm/Utils/Utils.js +102 -19
  74. package/package.json +1 -1
  75. package/report.html +2 -2
  76. package/tsparticles.engine.js +693 -334
  77. package/tsparticles.engine.min.js +1 -1
  78. package/tsparticles.engine.min.js.LICENSE.txt +1 -1
  79. package/types/Core/Interfaces/IParticleHslAnimation.d.ts +4 -4
  80. package/types/Core/Interfaces/IParticleValueAnimation.d.ts +4 -0
  81. package/types/Core/Interfaces/IShapeDrawData.d.ts +2 -2
  82. package/types/Core/Utils/Constants.d.ts +3 -0
  83. package/types/Core/Utils/ExternalInteractorBase.d.ts +1 -1
  84. package/types/Core/Utils/InteractionManager.d.ts +1 -1
  85. package/types/Core/Utils/ParticlesInteractorBase.d.ts +1 -1
  86. package/types/Core/Utils/Point.d.ts +1 -1
  87. package/types/Options/Classes/Options.d.ts +1 -0
  88. package/types/Options/Classes/Particles/Move/Move.d.ts +1 -2
  89. package/types/Options/Classes/Particles/Move/OutModes.d.ts +1 -2
  90. package/types/Options/Interfaces/IOptions.d.ts +1 -0
  91. package/types/Options/Interfaces/Interactivity/Modes/IModes.d.ts +1 -3
  92. package/types/Types/CustomEventArgs.d.ts +2 -2
  93. package/types/Types/ExportResult.d.ts +2 -2
  94. package/types/Types/ParticlesGroups.d.ts +1 -3
  95. package/types/Types/PathOptions.d.ts +1 -3
  96. package/types/Types/ShapeData.d.ts +1 -3
  97. package/types/Utils/CanvasUtils.d.ts +3 -2
  98. package/types/Utils/ColorUtils.d.ts +5 -0
  99. package/types/Utils/NumberUtils.d.ts +2 -2
  100. package/types/Utils/Utils.d.ts +9 -6
  101. package/umd/Core/Canvas.js +19 -19
  102. package/umd/Core/Container.js +46 -35
  103. package/umd/Core/Engine.js +36 -20
  104. package/umd/Core/Particle.js +35 -36
  105. package/umd/Core/Particles.js +30 -24
  106. package/umd/Core/Retina.js +5 -4
  107. package/umd/Core/Utils/Circle.js +4 -3
  108. package/umd/Core/Utils/Constants.js +4 -1
  109. package/umd/Core/Utils/EventListeners.js +17 -14
  110. package/umd/Core/Utils/ExternalInteractorBase.js +1 -1
  111. package/umd/Core/Utils/InteractionManager.js +14 -6
  112. package/umd/Core/Utils/ParticlesInteractorBase.js +1 -1
  113. package/umd/Core/Utils/QuadTree.js +5 -3
  114. package/umd/Core/Utils/Vector.js +7 -2
  115. package/umd/Core/Utils/Vector3d.js +14 -9
  116. package/umd/Options/Classes/ManualParticle.js +3 -2
  117. package/umd/Options/Classes/Options.js +3 -0
  118. package/umd/Utils/CanvasUtils.js +36 -26
  119. package/umd/Utils/ColorUtils.js +127 -46
  120. package/umd/Utils/EventDispatcher.js +6 -5
  121. package/umd/Utils/HslColorManager.js +5 -5
  122. package/umd/Utils/NumberUtils.js +38 -25
  123. package/umd/Utils/RgbColorManager.js +5 -5
  124. package/umd/Utils/Utils.js +104 -20
@@ -12,6 +12,11 @@
12
12
  exports.Vector3d = void 0;
13
13
  const Constants_js_1 = require("./Constants.js");
14
14
  const Utils_js_1 = require("../../Utils/Utils.js");
15
+ const origin = {
16
+ x: 0,
17
+ y: 0,
18
+ z: 0,
19
+ }, squareExp = 2, inverseFactorNumerator = 1.0;
15
20
  class Vector3d {
16
21
  constructor(xOrCoords, y, z) {
17
22
  this._updateFromAngle = (angle, length) => {
@@ -22,19 +27,19 @@
22
27
  this.x = xOrCoords.x;
23
28
  this.y = xOrCoords.y;
24
29
  const coords3d = xOrCoords;
25
- this.z = coords3d.z ? coords3d.z : 0;
30
+ this.z = coords3d.z ? coords3d.z : origin.z;
26
31
  }
27
32
  else if (xOrCoords !== undefined && y !== undefined) {
28
33
  this.x = xOrCoords;
29
34
  this.y = y;
30
- this.z = z ?? 0;
35
+ this.z = z ?? origin.z;
31
36
  }
32
37
  else {
33
38
  throw new Error(`${Constants_js_1.errorPrefix} Vector3d not initialized correctly`);
34
39
  }
35
40
  }
36
41
  static get origin() {
37
- return Vector3d.create(0, 0, 0);
42
+ return Vector3d.create(origin.x, origin.y, origin.z);
38
43
  }
39
44
  get angle() {
40
45
  return Math.atan2(this.y, this.x);
@@ -80,7 +85,7 @@
80
85
  this.z /= n;
81
86
  }
82
87
  getLengthSq() {
83
- return this.x ** 2 + this.y ** 2;
88
+ return this.x ** squareExp + this.y ** squareExp;
84
89
  }
85
90
  mult(n) {
86
91
  return Vector3d.create(this.x * n, this.y * n, this.z * n);
@@ -91,19 +96,19 @@
91
96
  this.z *= n;
92
97
  }
93
98
  normalize() {
94
- const length = this.length;
95
- if (length != 0) {
96
- this.multTo(1.0 / length);
99
+ const length = this.length, noLength = 0;
100
+ if (length != noLength) {
101
+ this.multTo(inverseFactorNumerator / length);
97
102
  }
98
103
  }
99
104
  rotate(angle) {
100
- return Vector3d.create(this.x * Math.cos(angle) - this.y * Math.sin(angle), this.x * Math.sin(angle) + this.y * Math.cos(angle), 0);
105
+ return Vector3d.create(this.x * Math.cos(angle) - this.y * Math.sin(angle), this.x * Math.sin(angle) + this.y * Math.cos(angle), origin.z);
101
106
  }
102
107
  setTo(c) {
103
108
  this.x = c.x;
104
109
  this.y = c.y;
105
110
  const v3d = c;
106
- this.z = v3d.z ? v3d.z : 0;
111
+ this.z = v3d.z ? v3d.z : origin.z;
107
112
  }
108
113
  sub(v) {
109
114
  return Vector3d.create(this.x - v.x, this.y - v.y, this.z - v.z);
@@ -11,6 +11,7 @@
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.ManualParticle = void 0;
13
13
  const Utils_js_1 = require("../../Utils/Utils.js");
14
+ const defaultPosition = 50;
14
15
  class ManualParticle {
15
16
  load(data) {
16
17
  if (!data) {
@@ -18,8 +19,8 @@
18
19
  }
19
20
  if (data.position) {
20
21
  this.position = {
21
- x: data.position.x ?? 50,
22
- y: data.position.y ?? 50,
22
+ x: data.position.x ?? defaultPosition,
23
+ y: data.position.y ?? defaultPosition,
23
24
  mode: data.position.mode ?? "percent",
24
25
  };
25
26
  }
@@ -65,6 +65,9 @@
65
65
  if (data.clear !== undefined) {
66
66
  this.clear = data.clear;
67
67
  }
68
+ if (data.key !== undefined) {
69
+ this.key = data.key;
70
+ }
68
71
  if (data.name !== undefined) {
69
72
  this.name = data.name;
70
73
  }
@@ -11,6 +11,7 @@
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.alterHsl = exports.drawParticlePlugin = exports.drawPlugin = exports.drawShapeAfterDraw = exports.drawShape = exports.drawEffect = exports.drawParticle = exports.clear = exports.paintImage = exports.paintBase = exports.drawLine = void 0;
13
13
  const ColorUtils_js_1 = require("./ColorUtils.js");
14
+ const origin = { x: 0, y: 0 };
14
15
  function drawLine(context, begin, end) {
15
16
  context.beginPath();
16
17
  context.moveTo(begin.x, begin.y);
@@ -20,7 +21,7 @@
20
21
  exports.drawLine = drawLine;
21
22
  function paintBase(context, dimension, baseColor) {
22
23
  context.fillStyle = baseColor ?? "rgba(0,0,0,0)";
23
- context.fillRect(0, 0, dimension.width, dimension.height);
24
+ context.fillRect(origin.x, origin.y, dimension.width, dimension.height);
24
25
  }
25
26
  exports.paintBase = paintBase;
26
27
  function paintImage(context, dimension, image, opacity) {
@@ -28,24 +29,23 @@
28
29
  return;
29
30
  }
30
31
  context.globalAlpha = opacity;
31
- context.drawImage(image, 0, 0, dimension.width, dimension.height);
32
+ context.drawImage(image, origin.x, origin.y, dimension.width, dimension.height);
32
33
  context.globalAlpha = 1;
33
34
  }
34
35
  exports.paintImage = paintImage;
35
36
  function clear(context, dimension) {
36
- context.clearRect(0, 0, dimension.width, dimension.height);
37
+ context.clearRect(origin.x, origin.y, dimension.width, dimension.height);
37
38
  }
38
39
  exports.clear = clear;
39
40
  function drawParticle(data) {
40
- const { container, context, particle, delta, colorStyles, backgroundMask, composite, radius, opacity, shadow, transform, } = data;
41
- const pos = particle.getPosition(), angle = particle.rotation + (particle.pathRotation ? particle.velocity.angle : 0), rotateData = {
41
+ const { container, context, particle, delta, colorStyles, backgroundMask, composite, radius, opacity, shadow, transform, } = data, pos = particle.getPosition(), defaultAngle = 0, angle = particle.rotation + (particle.pathRotation ? particle.velocity.angle : defaultAngle), rotateData = {
42
42
  sin: Math.sin(angle),
43
43
  cos: Math.cos(angle),
44
- }, transformData = {
45
- a: rotateData.cos * (transform.a ?? 1),
46
- b: rotateData.sin * (transform.b ?? 1),
47
- c: -rotateData.sin * (transform.c ?? 1),
48
- d: rotateData.cos * (transform.d ?? 1),
44
+ }, defaultTransformFactor = 1, transformData = {
45
+ a: rotateData.cos * (transform.a ?? defaultTransformFactor),
46
+ b: rotateData.sin * (transform.b ?? defaultTransformFactor),
47
+ c: -rotateData.sin * (transform.c ?? defaultTransformFactor),
48
+ d: rotateData.cos * (transform.d ?? defaultTransformFactor),
49
49
  };
50
50
  context.setTransform(transformData.a, transformData.b, transformData.c, transformData.d, pos.x, pos.y);
51
51
  if (backgroundMask) {
@@ -61,27 +61,26 @@
61
61
  if (colorStyles.fill) {
62
62
  context.fillStyle = colorStyles.fill;
63
63
  }
64
- const strokeWidth = particle.strokeWidth ?? 0;
64
+ const minStrokeWidth = 0, strokeWidth = particle.strokeWidth ?? minStrokeWidth;
65
65
  context.lineWidth = strokeWidth;
66
66
  if (colorStyles.stroke) {
67
67
  context.strokeStyle = colorStyles.stroke;
68
68
  }
69
- const drawData = { container, context, particle, radius, opacity, delta, transformData };
70
- context.beginPath();
69
+ const drawData = {
70
+ container,
71
+ context,
72
+ particle,
73
+ radius,
74
+ opacity,
75
+ delta,
76
+ transformData,
77
+ strokeWidth,
78
+ };
71
79
  drawShape(drawData);
72
- if (particle.shapeClose) {
73
- context.closePath();
74
- }
75
- if (strokeWidth > 0) {
76
- context.stroke();
77
- }
78
- if (particle.shapeFill) {
79
- context.fill();
80
- }
81
80
  drawShapeAfterDraw(drawData);
82
81
  drawEffect(drawData);
83
82
  context.globalCompositeOperation = "source-over";
84
- context.setTransform(1, 0, 0, 1, 0, 0);
83
+ context.resetTransform();
85
84
  }
86
85
  exports.drawParticle = drawParticle;
87
86
  function drawEffect(data) {
@@ -105,7 +104,7 @@
105
104
  }
106
105
  exports.drawEffect = drawEffect;
107
106
  function drawShape(data) {
108
- const { container, context, particle, radius, opacity, delta, transformData } = data;
107
+ const { container, context, particle, radius, opacity, delta, strokeWidth, transformData } = data, minStrokeWidth = 0;
109
108
  if (!particle.shape) {
110
109
  return;
111
110
  }
@@ -113,6 +112,7 @@
113
112
  if (!drawer) {
114
113
  return;
115
114
  }
115
+ context.beginPath();
116
116
  drawer.draw({
117
117
  context,
118
118
  particle,
@@ -122,6 +122,15 @@
122
122
  pixelRatio: container.retina.pixelRatio,
123
123
  transformData: { ...transformData },
124
124
  });
125
+ if (particle.shapeClose) {
126
+ context.closePath();
127
+ }
128
+ if (strokeWidth > minStrokeWidth) {
129
+ context.stroke();
130
+ }
131
+ if (particle.shapeFill) {
132
+ context.fill();
133
+ }
125
134
  }
126
135
  exports.drawShape = drawShape;
127
136
  function drawShapeAfterDraw(data) {
@@ -130,7 +139,7 @@
130
139
  return;
131
140
  }
132
141
  const drawer = container.shapeDrawers.get(particle.shape);
133
- if (!drawer || !drawer.afterDraw) {
142
+ if (!drawer?.afterDraw) {
134
143
  return;
135
144
  }
136
145
  drawer.afterDraw({
@@ -159,10 +168,11 @@
159
168
  }
160
169
  exports.drawParticlePlugin = drawParticlePlugin;
161
170
  function alterHsl(color, type, value) {
171
+ const lFactor = 1;
162
172
  return {
163
173
  h: color.h,
164
174
  s: color.s,
165
- l: color.l + (type === "darken" ? -1 : 1) * value,
175
+ l: color.l + (type === "darken" ? -lFactor : lFactor) * value,
166
176
  };
167
177
  }
168
178
  exports.alterHsl = alterHsl;
@@ -4,14 +4,15 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./NumberUtils.js", "./Utils.js"], factory);
7
+ define(["require", "exports", "./NumberUtils.js", "./Utils.js", "../Core/Utils/Constants.js"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getHslAnimationFromHsl = exports.getHslFromAnimation = exports.getLinkRandomColor = exports.getLinkColor = exports.colorMix = exports.getStyleFromHsl = exports.getStyleFromRgb = exports.getRandomRgbColor = exports.hslaToRgba = exports.hslToRgb = exports.stringToRgb = exports.stringToAlpha = exports.rgbToHsl = exports.rangeColorToHsl = exports.colorToHsl = exports.colorToRgb = exports.rangeColorToRgb = exports.addColorManager = void 0;
12
+ exports.updateColor = exports.updateColorValue = exports.getHslAnimationFromHsl = exports.getHslFromAnimation = exports.getLinkRandomColor = exports.getLinkColor = exports.colorMix = exports.getStyleFromHsl = exports.getStyleFromRgb = exports.getRandomRgbColor = exports.hslaToRgba = exports.hslToRgb = exports.stringToRgb = exports.stringToAlpha = exports.rgbToHsl = exports.rangeColorToHsl = exports.colorToHsl = exports.colorToRgb = exports.rangeColorToRgb = exports.addColorManager = void 0;
13
13
  const NumberUtils_js_1 = require("./NumberUtils.js");
14
14
  const Utils_js_1 = require("./Utils.js");
15
+ const Constants_js_1 = require("../Core/Utils/Constants.js");
15
16
  const randomColorValue = "random", midColorValue = "mid", colorManagers = new Map();
16
17
  function addColorManager(manager) {
17
18
  colorManagers.set(manager.key, manager);
@@ -25,13 +26,15 @@
25
26
  }
26
27
  const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])([a-f\d])?$/i, hexFixed = input.replace(shorthandRegex, (_, r, g, b, a) => {
27
28
  return r + r + g + g + b + b + (a !== undefined ? a + a : "");
28
- }), regex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i, result = regex.exec(hexFixed);
29
+ }), regex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i, result = regex.exec(hexFixed), radix = 16, defaultAlpha = 1, alphaFactor = 0xff;
29
30
  return result
30
31
  ? {
31
- a: result[4] !== undefined ? parseInt(result[4], 16) / 0xff : 1,
32
- b: parseInt(result[3], 16),
33
- g: parseInt(result[2], 16),
34
- r: parseInt(result[1], 16),
32
+ a: result[4] !== undefined
33
+ ? parseInt(result[4], radix) / alphaFactor
34
+ : defaultAlpha,
35
+ b: parseInt(result[3], radix),
36
+ g: parseInt(result[2], radix),
37
+ r: parseInt(result[1], radix),
35
38
  }
36
39
  : undefined;
37
40
  }
@@ -88,26 +91,26 @@
88
91
  }
89
92
  exports.rangeColorToHsl = rangeColorToHsl;
90
93
  function rgbToHsl(color) {
91
- const r1 = color.r / 255, g1 = color.g / 255, b1 = color.b / 255, max = Math.max(r1, g1, b1), min = Math.min(r1, g1, b1), res = {
92
- h: 0,
93
- l: (max + min) * 0.5,
94
- s: 0,
94
+ const rgbMax = 255, hMax = 360, sMax = 100, lMax = 100, hMin = 0, sMin = 0, hPhase = 60, half = 0.5, double = 2, r1 = color.r / rgbMax, g1 = color.g / rgbMax, b1 = color.b / rgbMax, max = Math.max(r1, g1, b1), min = Math.min(r1, g1, b1), res = {
95
+ h: hMin,
96
+ l: (max + min) * half,
97
+ s: sMin,
95
98
  };
96
99
  if (max !== min) {
97
- res.s = res.l < 0.5 ? (max - min) / (max + min) : (max - min) / (2.0 - max - min);
100
+ res.s = res.l < half ? (max - min) / (max + min) : (max - min) / (double - max - min);
98
101
  res.h =
99
102
  r1 === max
100
103
  ? (g1 - b1) / (max - min)
101
- : (res.h = g1 === max ? 2.0 + (b1 - r1) / (max - min) : 4.0 + (r1 - g1) / (max - min));
104
+ : (res.h = g1 === max ? double + (b1 - r1) / (max - min) : double * double + (r1 - g1) / (max - min));
102
105
  }
103
- res.l *= 100;
104
- res.s *= 100;
105
- res.h *= 60;
106
- if (res.h < 0) {
107
- res.h += 360;
106
+ res.l *= lMax;
107
+ res.s *= sMax;
108
+ res.h *= hPhase;
109
+ if (res.h < hMin) {
110
+ res.h += hMax;
108
111
  }
109
- if (res.h >= 360) {
110
- res.h -= 360;
112
+ if (res.h >= hMax) {
113
+ res.h -= hMax;
111
114
  }
112
115
  return res;
113
116
  }
@@ -121,29 +124,33 @@
121
124
  }
122
125
  exports.stringToRgb = stringToRgb;
123
126
  function hslToRgb(hsl) {
124
- const h = ((hsl.h % 360) + 360) % 360, s = Math.max(0, Math.min(100, hsl.s)), l = Math.max(0, Math.min(100, hsl.l)), hNormalized = h / 360, sNormalized = s / 100, lNormalized = l / 100;
125
- if (s === 0) {
126
- const grayscaleValue = Math.round(lNormalized * 255);
127
+ const hMax = 360, sMax = 100, lMax = 100, sMin = 0, lMin = 0, 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, rgbFactor = 255, triple = 3;
128
+ if (s === sMin) {
129
+ const grayscaleValue = Math.round(lNormalized * rgbFactor);
127
130
  return { r: grayscaleValue, g: grayscaleValue, b: grayscaleValue };
128
131
  }
129
- const channel = (temp1, temp2, temp3) => {
130
- if (temp3 < 0) {
131
- temp3 += 1;
132
+ const half = 0.5, double = 2, channel = (temp1, temp2, temp3) => {
133
+ const temp3Min = 0, temp3Max = 1, sextuple = 6;
134
+ if (temp3 < temp3Min) {
135
+ temp3++;
132
136
  }
133
- if (temp3 > 1) {
134
- temp3 -= 1;
137
+ if (temp3 > temp3Max) {
138
+ temp3--;
135
139
  }
136
- if (temp3 * 6 < 1) {
137
- return temp1 + (temp2 - temp1) * 6 * temp3;
140
+ if (temp3 * sextuple < temp3Max) {
141
+ return temp1 + (temp2 - temp1) * sextuple * temp3;
138
142
  }
139
- if (temp3 * 2 < 1) {
143
+ if (temp3 * double < temp3Max) {
140
144
  return temp2;
141
145
  }
142
- if (temp3 * 3 < 2) {
143
- return temp1 + (temp2 - temp1) * (2 / 3 - temp3) * 6;
146
+ if (temp3 * triple < temp3Max * double) {
147
+ const temp3Offset = double / triple;
148
+ return temp1 + (temp2 - temp1) * (temp3Offset - temp3) * sextuple;
144
149
  }
145
150
  return temp1;
146
- }, temp1 = lNormalized < 0.5 ? lNormalized * (1 + sNormalized) : lNormalized + sNormalized - lNormalized * sNormalized, temp2 = 2 * lNormalized - temp1, red = Math.min(255, 255 * channel(temp2, temp1, hNormalized + 1 / 3)), green = Math.min(255, 255 * channel(temp2, temp1, hNormalized)), blue = Math.min(255, 255 * channel(temp2, temp1, hNormalized - 1 / 3));
151
+ }, sNormalizedOffset = 1, temp1 = lNormalized < half
152
+ ? lNormalized * (sNormalizedOffset + sNormalized)
153
+ : lNormalized + sNormalized - lNormalized * sNormalized, temp2 = double * lNormalized - temp1, phaseNumerator = 1, phaseThird = phaseNumerator / triple, red = Math.min(rgbFactor, rgbFactor * channel(temp2, temp1, hNormalized + phaseThird)), green = Math.min(rgbFactor, rgbFactor * channel(temp2, temp1, hNormalized)), blue = Math.min(rgbFactor, rgbFactor * channel(temp2, temp1, hNormalized - phaseThird));
147
154
  return { r: Math.round(red), g: Math.round(green), b: Math.round(blue) };
148
155
  }
149
156
  exports.hslToRgb = hslToRgb;
@@ -158,20 +165,22 @@
158
165
  }
159
166
  exports.hslaToRgba = hslaToRgba;
160
167
  function getRandomRgbColor(min) {
161
- const fixedMin = min ?? 0;
168
+ const defaultMin = 0, fixedMin = min ?? defaultMin, rgbMax = 256;
162
169
  return {
163
- b: Math.floor((0, NumberUtils_js_1.randomInRange)((0, NumberUtils_js_1.setRangeValue)(fixedMin, 256))),
164
- g: Math.floor((0, NumberUtils_js_1.randomInRange)((0, NumberUtils_js_1.setRangeValue)(fixedMin, 256))),
165
- r: Math.floor((0, NumberUtils_js_1.randomInRange)((0, NumberUtils_js_1.setRangeValue)(fixedMin, 256))),
170
+ b: Math.floor((0, NumberUtils_js_1.randomInRange)((0, NumberUtils_js_1.setRangeValue)(fixedMin, rgbMax))),
171
+ g: Math.floor((0, NumberUtils_js_1.randomInRange)((0, NumberUtils_js_1.setRangeValue)(fixedMin, rgbMax))),
172
+ r: Math.floor((0, NumberUtils_js_1.randomInRange)((0, NumberUtils_js_1.setRangeValue)(fixedMin, rgbMax))),
166
173
  };
167
174
  }
168
175
  exports.getRandomRgbColor = getRandomRgbColor;
169
176
  function getStyleFromRgb(color, opacity) {
170
- return `rgba(${color.r}, ${color.g}, ${color.b}, ${opacity ?? 1})`;
177
+ const defaultOpacity = 1;
178
+ return `rgba(${color.r}, ${color.g}, ${color.b}, ${opacity ?? defaultOpacity})`;
171
179
  }
172
180
  exports.getStyleFromRgb = getStyleFromRgb;
173
181
  function getStyleFromHsl(color, opacity) {
174
- return `hsla(${color.h}, ${color.s}%, ${color.l}%, ${opacity ?? 1})`;
182
+ const defaultOpacity = 1;
183
+ return `hsla(${color.h}, ${color.s}%, ${color.l}%, ${opacity ?? defaultOpacity})`;
175
184
  }
176
185
  exports.getStyleFromHsl = getStyleFromHsl;
177
186
  function colorMix(color1, color2, size1, size2) {
@@ -268,22 +277,94 @@
268
277
  exports.getHslAnimationFromHsl = getHslAnimationFromHsl;
269
278
  function setColorAnimation(colorValue, colorAnimation, reduceFactor) {
270
279
  colorValue.enable = colorAnimation.enable;
280
+ const defaultVelocity = 0, decayOffset = 1, defaultLoops = 0, defaultTime = 0;
271
281
  if (colorValue.enable) {
272
- colorValue.velocity = ((0, NumberUtils_js_1.getRangeValue)(colorAnimation.speed) / 100) * reduceFactor;
273
- colorValue.decay = 1 - (0, NumberUtils_js_1.getRangeValue)(colorAnimation.decay);
282
+ colorValue.velocity = ((0, NumberUtils_js_1.getRangeValue)(colorAnimation.speed) / Constants_js_1.percentDenominator) * reduceFactor;
283
+ colorValue.decay = decayOffset - (0, NumberUtils_js_1.getRangeValue)(colorAnimation.decay);
274
284
  colorValue.status = "increasing";
275
- colorValue.loops = 0;
285
+ colorValue.loops = defaultLoops;
276
286
  colorValue.maxLoops = (0, NumberUtils_js_1.getRangeValue)(colorAnimation.count);
277
- colorValue.time = 0;
278
- colorValue.delayTime = (0, NumberUtils_js_1.getRangeValue)(colorAnimation.delay) * 1000;
287
+ colorValue.time = defaultTime;
288
+ colorValue.delayTime = (0, NumberUtils_js_1.getRangeValue)(colorAnimation.delay) * Constants_js_1.millisecondsToSeconds;
279
289
  if (!colorAnimation.sync) {
280
290
  colorValue.velocity *= (0, NumberUtils_js_1.getRandom)();
281
291
  colorValue.value *= (0, NumberUtils_js_1.getRandom)();
282
292
  }
283
293
  colorValue.initialValue = colorValue.value;
294
+ colorValue.offset = (0, NumberUtils_js_1.setRangeValue)(colorAnimation.offset);
284
295
  }
285
296
  else {
286
- colorValue.velocity = 0;
297
+ colorValue.velocity = defaultVelocity;
287
298
  }
288
299
  }
300
+ function updateColorValue(data, range, decrease, delta) {
301
+ const minLoops = 0, minDelay = 0, identity = 1, minVelocity = 0, minOffset = 0, velocityFactor = 3.6;
302
+ if (!data ||
303
+ !data.enable ||
304
+ ((data.maxLoops ?? minLoops) > minLoops && (data.loops ?? minLoops) > (data.maxLoops ?? minLoops))) {
305
+ return;
306
+ }
307
+ if (!data.time) {
308
+ data.time = 0;
309
+ }
310
+ if ((data.delayTime ?? minDelay) > minDelay && data.time < (data.delayTime ?? minDelay)) {
311
+ data.time += delta.value;
312
+ }
313
+ if ((data.delayTime ?? minDelay) > minDelay && data.time < (data.delayTime ?? minDelay)) {
314
+ return;
315
+ }
316
+ const offset = data.offset ? (0, NumberUtils_js_1.randomInRange)(data.offset) : minOffset, velocity = (data.velocity ?? minVelocity) * delta.factor + offset * velocityFactor, decay = data.decay ?? identity, max = (0, NumberUtils_js_1.getRangeMax)(range), min = (0, NumberUtils_js_1.getRangeMin)(range);
317
+ if (!decrease || data.status === "increasing") {
318
+ data.value += velocity;
319
+ if (data.value > max) {
320
+ if (!data.loops) {
321
+ data.loops = 0;
322
+ }
323
+ data.loops++;
324
+ if (decrease) {
325
+ data.status = "decreasing";
326
+ }
327
+ else {
328
+ data.value -= max;
329
+ }
330
+ }
331
+ }
332
+ else {
333
+ data.value -= velocity;
334
+ const minValue = 0;
335
+ if (data.value < minValue) {
336
+ if (!data.loops) {
337
+ data.loops = 0;
338
+ }
339
+ data.loops++;
340
+ data.status = "increasing";
341
+ }
342
+ }
343
+ if (data.velocity && decay !== identity) {
344
+ data.velocity *= decay;
345
+ }
346
+ data.value = (0, NumberUtils_js_1.clamp)(data.value, min, max);
347
+ }
348
+ exports.updateColorValue = updateColorValue;
349
+ function updateColor(color, delta) {
350
+ if (!color) {
351
+ return;
352
+ }
353
+ const { h, s, l } = color;
354
+ const ranges = {
355
+ h: { min: 0, max: 360 },
356
+ s: { min: 0, max: 100 },
357
+ l: { min: 0, max: 100 },
358
+ };
359
+ if (h) {
360
+ updateColorValue(h, ranges.h, false, delta);
361
+ }
362
+ if (s) {
363
+ updateColorValue(s, ranges.s, true, delta);
364
+ }
365
+ if (l) {
366
+ updateColorValue(l, ranges.l, true, delta);
367
+ }
368
+ }
369
+ exports.updateColor = updateColor;
289
370
  });
@@ -25,7 +25,7 @@
25
25
  }
26
26
  dispatchEvent(type, args) {
27
27
  const listeners = this._listeners.get(type);
28
- listeners && listeners.forEach((handler) => handler(args));
28
+ listeners?.forEach((handler) => handler(args));
29
29
  }
30
30
  hasEventListener(type) {
31
31
  return !!this._listeners.get(type);
@@ -43,15 +43,16 @@
43
43
  if (!arr) {
44
44
  return;
45
45
  }
46
- const length = arr.length, idx = arr.indexOf(listener);
47
- if (idx < 0) {
46
+ const length = arr.length, idx = arr.indexOf(listener), minIndex = 0;
47
+ if (idx < minIndex) {
48
48
  return;
49
49
  }
50
- if (length === 1) {
50
+ const deleteCount = 1;
51
+ if (length === deleteCount) {
51
52
  this._listeners.delete(type);
52
53
  }
53
54
  else {
54
- arr.splice(idx, 1);
55
+ arr.splice(idx, deleteCount);
55
56
  }
56
57
  }
57
58
  }
@@ -37,13 +37,13 @@
37
37
  if (!input.startsWith("hsl")) {
38
38
  return;
39
39
  }
40
- const regex = /hsla?\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*(,\s*([\d.%]+)\s*)?\)/i, result = regex.exec(input);
40
+ const regex = /hsla?\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*(,\s*([\d.%]+)\s*)?\)/i, result = regex.exec(input), minLength = 4, defaultAlpha = 1, radix = 10;
41
41
  return result
42
42
  ? (0, ColorUtils_js_1.hslaToRgba)({
43
- a: result.length > 4 ? (0, NumberUtils_js_1.parseAlpha)(result[5]) : 1,
44
- h: parseInt(result[1], 10),
45
- l: parseInt(result[3], 10),
46
- s: parseInt(result[2], 10),
43
+ a: result.length > minLength ? (0, NumberUtils_js_1.parseAlpha)(result[5]) : defaultAlpha,
44
+ h: parseInt(result[1], radix),
45
+ l: parseInt(result[3], radix),
46
+ s: parseInt(result[2], radix),
47
47
  })
48
48
  : undefined;
49
49
  }