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:
Name | Type | Description |
---|---|---|
xBounds | PhpTui\Tui\Extension\Core\Widget\Chart\AxisBounds | Bounds of the X Axis. Must be set if the canvas is to render. |
yBounds | PhpTui\Tui\Extension\Core\Widget\Chart\AxisBounds | Bounds of the Y Axis. Must be set if the canvas is to render. |
painter | Closure(CanvasContext): void | The painter closure can draw shapes onto the canvas. |
backgroundColor | PhpTui\Tui\Color\Color | Background color |
marker | PhpTui\Tui\Canvas\Marker | The marker type to use, e.g. Marker::Braille |