Skip to content

Commit

Permalink
[Task] Cleanup (#6)
Browse files Browse the repository at this point in the history
* extract build methods for testability

* add missing types

* handle nullable properties

* phpstan level 9

* update readme
  • Loading branch information
ProjektGopher committed Mar 31, 2023
1 parent 306208e commit 06565c9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
23 changes: 7 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Generate FFMpeg easing and tweening strings in Laravel.

[![Latest Version on Packagist](https://img.shields.io/packagist/v/projektgopher/laravel-ffmpeg-tween.svg?style=flat-square)](https://packagist.org/packages/projektgopher/laravel-ffmpeg-tween)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/projektgopher/laravel-ffmpeg-tween/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/projektgopher/laravel-ffmpeg-tween/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/projektgopher/laravel-ffmpeg-tween/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/projektgopher/laravel-ffmpeg-tween/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
Expand All @@ -26,22 +25,17 @@ The API is modelled after [The GreenSock Animation Platform (GSAP)](https://gree
and all the math for the easings is ported from [Easings.net](https://easings.net).
The stringification of these math strings is ported from [This Gitlab repo](https://gitlab.com/dak425/easing/-/blob/master/ffmpeg/ffmpeg.go)


## Installation

You can install the package via composer:

```bash
composer require projektgopher/laravel-ffmpeg-tween
```

### Using outside of a Laravel application

For now this package can only be used within a Laravel app, but there are plans to extract the core functionality out into a separate package that can be used without being bound to the framework.

## Usage
### Using outside of a Laravel application
For now this package can only be used within a Laravel app, but there are plans to extract the core functionality into a separate package that can be used without being bound to the framework.

Simple tween with delay and duration
### Simple tween with delay and duration
```php
use ProjektGopher\FFMpegTween\Tween;
use ProjektGopher\FFMpegTween\Timing;
Expand All @@ -55,7 +49,7 @@ $x = (new Tween())
->ease(Ease::OutSine);
```

Animation sequences using keyframes
### Animation sequences using keyframes
```php
use ProjektGopher\FFMpegTween\Keyframe;
use ProjektGopher\FFMpegTween\Timeline;
Expand All @@ -79,15 +73,16 @@ $x->keyframe((new Keyframe)
->duration(Timing::seconds(1))
);
```

> **Note** `new Timeline()` returns a _fluent_ api, meaning methods can be chained as well.
## Testing

```bash
composer test
```

### Visual Snapshot Testing
#### Easing
To generate plots of all `Ease` methods, from the project root, run
```bash
./scripts/generateEasings
Expand All @@ -96,6 +91,7 @@ The 256x256 PNGs will be generated in the `tests/Snapshots/Easings` 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).

#### Timelines
To generate a video using a `Timeline` with `Keyframes`, from the project root, run
```bash
./scripts/generateTimeline
Expand All @@ -110,22 +106,17 @@ chmod -R 777 ./scripts
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Security Vulnerabilities

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

## Credits

- [Len Woodward](https://github.com/ProjektGopher)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 4
level: 9
paths:
- src
tmpDir: build/phpstan
Expand Down
13 changes: 10 additions & 3 deletions src/Timeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ProjektGopher\FFMpegTween;

use ProjektGopher\FFMpegTween\Enums\Ease;

class Timeline
{
private array $keyframes = [];
Expand Down Expand Up @@ -42,8 +44,8 @@ public function buildTweens(): void
->from($this->getKeyframeByIndex($index - 1)->value)
->to($keyframe->value)
->delay(Timing::seconds($current_time))
->duration($keyframe->duration)
->ease($keyframe->ease);
->duration($keyframe->duration ?? Timing::seconds(0))
->ease($keyframe->ease ?? Ease::Linear);
}

$current_time += $keyframe->hold?->seconds;
Expand All @@ -56,7 +58,7 @@ public function getKeyframeByIndex(int $index): Keyframe
return $this->keyframes[$index];
}

public function __toString(): string
public function build(): string
{
if (count($this->tweens) === 0) {
$this->buildTweens();
Expand All @@ -75,4 +77,9 @@ public function __toString(): string

return $timeline;
}

public function __toString(): string
{
return $this->build();
}
}
13 changes: 9 additions & 4 deletions src/Tween.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class Tween

protected string $ease;

public function from($from): self
public function from(string $from): self
{
$this->from = "({$from})";

return $this;
}

public function to($to): self
public function to(string $to): self
{
$this->to = "({$to})";

Expand Down Expand Up @@ -70,7 +70,7 @@ private function getDelta(): string
* Builds the tween string which can be used in an FFMpeg command.
* This is called automatically at the end of the chain.
*/
public function __toString(): string
public function build(): string
{
if (! $this->ease) {
$this->ease(AvailableEasings::Linear);
Expand All @@ -90,7 +90,12 @@ public function __toString(): string
return "if(lt(t\,{$this->delay})\,{$this->from}\,if(gt(t\,{$this->delay}+{$this->duration})\,{$this->to}\,{$this->from}+({$this->getDelta()}*{$this->ease})))";
}

public static function __callStatic($name, $arguments): self
public function __toString(): string
{
return $this->build();
}

public static function __callStatic(string $name, array $arguments): self
{
return (new self)->$name(...$arguments);
}
Expand Down

0 comments on commit 06565c9

Please sign in to comment.