Skip to content

Instantly share code, notes, and snippets.

@kirjavascript
Last active Jul 6, 2017
Embed
What would you like to do?
animation primitives
// Usage;
//
// tween((t) => {
// const i = lerp(-20, 100, ease(t));
// console.log(i);
// }, 500);
function tween(callback, ticks = 500) {
let timer = performance.now();
~function frame() {
let diff = performance.now() - timer;
if (diff <= ticks) {
requestAnimationFrame(frame);
callback(diff / ticks);
}
else {
callback(1);
}
} ();
}
function lerp(start, end, i) {
return start + (end - start) * i;
}
function ease(t) {
// from https://gist.github.com/gre/1650294
return t<.5 ? 2*t*t : -1+(4-2*t)*t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment