View difference between Paste ID: ucEbAA4g and gUWUVhHV
SHOW: | | - or go back to the newest paste.
1-
package util;
1+
package util;
2-
2+
3-
typedef EasingFunction = Float->Float->Float->Float->Float->Float;
3+
typedef EasingFunction = Float->Float->Float->Float->Float->Float;
4-
4+
5-
class Easing
5+
class Easing
6-
{
6+
{
7-
	// simple linear tweening - no easing:Float, no acceleration
7+
	// simple linear tweening - no easing:Float, no acceleration
8-
	public static function linear(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
8+
	public static function linear(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
9-
	{
9+
	{
10-
		return changeInValue * time / duration + startValue;
10+
		return changeInValue * time / duration + startValue;
11-
	}
11+
	}
12-
			
12+
			
13-
	// quadratic easing in - accelerating from zero velocity
13+
	// quadratic easing in - accelerating from zero velocity
14-
	public static function easeInQuad(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
14+
	public static function easeInQuad(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
15-
	{
15+
	{
16-
		time /= duration;
16+
		time /= duration;
17-
		return changeInValue * time * time + startValue;
17+
		return changeInValue * time * time + startValue;
18-
	}
18+
	}
19-
			
19+
			
20-
	// quadratic easing out - decelerating to zero velocity
20+
	// quadratic easing out - decelerating to zero velocity
21-
	public static function easeOutQuad(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
21+
	public static function easeOutQuad(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
22-
	{
22+
	{
23-
		time /= duration;
23+
		time /= duration;
24-
		return - changeInValue * time * (time - 2) + startValue;
24+
		return - changeInValue * time * (time - 2) + startValue;
25-
	}
25+
	}
26-
26+
27-
	// quadratic easing in/out - acceleration until halfway:Float, then deceleration
27+
	// quadratic easing in/out - acceleration until halfway:Float, then deceleration
28-
	public static function easeInOutQuad(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
28+
	public static function easeInOutQuad(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
29-
	{
29+
	{
30-
		time /= duration / 2;
30+
		time /= duration / 2;
31-
		if(time < 1) return changeInValue / 2 * time * time + startValue;
31+
		if(time < 1) return changeInValue / 2 * time * time + startValue;
32-
		time --;
32+
		time --;
33-
		return - changeInValue / 2 * (time * (time - 2) - 1) + startValue;
33+
		return - changeInValue / 2 * (time * (time - 2) - 1) + startValue;
34-
	}
34+
	}
35-
35+
36-
	// cubic easing in - accelerating from zero velocity
36+
	// cubic easing in - accelerating from zero velocity
37-
	public static function easeInCubic(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
37+
	public static function easeInCubic(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
38-
	{
38+
	{
39-
		time /= duration;
39+
		time /= duration;
40-
		return changeInValue * Math.pow(time, 3) + startValue;
40+
		return changeInValue * Math.pow(time, 3) + startValue;
41-
	}
41+
	}
42-
42+
43-
	// cubic easing out - decelerating to zero velocity
43+
	// cubic easing out - decelerating to zero velocity
44-
	public static function easeOutCubic(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
44+
	public static function easeOutCubic(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
45-
	{
45+
	{
46-
		time /= duration;
46+
		time /= duration;
47-
		time --;
47+
		time --;
48-
		return changeInValue * (Math.pow(time, 3) + 1) + startValue;
48+
		return changeInValue * (Math.pow(time, 3) + 1) + startValue;
49-
	}
49+
	}
50-
50+
51-
	// cubic easing in/out - acceleration until halfway:Float, then deceleration
51+
	// cubic easing in/out - acceleration until halfway:Float, then deceleration
52-
	public static function easeInOutCubic(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
52+
	public static function easeInOutCubic(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
53-
	{
53+
	{
54-
		time /= duration / 2;
54+
		time /= duration / 2;
55-
		if(time < 1) return changeInValue / 2 * Math.pow(time, 3) + startValue;
55+
		if(time < 1) return changeInValue / 2 * Math.pow(time, 3) + startValue;
56-
		time -= 2;
56+
		time -= 2;
57-
		return changeInValue / 2 * (Math.pow(time, 3) + 2) + startValue;
57+
		return changeInValue / 2 * (Math.pow(time, 3) + 2) + startValue;
58-
	}
58+
	}
59-
59+
60-
	// quartic easing in - accelerating from zero velocity
60+
	// quartic easing in - accelerating from zero velocity
61-
	public static function easeInQuart(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
61+
	public static function easeInQuart(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
62-
	{
62+
	{
63-
		time /= duration;
63+
		time /= duration;
64-
		return changeInValue * Math.pow(time, 4) + startValue;
64+
		return changeInValue * Math.pow(time, 4) + startValue;
65-
	}
65+
	}
66-
66+
67-
	// quartic easing out - decelerating to zero velocity
67+
	// quartic easing out - decelerating to zero velocity
68-
	public static function easeOutQuart(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
68+
	public static function easeOutQuart(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
69-
	{
69+
	{
70-
		time /= duration;
70+
		time /= duration;
71-
		time --;
71+
		time --;
72-
		return - changeInValue * (Math.pow(time, 4) - 1) + startValue;
72+
		return - changeInValue * (Math.pow(time, 4) - 1) + startValue;
73-
	}
73+
	}
74-
74+
75-
	// quartic easing in/out - acceleration until halfway:Float, then deceleration
75+
	// quartic easing in/out - acceleration until halfway:Float, then deceleration
76-
	public static function easeInOutQuart(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
76+
	public static function easeInOutQuart(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
77-
	{
77+
	{
78-
		time /= duration / 2;
78+
		time /= duration / 2;
79-
		if(time < 1) return changeInValue / 2 * Math.pow(time, 4) + startValue;
79+
		if(time < 1) return changeInValue / 2 * Math.pow(time, 4) + startValue;
80-
		time -= 2;
80+
		time -= 2;
81-
		return - changeInValue / 2 * (Math.pow(time, 4) - 2) + startValue;
81+
		return - changeInValue / 2 * (Math.pow(time, 4) - 2) + startValue;
82-
	}
82+
	}
83-
83+
84-
	// quintic easing in - accelerating from zero velocity
84+
	// quintic easing in - accelerating from zero velocity
85-
	public static function easeInQuint(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
85+
	public static function easeInQuint(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
86-
	{
86+
	{
87-
		time /= duration;
87+
		time /= duration;
88-
		return changeInValue * Math.pow(time, 5) + startValue;
88+
		return changeInValue * Math.pow(time, 5) + startValue;
89-
	}
89+
	}
90-
90+
91-
	// quintic easing out - decelerating to zero velocity
91+
	// quintic easing out - decelerating to zero velocity
92-
	public static function easeOutQuint(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
92+
	public static function easeOutQuint(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
93-
	{
93+
	{
94-
		time /= duration;
94+
		time /= duration;
95-
		time --;
95+
		time --;
96-
		return changeInValue * (Math.pow(time, 5) + 1) + startValue;
96+
		return changeInValue * (Math.pow(time, 5) + 1) + startValue;
97-
	}
97+
	}
98-
98+
99-
	// quintic easing in/out - acceleration until halfway:Float, then deceleration
99+
	// quintic easing in/out - acceleration until halfway:Float, then deceleration
100-
	public static function easeInOutQuint(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
100+
	public static function easeInOutQuint(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
101-
	{
101+
	{
102-
		time /= duration / 2;
102+
		time /= duration / 2;
103-
		if(time < 1) return changeInValue / 2 * Math.pow(time, 5) + startValue;
103+
		if(time < 1) return changeInValue / 2 * Math.pow(time, 5) + startValue;
104-
		time -= 2;
104+
		time -= 2;
105-
		return changeInValue / 2 * (Math.pow(time, 5) + 2) + startValue;
105+
		return changeInValue / 2 * (Math.pow(time, 5) + 2) + startValue;
106-
	}
106+
	}
107-
107+
108-
	// sinusoidal easing in - accelerating from zero velocity
108+
	// sinusoidal easing in - accelerating from zero velocity
109-
	public static function easeInSine(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
109+
	public static function easeInSine(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
110-
	{
110+
	{
111-
		return - changeInValue * Math.cos(time / duration * (Math.PI / 2)) + changeInValue + startValue;
111+
		return - changeInValue * Math.cos(time / duration * (Math.PI / 2)) + changeInValue + startValue;
112-
	}	
112+
	}	
113-
113+
114-
	// sinusoidal easing out - decelerating to zero velocity
114+
	// sinusoidal easing out - decelerating to zero velocity
115-
	public static function easeOutSine(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
115+
	public static function easeOutSine(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
116-
	{
116+
	{
117-
		return changeInValue * Math.sin(time / duration * (Math.PI / 2)) + startValue;
117+
		return changeInValue * Math.sin(time / duration * (Math.PI / 2)) + startValue;
118-
	}		
118+
	}		
119-
119+
120-
	// sinusoidal easing in/out - accelerating until halfway:Float, then decelerating
120+
	// sinusoidal easing in/out - accelerating until halfway:Float, then decelerating
121-
	public static function easeInOutSine(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
121+
	public static function easeInOutSine(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
122-
	{
122+
	{
123-
		return - changeInValue / 2 * (Math.cos(Math.PI * time / d) - 1) + startValue;
123+
		return - changeInValue / 2 * (Math.cos(Math.PI * time / d) - 1) + startValue;
124-
	}		
124+
	}		
125-
125+
126-
	// exponential easing in - accelerating from zero velocity
126+
	// exponential easing in - accelerating from zero velocity
127-
	public static function easeInExpo(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
127+
	public static function easeInExpo(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
128-
	{
128+
	{
129-
		return changeInValue * Math.pow(2, 10 * (time / duration - 1) ) + startValue;
129+
		return changeInValue * Math.pow(2, 10 * (time / duration - 1) ) + startValue;
130-
	}
130+
	}
131-
131+
132-
	// exponential easing out - decelerating to zero velocity
132+
	// exponential easing out - decelerating to zero velocity
133-
	public static function easeOutExpo(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
133+
	public static function easeOutExpo(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
134-
	{
134+
	{
135-
		return changeInValue * (-Math.pow(2, - 10 * time / duration ) + 1 ) + startValue;
135+
		return changeInValue * (-Math.pow(2, - 10 * time / duration ) + 1 ) + startValue;
136-
	}		
136+
	}		
137-
137+
138-
	// exponential easing in/out - accelerating until halfway:Float, then decelerating
138+
	// exponential easing in/out - accelerating until halfway:Float, then decelerating
139-
	public static function easeInOutExpo(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
139+
	public static function easeInOutExpo(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
140-
	{
140+
	{
141-
		time /= duration / 2;
141+
		time /= duration / 2;
142-
		if(time < 1) return changeInValue / 2 * Math.pow(2, 10 * (time - 1) ) + startValue;
142+
		if(time < 1) return changeInValue / 2 * Math.pow(2, 10 * (time - 1) ) + startValue;
143-
		time --;
143+
		time --;
144-
		return changeInValue / 2 * (-Math.pow(2, - 10 * t) + 2 ) + startValue;
144+
		return changeInValue / 2 * (-Math.pow(2, - 10 * t) + 2 ) + startValue;
145-
	}
145+
	}
146-
146+
147-
	// circular easing in - accelerating from zero velocity
147+
	// circular easing in - accelerating from zero velocity
148-
	public static function easeInCirc(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
148+
	public static function easeInCirc(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
149-
	{
149+
	{
150-
		time /= duration;
150+
		time /= duration;
151-
		return - changeInValue * (Math.sqrt(1 - time * t) - 1) + startValue;
151+
		return - changeInValue * (Math.sqrt(1 - time * t) - 1) + startValue;
152-
	}
152+
	}
153-
153+
154-
	// circular easing out - decelerating to zero velocity
154+
	// circular easing out - decelerating to zero velocity
155-
	public static function easeOutCirc(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
155+
	public static function easeOutCirc(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
156-
	{
156+
	{
157-
		time /= duration;
157+
		time /= duration;
158-
		time --;
158+
		time --;
159-
		return changeInValue * Math.sqrt(1 - time * t) + startValue;
159+
		return changeInValue * Math.sqrt(1 - time * t) + startValue;
160-
	}			
160+
	}			
161-
161+
162-
	// circular easing in/out - acceleration until halfway:Float, then deceleration
162+
	// circular easing in/out - acceleration until halfway:Float, then deceleration
163-
	public static function easeInOutCirc(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
163+
	public static function easeInOutCirc(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
164-
	{
164+
	{
165-
		time /= duration / 2;
165+
		time /= duration / 2;
166-
		if(time < 1) return - changeInValue / 2 * (Math.sqrt(1 - time * t) - 1) + startValue;
166+
		if(time < 1) return - changeInValue / 2 * (Math.sqrt(1 - time * t) - 1) + startValue;
167-
		time -= 2;
167+
		time -= 2;
168-
		return changeInValue / 2 * (Math.sqrt(1 - time * t) + 1) + startValue;
168+
		return changeInValue / 2 * (Math.sqrt(1 - time * t) + 1) + startValue;
169-
	}	 
169+
	}	 
170-
170+
171-
	// back easing in - back up on source
171+
	// back easing in - back up on source
172-
	// o=1.70158 for a 10% bounce
172+
	// o=1.70158 for a 10% bounce
173-
	public static function easeInBack(time:Float, startValue:Float, changeInValue:Float, duration:Float, o:Float): Float
173+
	public static function easeInBack(time:Float, startValue:Float, changeInValue:Float, duration:Float, o:Float): Float
174-
	{
174+
	{
175-
		return changeInValue * (time /= d) * time * ((o + 1) * time - o) + startValue;
175+
		return changeInValue * (time /= d) * time * ((o + 1) * time - o) + startValue;
176-
	}
176+
	}
177-
	 
177+
	 
178-
	// back easing out - overshoot target
178+
	// back easing out - overshoot target
179-
	// o=1.70158 for a 10% bounce
179+
	// o=1.70158 for a 10% bounce
180-
	public static function easeOutBack(time:Float, startValue:Float, changeInValue:Float, duration:Float, o:Float): Float
180+
	public static function easeOutBack(time:Float, startValue:Float, changeInValue:Float, duration:Float, o:Float): Float
181-
	{
181+
	{
182-
		return changeInValue * ((time = time / d-1) * time * ((o+1) * time + o) + 1) + startValue;
182+
		return changeInValue * ((time = time / d-1) * time * ((o+1) * time + o) + 1) + startValue;
183-
	}
183+
	}
184-
	 
184+
	 
185-
	// back easing in/out - back up on source then overshoot target
185+
	// back easing in/out - back up on source then overshoot target
186-
	// o=1.70158 for a 10% bounce
186+
	// o=1.70158 for a 10% bounce
187-
	public static function easeInOutBack(time:Float, startValue:Float, changeInValue:Float, duration:Float, o:Float): Float
187+
	public static function easeInOutBack(time:Float, startValue:Float, changeInValue:Float, duration:Float, o:Float): Float
188-
	{
188+
	{
189-
		if ((time /=duration / 2) < 1) return changeInValue / 2 * (time * time * (((o *= (1.525)) + 1) * time - o)) + startValue;
189+
		if ((time /=duration / 2) < 1) return changeInValue / 2 * (time * time * (((o *= (1.525)) + 1) * time - o)) + startValue;
190-
		return changeInValue / 2 * ((time -= 2) * time * (((o *= (1.525)) + 1) * time + o) + 2) + startValue;
190+
		return changeInValue / 2 * ((time -= 2) * time * (((o *= (1.525)) + 1) * time + o) + 2) + startValue;
191-
	}
191+
	}
192-
192+
193-
	// bounce easing in
193+
	// bounce easing in
194-
	public static function easeInBounce(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
194+
	public static function easeInBounce(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
195-
	{
195+
	{
196-
		return changeInValue - easeOutBounce (duration - time, 0, changeInValue, d) + startValue;
196+
		return changeInValue - easeOutBounce (duration - time, 0, changeInValue, d) + startValue;
197-
	}
197+
	}
198-
	 
198+
	 
199-
	// bounce easing out
199+
	// bounce easing out
200-
	public static function easeOutBounce(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
200+
	public static function easeOutBounce(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
201-
	{
201+
	{
202-
		if ((time /= d) < (1 / 2.75))
202+
		if ((time /= d) < (1 / 2.75))
203-
			return changeInValue * (7.5625 * time * t) + startValue;
203+
			return changeInValue * (7.5625 * time * t) + startValue;
204-
		else if (time < (2 / 2.75))
204+
		else if (time < (2 / 2.75))
205-
			return changeInValue * (7.5625 * (time -= (1.5 / 2.75)) * time + .75) + startValue;
205+
			return changeInValue * (7.5625 * (time -= (1.5 / 2.75)) * time + .75) + startValue;
206-
		else if (time < (2.5 / 2.75))
206+
		else if (time < (2.5 / 2.75))
207-
			return changeInValue * (7.5625 * (time -= (2.25 / 2.75)) * time + .9375) + startValue;
207+
			return changeInValue * (7.5625 * (time -= (2.25 / 2.75)) * time + .9375) + startValue;
208-
		return changeInValue * (7.5625 * (time -= (2.625 / 2.75)) * time + .984375) + startValue;
208+
		return changeInValue * (7.5625 * (time -= (2.625 / 2.75)) * time + .984375) + startValue;
209-
	}
209+
	}
210-
	 
210+
	 
211-
	// bounce easing in/out
211+
	// bounce easing in/out
212-
	public static function easeInOutBounce(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
212+
	public static function easeInOutBounce(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
213-
	{
213+
	{
214-
		if (time < duration / 2) return easeInBounce (time * 2, 0, changeInValue, d) * .5 + startValue;
214+
		if (time < duration / 2) return easeInBounce (time * 2, 0, changeInValue, d) * .5 + startValue;
215-
		return easeOutBounce(time * 2 - duration, 0, changeInValue, d) * .5 + changeInValue * .5 + startValue;
215+
		return easeOutBounce(time * 2 - duration, 0, changeInValue, d) * .5 + changeInValue * .5 + startValue;
216-
	}
216+
	}
217
}