Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

Cleanup spring code

Julian Garnier35970ed7fe8a
diff --git a/lib/anime.esm.js b/lib/anime.esm.js
index 6ba8eeb7..c9dabccd 100644
--- a/lib/anime.esm.js
+++ b/lib/anime.esm.js
@@ -767,7 +767,7 @@ const shuffle = items => {
* @param {Number} v
* @return {Number}
*/
-const clampInfinity = v => v === Infinity ? maxValue : v === -Infinity ? -maxValue : v;
+const clampInfinity = v => v === Infinity ? maxValue : v === -Infinity ? -1e12 : v;
/**
* @param {Number} v
@@ -1589,6 +1589,7 @@ function getNodeList(v) {
*/
function parseTargets(targets) {
if (isNil(targets)) return /** @type {TargetsArray} */([]);
+ if (!isBrowser) return /** @type {JSTargetsArray} */(isArr(targets) && targets.flat(Infinity) || [targets]);
if (isArr(targets)) {
const flattened = targets.flat(Infinity);
/** @type {TargetsArray} */
@@ -1629,7 +1630,6 @@ function parseTargets(targets) {
}
return parsed;
}
- if (!isBrowser) return /** @type {JSTargetsArray} */([targets]);
const nodeList = getNodeList(targets);
if (nodeList) return /** @type {DOMTargetsArray} */(Array.from(nodeList));
return /** @type {TargetsArray} */([targets]);
@@ -1770,7 +1770,7 @@ const createDrawableProxy = ($el, start, end) => {
// const v1 = round(+value.slice(0, spaceIndex), precision);
// const v2 = round(+value.slice(spaceIndex + 1), precision);
const scaleFactor = getScaleFactor($scalled);
- const os = v1 * -pathLength * scaleFactor;
+ const os = v1 * -1e3 * scaleFactor;
const d1 = (v2 * pathLength * scaleFactor) + os;
const d2 = (pathLength * scaleFactor +
((v1 === 0 && v2 === 1) || (v1 === 1 && v2 === 0) ? 0 : 10 * scaleFactor) - d1);
@@ -5295,6 +5295,8 @@ const createAnimatable = (targets, parameters) => /** @type {AnimatableObject} *
* Webkit Copyright © 2016 Apple Inc
*/
+const maxSpringParamValue = K * 10;
+
/**
* @typedef {Object} SpringParams
* @property {Number} [mass=1] - Mass, default 1
@@ -5302,7 +5304,6 @@ const createAnimatable = (targets, parameters) => /** @type {AnimatableObject} *
* @property {Number} [damping=10] - Damping, default 10
* @property {Number} [velocity=0] - Initial velocity, default 0
*/
-
class Spring {
/**
* @param {SpringParams} [parameters]
@@ -5314,10 +5315,10 @@ class Spring {
this.maxDuration = 60000; // The maximum allowed spring duration in ms (default 1 min)
this.maxRestSteps = this.restDuration / this.timeStep / K; // How many steps allowed after reaching restThreshold before stopping the duration calculation
this.maxIterations = this.maxDuration / this.timeStep / K; // Calculate the maximum iterations allowed based on maxDuration
- this.m = clamp(setValue(parameters.mass, 1), 0, K);
- this.s = clamp(setValue(parameters.stiffness, 100), 1, K * 10);
- this.d = clamp(setValue(parameters.damping, 10), .1, K);
- this.v = clamp(setValue(parameters.velocity, 0), -K * 10, K * 10);
+ this.m = clamp(setValue(parameters.mass, 1), 0, maxSpringParamValue);
+ this.s = clamp(setValue(parameters.stiffness, 100), 1, maxSpringParamValue);
+ this.d = clamp(setValue(parameters.damping, 10), .1, maxSpringParamValue);
+ this.v = clamp(setValue(parameters.velocity, 0), -1e4, maxSpringParamValue);
this.w0 = 0;
this.zeta = 0;
this.wd = 0;
@@ -5368,7 +5369,7 @@ class Spring {
}
set mass(v) {
- this.m = clamp(setValue(v, 1), 0, K);
+ this.m = clamp(setValue(v, 1), 0, maxSpringParamValue);
this.compute();
}
@@ -5377,7 +5378,7 @@ class Spring {
}
set stiffness(v) {
- this.s = clamp(setValue(v, 100), 1, K * 10);
+ this.s = clamp(setValue(v, 100), 1, maxSpringParamValue);
this.compute();
}
@@ -5386,7 +5387,7 @@ class Spring {
}
set damping(v) {
- this.d = clamp(setValue(v, 10), .1, K);
+ this.d = clamp(setValue(v, 10), .1, maxSpringParamValue);
this.compute();
}
@@ -5395,7 +5396,7 @@ class Spring {
}
set velocity(v) {
- this.v = clamp(setValue(v, 0), -K * 10, K * 10);
+ this.v = clamp(setValue(v, 0), -1e4, maxSpringParamValue);
this.compute();
}
}
@@ -5665,7 +5666,7 @@ class Draggable {
/** @type {[Number, Number, Number, Number]} */
this.dragArea = [0, 0, 0, 0]; // x, y, w, h
/** @type {[Number, Number, Number, Number]} */
- this.containerBounds = [-maxValue, maxValue, maxValue, -maxValue]; // t, r, b, l
+ this.containerBounds = [-1e12, maxValue, maxValue, -1e12]; // t, r, b, l
/** @type {[Number, Number, Number, Number]} */
this.scrollBounds = [0, 0, 0, 0]; // t, r, b, l
/** @type {[Number, Number, Number, Number]} */
@@ -6135,10 +6136,10 @@ class Draggable {
const canScroll = this.canScroll;
if (!this.containerArray && this.isOutOfBounds(scrollBounds, x, y)) {
const [ st, sr, sb, sl ] = scrollBounds;
- const t = round(clamp(y - st, -maxValue, 0), 0);
+ const t = round(clamp(y - st, -1e12, 0), 0);
const r = round(clamp(x - sr, 0, maxValue), 0);
const b = round(clamp(y - sb, 0, maxValue), 0);
- const l = round(clamp(x - sl, -maxValue, 0), 0);
+ const l = round(clamp(x - sl, -1e12, 0), 0);
new JSAnimation(scroll, {
x: round(scroll.x + (l ? l - gap : r ? r + gap : 0), 0),
y: round(scroll.y + (t ? t - gap : b ? b + gap : 0), 0),
@@ -7640,7 +7641,7 @@ class ScrollObserver {
if (syncSmooth && isNum(syncSmooth)) {
if (/** @type {Number} */(syncSmooth) < 1) {
const step = 0.0001;
- const snap = lp < p && p === 1 ? step : lp > p && !p ? -step : 0;
+ const snap = lp < p && p === 1 ? step : lp > p && !p ? -1e-4 : 0;
p = round(lerp(lp, p, interpolate(.01, .2, /** @type {Number} */(syncSmooth)), false) + snap, 6);
}
} else if (syncEase) {
@@ -7746,7 +7747,7 @@ const onScroll = (parameters = {}) => new ScrollObserver(parameters);
-const segmenter = !isUnd(Intl) && Intl.Segmenter;
+const segmenter = (typeof Intl !== 'undefined') && Intl.Segmenter;
const valueRgx = /\{value\}/g;
const indexRgx = /\{i\}/g;
const whiteSpaceGroupRgx = /(\s+)/;
diff --git a/src/spring.js b/src/spring.js
index 2e50c5c2..07fe2a21 100644
--- a/src/spring.js
+++ b/src/spring.js
@@ -28,6 +28,8 @@ import {
* Webkit Copyright © 2016 Apple Inc
*/
+const maxSpringParamValue = K * 10;
+
/**
* @typedef {Object} SpringParams
* @property {Number} [mass=1] - Mass, default 1
@@ -35,7 +37,6 @@ import {
* @property {Number} [damping=10] - Damping, default 10
* @property {Number} [velocity=0] - Initial velocity, default 0
*/
-
export class Spring {
/**
* @param {SpringParams} [parameters]
@@ -47,10 +48,10 @@ export class Spring {
this.maxDuration = 60000; // The maximum allowed spring duration in ms (default 1 min)
this.maxRestSteps = this.restDuration / this.timeStep / K; // How many steps allowed after reaching restThreshold before stopping the duration calculation
this.maxIterations = this.maxDuration / this.timeStep / K; // Calculate the maximum iterations allowed based on maxDuration
- this.m = clamp(setValue(parameters.mass, 1), 0, K);
- this.s = clamp(setValue(parameters.stiffness, 100), 1, K * 10);
- this.d = clamp(setValue(parameters.damping, 10), .1, K);
- this.v = clamp(setValue(parameters.velocity, 0), -K * 10, K * 10);
+ this.m = clamp(setValue(parameters.mass, 1), 0, maxSpringParamValue);
+ this.s = clamp(setValue(parameters.stiffness, 100), 1, maxSpringParamValue);
+ this.d = clamp(setValue(parameters.damping, 10), .1, maxSpringParamValue);
+ this.v = clamp(setValue(parameters.velocity, 0), -maxSpringParamValue, maxSpringParamValue);
this.w0 = 0;
this.zeta = 0;
this.wd = 0;
@@ -101,7 +102,7 @@ export class Spring {
}
set mass(v) {
- this.m = clamp(setValue(v, 1), 0, K);
+ this.m = clamp(setValue(v, 1), 0, maxSpringParamValue);
this.compute();
}
@@ -110,7 +111,7 @@ export class Spring {
}
set stiffness(v) {
- this.s = clamp(setValue(v, 100), 1, K * 10);
+ this.s = clamp(setValue(v, 100), 1, maxSpringParamValue);
this.compute();
}
@@ -119,7 +120,7 @@ export class Spring {
}
set damping(v) {
- this.d = clamp(setValue(v, 10), .1, K);
+ this.d = clamp(setValue(v, 10), .1, maxSpringParamValue);
this.compute();
}
@@ -128,7 +129,7 @@ export class Spring {
}
set velocity(v) {
- this.v = clamp(setValue(v, 0), -K * 10, K * 10);
+ this.v = clamp(setValue(v, 0), -maxSpringParamValue, maxSpringParamValue);
this.compute();
}
}
diff --git a/tests/suites/eases.test.js b/tests/suites/eases.test.js
index c3f8b1be..29ff3d5d 100644
--- a/tests/suites/eases.test.js
+++ b/tests/suites/eases.test.js
@@ -214,89 +214,40 @@ suite('Eases', () => {
expect(spring.duration).to.equal(1680);
});
- test('Spring stiffness accepts increased limits up to 10,000', () => {
- const spring1 = createSpring({ stiffness: 5000 });
- expect(spring1.stiffness).to.equal(5000);
- expect(spring1.duration).to.be.above(0);
-
- const spring2 = createSpring({ stiffness: 10000 });
- expect(spring2.stiffness).to.equal(10000);
- expect(spring2.duration).to.be.above(0);
-
- // Test that values above 10,000 are clamped
- const spring3 = createSpring({ stiffness: 15000 });
- expect(spring3.stiffness).to.equal(10000);
-
- // Test setting stiffness after creation
- const spring4 = createSpring();
- spring4.stiffness = 7500;
- expect(spring4.stiffness).to.equal(7500);
- spring4.stiffness = 12000;
- expect(spring4.stiffness).to.equal(10000);
- });
-
- test('Spring velocity accepts increased limits up to ±10,000', () => {
- const spring1 = createSpring({ velocity: 5000 });
- expect(spring1.velocity).to.equal(5000);
- expect(spring1.duration).to.be.above(0);
-
- const spring2 = createSpring({ velocity: -5000 });
- expect(spring2.velocity).to.equal(-5000);
- expect(spring2.duration).to.be.above(0);
-
- const spring3 = createSpring({ velocity: 10000 });
- expect(spring3.velocity).to.equal(10000);
- expect(spring3.duration).to.be.above(0);
-
- const spring4 = createSpring({ velocity: -10000 });
- expect(spring4.velocity).to.equal(-10000);
- expect(spring4.duration).to.be.above(0);
-
- // Test that values above ±10,000 are clamped
- const spring5 = createSpring({ velocity: 15000 });
- expect(spring5.velocity).to.equal(10000);
-
- const spring6 = createSpring({ velocity: -15000 });
- expect(spring6.velocity).to.equal(-10000);
-
- // Test setting velocity after creation
- const spring7 = createSpring();
- spring7.velocity = 7500;
- expect(spring7.velocity).to.equal(7500);
- spring7.velocity = -7500;
- expect(spring7.velocity).to.equal(-7500);
- spring7.velocity = 12000;
- expect(spring7.velocity).to.equal(10000);
- spring7.velocity = -12000;
- expect(spring7.velocity).to.equal(-10000);
+ test('Spring parameters must be clamped at 10000', () => {
+ const spring = createSpring({
+ mass: 15000,
+ stiffness: 15000,
+ damping: 15000,
+ velocity: 15000,
+ });
+ expect(spring.mass).to.equal(10000);
+ expect(spring.stiffness).to.equal(10000);
+ expect(spring.damping).to.equal(10000);
+ expect(spring.velocity).to.equal(10000);
+ expect(spring.duration).to.be.above(0);
+
+ spring.mass = 20000;
+ spring.stiffness = 20000;
+ spring.damping = 20000;
+ spring.velocity = 20000;
+
+ expect(spring.mass).to.equal(10000);
+ expect(spring.stiffness).to.equal(10000);
+ expect(spring.damping).to.equal(10000);
+ expect(spring.velocity).to.equal(10000);
+ expect(spring.duration).to.be.above(0);
});
- test('Spring with high stiffness and velocity values produces valid animations', () => {
- const target = '#target-id';
-
- // Test high stiffness animation
- const highStiffnessAnim = animate(target, {
- opacity: [0, 1],
- ease: createSpring({ stiffness: 8000 }),
- autoplay: false
- });
- expect(highStiffnessAnim.duration).to.be.above(0);
-
- // Test high velocity animation
- const highVelocityAnim = animate(target, {
- opacity: [0, 1],
- ease: createSpring({ velocity: 8000 }),
- autoplay: false
- });
- expect(highVelocityAnim.duration).to.be.above(0);
-
- // Test combined high values
- const combinedHighAnim = animate(target, {
- opacity: [0, 1],
- ease: createSpring({ stiffness: 5000, velocity: 5000 }),
- autoplay: false
+ test('Spring velocity can be negative', () => {
+ const spring = createSpring({
+ velocity: -15000,
});
- expect(combinedHighAnim.duration).to.be.above(0);
+ expect(spring.velocity).to.equal(-10000);
+ expect(spring.duration).to.be.above(0);
+ spring.velocity = -20000;
+ expect(spring.velocity).to.equal(-10000);
+ expect(spring.duration).to.be.above(0);
});
test('Cubic bézier in: "cubicBezier(1,0,1,0)" / eases.cubicBezier(1,0,1,0)', () => {
diff --git a/types/index.js b/types/index.js
index b44b8395..6859b8b8 100644
--- a/types/index.js
+++ b/types/index.js
@@ -1033,6 +1033,8 @@ function getNodeList(v) {
function parseTargets(targets) {
if (isNil(targets))
return /** @type {TargetsArray} */ ([]);
+ if (!isBrowser)
+ return /** @type {JSTargetsArray} */ (isArr(targets) && targets.flat(Infinity) || [targets]);
if (isArr(targets)) {
const flattened = targets.flat(Infinity);
/** @type {TargetsArray} */
@@ -1074,8 +1076,6 @@ function parseTargets(targets) {
}
return parsed;
}
- if (!isBrowser)
- return /** @type {JSTargetsArray} */ ([targets]);
const nodeList = getNodeList(targets);
if (nodeList)
return /** @type {DOMTargetsArray} */ (Array.from(nodeList));
@@ -4480,6 +4480,7 @@ const createAnimatable = (targets, parameters) => /** @type {AnimatableObject} *
* Spring ease solver adapted from https://webkit.org/demos/spring/spring.js
* Webkit Copyright © 2016 Apple Inc
*/
+const maxSpringParamValue = K * 10;
/**
* @typedef {Object} SpringParams
* @property {Number} [mass=1] - Mass, default 1
@@ -4498,10 +4499,10 @@ class Spring {
this.maxDuration = 60000; // The maximum allowed spring duration in ms (default 1 min)
this.maxRestSteps = this.restDuration / this.timeStep / K; // How many steps allowed after reaching restThreshold before stopping the duration calculation
this.maxIterations = this.maxDuration / this.timeStep / K; // Calculate the maximum iterations allowed based on maxDuration
- this.m = clamp(setValue(parameters.mass, 1), 0, K);
- this.s = clamp(setValue(parameters.stiffness, 100), 1, K);
- this.d = clamp(setValue(parameters.damping, 10), .1, K);
- this.v = clamp(setValue(parameters.velocity, 0), -1e3, K);
+ this.m = clamp(setValue(parameters.mass, 1), 0, maxSpringParamValue);
+ this.s = clamp(setValue(parameters.stiffness, 100), 1, maxSpringParamValue);
+ this.d = clamp(setValue(parameters.damping, 10), .1, maxSpringParamValue);
+ this.v = clamp(setValue(parameters.velocity, 0), -1e4, maxSpringParamValue);
this.w0 = 0;
this.zeta = 0;
this.wd = 0;
@@ -4550,28 +4551,28 @@ class Spring {
return this.m;
}
set mass(v) {
- this.m = clamp(setValue(v, 1), 0, K);
+ this.m = clamp(setValue(v, 1), 0, maxSpringParamValue);
this.compute();
}
get stiffness() {
return this.s;
}
set stiffness(v) {
- this.s = clamp(setValue(v, 100), 1, K);
+ this.s = clamp(setValue(v, 100), 1, maxSpringParamValue);
this.compute();
}
get damping() {
return this.d;
}
set damping(v) {
- this.d = clamp(setValue(v, 10), .1, K);
+ this.d = clamp(setValue(v, 10), .1, maxSpringParamValue);
this.compute();
}
get velocity() {
return this.v;
}
set velocity(v) {
- this.v = clamp(setValue(v, 0), -1e3, K);
+ this.v = clamp(setValue(v, 0), -1e4, maxSpringParamValue);
this.compute();
}
}
@@ -6861,7 +6862,7 @@ class ScrollObserver {
const onScroll = (parameters = {}) => new ScrollObserver(parameters);
-const segmenter = !isUnd(Intl) && Intl.Segmenter;
+const segmenter = (typeof Intl !== 'undefined') && Intl.Segmenter;
const valueRgx = /\{value\}/g;
const indexRgx = /\{i\}/g;
const whiteSpaceGroupRgx = /(\s+)/;