Skip to content

Commit cef698b

Browse files
committed
copy classes from PoC
1 parent 94df118 commit cef698b

File tree

4 files changed

+321
-0
lines changed

4 files changed

+321
-0
lines changed

src/Ease.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
}

src/Enums/Ease.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace ProjektGopher\FFMpegTween\Enums;
4+
5+
enum Ease: string
6+
{
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+
}

src/Timing.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace ProjektGopher\FFMpegTween;
4+
5+
class Timing
6+
{
7+
public function __construct(
8+
public float $seconds,
9+
)
10+
{}
11+
12+
public static function seconds(float $seconds): self
13+
{
14+
return new self($seconds);
15+
}
16+
17+
public static function milliseconds(int $milliseconds): self
18+
{
19+
return new self($milliseconds / 1000);
20+
}
21+
22+
public static function ms(int $milliseconds): self
23+
{
24+
return self::milliseconds($milliseconds);
25+
}
26+
27+
public function __toString():string{
28+
return (string) $this->seconds;
29+
}
30+
}

src/Tween.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace ProjektGopher\FFMpegTween;
4+
5+
use ProjektGopher\FFMpegTween\Enums\Ease as AvailableEasings;
6+
7+
class Tween
8+
{
9+
protected string $from;
10+
protected string $to;
11+
protected string $duration = '0.3';
12+
protected string $delay = '0';
13+
protected string $ease;
14+
15+
public function from($from): self
16+
{
17+
$this->from = "({$from})";
18+
return $this;
19+
}
20+
21+
public function to($to): self
22+
{
23+
$this->to = "({$to})";
24+
return $this;
25+
}
26+
27+
public function duration(Timing $duration): self
28+
{
29+
$this->duration = (string) $duration;
30+
return $this;
31+
}
32+
33+
public function delay(Timing $delay): self
34+
{
35+
$this->delay = (string) $delay;
36+
return $this;
37+
}
38+
39+
public function ease(AvailableEasings $ease): self
40+
{
41+
$easeString = Ease::{$ease->value}("(t-{$this->delay})/{$this->duration}");
42+
$this->ease = "({$easeString})";
43+
return $this;
44+
}
45+
46+
/**
47+
* This should always evaluate to a value between
48+
* zero and one.
49+
*/
50+
private function getDelta(): string
51+
{
52+
return "({$this->to}-{$this->from})";
53+
}
54+
55+
/**
56+
* Builds the tween string which can be used in an FFMpeg command.
57+
* This is called automatically at the end of the chain.
58+
*
59+
* @return string
60+
*/
61+
public function __toString(): string
62+
{
63+
if (! $this->ease) $this->ease(AvailableEasings::Linear);
64+
65+
// if t is less than delay, from
66+
// else if t is greater than delay + duration, to
67+
// else, from + delta*ease
68+
69+
// if( lt(t,{$this->delay}),
70+
// {$this->from},
71+
// if( gt(t,{$this->delay}+{$this->duration}),
72+
// {$this->to},
73+
// {$this->from}+({$this->getDelta()}*{$this->ease})
74+
// )
75+
// )
76+
return "if(lt(t\,{$this->delay})\,{$this->from}\,if(gt(t\,{$this->delay}+{$this->duration})\,{$this->to}\,{$this->from}+({$this->getDelta()}*{$this->ease})))";
77+
}
78+
79+
public static function __callStatic($name, $arguments): self
80+
{
81+
return (new self)->$name(...$arguments);
82+
}
83+
}

0 commit comments

Comments
 (0)