Skip to content

Commit

Permalink
[Feat/Fix] Easing snapshots (#4)
Browse files Browse the repository at this point in the history
* create snapshots folder, but ignore its contents

* script to generate snapshots of easing plots

* fix all broken easings

* update readme
  • Loading branch information
ProjektGopher authored Mar 27, 2023
1 parent 0e5a555 commit 733a8ce
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ phpunit.xml
phpstan.neon
testbench.yaml
vendor
tests/Snapshots/*.png
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ $x = (new Tween())
composer test
```

### Visual Snapshot Testing
To generate plots of all `Ease` methods, from the project root, run
```bash
./scripts/generateSnapshots
```
The 256x256 PNGs will be generated in the `tests/Snapshots` directory.
These snapshots will be ignored by git, but allow visual inspection of the plots to
compare against known good sources, like [Easings.net](https://easings.net).

> **Note** The `scripts` directory _may_ need to have its permissions changed to allow script execution
```bash
chmod -R 777 ./scripts
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
36 changes: 36 additions & 0 deletions scripts/generateSnapshots
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env php
<?php

require_once __DIR__.'/../vendor/autoload.php';

use ProjektGopher\FFMpegTween\Enums\Ease as AvailableEasings;
use ProjektGopher\FFMpegTween\Ease;

echo 'starting... (fail fast)'.PHP_EOL;

$time = "X/H";

foreach (AvailableEasings::cases() as $ease) {
echo "Generating snapshot for {$ease->value} easing...".PHP_EOL;
$easeMultiplier = Ease::{$ease->value}($time);

$input = "-f lavfi -i \"color=c=black:s=256x256:d=1\"";
$margin = '28';
$filter = "-vf \"geq=if(eq(round((H-2*{$margin})*({$easeMultiplier}))\,H-Y-{$margin})\,128\,0):128:128\"";
$out = "-frames:v 1 -update 1 tests/Snapshots/{$ease->value}.png";
$redirect = '2>&1'; // redirect stderr to stdout

$cmd = "ffmpeg -y {$input} {$filter} {$out} {$redirect}";

(array) $output = [];
(int) $code = 0;
exec($cmd, $output, $code);

if ($code !== 0) {
echo PHP_EOL;
echo "Failed to generate snapshot for {$ease->value} easing.".PHP_EOL;
echo "Output: ".PHP_EOL;
echo implode(PHP_EOL, $output).PHP_EOL;
exit(1);
}
}
25 changes: 13 additions & 12 deletions src/Ease.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public static function EaseInOutExpo(string $time): string
$firstExp = "pow(2\\,20*({$time})-10)/2";
$secondExp = "(2-pow(2\\,-20*({$time})+10))/2";

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)))";
// We have to set the return value for 0 and 1 explicitly as we lose accuracy at the 4th decimal place.
return "if(eq(({$time})\\,0)\\,0\\,if(eq(({$time})\\,1)\\,1\\,if(lt(({$time})\\,0.5)\\,{$firstExp}\\,{$secondExp})))";
}

public static function EaseInCirc(string $time): string
Expand All @@ -134,7 +135,7 @@ public static function EaseInBack(string $time): string
$c1 = 1.70158;
$c3 = $c1 + 1;

return "{$c1}*pow(({$time})\\,3)-{$c3}*pow(({$time})\\,2)";
return "{$c3}*pow(({$time})\\,3)-{$c1}*pow(({$time})\\,2)";
}

public static function EaseOutBack(string $time): string
Expand All @@ -157,7 +158,7 @@ public static function EaseInElastic(string $time): string
{
$c4 = (2 * M_PI) / 3;

return "if(eq(({$time})\\,0)\\,0\\,if(eq(({$time})\\,1)\\,1\\,-pow(2\\,10*({$time})-10)*sin(({$time})*10-10.75)*{$c4}))";
return "if(eq(({$time})\\,0)\\,0\\,if(eq(({$time})\\,1)\\,1\\,-pow(2\\,10*({$time})-10)*sin((({$time})*10-10.75)*{$c4})))";
}

public static function EaseOutElastic(string $time): string
Expand Down Expand Up @@ -188,12 +189,12 @@ public static function EaseOutBounce(string $time): string
$n1 = 7.5625;
$d1 = 2.75;
$firstExpr = "{$n1}*pow(({$time})\\,2)";
$secondTime = "(({$time})-1.5)";
$secondExpr = "{$n1}*({$secondTime}/{$d1})*({$secondTime})+0.75";
$thirdTime = "(({$time})-2.25)";
$thirdExpr = "{$n1}*({$thirdTime}/{$d1})*({$thirdTime})+0.9375";
$fourthTime = "(({$time})-2.65)";
$fourthExpr = "{$n1}*({$fourthTime}/{$d1})*({$fourthTime})+0.984375";
$secondTime = "(({$time})-1.5/{$d1})";
$secondExpr = "{$n1}*{$secondTime}*{$secondTime}+0.75";
$thirdTime = "(({$time})-2.25/{$d1})";
$thirdExpr = "{$n1}*{$thirdTime}*{$thirdTime}+0.9375";
$fourthTime = "(({$time})-2.65/{$d1})";
$fourthExpr = "{$n1}*{$fourthTime}*{$fourthTime}+0.984375";

return "if(lt(({$time})\\, 1/{$d1})\\,{$firstExpr}\\,if(lt(({$time})\\,2/{$d1})\\,{$secondExpr}\\,if(lt(({$time})\\,2.5/{$d1})\\,{$thirdExpr}\\,{$fourthExpr})))";
}
Expand All @@ -202,9 +203,9 @@ public static function EaseInOutBounce(string $time): string
{
$x1 = self::EaseOutBounce("1-2*({$time})");
$x2 = self::EaseOutBounce("2*({$time})-1");
$gtExpr = "(1-({$x1}))/2";
$ltExpr = "(1+({$x2}))/2";
$ltExpr = "(1-({$x1}))/2";
$gtExpr = "(1+({$x2}))/2";

return "if(lt(({$time})\\,0.5)\\,{$ltExpr}\\,{$gtExpr}";
return "if(lt(({$time})\\,0.5)\\,{$ltExpr}\\,{$gtExpr})";
}
}
Empty file added tests/Snapshots/.gitkeep
Empty file.

0 comments on commit 733a8ce

Please sign in to comment.