Canvas

Canvas #

PhpTui\Tui\Extension\Core\Widget\CanvasWidget

The canvas widget provides a surface, of arbitrary scale, upon which shapes can be drawn.

Example #

Show code
<?php

declare(strict_types=1);

use PhpTui\Tui\Canvas\CanvasContext;
use PhpTui\Tui\Canvas\Marker;
use PhpTui\Tui\Color\AnsiColor;
use PhpTui\Tui\DisplayBuilder;
use PhpTui\Tui\Extension\Core\Shape\CircleShape;
use PhpTui\Tui\Extension\Core\Widget\CanvasWidget;

require 'vendor/autoload.php';

$display = DisplayBuilder::default()->build();
$display->draw(
    CanvasWidget::fromIntBounds(-1, 21, -1, 21)
        // the marker determines both the effective resolution of
        // the canvas and the "mark" that is made
        ->marker(Marker::Dot)

        // note can use `$canvas->draw($shape, ...)` without the closure for
        // most cases
        ->paint(function (CanvasContext $context): void {

            $context->draw(CircleShape::fromScalars(10, 10, 10)->color(AnsiColor::Green));
        })
);

Parameters #

Configure the widget using the builder methods named as follows:

NameTypeDescription
xBoundsPhpTui\Tui\Extension\Core\Widget\Chart\AxisBoundsBounds of the X Axis. Must be set if the canvas is to render.
yBoundsPhpTui\Tui\Extension\Core\Widget\Chart\AxisBoundsBounds of the Y Axis. Must be set if the canvas is to render.
painterClosure(CanvasContext): voidThe painter closure can draw shapes onto the canvas.
backgroundColorPhpTui\Tui\Color\ColorBackground color
markerPhpTui\Tui\Canvas\MarkerThe marker type to use, e.g. Marker::Braille