I was feeling bored. I was feeling quEASY. I also felt like I needed some curry and composition. What do you do when you have all of these feelings running through your head at once?
[ul]
[li]NORMALIZED EASING FUNCTIONS!!![/li]
[li]EXAMPLE!!![/li]
[/ul]
CODE!!!
[code=lua]local Easing do
local function reverse(fn)
return function(x)
return 1-fn(1-x);
end
end;
local function symmetric(fn)
local r_fn = reverse(fn);
return function(x)
if (x < 0.5) then
return fn(x*2)/2;
else
return 0.5 + r_fn((x - 0.5)*2)/2;
end
end
end;
local function linear(x)
return x;
end;
local function polynomial(p)
return function(x)
return x^p;
end
end;
local function exponential(b, r)
r = r or 10;
return function(x)
if (x > 0) then
return b^(r*(x-1));
else
return 0;
end
end
end
local function sin(x)
return math.sin(x * math.pi/2);
end
local function generate(fn)
return {
In = fn,
Out = reverse( fn ),
InOut = symmetric( fn );
OutIn = symmetric( reverse( fn ) );
}
end
Easing = {
Linear = linear;
Quadratic = generate( polynomial(2) );
Cubic = generate( polynomial(3) );
Quartic = generate( polynomial(4) );
Quintic = generate( polynomial(5) );
Exponential = generate( exponential(2) );
Sine = generate( reverse( sin ) ); -- nothing wrong here
}
end[/code]
API!!!
[code]Both x and y are numerical values between 0 and 1.
y = Easing.Linear(x)
y = Easing.Quadratic.In(x)
y = Easing.Quadratic.Out(x)
y = Easing.Quadratic.InOut(x)
y = Easing.Quadratic.OutIn(x)
y = Easing.Cubic.In(x)
y = Easing.Cubic.Out(x)
y = Easing.Cubic.InOut(x)
y = Easing.Cubic.OutIn(x)
y = Easing.Quartic.In(x)
y = Easing.Quartic.Out(x)
y = Easing.Quartic.InOut(x)
y = Easing.Quartic.OutIn(x)
y = Easing.Quintic.In(x)
y = Easing.Quintic.Out(x)
y = Easing.Quintic.InOut(x)
y = Easing.Quintic.OutIn(x)
y = Easing.Exponential.In(x)
y = Easing.Exponential.Out(x)
y = Easing.Exponential.InOut(x)
y = Easing.Exponential.OutIn(x)
y = Easing.Sine.In(x)
y = Easing.Sine.Out(x)
y = Easing.Sine.InOut(x)
y = Easing.Sine.OutIn(x)[/code]