Skip to content

Instantly share code, notes, and snippets.

@abiodun0
Last active January 6, 2018 01:19
Show Gist options
  • Save abiodun0/2f72c32ab01f3c2b3a3e5e472971e677 to your computer and use it in GitHub Desktop.
Save abiodun0/2f72c32ab01f3c2b3a3e5e472971e677 to your computer and use it in GitHub Desktop.
requestAnimation
function getPinStyle (pin, scrollPosition) {
// do some calculation based on the pin that was passed in and the current scroll position
return { rotate, scale, translate }
}
requestAnimationFrame(function frame () {
pins.forEach(pin => applyPinStyles(pin, getPinStyle(pin, window.scrollTop)))
requestAnimationFrame(frame)
})
// `requestAnimationFrame` in this context saves you from a certain kind of bug in `setInterval`,
// which is that `setInterval` calls are scheduled based on the computer clock (loosely speaking)
// while `rAF` schedules the next call subsequent to the previous call.
//But `setInterval` can occasionally drop frames (IIRC), or stack frames within the same event loop turn.
// It’s a bit of a consistency nightmare.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment