feat: useAnimationFrame

This commit is contained in:
cha0s 2024-07-30 22:58:03 -05:00
parent 3191ee83d4
commit a1a6b98639

View File

@ -0,0 +1,19 @@
import {useEffect, useRef} from 'react';
export default function useAnimationFrame(callback) {
const handle = useRef();
const last = useRef();
function animate(time) {
if (last.current != undefined) {
callback((time - last.current) / 1000)
}
last.current = time;
handle.current = requestAnimationFrame(animate);
}
useEffect(() => {
handle.current = requestAnimationFrame(animate);
return () => {
cancelAnimationFrame(handle.current);
};
}, [callback]);
}