Skip to content

Instantly share code, notes, and snippets.

@alexsad
Created June 3, 2024 12:06
Show Gist options
  • Save alexsad/785b41941e2052338d3459d852a6a7ea to your computer and use it in GitHub Desktop.
Save alexsad/785b41941e2052338d3459d852a6a7ea to your computer and use it in GitHub Desktop.
a FPS generator control utility
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