Last active
March 22, 2020 23:24
-
-
Save Sphinxxxx/fc9bcc601a2f903ff167112855d0437c to your computer and use it in GitHub Desktop.
Super-duper-lightweight tweening "library"
This file contains hidden or 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
//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); | |
} |
This file contains hidden or 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
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