|
1 | 1 | # Canvas Widget |
2 | 2 |
|
3 | | -The **Canvas** widget provides a drawing area for graphics, shapes, and custom rendering. |
| 3 | +The **Canvas** widget provides a 2D drawing area where you can programmatically render lines, rectangles, ovals, and text. Every draw method returns a Tk item ID (as a `string`) that can be used to delete specific items later. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +### Constructor |
| 8 | + |
| 9 | +```php |
| 10 | +new Canvas(string $parentId, array $options = []) |
| 11 | +``` |
| 12 | + |
| 13 | +| Parameter | Type | Description | |
| 14 | +|-------------|----------|------------------------------------------| |
| 15 | +| `$parentId` | `string` | `getId()` of the parent widget. | |
| 16 | +| `$options` | `array` | Configuration options — see table below. | |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +### Options |
| 21 | + |
| 22 | +| Key | Type | Description | |
| 23 | +|----------|----------|------------------------------------| |
| 24 | +| `width` | `int` | Canvas width in pixels. | |
| 25 | +| `height` | `int` | Canvas height in pixels. | |
| 26 | +| `bg` | `string` | Background color of the canvas. | |
| 27 | + |
| 28 | +All options are forwarded directly to the Tk `canvas` command. |
| 29 | + |
| 30 | +--- |
4 | 31 |
|
5 | 32 | ### Example |
| 33 | + |
6 | 34 | ```php |
7 | 35 | use PhpGui\Widget\Canvas; |
8 | | -$canvas = new Canvas('parentId'); |
| 36 | + |
| 37 | +$canvas = new Canvas($window->getId(), [ |
| 38 | + 'width' => 400, |
| 39 | + 'height' => 300, |
| 40 | + 'bg' => 'white' |
| 41 | +]); |
| 42 | +$canvas->pack(['pady' => 10]); |
| 43 | + |
| 44 | +// Draw shapes |
| 45 | +$lineId = $canvas->drawLine(10, 10, 200, 10, ['fill' => 'black', 'width' => 2]); |
| 46 | +$rectId = $canvas->drawRectangle(50, 50, 200, 150, ['fill' => 'lightblue', 'outline' => 'navy']); |
| 47 | +$ovalId = $canvas->drawOval(220, 50, 370, 150, ['fill' => 'salmon']); |
| 48 | +$textId = $canvas->drawText(100, 180, 'Hello Canvas', ['fill' => 'darkgreen', 'font' => 'Arial 14 bold']); |
| 49 | + |
| 50 | +// Delete a specific item by its returned ID |
| 51 | +$canvas->delete($rectId); |
| 52 | + |
| 53 | +// Clear the entire canvas |
| 54 | +$canvas->clear(); |
9 | 55 | ``` |
10 | 56 |
|
11 | | -### Details |
12 | | -- Typically used for custom drawings. |
13 | | -- Can be modified using additional Tcl commands. |
14 | | -- Offers a flexible area for graphical content. |
| 57 | +--- |
| 58 | + |
| 59 | +### Methods |
| 60 | + |
| 61 | +| Method | Signature | Returns | Description | |
| 62 | +|---------------------|----------------------------------------------------------------------|----------|---------------------------------------------------------------| |
| 63 | +| `drawLine()` | `(int $x1, int $y1, int $x2, int $y2, array $options = []): string` | item ID | Draws a line between two points. | |
| 64 | +| `drawRectangle()` | `(int $x1, int $y1, int $x2, int $y2, array $options = []): string` | item ID | Draws a rectangle from top-left to bottom-right. | |
| 65 | +| `drawOval()` | `(int $x1, int $y1, int $x2, int $y2, array $options = []): string` | item ID | Draws an oval/ellipse bounded by the given rectangle. | |
| 66 | +| `drawText()` | `(int $x, int $y, string $text, array $options = []): string` | item ID | Draws text anchored at the given coordinates. | |
| 67 | +| `delete()` | `(string $itemId): void` | — | Deletes a single item by the ID returned from a draw method. | |
| 68 | +| `clear()` | `(): void` | — | Deletes all items on the canvas (`delete all`). | |
| 69 | +| `pack()` | `(array $opts = []): void` | — | Inherited. Pack layout manager. | |
| 70 | +| `place()` | `(array $opts = []): void` | — | Inherited. Place layout manager. | |
| 71 | +| `grid()` | `(array $opts = []): void` | — | Inherited. Grid layout manager. | |
| 72 | +| `destroy()` | `(): void` | — | Inherited. Removes the canvas widget. | |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +### Draw Options Reference |
| 77 | + |
| 78 | +Options arrays for draw methods accept standard Tk item configuration keys: |
| 79 | + |
| 80 | +| Key | Applicable to | Description | |
| 81 | +|-----------|-----------------------|------------------------------------| |
| 82 | +| `fill` | all shapes, text | Fill color. | |
| 83 | +| `outline` | rectangle, oval | Border color. | |
| 84 | +| `width` | line, rectangle, oval | Border/stroke width in pixels. | |
| 85 | +| `font` | text | Font string, e.g. `'Arial 14 bold'`.| |
| 86 | +| `anchor` | text | Anchor point: `center`, `nw`, etc. | |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +### Notes |
| 91 | + |
| 92 | +- `drawText()` uses `-text {text}` so the text string is passed as a Tk list element — it can contain spaces. |
| 93 | +- All coordinate values are in pixels, measured from the top-left corner of the canvas. |
| 94 | +- `Canvas` overrides `formatOptions()` from `AbstractWidget` to produce `-key value` without wrapping values in quotes (important for numeric coordinates). |
0 commit comments