animejs 4.1.4 → 4.2.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 (188) hide show
  1. package/README.md +17 -8
  2. package/{lib → dist/bundles}/anime.esm.js +6476 -6106
  3. package/dist/bundles/anime.esm.min.js +7 -0
  4. package/dist/bundles/anime.umd.js +8809 -0
  5. package/dist/bundles/anime.umd.min.js +7 -0
  6. package/dist/modules/animatable/animatable.cjs +150 -0
  7. package/dist/modules/animatable/animatable.d.ts +28 -0
  8. package/dist/modules/animatable/animatable.js +147 -0
  9. package/dist/modules/animatable/index.cjs +15 -0
  10. package/dist/modules/animatable/index.d.ts +1 -0
  11. package/dist/modules/animatable/index.js +8 -0
  12. package/dist/modules/animation/additive.cjs +82 -0
  13. package/dist/modules/animation/additive.d.ts +15 -0
  14. package/dist/modules/animation/additive.js +79 -0
  15. package/dist/modules/animation/animation.cjs +667 -0
  16. package/dist/modules/animation/animation.d.ts +45 -0
  17. package/dist/modules/animation/animation.js +664 -0
  18. package/dist/modules/animation/composition.cjs +383 -0
  19. package/dist/modules/animation/composition.d.ts +10 -0
  20. package/dist/modules/animation/composition.js +377 -0
  21. package/dist/modules/animation/index.cjs +15 -0
  22. package/dist/modules/animation/index.d.ts +1 -0
  23. package/dist/modules/animation/index.js +8 -0
  24. package/dist/modules/core/clock.cjs +110 -0
  25. package/dist/modules/core/clock.d.ts +51 -0
  26. package/dist/modules/core/clock.js +108 -0
  27. package/dist/modules/core/colors.cjs +102 -0
  28. package/dist/modules/core/colors.d.ts +2 -0
  29. package/dist/modules/core/colors.js +100 -0
  30. package/dist/modules/core/consts.cjs +159 -0
  31. package/dist/modules/core/consts.d.ts +61 -0
  32. package/dist/modules/core/consts.js +124 -0
  33. package/dist/modules/core/globals.cjs +78 -0
  34. package/dist/modules/core/globals.d.ts +29 -0
  35. package/dist/modules/core/globals.js +73 -0
  36. package/dist/modules/core/helpers.cjs +304 -0
  37. package/dist/modules/core/helpers.d.ts +43 -0
  38. package/dist/modules/core/helpers.js +261 -0
  39. package/dist/modules/core/render.cjs +391 -0
  40. package/dist/modules/core/render.d.ts +4 -0
  41. package/dist/modules/core/render.js +388 -0
  42. package/dist/modules/core/styles.cjs +116 -0
  43. package/dist/modules/core/styles.d.ts +5 -0
  44. package/dist/modules/core/styles.js +113 -0
  45. package/dist/modules/core/targets.cjs +136 -0
  46. package/dist/modules/core/targets.d.ts +118 -0
  47. package/dist/modules/core/targets.js +132 -0
  48. package/dist/modules/core/transforms.cjs +49 -0
  49. package/dist/modules/core/transforms.d.ts +2 -0
  50. package/dist/modules/core/transforms.js +47 -0
  51. package/dist/modules/core/units.cjs +67 -0
  52. package/dist/modules/core/units.d.ts +3 -0
  53. package/dist/modules/core/units.js +65 -0
  54. package/dist/modules/core/values.cjs +226 -0
  55. package/dist/modules/core/values.d.ts +14 -0
  56. package/dist/modules/core/values.js +216 -0
  57. package/dist/modules/draggable/draggable.cjs +1226 -0
  58. package/dist/modules/draggable/draggable.d.ts +272 -0
  59. package/dist/modules/draggable/draggable.js +1223 -0
  60. package/dist/modules/draggable/index.cjs +15 -0
  61. package/dist/modules/draggable/index.d.ts +1 -0
  62. package/dist/modules/draggable/index.js +8 -0
  63. package/dist/modules/easings/cubic-bezier/index.cjs +64 -0
  64. package/dist/modules/easings/cubic-bezier/index.d.ts +2 -0
  65. package/dist/modules/easings/cubic-bezier/index.js +62 -0
  66. package/dist/modules/easings/eases/index.cjs +14 -0
  67. package/dist/modules/easings/eases/index.d.ts +1 -0
  68. package/dist/modules/easings/eases/index.js +8 -0
  69. package/dist/modules/easings/eases/parser.cjs +191 -0
  70. package/dist/modules/easings/eases/parser.d.ts +125 -0
  71. package/dist/modules/easings/eases/parser.js +185 -0
  72. package/dist/modules/easings/index.cjs +26 -0
  73. package/dist/modules/easings/index.d.ts +6 -0
  74. package/dist/modules/easings/index.js +13 -0
  75. package/dist/modules/easings/irregular/index.cjs +41 -0
  76. package/dist/modules/easings/irregular/index.d.ts +2 -0
  77. package/dist/modules/easings/irregular/index.js +39 -0
  78. package/dist/modules/easings/linear/index.cjs +59 -0
  79. package/dist/modules/easings/linear/index.d.ts +2 -0
  80. package/dist/modules/easings/linear/index.js +57 -0
  81. package/dist/modules/easings/none.cjs +19 -0
  82. package/dist/modules/easings/none.d.ts +8 -0
  83. package/dist/modules/easings/none.js +17 -0
  84. package/dist/modules/easings/spring/index.cjs +255 -0
  85. package/dist/modules/easings/spring/index.d.ts +53 -0
  86. package/dist/modules/easings/spring/index.js +251 -0
  87. package/dist/modules/easings/steps/index.cjs +30 -0
  88. package/dist/modules/easings/steps/index.d.ts +2 -0
  89. package/dist/modules/easings/steps/index.js +28 -0
  90. package/dist/modules/engine/engine.cjs +168 -0
  91. package/dist/modules/engine/engine.d.ts +21 -0
  92. package/dist/modules/engine/engine.js +166 -0
  93. package/dist/modules/engine/index.cjs +14 -0
  94. package/dist/modules/engine/index.d.ts +1 -0
  95. package/dist/modules/engine/index.js +8 -0
  96. package/dist/modules/events/index.cjs +16 -0
  97. package/dist/modules/events/index.d.ts +1 -0
  98. package/dist/modules/events/index.js +8 -0
  99. package/dist/modules/events/scroll.cjs +940 -0
  100. package/dist/modules/events/scroll.d.ts +191 -0
  101. package/dist/modules/events/scroll.js +936 -0
  102. package/dist/modules/index.cjs +102 -0
  103. package/dist/modules/index.d.ts +18 -0
  104. package/dist/modules/index.js +41 -0
  105. package/dist/modules/scope/index.cjs +15 -0
  106. package/dist/modules/scope/index.d.ts +1 -0
  107. package/dist/modules/scope/index.js +8 -0
  108. package/dist/modules/scope/scope.cjs +254 -0
  109. package/dist/modules/scope/scope.d.ts +115 -0
  110. package/dist/modules/scope/scope.js +251 -0
  111. package/dist/modules/svg/drawable.cjs +119 -0
  112. package/dist/modules/svg/drawable.d.ts +3 -0
  113. package/dist/modules/svg/drawable.js +117 -0
  114. package/dist/modules/svg/helpers.cjs +30 -0
  115. package/dist/modules/svg/helpers.d.ts +2 -0
  116. package/dist/modules/svg/helpers.js +28 -0
  117. package/dist/modules/svg/index.cjs +18 -0
  118. package/dist/modules/svg/index.d.ts +3 -0
  119. package/dist/modules/svg/index.js +10 -0
  120. package/dist/modules/svg/morphto.cjs +58 -0
  121. package/dist/modules/svg/morphto.d.ts +3 -0
  122. package/dist/modules/svg/morphto.js +56 -0
  123. package/dist/modules/svg/motionpath.cjs +79 -0
  124. package/dist/modules/svg/motionpath.d.ts +7 -0
  125. package/dist/modules/svg/motionpath.js +77 -0
  126. package/dist/modules/text/index.cjs +16 -0
  127. package/dist/modules/text/index.d.ts +1 -0
  128. package/dist/modules/text/index.js +8 -0
  129. package/dist/modules/text/split.cjs +488 -0
  130. package/dist/modules/text/split.d.ts +62 -0
  131. package/dist/modules/text/split.js +484 -0
  132. package/dist/modules/timeline/index.cjs +15 -0
  133. package/dist/modules/timeline/index.d.ts +1 -0
  134. package/dist/modules/timeline/index.js +8 -0
  135. package/dist/modules/timeline/position.cjs +72 -0
  136. package/dist/modules/timeline/position.d.ts +3 -0
  137. package/dist/modules/timeline/position.js +70 -0
  138. package/dist/modules/timeline/timeline.cjs +312 -0
  139. package/dist/modules/timeline/timeline.d.ts +163 -0
  140. package/dist/modules/timeline/timeline.js +309 -0
  141. package/dist/modules/timer/index.cjs +15 -0
  142. package/dist/modules/timer/index.d.ts +1 -0
  143. package/dist/modules/timer/index.js +8 -0
  144. package/dist/modules/timer/timer.cjs +491 -0
  145. package/dist/modules/timer/timer.d.ts +141 -0
  146. package/dist/modules/timer/timer.js +488 -0
  147. package/dist/modules/types/index.d.ts +404 -0
  148. package/dist/modules/utils/chainable.cjs +190 -0
  149. package/dist/modules/utils/chainable.d.ts +135 -0
  150. package/dist/modules/utils/chainable.js +177 -0
  151. package/dist/modules/utils/index.cjs +43 -0
  152. package/dist/modules/utils/index.d.ts +5 -0
  153. package/dist/modules/utils/index.js +14 -0
  154. package/dist/modules/utils/number.cjs +97 -0
  155. package/dist/modules/utils/number.d.ts +9 -0
  156. package/dist/modules/utils/number.js +85 -0
  157. package/dist/modules/utils/random.cjs +77 -0
  158. package/dist/modules/utils/random.d.ts +22 -0
  159. package/dist/modules/utils/random.js +72 -0
  160. package/dist/modules/utils/stagger.cjs +122 -0
  161. package/dist/modules/utils/stagger.d.ts +30 -0
  162. package/dist/modules/utils/stagger.js +120 -0
  163. package/dist/modules/utils/target.cjs +130 -0
  164. package/dist/modules/utils/target.d.ts +126 -0
  165. package/dist/modules/utils/target.js +125 -0
  166. package/dist/modules/utils/time.cjs +57 -0
  167. package/dist/modules/utils/time.d.ts +5 -0
  168. package/dist/modules/utils/time.js +54 -0
  169. package/dist/modules/waapi/composition.cjs +95 -0
  170. package/dist/modules/waapi/composition.d.ts +4 -0
  171. package/dist/modules/waapi/composition.js +92 -0
  172. package/dist/modules/waapi/index.cjs +15 -0
  173. package/dist/modules/waapi/index.d.ts +1 -0
  174. package/dist/modules/waapi/index.js +8 -0
  175. package/dist/modules/waapi/waapi.cjs +492 -0
  176. package/dist/modules/waapi/waapi.d.ts +116 -0
  177. package/dist/modules/waapi/waapi.js +489 -0
  178. package/package.json +166 -33
  179. package/lib/anime.cjs +0 -9
  180. package/lib/anime.esm.min.js +0 -9
  181. package/lib/anime.iife.js +0 -9
  182. package/lib/anime.iife.min.js +0 -9
  183. package/lib/anime.min.cjs +0 -9
  184. package/lib/anime.umd.js +0 -9
  185. package/lib/anime.umd.min.js +0 -9
  186. package/lib/gui/index.js +0 -6341
  187. package/types/index.d.ts +0 -1127
  188. package/types/index.js +0 -7395
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Anime.js - easings - CJS
3
+ * @version v4.2.0
4
+ * @license MIT
5
+ * @copyright 2025 - Julian Garnier
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ var index = require('./cubic-bezier/index.cjs');
11
+ var index$1 = require('./steps/index.cjs');
12
+ var index$2 = require('./linear/index.cjs');
13
+ var index$3 = require('./irregular/index.cjs');
14
+ var index$4 = require('./spring/index.cjs');
15
+ var parser = require('./eases/parser.cjs');
16
+
17
+
18
+
19
+ exports.cubicBezier = index.cubicBezier;
20
+ exports.steps = index$1.steps;
21
+ exports.linear = index$2.linear;
22
+ exports.irregular = index$3.irregular;
23
+ exports.Spring = index$4.Spring;
24
+ exports.createSpring = index$4.createSpring;
25
+ exports.spring = index$4.spring;
26
+ exports.eases = parser.eases;
@@ -0,0 +1,6 @@
1
+ export * from "./cubic-bezier/index.js";
2
+ export * from "./steps/index.js";
3
+ export * from "./linear/index.js";
4
+ export * from "./irregular/index.js";
5
+ export * from "./spring/index.js";
6
+ export * from "./eases/index.js";
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Anime.js - easings - ESM
3
+ * @version v4.2.0
4
+ * @license MIT
5
+ * @copyright 2025 - Julian Garnier
6
+ */
7
+
8
+ export { cubicBezier } from './cubic-bezier/index.js';
9
+ export { steps } from './steps/index.js';
10
+ export { linear } from './linear/index.js';
11
+ export { irregular } from './irregular/index.js';
12
+ export { Spring, createSpring, spring } from './spring/index.js';
13
+ export { eases } from './eases/parser.js';
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Anime.js - easings - CJS
3
+ * @version v4.2.0
4
+ * @license MIT
5
+ * @copyright 2025 - Julian Garnier
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ var helpers = require('../../core/helpers.cjs');
11
+ var index = require('../linear/index.cjs');
12
+
13
+ /**
14
+ * @import {
15
+ * EasingFunction,
16
+ * } from '../../types/index.js'
17
+ */
18
+
19
+ /**
20
+ * Generate random steps
21
+ * @param {Number} [length] - The number of steps
22
+ * @param {Number} [randomness] - How strong the randomness is
23
+ * @return {EasingFunction}
24
+ */
25
+ const irregular = (length = 10, randomness = 1) => {
26
+ const values = [0];
27
+ const total = length - 1;
28
+ for (let i = 1; i < total; i++) {
29
+ const previousValue = values[i - 1];
30
+ const spacing = i / total;
31
+ const segmentEnd = (i + 1) / total;
32
+ const randomVariation = spacing + (segmentEnd - spacing) * Math.random();
33
+ // Mix the even spacing and random variation based on the randomness parameter
34
+ const randomValue = spacing * (1 - randomness) + randomVariation * randomness;
35
+ values.push(helpers.clamp(randomValue, previousValue, 1));
36
+ }
37
+ values.push(1);
38
+ return index.linear(...values);
39
+ };
40
+
41
+ exports.irregular = irregular;
@@ -0,0 +1,2 @@
1
+ export function irregular(length?: number, randomness?: number): EasingFunction;
2
+ import type { EasingFunction } from '../../types/index.js';
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Anime.js - easings - ESM
3
+ * @version v4.2.0
4
+ * @license MIT
5
+ * @copyright 2025 - Julian Garnier
6
+ */
7
+
8
+ import { clamp } from '../../core/helpers.js';
9
+ import { linear } from '../linear/index.js';
10
+
11
+ /**
12
+ * @import {
13
+ * EasingFunction,
14
+ * } from '../../types/index.js'
15
+ */
16
+
17
+ /**
18
+ * Generate random steps
19
+ * @param {Number} [length] - The number of steps
20
+ * @param {Number} [randomness] - How strong the randomness is
21
+ * @return {EasingFunction}
22
+ */
23
+ const irregular = (length = 10, randomness = 1) => {
24
+ const values = [0];
25
+ const total = length - 1;
26
+ for (let i = 1; i < total; i++) {
27
+ const previousValue = values[i - 1];
28
+ const spacing = i / total;
29
+ const segmentEnd = (i + 1) / total;
30
+ const randomVariation = spacing + (segmentEnd - spacing) * Math.random();
31
+ // Mix the even spacing and random variation based on the randomness parameter
32
+ const randomValue = spacing * (1 - randomness) + randomVariation * randomness;
33
+ values.push(clamp(randomValue, previousValue, 1));
34
+ }
35
+ values.push(1);
36
+ return linear(...values);
37
+ };
38
+
39
+ export { irregular };
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Anime.js - easings - CJS
3
+ * @version v4.2.0
4
+ * @license MIT
5
+ * @copyright 2025 - Julian Garnier
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ var helpers = require('../../core/helpers.cjs');
11
+ var none = require('../none.cjs');
12
+
13
+ /**
14
+ * @import {
15
+ * EasingFunction,
16
+ * } from '../../types/index.js'
17
+ */
18
+
19
+ /**
20
+ * Without parameters, the linear function creates a non-eased transition.
21
+ * Parameters, if used, creates a piecewise linear easing by interpolating linearly between the specified points.
22
+ *
23
+ * @param {...(String|Number)} args - Points
24
+ * @return {EasingFunction}
25
+ */
26
+ const linear = (...args) => {
27
+ const argsLength = args.length;
28
+ if (!argsLength) return none.none;
29
+ const totalPoints = argsLength - 1;
30
+ const firstArg = args[0];
31
+ const lastArg = args[totalPoints];
32
+ const xPoints = [0];
33
+ const yPoints = [helpers.parseNumber(firstArg)];
34
+ for (let i = 1; i < totalPoints; i++) {
35
+ const arg = args[i];
36
+ const splitValue = helpers.isStr(arg) ?
37
+ /** @type {String} */(arg).trim().split(' ') :
38
+ [arg];
39
+ const value = splitValue[0];
40
+ const percent = splitValue[1];
41
+ xPoints.push(!helpers.isUnd(percent) ? helpers.parseNumber(percent) / 100 : i / totalPoints);
42
+ yPoints.push(helpers.parseNumber(value));
43
+ }
44
+ yPoints.push(helpers.parseNumber(lastArg));
45
+ xPoints.push(1);
46
+ return function easeLinear(t) {
47
+ for (let i = 1, l = xPoints.length; i < l; i++) {
48
+ const currentX = xPoints[i];
49
+ if (t <= currentX) {
50
+ const prevX = xPoints[i - 1];
51
+ const prevY = yPoints[i - 1];
52
+ return prevY + (yPoints[i] - prevY) * (t - prevX) / (currentX - prevX);
53
+ }
54
+ }
55
+ return yPoints[yPoints.length - 1];
56
+ }
57
+ };
58
+
59
+ exports.linear = linear;
@@ -0,0 +1,2 @@
1
+ export function linear(...args: (string | number)[]): EasingFunction;
2
+ import type { EasingFunction } from '../../types/index.js';
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Anime.js - easings - ESM
3
+ * @version v4.2.0
4
+ * @license MIT
5
+ * @copyright 2025 - Julian Garnier
6
+ */
7
+
8
+ import { isStr, parseNumber, isUnd } from '../../core/helpers.js';
9
+ import { none } from '../none.js';
10
+
11
+ /**
12
+ * @import {
13
+ * EasingFunction,
14
+ * } from '../../types/index.js'
15
+ */
16
+
17
+ /**
18
+ * Without parameters, the linear function creates a non-eased transition.
19
+ * Parameters, if used, creates a piecewise linear easing by interpolating linearly between the specified points.
20
+ *
21
+ * @param {...(String|Number)} args - Points
22
+ * @return {EasingFunction}
23
+ */
24
+ const linear = (...args) => {
25
+ const argsLength = args.length;
26
+ if (!argsLength) return none;
27
+ const totalPoints = argsLength - 1;
28
+ const firstArg = args[0];
29
+ const lastArg = args[totalPoints];
30
+ const xPoints = [0];
31
+ const yPoints = [parseNumber(firstArg)];
32
+ for (let i = 1; i < totalPoints; i++) {
33
+ const arg = args[i];
34
+ const splitValue = isStr(arg) ?
35
+ /** @type {String} */(arg).trim().split(' ') :
36
+ [arg];
37
+ const value = splitValue[0];
38
+ const percent = splitValue[1];
39
+ xPoints.push(!isUnd(percent) ? parseNumber(percent) / 100 : i / totalPoints);
40
+ yPoints.push(parseNumber(value));
41
+ }
42
+ yPoints.push(parseNumber(lastArg));
43
+ xPoints.push(1);
44
+ return function easeLinear(t) {
45
+ for (let i = 1, l = xPoints.length; i < l; i++) {
46
+ const currentX = xPoints[i];
47
+ if (t <= currentX) {
48
+ const prevX = xPoints[i - 1];
49
+ const prevY = yPoints[i - 1];
50
+ return prevY + (yPoints[i] - prevY) * (t - prevX) / (currentX - prevX);
51
+ }
52
+ }
53
+ return yPoints[yPoints.length - 1];
54
+ }
55
+ };
56
+
57
+ export { linear };
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Anime.js - easings - CJS
3
+ * @version v4.2.0
4
+ * @license MIT
5
+ * @copyright 2025 - Julian Garnier
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ /**
11
+ * @import {
12
+ * EasingFunction,
13
+ * } from '../types/index.js'
14
+ */
15
+
16
+ /** @type {EasingFunction} */
17
+ const none = t => t;
18
+
19
+ exports.none = none;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @import {
3
+ * EasingFunction,
4
+ * } from '../types/index.js'
5
+ */
6
+ /** @type {EasingFunction} */
7
+ export const none: EasingFunction;
8
+ import type { EasingFunction } from '../types/index.js';
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Anime.js - easings - ESM
3
+ * @version v4.2.0
4
+ * @license MIT
5
+ * @copyright 2025 - Julian Garnier
6
+ */
7
+
8
+ /**
9
+ * @import {
10
+ * EasingFunction,
11
+ * } from '../types/index.js'
12
+ */
13
+
14
+ /** @type {EasingFunction} */
15
+ const none = t => t;
16
+
17
+ export { none };
@@ -0,0 +1,255 @@
1
+ /**
2
+ * Anime.js - easings - CJS
3
+ * @version v4.2.0
4
+ * @license MIT
5
+ * @copyright 2025 - Julian Garnier
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ var consts = require('../../core/consts.cjs');
11
+ var globals = require('../../core/globals.cjs');
12
+ var helpers = require('../../core/helpers.cjs');
13
+ var values = require('../../core/values.cjs');
14
+
15
+ /**
16
+ * @import {
17
+ * JSAnimation,
18
+ * } from '../../animation/animation.js'
19
+ */
20
+
21
+ /**
22
+ * @import {
23
+ * EasingFunction,
24
+ * SpringParams,
25
+ * Callback,
26
+ * } from '../../types/index.js'
27
+ */
28
+
29
+ /*
30
+ * Spring easing solver adapted from https://webkit.org/demos/spring/spring.js
31
+ * (c) 2016 Webkit - Apple Inc
32
+ */
33
+
34
+ const maxSpringParamValue = consts.K * 10;
35
+
36
+ class Spring {
37
+ /**
38
+ * @param {SpringParams} [parameters]
39
+ */
40
+ constructor(parameters = {}) {
41
+ const hasBounceOrDuration = !helpers.isUnd(parameters.bounce) || !helpers.isUnd(parameters.duration);
42
+ this.timeStep = .02; // Interval fed to the solver to calculate duration
43
+ this.restThreshold = .0005; // Values below this threshold are considered resting position
44
+ this.restDuration = 200; // Duration in ms used to check if the spring is resting after reaching restThreshold
45
+ this.maxDuration = 60000; // The maximum allowed spring duration in ms (default 1 min)
46
+ this.maxRestSteps = this.restDuration / this.timeStep / consts.K; // How many steps allowed after reaching restThreshold before stopping the duration calculation
47
+ this.maxIterations = this.maxDuration / this.timeStep / consts.K; // Calculate the maximum iterations allowed based on maxDuration
48
+ this.bn = helpers.clamp(values.setValue(parameters.bounce, .5), -1, 1); // The bounce percentage between -1 and 1.
49
+ this.pd = helpers.clamp(values.setValue(parameters.duration, 628), 10 * globals.globals.timeScale, maxSpringParamValue * globals.globals.timeScale); // The perceived duration
50
+ this.m = helpers.clamp(values.setValue(parameters.mass, 1), 1, maxSpringParamValue);
51
+ this.s = helpers.clamp(values.setValue(parameters.stiffness, 100), consts.minValue, maxSpringParamValue);
52
+ this.d = helpers.clamp(values.setValue(parameters.damping, 10), consts.minValue, maxSpringParamValue);
53
+ this.v = helpers.clamp(values.setValue(parameters.velocity, 0), -maxSpringParamValue, maxSpringParamValue);
54
+ this.w0 = 0;
55
+ this.zeta = 0;
56
+ this.wd = 0;
57
+ this.b = 0;
58
+ this.completed = false;
59
+ this.solverDuration = 0;
60
+ this.settlingDuration = 0;
61
+ /** @type {JSAnimation} */
62
+ this.parent = null;
63
+ /** @type {Callback<JSAnimation>} */
64
+ this.onComplete = parameters.onComplete || consts.noop;
65
+ if (hasBounceOrDuration) this.calculateSDFromBD();
66
+ this.compute();
67
+ /** @type {EasingFunction} */
68
+ this.ease = t => {
69
+ const currentTime = t * this.settlingDuration;
70
+ const completed = this.completed;
71
+ const perceivedTime = this.pd;
72
+ if (currentTime >= perceivedTime && !completed) {
73
+ this.completed = true;
74
+ this.onComplete(this.parent);
75
+ }
76
+ if (currentTime < perceivedTime && completed) {
77
+ this.completed = false;
78
+ }
79
+ return t === 0 || t === 1 ? t : this.solve(t * this.solverDuration);
80
+ };
81
+ }
82
+
83
+ /** @type {EasingFunction} */
84
+ solve(time) {
85
+ const { zeta, w0, wd, b } = this;
86
+ let t = time;
87
+ if (zeta < 1) {
88
+ // Underdamped
89
+ t = helpers.exp(-t * zeta * w0) * (1 * helpers.cos(wd * t) + b * helpers.sin(wd * t));
90
+ } else if (zeta === 1) {
91
+ // Critically damped
92
+ t = (1 + b * t) * helpers.exp(-t * w0);
93
+ } else {
94
+ // Overdamped
95
+ // Using exponential instead of cosh and sinh functions to prevent Infinity
96
+ // Original exp(-zeta * w0 * t) * (cosh(wd * t) + b * sinh(wd * t))
97
+ t = ((1 + b) * helpers.exp((-zeta * w0 + wd) * t) + (1 - b) * helpers.exp((-zeta * w0 - wd) * t)) / 2;
98
+ }
99
+ return 1 - t;
100
+ }
101
+
102
+ calculateSDFromBD() {
103
+ // Apple's SwiftUI perceived spring duration implementation https://developer.apple.com/videos/play/wwdc2023/10158/?time=1010
104
+ // Equations taken from Kevin Grajeda's article https://www.kvin.me/posts/effortless-ui-spring-animations
105
+ const pds = globals.globals.timeScale === 1 ? this.pd / consts.K : this.pd;
106
+ // Mass and velocity should be set to their default values
107
+ this.m = 1;
108
+ this.v = 0;
109
+ // Stiffness = (2π ÷ perceptualDuration)²
110
+ this.s = helpers.pow((2 * helpers.PI) / pds, 2);
111
+ if (this.bn >= 0) {
112
+ // For bounce ≥ 0 (critically damped to underdamped)
113
+ // damping = ((1 - bounce) × 4π) ÷ perceptualDuration
114
+ this.d = ((1 - this.bn) * 4 * helpers.PI) / pds;
115
+ } else {
116
+ // For bounce < 0 (overdamped)
117
+ // damping = 4π ÷ (perceptualDuration × (1 + bounce))
118
+ // Note: (1 + bounce) is positive since bounce is negative
119
+ this.d = (4 * helpers.PI) / (pds * (1 + this.bn));
120
+ }
121
+ this.s = helpers.round(helpers.clamp(this.s, consts.minValue, maxSpringParamValue), 3);
122
+ this.d = helpers.round(helpers.clamp(this.d, consts.minValue, 300), 3); // Clamping to 300 is needed to prevent insane values in the solver
123
+ }
124
+
125
+ calculateBDFromSD() {
126
+ // Calculate perceived duration and bounce from stiffness and damping
127
+ // Note: We assumes m = 1 and v = 0 for these calculations
128
+ const pds = (2 * helpers.PI) / helpers.sqrt(this.s);
129
+ this.pd = pds * (globals.globals.timeScale === 1 ? consts.K : 1);
130
+ const zeta = this.d / (2 * helpers.sqrt(this.s));
131
+ if (zeta <= 1) {
132
+ // Critically damped to underdamped
133
+ this.bn = 1 - (this.d * pds) / (4 * helpers.PI);
134
+ } else {
135
+ // Overdamped
136
+ this.bn = (4 * helpers.PI) / (this.d * pds) - 1;
137
+ }
138
+ this.bn = helpers.round(helpers.clamp(this.bn, -1, 1), 3);
139
+ this.pd = helpers.round(helpers.clamp(this.pd, 10 * globals.globals.timeScale, maxSpringParamValue * globals.globals.timeScale), 3);
140
+ }
141
+
142
+ compute() {
143
+ const { maxRestSteps, maxIterations, restThreshold, timeStep, m, d, s, v } = this;
144
+ const w0 = this.w0 = helpers.clamp(helpers.sqrt(s / m), consts.minValue, consts.K);
145
+ const bouncedZeta = this.zeta = d / (2 * helpers.sqrt(s * m));
146
+ // Calculate wd based on damping type
147
+ if (bouncedZeta < 1) {
148
+ // Underdamped
149
+ this.wd = w0 * helpers.sqrt(1 - bouncedZeta * bouncedZeta);
150
+ this.b = (bouncedZeta * w0 + -v) / this.wd;
151
+ } else if (bouncedZeta === 1) {
152
+ // Critically damped
153
+ this.wd = 0;
154
+ this.b = -v + w0;
155
+ } else {
156
+ // Overdamped
157
+ this.wd = w0 * helpers.sqrt(bouncedZeta * bouncedZeta - 1);
158
+ this.b = (bouncedZeta * w0 + -v) / this.wd;
159
+ }
160
+
161
+ let solverTime = 0;
162
+ let restSteps = 0;
163
+ let iterations = 0;
164
+ while (restSteps <= maxRestSteps && iterations <= maxIterations) {
165
+ if (helpers.abs(1 - this.solve(solverTime)) < restThreshold) {
166
+ restSteps++;
167
+ } else {
168
+ restSteps = 0;
169
+ }
170
+ this.solverDuration = solverTime;
171
+ solverTime += timeStep;
172
+ iterations++;
173
+ }
174
+ this.settlingDuration = helpers.round(this.solverDuration * consts.K, 0) * globals.globals.timeScale;
175
+ }
176
+
177
+ get bounce() {
178
+ return this.bn;
179
+ }
180
+
181
+ set bounce(v) {
182
+ this.bn = helpers.clamp(values.setValue(v, 1), -1, 1);
183
+ this.calculateSDFromBD();
184
+ this.compute();
185
+ }
186
+
187
+ get duration() {
188
+ return this.pd;
189
+ }
190
+
191
+ set duration(v) {
192
+ this.pd = helpers.clamp(values.setValue(v, 1), 10 * globals.globals.timeScale, maxSpringParamValue * globals.globals.timeScale);
193
+ this.calculateSDFromBD();
194
+ this.compute();
195
+ }
196
+
197
+ get stiffness() {
198
+ return this.s;
199
+ }
200
+
201
+ set stiffness(v) {
202
+ this.s = helpers.clamp(values.setValue(v, 100), consts.minValue, maxSpringParamValue);
203
+ this.calculateBDFromSD();
204
+ this.compute();
205
+ }
206
+
207
+ get damping() {
208
+ return this.d;
209
+ }
210
+
211
+ set damping(v) {
212
+ this.d = helpers.clamp(values.setValue(v, 10), consts.minValue, maxSpringParamValue);
213
+ this.calculateBDFromSD();
214
+ this.compute();
215
+ }
216
+
217
+ get mass() {
218
+ return this.m;
219
+ }
220
+
221
+ set mass(v) {
222
+ this.m = helpers.clamp(values.setValue(v, 1), 1, maxSpringParamValue);
223
+ this.compute();
224
+ }
225
+
226
+ get velocity() {
227
+ return this.v;
228
+ }
229
+
230
+ set velocity(v) {
231
+ this.v = helpers.clamp(values.setValue(v, 0), -maxSpringParamValue, maxSpringParamValue);
232
+ this.compute();
233
+ }
234
+ }
235
+
236
+ /**
237
+ * @param {SpringParams} [parameters]
238
+ * @returns {Spring}
239
+ */
240
+ const spring = (parameters) => new Spring(parameters);
241
+
242
+ /**
243
+ * @deprecated createSpring() is deprecated use spring() instead
244
+ *
245
+ * @param {SpringParams} [parameters]
246
+ * @returns {Spring}
247
+ */
248
+ const createSpring = (parameters) => {
249
+ console.warn('createSpring() is deprecated use spring() instead');
250
+ return new Spring(parameters);
251
+ };
252
+
253
+ exports.Spring = Spring;
254
+ exports.createSpring = createSpring;
255
+ exports.spring = spring;
@@ -0,0 +1,53 @@
1
+ export class Spring {
2
+ /**
3
+ * @param {SpringParams} [parameters]
4
+ */
5
+ constructor(parameters?: SpringParams);
6
+ timeStep: number;
7
+ restThreshold: number;
8
+ restDuration: number;
9
+ maxDuration: number;
10
+ maxRestSteps: number;
11
+ maxIterations: number;
12
+ bn: number;
13
+ pd: number;
14
+ m: number;
15
+ s: number;
16
+ d: number;
17
+ v: number;
18
+ w0: number;
19
+ zeta: number;
20
+ wd: number;
21
+ b: number;
22
+ completed: boolean;
23
+ solverDuration: number;
24
+ settlingDuration: number;
25
+ /** @type {JSAnimation} */
26
+ parent: JSAnimation;
27
+ /** @type {Callback<JSAnimation>} */
28
+ onComplete: Callback<JSAnimation>;
29
+ /** @type {EasingFunction} */
30
+ ease: EasingFunction;
31
+ solve(time: number): number;
32
+ calculateSDFromBD(): void;
33
+ calculateBDFromSD(): void;
34
+ compute(): void;
35
+ set bounce(v: number);
36
+ get bounce(): number;
37
+ set duration(v: number);
38
+ get duration(): number;
39
+ set stiffness(v: number);
40
+ get stiffness(): number;
41
+ set damping(v: number);
42
+ get damping(): number;
43
+ set mass(v: number);
44
+ get mass(): number;
45
+ set velocity(v: number);
46
+ get velocity(): number;
47
+ }
48
+ export function spring(parameters?: SpringParams): Spring;
49
+ export function createSpring(parameters?: SpringParams): Spring;
50
+ import type { JSAnimation } from '../../animation/animation.js';
51
+ import type { Callback } from '../../types/index.js';
52
+ import type { EasingFunction } from '../../types/index.js';
53
+ import type { SpringParams } from '../../types/index.js';