Skip to content

Commit

Permalink
fix dynamic and spring number animators
Browse files Browse the repository at this point in the history
  • Loading branch information
TahaSh committed Jul 2, 2024
1 parent 127b8ca commit 2f59f5f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
8 changes: 2 additions & 6 deletions src/animators/DynamicAnimator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Vec2 } from '../math'
import { CSSNumber, createCSSNumber } from '../utils/CSSNumber'
import { almostEqual } from '../utils/Math'
import {
Animator,
AnimatorUpdateData,
Expand Down Expand Up @@ -61,7 +62,7 @@ export class NumberDynamicAnimator extends DynamicAnimator<number> {

let result = current + velocity * dt

if (this._shouldFinish(target, current, velocity)) {
if (almostEqual(result, target)) {
result = target
requestAnimationFrame(() => {
animatorProp.callCompleteCallback()
Expand All @@ -72,11 +73,6 @@ export class NumberDynamicAnimator extends DynamicAnimator<number> {

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<CSSNumber[]> {
Expand Down
8 changes: 2 additions & 6 deletions src/animators/SpringAnimator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Vec2 } from '../math'
import { CSSNumber, createCSSNumber } from '../utils/CSSNumber'
import { almostEqual } from '../utils/Math'
import {
Animator,
AnimatorUpdateData,
Expand Down Expand Up @@ -76,7 +77,7 @@ export class NumberSpringAnimator extends SpringAnimator<number> {

let result = current + this._velocity * dt * this._config.speed

if (this._shouldFinish(target, current)) {
if (almostEqual(result, target)) {
result = target
requestAnimationFrame(() => {
animatorProp.callCompleteCallback()
Expand All @@ -85,11 +86,6 @@ export class NumberSpringAnimator extends SpringAnimator<number> {

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<CSSNumber[]> {
Expand Down

0 comments on commit 2f59f5f

Please sign in to comment.