Skip to content

Instantly share code, notes, and snippets.

@kirjavascript
Last active July 6, 2017 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirjavascript/c9494af761807c05e94c876207574115 to your computer and use it in GitHub Desktop.
Save kirjavascript/c9494af761807c05e94c876207574115 to your computer and use it in GitHub Desktop.
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