JavaScript animation engine
Fix floating points issue when stretching a timer / animation #1005
Julian Garnier163c9aaeff69
diff --git a/lib/anime.esm.js b/lib/anime.esm.jsindex 60764e6b..d76ac0de 100644--- a/lib/anime.esm.js+++ b/lib/anime.esm.js@@ -531,7 +531,6 @@ const atan2 = Math.atan2;const PI = Math.PI;const _round = Math.round;-/*** @param {Number} v* @param {Number} min@@ -580,7 +579,7 @@ const clampInfinity = v => v === Infinity ? maxValue : v === -Infinity ? -1e12 :* @param {Number} v* @return {Number}*/-const clampZero = v => v < minValue ? minValue : v;+const normalizeTime = v => v <= minValue ? minValue : clampInfinity(round(v, 11));// Arrays@@ -2589,11 +2588,12 @@ class Timer extends Clock {*/stretch(newDuration) {const currentDuration = this.duration;- if (currentDuration === clampZero(newDuration)) return this;+ const normlizedDuration = normalizeTime(newDuration);+ if (currentDuration === normlizedDuration) return this;const timeScale = newDuration / currentDuration;const isSetter = newDuration <= minValue;- this.duration = isSetter ? minValue : clampZero(clampInfinity(round(currentDuration * timeScale, 12)));- this.iterationDuration = isSetter ? minValue : clampZero(clampInfinity(round(this.iterationDuration * timeScale, 12)));+ this.duration = isSetter ? minValue : normlizedDuration;+ this.iterationDuration = isSetter ? minValue : normalizeTime(this.iterationDuration * timeScale);this._offset *= timeScale;this._delay *= timeScale;this._loopDelay *= timeScale;@@ -3637,13 +3637,13 @@ class JSAnimation extends Timer {*/stretch(newDuration) {const currentDuration = this.duration;- if (currentDuration === clampZero(newDuration)) return this;+ if (currentDuration === normalizeTime(newDuration)) return this;const timeScale = newDuration / currentDuration;// NOTE: Find a better way to handle the stretch of an animation after stretch = 0forEachChildren(this, (/** @type {Tween} */tween) => {// Rounding is necessary here to minimize floating point errors- tween._updateDuration = clampZero(round(tween._updateDuration * timeScale, 12));- tween._changeDuration = clampZero(round(tween._changeDuration * timeScale, 12));+ tween._updateDuration = normalizeTime(tween._updateDuration * timeScale);+ tween._changeDuration = normalizeTime(tween._changeDuration * timeScale);tween._currentTime *= timeScale;tween._startTime *= timeScale;tween._absoluteStartTime *= timeScale;@@ -4916,15 +4916,11 @@ class Timeline extends Timer {*/stretch(newDuration) {const currentDuration = this.duration;- if (currentDuration === clampZero(newDuration)) return this;+ if (currentDuration === normalizeTime(newDuration)) return this;const timeScale = newDuration / currentDuration;const labels = this.labels;- forEachChildren(this, (/** @type {JSAnimation} */child) => {- child.stretch(child.duration * timeScale);- });- for (let labelName in labels) {- labels[labelName] *= timeScale;- }+ forEachChildren(this, (/** @type {JSAnimation} */child) => child.stretch(child.duration * timeScale));+ for (let labelName in labels) labels[labelName] *= timeScale;return super.stretch(newDuration);}diff --git a/src/animation.js b/src/animation.jsindex 8c1ae546..a0c83fdd 100644--- a/src/animation.js+++ b/src/animation.js@@ -22,7 +22,7 @@ import {addChild,forEachChildren,clampInfinity,- clampZero,+ normalizeTime,isNum,round,} from './helpers.js';@@ -675,13 +675,13 @@ export class JSAnimation extends Timer {*/stretch(newDuration) {const currentDuration = this.duration;- if (currentDuration === clampZero(newDuration)) return this;+ if (currentDuration === normalizeTime(newDuration)) return this;const timeScale = newDuration / currentDuration;// NOTE: Find a better way to handle the stretch of an animation after stretch = 0forEachChildren(this, (/** @type {Tween} */tween) => {// Rounding is necessary here to minimize floating point errors- tween._updateDuration = clampZero(round(tween._updateDuration * timeScale, 12));- tween._changeDuration = clampZero(round(tween._changeDuration * timeScale, 12));+ tween._updateDuration = normalizeTime(tween._updateDuration * timeScale);+ tween._changeDuration = normalizeTime(tween._changeDuration * timeScale);tween._currentTime *= timeScale;tween._startTime *= timeScale;tween._absoluteStartTime *= timeScale;diff --git a/src/helpers.js b/src/helpers.jsindex f1a2d21c..e3e9c3e0 100644--- a/src/helpers.js+++ b/src/helpers.js@@ -84,7 +84,6 @@ export const atan2 = Math.atan2;export const PI = Math.PI;export const _round = Math.round;-/*** @param {Number} v* @param {Number} min@@ -133,7 +132,7 @@ export const clampInfinity = v => v === Infinity ? maxValue : v === -Infinity ?* @param {Number} v* @return {Number}*/-export const clampZero = v => v < minValue ? minValue : v;+export const normalizeTime = v => v <= minValue ? minValue : clampInfinity(round(v, 11));// Arraysdiff --git a/src/timeline.js b/src/timeline.jsindex 3d6031d4..c17c987d 100644--- a/src/timeline.js+++ b/src/timeline.js@@ -22,8 +22,9 @@ import {stringStartsWith,mergeObjects,clampInfinity,- clampZero,+ normalizeTime,isStr,+ round,} from './helpers.js';import {@@ -327,15 +328,11 @@ export class Timeline extends Timer {*/stretch(newDuration) {const currentDuration = this.duration;- if (currentDuration === clampZero(newDuration)) return this;+ if (currentDuration === normalizeTime(newDuration)) return this;const timeScale = newDuration / currentDuration;const labels = this.labels;- forEachChildren(this, (/** @type {JSAnimation} */child) => {- child.stretch(child.duration * timeScale);- });- for (let labelName in labels) {- labels[labelName] *= timeScale;- }+ forEachChildren(this, (/** @type {JSAnimation} */child) => child.stretch(child.duration * timeScale));+ for (let labelName in labels) labels[labelName] *= timeScale;return super.stretch(newDuration);}diff --git a/src/timer.js b/src/timer.jsindex cbe3a6af..05e453ce 100644--- a/src/timer.js+++ b/src/timer.js@@ -15,7 +15,7 @@ import {forEachChildren,clampInfinity,round,- clampZero,+ normalizeTime,isFnc,clamp,floor,@@ -442,11 +442,12 @@ export class Timer extends Clock {*/stretch(newDuration) {const currentDuration = this.duration;- if (currentDuration === clampZero(newDuration)) return this;+ const normlizedDuration = normalizeTime(newDuration);+ if (currentDuration === normlizedDuration) return this;const timeScale = newDuration / currentDuration;const isSetter = newDuration <= minValue;- this.duration = isSetter ? minValue : clampZero(clampInfinity(round(currentDuration * timeScale, 12)));- this.iterationDuration = isSetter ? minValue : clampZero(clampInfinity(round(this.iterationDuration * timeScale, 12)));+ this.duration = isSetter ? minValue : normlizedDuration;+ this.iterationDuration = isSetter ? minValue : normalizeTime(this.iterationDuration * timeScale);this._offset *= timeScale;this._delay *= timeScale;this._loopDelay *= timeScale;diff --git a/tests/suites/controls.test.js b/tests/suites/controls.test.jsindex 6186cee4..637102e0 100644--- a/tests/suites/controls.test.js+++ b/tests/suites/controls.test.js@@ -642,18 +642,37 @@ suite('Controls', () => {test('Stretch a timer', () => {const timer1 = createTimer({- duration: 100,- autoplay: false+ duration: 1273,+ autoplay: false,+ });+ expect(timer1.duration).to.equal(1273);+ for (let i = 0, l = 9999; i < l; i++) {+ const newTime = 1 + i;+ timer1.stretch(newTime);+ expect(timer1.duration).to.equal(newTime);+ expect(timer1.iterationDuration).to.equal(newTime);+ }+ timer1.stretch(0);+ expect(timer1.duration).to.equal(minValue);+ expect(timer1.iterationDuration).to.equal(minValue);+ });++ test('Stretch a looped timer', () => {+ const timer1 = createTimer({+ duration: 1273,+ autoplay: false,+ loop: 3,});- expect(timer1.duration).to.equal(100);- timer1.stretch(600);- expect(timer1.duration).to.equal(600);- timer1.stretch(30);- expect(timer1.duration).to.equal(30);+ expect(timer1.duration).to.equal(1273 * 4);+ for (let i = 0, l = 9999; i < l; i++) {+ const newTime = 1 + i;+ timer1.stretch(newTime);+ expect(timer1.duration).to.equal(newTime);+ expect(timer1.iterationDuration).to.equal(newTime / timer1.iterationCount);+ }timer1.stretch(0);expect(timer1.duration).to.equal(minValue);- timer1.stretch(30);- expect(timer1.duration).to.equal(30);+ expect(timer1.iterationDuration).to.equal(minValue);});test('Stretch an animation', () => {diff --git a/tests/suites/seconds.test.js b/tests/suites/seconds.test.jsindex 3c66950a..3d697ff1 100644--- a/tests/suites/seconds.test.js+++ b/tests/suites/seconds.test.js@@ -53,28 +53,44 @@ suite('Seconds', () => {});- test('Stretch a timer', () => {-+ test('Stretch a looped timer', () => {engine.timeUnit = 's';-const timer1 = createTimer({duration: .1,- autoplay: false+ autoplay: false,+ loop: 0,});expect(timer1.duration).to.equal(.1);- timer1.stretch(.6);- expect(timer1.duration).to.equal(.6);- timer1.stretch(.3);- expect(timer1.duration).to.equal(.3);+ for (let i = 1, l = 9999; i < l; i++) {+ const newTime = +(i * .1).toFixed(1);+ timer1.stretch(newTime);+ expect(timer1.duration).to.equal(newTime);+ expect(timer1.iterationDuration).to.equal(newTime);+ }timer1.stretch(0);expect(timer1.duration).to.equal(minValue);expect(timer1.iterationDuration).to.equal(minValue);- timer1.stretch(.3);- expect(timer1.duration).to.equal(.3);- expect(timer1.iterationDuration).to.equal(.3);-engine.timeUnit = 'ms';+ });+ test('Stretch a looped timer', () => {+ engine.timeUnit = 's';+ const timer1 = createTimer({+ duration: .1,+ autoplay: false,+ loop: 3,+ });+ expect(timer1.duration).to.equal(.1 * 4);+ for (let i = 1, l = 9999; i < l; i++) {+ const newTime = +(i * .1).toFixed(1);+ timer1.stretch(newTime);+ expect(timer1.duration).to.equal(newTime);+ expect(timer1.iterationDuration).to.equal(newTime / timer1.iterationCount);+ }+ timer1.stretch(0);+ expect(timer1.duration).to.equal(minValue);+ expect(timer1.iterationDuration).to.equal(minValue);+ engine.timeUnit = 'ms';});test('Stretch an animation', () => {diff --git a/types/index.js b/types/index.jsindex 4fc0070e..4df837ac 100644--- a/types/index.js+++ b/types/index.js@@ -253,7 +253,7 @@ const clampInfinity = v => v === Infinity ? maxValue : v === -Infinity ? -1e12 :* @param {Number} v* @return {Number}*/-const clampZero = v => v < minValue ? minValue : v;+const normalizeTime = v => v <= minValue ? minValue : clampInfinity(round(v, 11));// Arrays/*** @template T@@ -2093,12 +2093,13 @@ class Timer extends Clock {*/stretch(newDuration) {const currentDuration = this.duration;- if (currentDuration === clampZero(newDuration))+ const normlizedDuration = normalizeTime(newDuration);+ if (currentDuration === normlizedDuration)return this;const timeScale = newDuration / currentDuration;const isSetter = newDuration <= minValue;- this.duration = isSetter ? minValue : clampZero(clampInfinity(round(currentDuration * timeScale, 12)));- this.iterationDuration = isSetter ? minValue : clampZero(clampInfinity(round(this.iterationDuration * timeScale, 12)));+ this.duration = isSetter ? minValue : normlizedDuration;+ this.iterationDuration = isSetter ? minValue : normalizeTime(this.iterationDuration * timeScale);this._offset *= timeScale;this._delay *= timeScale;this._loopDelay *= timeScale;@@ -3051,14 +3052,14 @@ class JSAnimation extends Timer {*/stretch(newDuration) {const currentDuration = this.duration;- if (currentDuration === clampZero(newDuration))+ if (currentDuration === normalizeTime(newDuration))return this;const timeScale = newDuration / currentDuration;// NOTE: Find a better way to handle the stretch of an animation after stretch = 0forEachChildren(this, (/** @type {Tween} */ tween) => {// Rounding is necessary here to minimize floating point errors- tween._updateDuration = clampZero(round(tween._updateDuration * timeScale, 12));- tween._changeDuration = clampZero(round(tween._changeDuration * timeScale, 12));+ tween._updateDuration = normalizeTime(tween._updateDuration * timeScale);+ tween._changeDuration = normalizeTime(tween._changeDuration * timeScale);tween._currentTime *= timeScale;tween._startTime *= timeScale;tween._absoluteStartTime *= timeScale;@@ -4272,16 +4273,13 @@ class Timeline extends Timer {*/stretch(newDuration) {const currentDuration = this.duration;- if (currentDuration === clampZero(newDuration))+ if (currentDuration === normalizeTime(newDuration))return this;const timeScale = newDuration / currentDuration;const labels = this.labels;- forEachChildren(this, (/** @type {JSAnimation} */ child) => {- child.stretch(child.duration * timeScale);- });- for (let labelName in labels) {+ forEachChildren(this, (/** @type {JSAnimation} */ child) => child.stretch(child.duration * timeScale));+ for (let labelName in labels)labels[labelName] *= timeScale;- }return super.stretch(newDuration);}/**