Spaces:
Runtime error
Runtime error
File size: 581 Bytes
0ce9643 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import { useCallback, useEffect, useRef } from 'react';
export function useAnimationFrame(callback: (time: number) => void, active = true) {
const rafRef = useRef<number>(0);
const callbackRef = useRef(callback);
callbackRef.current = callback;
const animate = useCallback((time: number) => {
callbackRef.current(time);
rafRef.current = requestAnimationFrame(animate);
}, []);
useEffect(() => {
if (active) {
rafRef.current = requestAnimationFrame(animate);
return () => cancelAnimationFrame(rafRef.current);
}
}, [active, animate]);
}
|