Skip to content

Instantly share code, notes, and snippets.

@madfriend
Created Jun 21, 2018
Embed
What would you like to do?
animate
let frameCounter = 0;
function countFrames () {
frameCounter++;
requestAnimationFrame(countFrames);
}
requestAnimationFrame(countFrames);
function* animate (from, to, numFrames) {
let lastFrameCount = frameCounter;
const delta = (to - from) / numFrames;
let current = from;
while (numFrames > 0) {
const framesPassed = frameCounter - lastFrameCount;
numFrames -= framesPassed;
lastFrameCount = frameCounter;
current += delta * framesPassed;
yield current;
}
if (current !== to) yield to;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment