feat: useAnimationFrame
This commit is contained in:
parent
3191ee83d4
commit
a1a6b98639
19
app/react/hooks/use-animation-frame.js
Normal file
19
app/react/hooks/use-animation-frame.js
Normal 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]);
|
||||
}
|
Loading…
Reference in New Issue
Block a user