Skip to content

Instantly share code, notes, and snippets.

@bendc
Created August 24, 2017 16:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bendc/aac89c40d88385d8f448fc4f4aee9f4a to your computer and use it in GitHub Desktop.
Save bendc/aac89c40d88385d8f448fc4f4aee9f4a to your computer and use it in GitHub Desktop.
rAF tutorial: basic animation
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