Skip to content

Instantly share code, notes, and snippets.

@simonkberg
Created July 22, 2016 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonkberg/461dec93aa9f99e4c2026a98d1438a81 to your computer and use it in GitHub Desktop.
Save simonkberg/461dec93aa9f99e4c2026a98d1438a81 to your computer and use it in GitHub Desktop.
Simple helper for working with requestAnimationFrame
import raf from 'raf'
export default function animationFrame (fn) {
let frame = {
id: 0,
cancel () {
raf.cancel(this.id)
},
}
let cb = (ts) => {
frame.id = fn(ts, frame) && raf(cb)
}
frame.id = raf(cb)
return frame
}
/*
// Stop running frame after 3 seconds
let counter = 0
let frame = animationFrame(() => {
console.log('animationFrame', counter)
return ++counter
})
setTimeout(() => {
frame.cancel()
console.log(frame)
}, 3000)
*/
/*
// Stop from inside frame
let counter = 0
animationFrame(() => {
console.log('animationFrame', counter)
return ++counter < 100
})
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment