|
2 | 2 |
|
3 | 3 | namespace ProjektGopher\FFMpegTween; |
4 | 4 |
|
5 | | -/** |
6 | | - * Ease |
7 | | - * |
8 | | - * Returns a string which will evaluate to a value between 0 and 1. |
9 | | - * This allows us to multiply the _change_ in value by the chosen |
10 | | - * easing function to get the actual value at a given time. |
11 | | - * |
12 | | - * (paraphrased from easings.net) |
13 | | - * The argument `$time` should be a `string` which represents the absolute |
14 | | - * progress of the animation in the bounds of 0 (beginning of the animation) |
15 | | - * and 1 (end of animation). |
16 | | - */ |
17 | | -class Ease |
| 5 | +enum Ease: string |
18 | 6 | { |
19 | | - public static function Linear(string $time): string |
20 | | - { |
21 | | - return $time; |
22 | | - } |
23 | | - |
24 | | - public static function EaseInSine(string $time): string |
25 | | - { |
26 | | - return "1-cos((({$time})*PI)/2)"; |
27 | | - } |
28 | | - |
29 | | - public static function EaseOutSine(string $time): string |
30 | | - { |
31 | | - return "sin((({$time})*PI)/2)"; |
32 | | - } |
33 | | - |
34 | | - public static function EaseInOutSine(string $time): string |
35 | | - { |
36 | | - return "-(cos(PI*({$time}))-1)/2"; |
37 | | - } |
38 | | - |
39 | | - public static function EaseInQuad(string $time): string |
40 | | - { |
41 | | - return "pow(({$time})\\,2)"; |
42 | | - } |
43 | | - |
44 | | - public static function EaseOutQuad(string $time): string |
45 | | - { |
46 | | - return "1-(1-({$time}))*(1-({$time}))"; |
47 | | - } |
48 | | - |
49 | | - public static function EaseInOutQuad(string $time): string |
50 | | - { |
51 | | - return "if(lt(({$time})\\,0.5)\\,2*pow(({$time})\\,2)\\,1-pow(-2*({$time})+2\\,2)/2)"; |
52 | | - } |
53 | | - |
54 | | - public static function EaseInCubic(string $time): string |
55 | | - { |
56 | | - return "pow(({$time})\\,3)"; |
57 | | - } |
58 | | - |
59 | | - public static function EaseOutCubic(string $time): string |
60 | | - { |
61 | | - return "1-pow(1-({$time})\\,3)"; |
62 | | - } |
63 | | - |
64 | | - public static function EaseInOutCubic(string $time): string |
65 | | - { |
66 | | - return "if(lt(({$time})\\,0.5)\\,4*pow(({$time})\\,3)\\,1-pow(-2*({$time})+2\\,3)/2)"; |
67 | | - } |
68 | | - |
69 | | - public static function EaseInQuart(string $time): string |
70 | | - { |
71 | | - return "pow(({$time})\\,4)"; |
72 | | - } |
73 | | - |
74 | | - public static function EaseOutQuart(string $time): string |
75 | | - { |
76 | | - return "1-pow(1-({$time})\\,4)"; |
77 | | - } |
78 | | - |
79 | | - public static function EaseInOutQuart(string $time): string |
80 | | - { |
81 | | - return "if(lt(({$time})\\,0.5)\\,8*pow(({$time})\\,4)\\,1-pow(-2*({$time})+2\\,4)/2)"; |
82 | | - } |
83 | | - |
84 | | - public static function EaseInQuint(string $time): string |
85 | | - { |
86 | | - return "pow(({$time})\\,5)"; |
87 | | - } |
88 | | - |
89 | | - public static function EaseOutQuint(string $time): string |
90 | | - { |
91 | | - return "1-pow(1-({$time})\\,5)"; |
92 | | - } |
93 | | - |
94 | | - public static function EaseInOutQuint(string $time): string |
95 | | - { |
96 | | - return "if(lt(({$time})\\,0.5)\\,16*pow(({$time})\\,5)\\,1-pow(-2*({$time})+2\\,5)/2)"; |
97 | | - } |
98 | | - |
99 | | - public static function EaseInExpo(string $time): string |
100 | | - { |
101 | | - return "if(eq(({$time})\\,0)\\,0\\,pow(2\\,10*({$time})-10))"; |
102 | | - } |
103 | | - |
104 | | - public static function EaseOutExpo(string $time): string |
105 | | - { |
106 | | - return "if(eq(({$time})\\,1)\\,1\\,1-pow(2\\,-10*({$time})))"; |
107 | | - } |
108 | | - |
109 | | - public static function EaseInOutExpo(string $time): string |
110 | | - { |
111 | | - $firstExp = "pow(2\\,20*({$time})-10)/2"; |
112 | | - $secondExp = "(2-pow(2\\,-20*({$time})+10))/2"; |
113 | | - |
114 | | - // We have to set the return value for 0 and 1 explicitly as we lose accuracy at the 4th decimal place. |
115 | | - return "if(eq(({$time})\\,0)\\,0\\,if(eq(({$time})\\,1)\\,1\\,if(lt(({$time})\\,0.5)\\,{$firstExp}\\,{$secondExp})))"; |
116 | | - } |
117 | | - |
118 | | - public static function EaseInCirc(string $time): string |
119 | | - { |
120 | | - return "1-sqrt(1-pow(({$time})\\,2))"; |
121 | | - } |
122 | | - |
123 | | - public static function EaseOutCirc(string $time): string |
124 | | - { |
125 | | - return "sqrt(1-pow(({$time})-1\\,2))"; |
126 | | - } |
127 | | - |
128 | | - public static function EaseInOutCirc(string $time): string |
129 | | - { |
130 | | - return "if(lt(({$time})\\,0.5)\\,(1-sqrt(1-pow(2*({$time})\\,2)))/2\\,(sqrt(1-pow(-2*({$time})+2\\,2))+1)/2)"; |
131 | | - } |
132 | | - |
133 | | - public static function EaseInBack(string $time): string |
134 | | - { |
135 | | - $c1 = 1.70158; |
136 | | - $c3 = $c1 + 1; |
137 | | - |
138 | | - return "{$c3}*pow(({$time})\\,3)-{$c1}*pow(({$time})\\,2)"; |
139 | | - } |
140 | | - |
141 | | - public static function EaseOutBack(string $time): string |
142 | | - { |
143 | | - $c1 = 1.70158; |
144 | | - $c3 = $c1 + 1; |
145 | | - |
146 | | - return "1+{$c3}*pow(({$time})-1\\,3)+{$c1}*pow(({$time})-1\\,2)"; |
147 | | - } |
148 | | - |
149 | | - public static function EaseInOutBack(string $time): string |
150 | | - { |
151 | | - $c1 = 1.70158; |
152 | | - $c2 = $c1 * 1.525; |
153 | | - |
154 | | - return "if(lt(({$time})\\,0.5)\\,(pow(2*({$time})\\,2)*(({$c2}+1)*2*({$time})-{$c2}))/2\\,(pow(2*({$time})-2\\,2)*(({$c2}+1)*(({$time})*2-2)+{$c2})+2)/2)"; |
155 | | - } |
156 | | - |
157 | | - public static function EaseInElastic(string $time): string |
158 | | - { |
159 | | - $c4 = (2 * M_PI) / 3; |
160 | | - |
161 | | - return "if(eq(({$time})\\,0)\\,0\\,if(eq(({$time})\\,1)\\,1\\,-pow(2\\,10*({$time})-10)*sin((({$time})*10-10.75)*{$c4})))"; |
162 | | - } |
163 | | - |
164 | | - public static function EaseOutElastic(string $time): string |
165 | | - { |
166 | | - $c4 = (2 * M_PI) / 3; |
167 | | - |
168 | | - return "if(eq(({$time})\\,0)\\,0\\,if(eq(({$time})\\,1)\\,1\\,pow(2\\,-10*({$time}))*sin((({$time})*10-0.75)*{$c4})+1))"; |
169 | | - } |
170 | | - |
171 | | - public static function EaseInOutElastic(string $time): string |
172 | | - { |
173 | | - $c5 = (2 * M_PI) / 4.5; |
174 | | - $ltExpr = "-(pow(2\\,20*({$time})-10)*sin((20*({$time})-11.125)*{$c5}))/2"; |
175 | | - $gtExpr = "(pow(2\\,-20*({$time})+10)*sin((20*({$time})-11.125)*{$c5}))/2+1"; |
176 | | - |
177 | | - return "if(eq(({$time})\\,0)\\,0\\,if(eq(({$time})\\,1)\\,1\\,if(lt(({$time})\\,0.5)\\,{$ltExpr}\\,{$gtExpr})))"; |
178 | | - } |
179 | | - |
180 | | - public static function EaseInBounce(string $time): string |
181 | | - { |
182 | | - $x = self::EaseOutBounce("1-({$time})"); |
183 | | - |
184 | | - return "1-({$x})"; |
185 | | - } |
186 | | - |
187 | | - public static function EaseOutBounce(string $time): string |
188 | | - { |
189 | | - $n1 = 7.5625; |
190 | | - $d1 = 2.75; |
191 | | - $firstExpr = "{$n1}*pow(({$time})\\,2)"; |
192 | | - $secondTime = "(({$time})-1.5/{$d1})"; |
193 | | - $secondExpr = "{$n1}*{$secondTime}*{$secondTime}+0.75"; |
194 | | - $thirdTime = "(({$time})-2.25/{$d1})"; |
195 | | - $thirdExpr = "{$n1}*{$thirdTime}*{$thirdTime}+0.9375"; |
196 | | - $fourthTime = "(({$time})-2.65/{$d1})"; |
197 | | - $fourthExpr = "{$n1}*{$fourthTime}*{$fourthTime}+0.984375"; |
198 | | - |
199 | | - return "if(lt(({$time})\\, 1/{$d1})\\,{$firstExpr}\\,if(lt(({$time})\\,2/{$d1})\\,{$secondExpr}\\,if(lt(({$time})\\,2.5/{$d1})\\,{$thirdExpr}\\,{$fourthExpr})))"; |
200 | | - } |
201 | | - |
202 | | - public static function EaseInOutBounce(string $time): string |
203 | | - { |
204 | | - $x1 = self::EaseOutBounce("1-2*({$time})"); |
205 | | - $x2 = self::EaseOutBounce("2*({$time})-1"); |
206 | | - $ltExpr = "(1-({$x1}))/2"; |
207 | | - $gtExpr = "(1+({$x2}))/2"; |
208 | | - |
209 | | - return "if(lt(({$time})\\,0.5)\\,{$ltExpr}\\,{$gtExpr})"; |
| 7 | + case Linear = 'Linear'; |
| 8 | + case InSine = 'EaseInSine'; |
| 9 | + case OutSine = 'EaseOutSine'; |
| 10 | + case InOutSine = 'EaseInOutSine'; |
| 11 | + case InQuad = 'EaseInQuad'; |
| 12 | + case OutQuad = 'EaseOutQuad'; |
| 13 | + case InOutQuad = 'EaseInOutQuad'; |
| 14 | + case InCubic = 'EaseInCubic'; |
| 15 | + case OutCubic = 'EaseOutCubic'; |
| 16 | + case InOutCubic = 'EaseInOutCubic'; |
| 17 | + case InQuart = 'EaseInQuart'; |
| 18 | + case OutQuart = 'EaseOutQuart'; |
| 19 | + case InOutQuart = 'EaseInOutQuart'; |
| 20 | + case InQuint = 'EaseInQuint'; |
| 21 | + case OutQuint = 'EaseOutQuint'; |
| 22 | + case InOutQuint = 'EaseInOutQuint'; |
| 23 | + case InExpo = 'EaseInExpo'; |
| 24 | + case OutExpo = 'EaseOutExpo'; |
| 25 | + case InOutExpo = 'EaseInOutExpo'; |
| 26 | + case InCirc = 'EaseInCirc'; |
| 27 | + case OutCirc = 'EaseOutCirc'; |
| 28 | + case InOutCirc = 'EaseInOutCirc'; |
| 29 | + case InBack = 'EaseInBack'; |
| 30 | + case OutBack = 'EaseOutBack'; |
| 31 | + case InOutBack = 'EaseInOutBack'; |
| 32 | + case InElastic = 'EaseInElastic'; |
| 33 | + case OutElastic = 'EaseOutElastic'; |
| 34 | + case InOutElastic = 'EaseInOutElastic'; |
| 35 | + case InBounce = 'EaseInBounce'; |
| 36 | + case OutBounce = 'EaseOutBounce'; |
| 37 | + case InOutBounce = 'EaseInOutBounce'; |
| 38 | + |
| 39 | + public function make(string $time): string |
| 40 | + { |
| 41 | + // Wrap the result in parentheses to ensure that it |
| 42 | + // doesn't interfere with surrounding expressions |
| 43 | + return implode([ |
| 44 | + '(', |
| 45 | + EaseFunctions::{$this->value}($time), |
| 46 | + ')', |
| 47 | + ]); |
210 | 48 | } |
211 | 49 | } |
0 commit comments