requestAnimationFrame loop that tracks elapsed time for wobble animation

This commit is contained in:
2026-01-25 09:36:30 +00:00
parent 0b370298e4
commit 0638425c67
2 changed files with 40 additions and 2 deletions

View File

@@ -0,0 +1,38 @@
import { useEffect, useRef, useState } from "react";
// requestAnimationFrame loop that tracks elapsed time for wobble animation
export function useWobbleAnimation(speed: number): number {
const [time, setTime] = useState(0);
const rafRef = useRef<number>(0);
const lastFrameRef = useRef<number>(0);
useEffect(() => {
if (speed === 0) {
return;
}
const animate = (timestamp: number) => {
if (lastFrameRef.current === 0) {
lastFrameRef.current = timestamp;
}
const delta = timestamp - lastFrameRef.current;
lastFrameRef.current = timestamp;
// speed 50 = 1x, speed 100 = 2x, speed 0 = 0x
const speedMultiplier = speed / 50;
setTime((t) => t + (delta / 1000) * speedMultiplier);
rafRef.current = requestAnimationFrame(animate);
};
rafRef.current = requestAnimationFrame(animate);
return () => {
cancelAnimationFrame(rafRef.current);
lastFrameRef.current = 0;
};
}, [speed]);
return time;
}