Last active
August 4, 2017 15:02
-
-
Save zmsaunders/9934dcd6f75b59ad77414c7525742caa to your computer and use it in GitHub Desktop.
RequestAnimationFrame Queue Lib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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