Created
June 23, 2015 09:06
-
-
Save willcalderbank/36bd8f7b0b987a85a512 to your computer and use it in GitHub Desktop.
JS animation loop with duration
This file contains 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
_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