|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProjektGopher\FFMpegTween; |
| 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 |
| 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 | + } |
| 169 | +} |
0 commit comments