Created
May 6, 2017 18:45
-
-
Save nkt/5b60bba1bad3c46ba35ce7ae5a8ddd1d to your computer and use it in GitHub Desktop.
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
/* | |
* Copyright 2017 dialog LLC <[email protected]> | |
* @flow | |
*/ | |
/* eslint no-mixed-operators:0 */ | |
type Config = { | |
start: number, | |
end: number, | |
duration: number, | |
onUpdate: (value: number) => void, | |
animation: (x: number) => number | |
}; | |
function animate(config: Config): void { | |
const startTime = Date.now(); | |
const step = () => { | |
const elapsed = Date.now() - startTime; | |
if (elapsed >= config.duration) { | |
config.onUpdate(config.end); | |
} else { | |
const animation = config.animation(elapsed / config.duration); | |
config.onUpdate(config.start + (config.end - config.start) * animation); | |
requestAnimationFrame(step); | |
} | |
}; | |
step(); | |
} | |
export default animate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment