Skip to content

Instantly share code, notes, and snippets.

@alepez
Created February 4, 2014 10:14
Show Gist options
  • Save alepez/8801073 to your computer and use it in GitHub Desktop.
Save alepez/8801073 to your computer and use it in GitHub Desktop.
requestAnimationFrame and time since last frame (seconds)
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