Skip to content

Canvas ​

Paints rectangles and vector paths from a list of commands. The commands travel in the snapshot and run entirely in Rust, so no Go callback runs during painting.

go
chart := scene.Canvas(1,
	&scene.DrawCommand{Kind: &scene.DrawCommandRect{Rect: &scene.DrawRect{
		Bounds: &scene.Bounds{
			Origin: &scene.Point{X: 10, Y: 10},
			Size:   &scene.Size{Width: 130, Height: 70},
		},
		Fill:   scene.RGB(0x2563eb),
		Radius: 8,
	}}},
	&scene.DrawCommand{Kind: &scene.DrawCommandPath{Path: &scene.DrawPath{
		Color:       scene.RGB(0x34d399),
		StrokeWidth: 4,
		Steps: []*scene.PathStep{
			{Verb: scene.PathMove, Point: &scene.Point{X: 10, Y: 100}},
			{Verb: scene.PathLine, Point: &scene.Point{X: 140, Y: 100}},
		},
	}}},
).Style(&scene.Style{
	Width:  scene.Pixels(160),
	Height: scene.Pixels(120),
})

A blue rounded rectangle above a green line

Signature ​

text
func Canvas(id uint64, commands ...*DrawCommand) *Node

Coordinates are logical pixels relative to the element's top-left corner, and drawing is clipped to the element. The canvas fills its box, so set Width and Height. Commands paint in order, later ones on top. A canvas cannot have children.

Rectangle ​

scene.DrawCommandRect wraps a scene.DrawRect:

FieldTypeDescription
Bounds*scene.BoundsRequired Origin and Size. Width and height cannot be negative
Fill*scene.ColorFill color. Transparent if nil
Border*scene.ColorBorder color. Transparent if nil
BorderWidthfloat32Border width, at least 0
Radiusfloat32Corner radius, at least 0

Rectangles showing a fill, a border and a corner radius

Path ​

scene.DrawCommandPath wraps a scene.DrawPath:

FieldTypeDescription
Steps[]*scene.PathStepRequired. The first step must be scene.PathMove
Color*scene.ColorPaint color. Transparent if nil
StrokeWidthfloat320 fills the path. A larger value strokes it at that width

Each scene.PathStep has a Verb and the points it requires:

VerbRequired fieldsEffect
scene.PathMovePointStarts a new subpath at Point
scene.PathLinePointDraws a straight line to Point
scene.PathQuadraticPoint, Control1Draws a quadratic curve to Point
scene.PathCubicPoint, Control1, Control2Draws a cubic curve to Point
scene.PathCloseNoneCloses the subpath

A straight line, a quadratic curve, a cubic curve and a filled triangle

Limits ​

LimitValue
Commands per canvas4096
Path steps per canvas, across all paths16,384
Coordinate and size magnitude1,000,000
Color channels0 to 1, finite

Available in other APIs ​

Canvas is only available through scene.

Released under the Apache License 2.0. The optional Zed editor component is GPL-3.0-or-later.