Skip to content

Instantly share code, notes, and snippets.

@tvlooy
Created October 7, 2020 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvlooy/7f91b437d4acf39e07e0b43d4059e7aa to your computer and use it in GitHub Desktop.
Save tvlooy/7f91b437d4acf39e07e0b43d4059e7aa to your computer and use it in GitHub Desktop.
Intracto IO animation
#!/usr/bin/env php
<?php
require __DIR__.'/vendor/autoload.php';
use Symfony\Component\Console\Cursor;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Console\Terminal;
define('LOGOS', 50);
define('SPEED', 250000);
(new SingleCommandApplication())
->setCode(function (InputInterface $input, OutputInterface $output) {
$terminal = new Terminal();
$width = $terminal->getWidth();
$height = $terminal->getHeight();
$cursor = new Cursor($output);
$cursor->hide();
$cursor->clearScreen();
$logos = [];
while (true) {
// smoother startup
for ($i = round((LOGOS - count($logos)) * 0.3); $i > 0; $i--) {
$logos[] = new IOLogo(
$output,
$cursor,
rand(0, $width - 16),
rand(0, $height - 5)
);
}
foreach ($logos as $i => $logo) {
$logo->animate();
if (! $logo->visible()) {
unset($logos[$i]);
}
}
usleep(SPEED);
}
return 0;
})->run();
class IOLogo
{
private OutputInterface $output;
private Cursor $cursor;
private int $x;
private int $y;
private array $fg;
private array $bg;
public function __construct(OutputInterface $output, Cursor $cursor, int $x, int $y)
{
$this->output = $output;
$this->cursor = $cursor;
$this->x = $x;
$this->y = $y;
$this->fg = [ rand(0, 127), rand(0, 127), rand(0, 127) ];
$this->bg = [
min([255, $this->fg[0]+128]),
min([255, $this->fg[1]+128]),
min([255, $this->fg[2]+128])
];
}
public function animate(): void
{
$this->fade();
$this->write(0, " ,--. ,-----. ");
$this->write(1, " | | ' .-. ' ");
$this->write(2, " | | | | | | ");
$this->write(3, " ' | ' '-' ' ");
$this->write(4, " `-' `-----' ");
}
public function visible(): bool
{
return 255*6 >
$this->fg[0]+$this->fg[1]+$this->fg[2]+
$this->bg[0]+$this->bg[1]+$this->bg[2]
;
}
private function fade(): void
{
$this->fg = [
min([255, $this->fg[0]+20]),
min([255, $this->fg[1]+20]),
min([255, $this->fg[2]+20])
];
$this->bg = [
min([255, $this->bg[0]+20]),
min([255, $this->bg[1]+20]),
min([255, $this->bg[2]+20])
];
}
private function write($line, $txt): void
{
$this->cursor->moveToPosition($this->x, $this->y+$line);
$this->output->write('<fg=#'.$this->hexval($this->fg).';bg=#'.$this->hexval($this->bg).'>'.$txt.'</>');
}
private function hexval(array $value): string
{
return
str_pad(dechex($value[0]), 2, '0', STR_PAD_LEFT).
str_pad(dechex($value[1]), 2, '0', STR_PAD_LEFT).
str_pad(dechex($value[2]), 2, '0', STR_PAD_LEFT)
;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment