Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

Increase spring stiffness and velocity limits for realistic physics

李笑林58de3cba4522
diff --git a/lib/anime.esm.js b/lib/anime.esm.js
index 6ce2459a..0ec2d2ac 100644
--- a/lib/anime.esm.js
+++ b/lib/anime.esm.js
@@ -627,7 +627,7 @@ const interpolate = (start, end, progress) => start + (end - start) * progress;
* @param {Number} v
* @return {Number}
*/
-const clampInfinity = v => v === Infinity ? maxValue : v === -Infinity ? -1e12 : v;
+const clampInfinity = v => v === Infinity ? maxValue : v === -Infinity ? -maxValue : v;
/**
* @param {Number} v
@@ -1604,7 +1604,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 * -1e3 * scaleFactor;
+ const os = v1 * -pathLength * scaleFactor;
const d1 = (v2 * pathLength * scaleFactor) + os;
const d2 = (pathLength * scaleFactor +
((v1 === 0 && v2 === 1) || (v1 === 1 && v2 === 0) ? 0 : 10 * scaleFactor) - d1);
@@ -5158,9 +5158,9 @@ class Spring {
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.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), -1e3, K);
+ this.v = clamp(setValue(parameters.velocity, 0), -K * 10, K * 10);
this.w0 = 0;
this.zeta = 0;
this.wd = 0;
@@ -5220,7 +5220,7 @@ class Spring {
}
set stiffness(v) {
- this.s = clamp(setValue(v, 100), 1, K);
+ this.s = clamp(setValue(v, 100), 1, K * 10);
this.compute();
}
@@ -5238,7 +5238,7 @@ class Spring {
}
set velocity(v) {
- this.v = clamp(setValue(v, 0), -1e3, K);
+ this.v = clamp(setValue(v, 0), -K * 10, K * 10);
this.compute();
}
}
@@ -5508,7 +5508,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 = [-1e12, maxValue, maxValue, -1e12]; // t, r, b, l
+ this.containerBounds = [-maxValue, maxValue, maxValue, -maxValue]; // t, r, b, l
/** @type {[Number, Number, Number, Number]} */
this.scrollBounds = [0, 0, 0, 0]; // t, r, b, l
/** @type {[Number, Number, Number, Number]} */
@@ -5977,10 +5977,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, -1e12, 0), 0);
+ const t = round(clamp(y - st, -maxValue, 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, -1e12, 0), 0);
+ const l = round(clamp(x - sl, -maxValue, 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),
@@ -7439,7 +7439,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 ? -1e-4 : 0;
+ const snap = lp < p && p === 1 ? step : lp > p && !p ? -step : 0;
p = round(lerp(lp, p, interpolate(.01, .2, /** @type {Number} */(syncSmooth)), false) + snap, 6);
}
} else if (syncEase) {
diff --git a/rollup.config.js b/rollup.config.js
index 6d331d30..95932555 100644
--- a/rollup.config.js
+++ b/rollup.config.js
@@ -1,8 +1,8 @@
import terser from '@rollup/plugin-terser';
import filesize from 'rollup-plugin-filesize';
import ts from 'rollup-plugin-ts';
-import pkg from './package.json' assert { type: 'json' };
import fs from 'fs';
+const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
import path from 'path';
const inputPath = 'src/anime.js';
diff --git a/src/spring.js b/src/spring.js
index 21bf950b..2e50c5c2 100644
--- a/src/spring.js
+++ b/src/spring.js
@@ -48,9 +48,9 @@ export class Spring {
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.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, K);
+ this.v = clamp(setValue(parameters.velocity, 0), -K * 10, K * 10);
this.w0 = 0;
this.zeta = 0;
this.wd = 0;
@@ -110,7 +110,7 @@ export class Spring {
}
set stiffness(v) {
- this.s = clamp(setValue(v, 100), 1, K);
+ this.s = clamp(setValue(v, 100), 1, K * 10);
this.compute();
}
@@ -128,7 +128,7 @@ export class Spring {
}
set velocity(v) {
- this.v = clamp(setValue(v, 0), -K, K);
+ this.v = clamp(setValue(v, 0), -K * 10, K * 10);
this.compute();
}
}
diff --git a/tests/suites/eases.test.js b/tests/suites/eases.test.js
index f0fc55d9..c3f8b1be 100644
--- a/tests/suites/eases.test.js
+++ b/tests/suites/eases.test.js
@@ -214,6 +214,91 @@ 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 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
+ });
+ expect(combinedHighAnim.duration).to.be.above(0);
+ });
+
test('Cubic bézier in: "cubicBezier(1,0,1,0)" / eases.cubicBezier(1,0,1,0)', () => {
const cubicBezierIn = animate('#target-id', createEasingParam(eases.cubicBezier(1,0,1,0)));
cubicBezierIn.seek(50);