Skip to content
CShift0.32

Canvas

Struct · namespace System.Ui · Since 0.32

struct Canvas

Draws into an Image: everything is clipped to Clip, colors with an alpha below 255 are blended with what is already there, and the edges of rounded rectangles, circles and lines are smooth. A canvas shares the pixels of its image (an Image holds them by reference), so drawing changes the image.

var image = Image.Create(200, 100);
var canvas = Canvas.Create(image);
canvas.Clear(Color.White());
canvas.FillRoundRect(Rect.Create(10, 10, 120, 30), 6, Color.FromRgb(40, 110, 220));
canvas.Text(Font.Fixed7x13(), 20, 18, "Hello", Color.White());
Name Description
Target The image that is drawn into.
Clip Only pixels inside this rectangle are changed.
Image Target

The image that is drawn into.

Rect Clip

Only pixels inside this rectangle are changed.

Name Description
Create A canvas for the whole image.
static Canvas Create(Image target)

A canvas for the whole image.

Name Description
SetClip Restricts drawing to a rectangle (within the image).
Clear Fills the whole image (ignoring the clip rectangle) with one color.
Blend Blends one pixel: coverage 0 (nothing) to 255 (the color with its own alpha).
FillRect Fills a rectangle.
StrokeRect Draws the outline of a rectangle, width pixels thick, inside the rectangle.
FillRoundRect Fills a rectangle with rounded corners of the given radius (smooth edges).
StrokeRoundRect Draws the outline of a rounded rectangle, width pixels thick, inside the rectangle.
FillCircle Fills a circle (smooth edge); the center may lie between pixels.
Line Draws a line width pixels thick with round ends (smooth edges); coordinates may lie between pixels.
Text Draws text with its top left corner at (x, y) and returns its width.
DrawImage Draws an image with its top left corner at (x, y), blended by the alpha of its pixels.
void SetClip(Rect clip)

Restricts drawing to a rectangle (within the image).

void Clear(Color color)

Fills the whole image (ignoring the clip rectangle) with one color.

void Blend(int x, int y, Color color, int coverage)

Blends one pixel: coverage 0 (nothing) to 255 (the color with its own alpha).

void FillRect(Rect rect, Color color)

Fills a rectangle.

void StrokeRect(Rect rect, int width, Color color)

Draws the outline of a rectangle, width pixels thick, inside the rectangle.

void FillRoundRect(Rect rect, int radius, Color color)

Fills a rectangle with rounded corners of the given radius (smooth edges).

void StrokeRoundRect(Rect rect, int radius, int width, Color color)

Draws the outline of a rounded rectangle, width pixels thick, inside the rectangle.

void FillCircle(double cx, double cy, double radius, Color color)

Fills a circle (smooth edge); the center may lie between pixels.

void Line(double x0, double y0, double x1, double y1, double width, Color color)

Draws a line width pixels thick with round ends (smooth edges); coordinates may lie between pixels.

int Text(Font font, int x, int y, StringSlice text, Color color)

Draws text with its top left corner at (x, y) and returns its width. Line ends are not interpreted.

void DrawImage(Image image, int x, int y)

Draws an image with its top left corner at (x, y), blended by the alpha of its pixels.