Skip to content

Instantly share code, notes, and snippets.

@willcalderbank
Created June 23, 2015 09:06
Show Gist options
  • Save willcalderbank/36bd8f7b0b987a85a512 to your computer and use it in GitHub Desktop.
Save willcalderbank/36bd8f7b0b987a85a512 to your computer and use it in GitHub Desktop.
JS animation loop with duration
_animateSection: function(duration) {
if (duration === undefined) duration = 1;
var self = this;
_animationLoop();
function _animationLoop(_startTime, _timestamp) {
if (_startTime === undefined || _timestamp === undefined) {
requestAnimationFrame(function (timestamp) {
_animationLoop(timestamp, timestamp);
});
return;
}
var timeElapsed = _timestamp - _startTime;
var percent = ((timeElapsed / (duration * 1000)));
if (timeElapsed < duration * 1000) {
/**
* DO STUFF HERE
*/
// keep the animation loop going
requestAnimationFrame(function (timestamp) {
if (_startTime === undefined) _startTime = timestamp;
_animationLoop(_startTime, timestamp);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment