Created
September 3, 2019 09:28
-
-
Save sebbdk/efa8f8ee413fdb85062f29f41264bfe1 to your computer and use it in GitHub Desktop.
Javascript - animate()
This file contains 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
export function animate(cb, time) { | |
return new Promise((resolve, reject) => { | |
const startTime = performance.now(); | |
const endTime = startTime + time; | |
function run() { | |
const currentTime = performance.now(); | |
const deltaTime = currentTime - startTime; | |
const pctDone = deltaTime / time; | |
if (currentTime < endTime) { | |
if (cb(pctDone) !== false) { | |
window.requestAnimationFrame(run); | |
} else { | |
reject(); | |
} | |
} else { | |
cb(1) | |
resolve(); | |
} | |
} | |
window.requestAnimationFrame(run) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment