Sprite

Sprite #

PhpTui\Tui\Extension\Core\Shape\SpriteShape

Renders a “sprite” based on a given “ascii art”

Each sprite can have a single color but they can be layered on the canvas.

Example #

Show code
<?php

declare(strict_types=1);

use PhpTui\Tui\Canvas\Marker;
use PhpTui\Tui\Color\AnsiColor;
use PhpTui\Tui\DisplayBuilder;
use PhpTui\Tui\Extension\Core\Shape\SpriteShape;
use PhpTui\Tui\Extension\Core\Widget\CanvasWidget;
use PhpTui\Tui\Position\FloatPosition;

require 'vendor/autoload.php';

$display = DisplayBuilder::default()->build();
$display->draw(
    CanvasWidget::fromIntBounds(0, 30, 0, 15)
        ->marker(Marker::Block)
        ->draw(
            new SpriteShape(
                rows: [
                    ' XXX ',
                    'X   X ',
                    'X   X ',
                    ' XXX ',
                    '  X',
                    'XXXXX',
                    '  X       ', // rows do not need
                    '  X       ', // equals numbers of chars
                    ' X X     ',
                    'X   X    ',
                ],
                color: AnsiColor::White,
                position: FloatPosition::at(2, 2),
            )
        )
);

Parameters #

Configure the shape using the builder methods named as follows:

NameTypeDescription
rowsarraySet of lines/rows which make up the Sprite. e.g. [' ', ' x ']. The lines do not have to be of equal length.
colorPhpTui\Tui\Color\ColorColor of the sprite
positionPhpTui\Tui\Position\FloatPositionPosition to place the sprite at (bottom left)
alphaCharstringCharacter to use as the “alpha” (transparent) “channel”. Defaults to empty space.
xScalefloatX scale
yScalefloatY scale