Last active
December 7, 2020 18:10
-
-
Save dbabbs/3f843443ec14dd9ec6a4625fa17e8ca2 to your computer and use it in GitHub Desktop.
React hook for requestAnimation Frame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Usage: | |
import { useState } from 'react'; | |
import useAnimationFrame from '../../hooks/useAnimationFrame'; | |
const [animationActive, setAnimationActive] = useState(true); | |
useAnimationFrame(() => { | |
//perform action here | |
}, animationActive); | |
*/ | |
import { useRef, useEffect, useCallback } from 'react'; | |
function useAnimationFrame(exec, active) { | |
const savedExec = useRef(); | |
const interval = useRef(); | |
useEffect(() => { | |
savedExec.current = exec; | |
}, [exec]); | |
const step = useCallback(() => { | |
savedExec.current(); | |
if (interval.current) { | |
requestAnimationFrame(step); | |
} | |
}); | |
const cancel = useCallback(() => { | |
cancelAnimationFrame(interval.current); | |
interval.current = null; | |
}); | |
useEffect(() => { | |
if (active) { | |
interval.current = requestAnimationFrame(step); | |
} | |
return () => cancel(); | |
}, [active]); | |
} | |
export default useAnimationFrame; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Every animation function needs to know the elapsed time to determine the progress of the animation. so: