From 2f59f5f45ff97b3f43fb9a7ab4ee1f627311594c Mon Sep 17 00:00:00 2001 From: Taha Shashtari Date: Tue, 2 Jul 2024 18:37:46 +0300 Subject: [PATCH] fix dynamic and spring number animators --- src/animators/DynamicAnimator.ts | 8 ++------ src/animators/SpringAnimator.ts | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/animators/DynamicAnimator.ts b/src/animators/DynamicAnimator.ts index 0b33673..1604bb6 100644 --- a/src/animators/DynamicAnimator.ts +++ b/src/animators/DynamicAnimator.ts @@ -1,5 +1,6 @@ import { Vec2 } from '../math' import { CSSNumber, createCSSNumber } from '../utils/CSSNumber' +import { almostEqual } from '../utils/Math' import { Animator, AnimatorUpdateData, @@ -61,7 +62,7 @@ export class NumberDynamicAnimator extends DynamicAnimator { let result = current + velocity * dt - if (this._shouldFinish(target, current, velocity)) { + if (almostEqual(result, target)) { result = target requestAnimationFrame(() => { animatorProp.callCompleteCallback() @@ -72,11 +73,6 @@ export class NumberDynamicAnimator extends DynamicAnimator { return result } - - private _shouldFinish(target: number, current: number, velocity: number) { - const diff = Math.abs(target - current) - return diff < ERROR_OFFSET && Math.abs(velocity) < ERROR_OFFSET - } } export class CSSNumbersDynamicAnimator extends DynamicAnimator { diff --git a/src/animators/SpringAnimator.ts b/src/animators/SpringAnimator.ts index 7b75118..7d44a85 100644 --- a/src/animators/SpringAnimator.ts +++ b/src/animators/SpringAnimator.ts @@ -1,5 +1,6 @@ import { Vec2 } from '../math' import { CSSNumber, createCSSNumber } from '../utils/CSSNumber' +import { almostEqual } from '../utils/Math' import { Animator, AnimatorUpdateData, @@ -76,7 +77,7 @@ export class NumberSpringAnimator extends SpringAnimator { let result = current + this._velocity * dt * this._config.speed - if (this._shouldFinish(target, current)) { + if (almostEqual(result, target)) { result = target requestAnimationFrame(() => { animatorProp.callCompleteCallback() @@ -85,11 +86,6 @@ export class NumberSpringAnimator extends SpringAnimator { return result } - - private _shouldFinish(target: number, current: number) { - const diff = Math.abs(target - current) - return diff < ERROR_OFFSET && Math.abs(this._velocity) < ERROR_OFFSET - } } export class CSSNumbersSpringAnimator extends SpringAnimator {