Created
June 21, 2018 21:14
-
-
Save madfriend/4e4fe84d5f706ee7b3673c966def956f to your computer and use it in GitHub Desktop.
animate
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
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