Skip to content

Instantly share code, notes, and snippets.

@madfriend
Created June 21, 2018 21:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save madfriend/4e4fe84d5f706ee7b3673c966def956f to your computer and use it in GitHub Desktop.
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