Skip to content

Instantly share code, notes, and snippets.

@zmsaunders
Last active August 4, 2017 15:02
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 zmsaunders/9934dcd6f75b59ad77414c7525742caa to your computer and use it in GitHub Desktop.
Save zmsaunders/9934dcd6f75b59ad77414c7525742caa to your computer and use it in GitHub Desktop.
RequestAnimationFrame Queue Lib
var AnimationQueue = function() {
this.animations = [];
this.lastRun = 0;
this.runAnimations();
};
AnimationQueue.prototype = {
addAnimation(func, fps) {
this.animations.push({
call : func,
fpsInt : 1000 / fps,
lastCall : performance.now()
});
},
runAnimations(activeRun) {
// Requeue
requestAnimationFrame((stamp) => {this.runAnimations(stamp)});
// Call each animation
for(i = 0; i < this.animations.length; i++) {
if (activeRun - this.animations[i].lastCall > this.animations[i].fpsInt) {
this.animations[i].call();
this.animations[i].lastCall = activeRun;
}
}
this.lastRun = activeRun;
}
};
window.AnimationQueue = new AnimationQueue();
// Use with
// AnimationQueue.addAnimation(() => {renderFunction()}, 60);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment