Created
November 30, 2019 18:53
-
-
Save stryju/4694ac189925f7120b139a1c1b2b29b9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function useFPS() { | |
const [fps, setFPS] = useState(0); | |
useEffect(() => { | |
let then = performance.now(); | |
let frames = 0; | |
let req; | |
(function loop(now) { | |
req = window.requestAnimationFrame(loop); | |
frames++; | |
if (now - then >= 1000) { | |
setFPS(Math.round((frames * 1000) / (now - then))); | |
frames = 0; | |
then = now; | |
} | |
})(then); | |
return () => window.cancelAnimationFrame(req); | |
}, [setFPS]); | |
return fps; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment