Skip to content

Instantly share code, notes, and snippets.

@snggeng
Last active June 6, 2017 01:20
Show Gist options
  • Save snggeng/9299cd91d9f1165f3a917804fcf2463f to your computer and use it in GitHub Desktop.
Save snggeng/9299cd91d9f1165f3a917804fcf2463f to your computer and use it in GitHub Desktop.
Using window.request and cancel AnimationFrame
/* Animation */
var self = this;
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback){
window.setTimeout(callback, 1000 / 60); // 60 frames per sec
};
})();
var request; //
var loop = function() {
self.render();
request = requestAnimFrame(loop);
frame++;
};
loop();
window.cancelRequestAnimFrame = ( function() {
return window.cancelAnimationFrame ||
window.webkitCancelRequestAnimationFrame ||
window.mozCancelRequestAnimationFrame ||
window.oCancelRequestAnimationFrame ||
window.msCancelRequestAnimationFrame ||
clearTimeout
} )();
init(); // initialize settings before game begins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment