Created
August 24, 2017 16:45
-
-
Save bendc/aac89c40d88385d8f448fc4f4aee9f4a to your computer and use it in GitHub Desktop.
rAF tutorial: basic animation
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
const element = document.querySelector("span"); | |
const finalPosition = 600; | |
const time = { | |
start: performance.now(), | |
total: 2000 | |
}; | |
const tick = now => { | |
time.elapsed = now - time.start; | |
const progress = time.elapsed / time.total; | |
const position = progress * finalPosition; | |
element.style.transform = `translate(${position}px)`; | |
if (progress < 1) requestAnimationFrame(tick); | |
}; | |
requestAnimationFrame(tick); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment