Created
October 23, 2018 13:05
-
-
Save zhjgithub/0be96586d3be08814886b775d7e56c72 to your computer and use it in GitHub Desktop.
requestAnimationFrame callback with lastTime
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
let cat = document.querySelector("img"); | |
let angle = Math.PI / 2; | |
function animate(time, lastTime) { | |
if (lastTime != null) { | |
angle += (time - lastTime) * 0.001; | |
} | |
cat.style.top = (Math.sin(angle) * 20) + "px"; | |
cat.style.left = (Math.cos(angle) * 200) + "px"; | |
requestAnimationFrame(newTime => animate(newTime, time)); | |
} | |
requestAnimationFrame(animate); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment