Canvas

class Canvas

User-facing drawing API.

Canvas provides high-level drawing commands that are recorded through a Device. Use save()/restore() for state management and clipRect() for clipping.

Paint-based drawing (advanced)

void draw(Rect r, const Paint &paint)

Draw a rectangle with a Paint (supports blend modes, opacity).

void draw(Point p1, Point p2, const Paint &paint)

Draw a line with a Paint (supports blend modes, opacity).

Public Functions

explicit Canvas(Device *device)

Construct a Canvas that records into a device.

Parameters:

device – The device to record drawing commands into.

void clipRect(Rect r)

Intersect the current clip with a rectangle.

Parameters:

r – The clipping rectangle.

void concat(const Matrix &m)

Post-concatenate a matrix onto the current transform.

void drawImage(std::shared_ptr<Image> image, f32 x, f32 y)

Draw an image at the given position (alpha-blended).

Parameters:
  • image – Shared pointer to the image.

  • x – X position.

  • y – Y position.

void drawLine(Point p1, Point p2, Color c, f32 width = 1.0f)

Draw a line between two points.

Parameters:
  • p1 – Start point.

  • p2 – End point.

  • c – Line color.

  • width – Line width (default 1.0).

void drawPolyline(const Point *pts, i32 count, Color c, f32 width = 1.0f)

Draw a connected series of line segments.

Parameters:
  • pts – Array of vertices.

  • count – Number of points.

  • c – Line color.

  • width – Line width (default 1.0).

void drawText(Point p, std::string_view text, Color c)

Draw text at the given position.

Parameters:
  • p – Position of the text baseline.

  • text – UTF-8 text to draw.

  • c – Text color.

void fillCircle(f32 cx, f32 cy, f32 radius, Color c)

Fill a circle with a solid color.

Parameters:
  • cx – Center X.

  • cy – Center Y.

  • radius – Circle radius.

  • c – Fill color.

void fillRect(Rect r, Color c)

Fill a rectangle with a solid color.

Parameters:
  • r – The rectangle to fill.

  • c – Fill color.

void fillRoundRect(Rect r, f32 rx, f32 ry, Color c)

Fill a rounded rectangle with a solid color.

Parameters:
  • r – The rectangle.

  • rx – Corner radius in X.

  • ry – Corner radius in Y.

  • c – Fill color.

Matrix getMatrix() const

Return the current transform matrix.

void restore()

Restore the most recently saved clip state.

void rotate(f32 radians)

Rotate the current transform.

void save()

Save the current clip state onto the stack.

void scale(f32 sx, f32 sy)

Scale the current transform.

void setMatrix(const Matrix &m)

Replace the current transform with the given matrix.

void strokeCircle(f32 cx, f32 cy, f32 radius, Color c, f32 width = 1.0f)

Stroke a circle outline.

Parameters:
  • cx – Center X.

  • cy – Center Y.

  • radius – Circle radius.

  • c – Stroke color.

  • width – Stroke line width (default 1.0).

void strokeRect(Rect r, Color c, f32 width = 1.0f)

Stroke a rectangle outline.

Parameters:
  • r – The rectangle to stroke.

  • c – Stroke color.

  • width – Stroke line width (default 1.0).

void strokeRoundRect(Rect r, f32 rx, f32 ry, Color c, f32 width = 1.0f)

Stroke a rounded rectangle outline.

Parameters:
  • r – The rectangle.

  • rx – Corner radius in X.

  • ry – Corner radius in Y.

  • c – Stroke color.

  • width – Stroke line width (default 1.0).

void translate(f32 dx, f32 dy)

Translate the current transform.