Created
June 3, 2024 12:06
-
-
Save alexsad/785b41941e2052338d3459d852a6a7ea to your computer and use it in GitHub Desktop.
a FPS generator control utility
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
async function* nextFrame(fps: number) { | |
let then = performance.now(); | |
const interval = 1000 / fps; | |
let delta = 0; | |
while (true) { | |
let now = await new Promise(requestAnimationFrame); | |
if (now - then < interval - delta) { | |
continue; | |
}; | |
delta = Math.min(interval, delta + now - then - interval); | |
yield (now - then); | |
then = now; | |
} | |
} | |
export { nextFrame }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment