import { CubicCoeff } from "@noya-app/noya-geometry"; import { lerp } from "@noya-app/noya-utils"; const EaseInOut: CubicCoeff = CubicCoeff.fromCubic({ P0: { x: 0, y: 0 }, P1: { x: 0.25, y: 0 }, P2: { x: 0.75, y: 1 }, P3: { x: 1, y: 1 }, }); function easeInOut(progress: number) { return EaseInOut.eval(progress).y; } function linear(progress: number) { return progress; } export const Easing = { easeInOut, linear, }; export function animateValue({ start, end, duration, easing = linear, onChange, }: { start: number; end: number; duration: number; easing?: (progress: number) => number; onChange: (value: number) => void; }) { const startTime = performance.now(); let animationFrame: number; function runAnimation() { const currentTime = performance.now(); const progress = Math.min((currentTime - startTime) / duration, 1); const value = lerp(start, end, easing(progress)); onChange(value); if (progress < 1) { animationFrame = requestAnimationFrame(runAnimation); } } runAnimation(); return () => { if (animationFrame) { cancelAnimationFrame(animationFrame); } }; }