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