Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zhjgithub/0be96586d3be08814886b775d7e56c72 to your computer and use it in GitHub Desktop.
Save zhjgithub/0be96586d3be08814886b775d7e56c72 to your computer and use it in GitHub Desktop.
requestAnimationFrame callback with lastTime
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