Skip to content

Instantly share code, notes, and snippets.

@mattisfrommars
Created April 11, 2013 14:48
Show Gist options
  • Save mattisfrommars/5363987 to your computer and use it in GitHub Desktop.
Save mattisfrommars/5363987 to your computer and use it in GitHub Desktop.
Wrapper for requestAnimationFrame that accepts a callback and duration. Gives the callback a "completion" value between 0 and 1 every animation frame
var animateObject = function (callback, duration) {
var startTime = 0;
var slideFrame = function(animationMiliSeconds) {
startTime = startTime || animationMiliSeconds;
var timeTaken = animationMiliSeconds - startTime;
var completion = timeTaken / duration;
if (animationMiliSeconds <= startTime+duration) {
callback(completion);
requestAnimationFrame(slideFrame);
} else {
callback(1);
}
}
requestAnimationFrame(slideFrame);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment