octobanana / asciimation

An ASCII animation interpreter for the terminal.
MIT License
104 stars 5 forks source link

Allow functions to generate single frames #1

Open Kaligule opened 6 years ago

Kaligule commented 6 years ago

From a reddit threat:

Nice. This still seems to be a lot of work, though. The plane file has 814 lines. Wouldn't it be nice to allow specifying a function (with frame number, width and hight as arguments) and use its output as the image of the frame?

I wrote an example how this could look (simple bash script written today in the tramway, so don't expect too much of it):

#!/usr/bin/env bash

function scroll  {
    scroll_by=$1
    max_length=$2
    line=$3
    end_chars=$(printf %"$max_length"s)
    # from the string $line take the last $scroll_by chars, then take
    # only the last max_lenght chars of those.
    echo -n "$end_chars" "$line" | tail -c $scroll_by  | head -c $max_length
    # if the newline in the command above wouldn't be suppressed it
    # might be cut away by head, so we add it here afterwards
    echo ""
}

function plane {
    # naming the arguments for readability
    frame=$1
    width=$2
    height=$3

    case $(($frame%4)) in
        0)
            scroll $frame $width "/----------------------\              ___               "
            scroll $frame $width "|                      |              \~~\_____/~~\__   "
            scroll $frame $width "|      Automation      |______________ \______====== )-+"
            scroll $frame $width "|                      |                       ~~|/~~   "
            scroll $frame $width "\----------------------/                         ()     "
            ;;
        2)
            scroll $frame $width "/----------------------\              ___               "
            scroll $frame $width "|                      |              \~~\_____/~~\__  |"
            scroll $frame $width "|      Automation      |______________ \______====== )-+"
            scroll $frame $width "|                      |                       ~~|/~~  |"
            scroll $frame $width "\----------------------/                         ()     "
            ;;
        [1,3])
            scroll $frame $width "/----------------------\              ___               "
            scroll $frame $width "|                      |              \~~\_____/~~\__  ."
            scroll $frame $width "|      Automation      |______________ \______====== )-+"
            scroll $frame $width "|                      |                       ~~|/~~  '"
            scroll $frame $width "\----------------------/                         ()     "
            ;;
    esac
}

# main
clear
for i in $(seq 150);
do
    plane $i 50 5
    sleep 0.08
    clear
done

I think it provides a simpler way to compose animations and to adapt them later (like in the example to add another frame to the plane.

MarketingPip commented 3 weeks ago

@Kaligule - I thank you very much years later for this. Ended up using this logic in JS for this animation. Much appreciated. Cheers!