Skip to content

Instantly share code, notes, and snippets.

@nat-n
Last active August 31, 2018 07:59
Show Gist options
  • Save nat-n/cb00b74aa3183269b37b5ca6391989ee to your computer and use it in GitHub Desktop.
Save nat-n/cb00b74aa3183269b37b5ca6391989ee to your computer and use it in GitHub Desktop.
Generic linear animation function
window.animate = (function () {
function animate(initialValue, finalValue, duration, callback, ease, startTime, currentTime) {
if (startTime) {
if (currentTime - startTime >= duration) {
callback(finalValue);
return;
}
callback(ease(currentTime - startTime, initialValue, finalValue - initialValue, duration));
}
window.requestAnimationFrame(function (currentTime) {
startTime = startTime || currentTime;
ease = ease || animate.easingFunctions.linear;
animate(initialValue, finalValue, duration, callback, ease, startTime, currentTime);
});
}
animate.easingFunctions = {
linear: function (t, b, c, d) {
return c * t / d + b;
},
inOutQuad: function (t, b, c, d) {
t /= d / 2;
if (t < 1) return c / 2 * t * t + b;
t--;
return -c / 2 * (t * (t - 2) - 1) + b;
},
// find more functions at http://gizma.com/easing/
}
return animate;
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment