Skip to content

Instantly share code, notes, and snippets.

@Sphinxxxx
Last active March 22, 2020 23:24
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 Sphinxxxx/fc9bcc601a2f903ff167112855d0437c to your computer and use it in GitHub Desktop.
Save Sphinxxxx/fc9bcc601a2f903ff167112855d0437c to your computer and use it in GitHub Desktop.
Super-duper-lightweight tweening "library"
//Super-duper-lightweight tweening "library".
//An actual library like https://github.com/tweenjs/tween.js would probably be better..
function TWEEN(from, to, duration, callback, easing) {
//ease-out quad: https://github.com/tweenjs/tween.js/blob/master/src/Tween.js#L544
easing = easing || function(t) { return t * (2 - t); };
var diff = to - from,
startTime;
function anim(time) {
var prog, val;
if(!startTime) {
startTime = time;
prog = 0;
val = from;
}
else {
prog = (time - startTime) / duration;
val = (prog < 1) ? from + (diff * easing(prog)) : to;
}
callback(val);
if(prog < 1) {
requestAnimationFrame(anim);
}
}
requestAnimationFrame(anim);
}
function TWEEN(b,e,h,k,c){function f(a){if(d){a=(a-d)/h;var g=1>a?b+l*c(a):e}else d=a,a=0,g=b;k(g);1>a&&requestAnimationFrame(f)}c=c||function(a){return a*(2-a)};var l=e-b,d;requestAnimationFrame(f)};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment