Last active
July 6, 2017 07:52
-
-
Save kirjavascript/c9494af761807c05e94c876207574115 to your computer and use it in GitHub Desktop.
animation primitives
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
// 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