Last active
August 31, 2018 07:59
-
-
Save nat-n/cb00b74aa3183269b37b5ca6391989ee to your computer and use it in GitHub Desktop.
Generic linear animation function
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
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