Skip to content

Instantly share code, notes, and snippets.

@SaulDoesCode
Created May 2, 2016 21:15
Show Gist options
  • Save SaulDoesCode/c2fe9ff45b0ac830f940ff8b77cdb230 to your computer and use it in GitHub Desktop.
Save SaulDoesCode/c2fe9ff45b0ac830f940ff8b77cdb230 to your computer and use it in GitHub Desktop.
lil' system to make requesting and canceling AnimationFrames a breeze
function animFrame(func) {
return new class {
constructor(fn) {
this.fn = fn
}
start() {
this.fn();
this.interval = requestAnimationFrame(this.start.bind(this));
return this
}
stop() {
if (this.interval != undefined) cancelAnimationFrame(this.interval);
return this
}
reset(fn) {
this.stop();
this.fn = fn;
return this.start()
}
}(func)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment