Skip to content

Instantly share code, notes, and snippets.

Created January 2, 2015 14:19
Show Gist options
  • Save anonymous/9a0f215eb9a530b8785a to your computer and use it in GitHub Desktop.
Save anonymous/9a0f215eb9a530b8785a to your computer and use it in GitHub Desktop.
Easing Sample
import flash.display.Sprite;
import flash.Lib;
class Test {
static function linear(t:Float) : Float return t;
static function quad(t:Float) : Float return t * t;
static function cubic(t:Float) : Float return t * t * t;
static function quart(t:Float) : Float return t * t * t * t;
static function quint(t:Float) : Float return t * t * t * t * t;
static function poly(t:Float, p:Float=2.0) : Float return Math.pow(t, p);
static function sine(t:Float) : Float return 1 - Math.sin( (1 - t) * Math.PI/2 );
static function circ(t:Float) : Float return 1 - Math.sqrt( 1 - t*t );
static function expo(t:Float, p:Float=10.0) : Float return Math.pow( 2, p * (t-1) );
static function back(t:Float, p:Float=1.70158) : Float return t * t * ( (p+1) * t - p );
static function out(f:Dynamic, t:Float) : Float return 1 - f( 1 - t );
static function inout(f:Dynamic, t:Float) : Float return (t < 0.5)? f(2*t)/2 : (2-f(2-2*t))/2;
static function outin(f:Dynamic, t:Float) : Float return (t < 0.5)? (1-f(1-2*t))/2 : (1+f(2*t-1))/2;
static function lerp(a:Float, b:Float, t:Float) : Float return a + (b-a) * t;
static var flag : Bool = true;
static var t : Float;
static var bt : Float;
static var duration : Float = 1;
static var timer = new haxe.Timer(40);
static var ease : Array<Array<Dynamic>>;
static var sprite : Array<Array<Sprite>>;
static function main() {
var a_in:Array<Dynamic> = [linear, quad, cubic, quart, quint, poly.bind(_, 6), sine, circ, expo, back];
var a_out = [];
var a_inout = [];
var a_outin = [];
for(i in a_in)
{
a_out.push(out.bind(i));
a_inout.push(inout.bind(i));
a_outin.push(outin.bind(i));
}
ease = [a_in, a_out, a_inout, a_outin];
sprite = [];
for(j in 0...ease.length)
{
sprite.push([]);
for(i in 0...ease[j].length)
{
var s = new Sprite();
s.graphics.beginFill(0);
s.graphics.drawCircle(0, 0, 5);
Lib.current.addChild(s);
sprite[j].push(s);
//trace(j + " " + i + " " + ease[j][i](0.5));
}
}
t = 0;
bt = haxe.Timer.stamp();
timer.run = run;
}
static function run()
{
var stamp = haxe.Timer.stamp();
t += stamp - bt;
bt = stamp;
while(t > duration)
{
t -= duration;
flag = !flag;
}
for(j in 0...ease.length)
{
for(i in 0...ease[j].length)
{
var s = sprite[j][i];
s.y = i*20 + 10;
var start = flag? 0 : 100;
var end = flag? 100 : 0;
s.x = 10 + 120 * j + lerp(start, end, ease[j][i](t/duration));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment