Skip to content

Instantly share code, notes, and snippets.

@Gerhut
Last active December 26, 2015 02:28
Show Gist options
  • Save Gerhut/7078257 to your computer and use it in GitHub Desktop.
Save Gerhut/7078257 to your computer and use it in GitHub Desktop.
Simple Easing Method.
/**
* Easing Function f(t) where t, f(t) <- [0, 1]
* return an Easing Factory.
*/
function easing(f) {
/**
* x = f(t) where x <- [x0, x1], t <- [0, t1]
* return an Easing Instance.
*/
return function(x0, x1, t1) { // x = f(t), x <-[x0, x1]
var t = 0;
/**
* Each call returns the next value.
* Return null means no more value.
*/
return function () {
return (t <= t1)
? f(t++ / t1) * (x1 - x0) + x0
: null;
}
}
}
var linearFunc = function (x) { return x; }
, linearEasingFactory = easing(linearFunc)
, myEasingInstance = linearEasingFactory(5, 10, 10)
, x;
while((x = myEasingInstance()) !== null)
console.log(x);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment