Created
February 4, 2014 10:14
-
-
Save alepez/8801073 to your computer and use it in GitHub Desktop.
requestAnimationFrame and time since last frame (seconds)
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 Loop = function (step) { | |
var self = this; | |
var lastFrameTimestamp = null; | |
var nextStep = function (timestamp) { | |
if (lastFrameTimestamp === null) { | |
lastFrameTimestamp = timestamp; | |
} | |
var tslf = timestamp - lastFrameTimestamp; | |
step(tslf); | |
lastFrameTimestamp = timestamp; | |
self.requestID = window.requestAnimationFrame(nextStep); | |
}; | |
self.stop = function() { | |
window.cancelAnimationFrame(self.requestID); | |
} | |
nextStep(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment