Last active
June 6, 2017 01:20
-
-
Save snggeng/9299cd91d9f1165f3a917804fcf2463f to your computer and use it in GitHub Desktop.
Using window.request and cancel AnimationFrame
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
/* 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