Skip to content

Instantly share code, notes, and snippets.

@clort81
Last active November 8, 2021 08:48
Show Gist options
  • Save clort81/fb7d06f53d778b9226eedcd2259353f7 to your computer and use it in GitHub Desktop.
Save clort81/fb7d06f53d778b9226eedcd2259353f7 to your computer and use it in GitHub Desktop.
ANSI Abstrace Terminal Art Perl Animation
#! /usr/bin/env perl
use strict;
use warnings;
use utf8;
use feature qw(say);
use experimental qw(signatures);
use List::Util qw(max any);
use Term::ReadKey qw(GetTerminalSize);
use Time::HiRes qw(sleep);
*STDOUT->binmode(':encoding(UTF-8)');
srand(time);
my ($cols, $rows) = GetTerminalSize();
use sigtrap handler => sub {
($cols, $rows) = GetTerminalSize();
}, qw(WINCH);
print "\e[?25l"; # hide cursor
use sigtrap handler => sub {
print "\e[?25h", # show cursor
"\e[0m", # reset
"\e[H", # home position
"\e[2J"; # clear
exit;
}, qw(INT);
my @blocks = qw( ░ ▒ ▓ );
my $initwongtime = rand 10_000;
my @dude;
my $tick = 0;
while (1) {
my $dudes = 6 + int( (1.17 + sin($tick/16000)) * 270 );
for my $id (0 .. $dudes) {
print renew_dude($id, $tick);
}
$tick += 100;
sleep(0.01);
}
sub renew_dude ($id, $tick) {
my ($x, $y) = move_dude($id, off_dude(int rand 26), ($dude[$id] || [])->@*);
$dude[$id] = [$x, $y];
my $str = join "",
term_pos(map { $_ + 1 } $x, $y),
term_color( color_dude($id % 126 + 2, $tick, $x, $y) ),
$blocks[$id % @blocks];
}
sub term_pos ($x, $y) {
"\e[$y;${x}f"
}
sub term_color ($rgb1, $rgb2) {
"\e[48;2;" . join(";", @$rgb1) . "m" .
"\e[38;2;" . join(";", @$rgb2) . "m"
}
sub off_dude ($dir) {
my ($x_off, $y_off) = (0, 0);
--$x_off if any { $dir == $_ } 1, 5, 7, 10, 12 .. 15;
++$x_off if any { $dir == $_ } 2, 6, 8, 11;
--$y_off if any { $dir == $_ } 3, 5, 8;
++$y_off if any { $dir == $_ } 4, 6, 7, 10 .. 16;
[$x_off, $y_off]
}
sub move_dude ($id, $off, $x=int rand $cols, $y=int rand $rows) {
($x + $off->[0]) % $cols,
($y + $off->[1]) % $rows
}
sub color_dude ($c, $i, $x, $y) {
my @rgb1 = map { max 0, int }
(($c+65*(0.32+(sin( ($i/8370)+(($x+30)/(49+138*sin($initwongtime+($i/29000)))) ))))/1.33),
(($c+65*(0.32+(sin( ($i/9170)+(($x+30)/(49+138*sin($initwongtime+($i/29000)))) ))))/1.33),
(($c+65*(0.32+(cos( ($i/9990)+(($x+30)/(49+138*sin($initwongtime+($i/29000)))) ))))/1.33);
my @rgb2 = map { max 0, int }
($c+102*(0.27+(cos( (-$i/7193)+(($x+$y)/(219*sin($initwongtime+($i/17000)))) ))))/1.53,
($c+102*(0.27+(cos( (-$i/7093)+(($x+$y)/(199*sin($initwongtime+($i/17000)))) ))))/1.53,
($c+102*(0.27+(cos( (-$i/7293)+(($x+$y)/(209*sin($initwongtime+($i/17000)))) ))))/1.53;
\@rgb1, \@rgb2
}
@clort81
Copy link
Author

clort81 commented Nov 8, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment