Skip to content
CShift0.45

Canvas

Struct · namespace System.Graphics · Since 0.45

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 the part source of an image with its top left corner at (x, y), unscaled (a tile of a tile set).
Plot Paints one pixel (blended by the alpha of the color).
PixelLine Draws a line one pixel wide from (x0, y0) to (x1, y1), both ends included, without smooth edges (Bresenham): for pixel art.
PixelCircle Draws the outline of a circle one pixel wide around the pixel (cx, cy), without smooth edges: for pixel art.
FillPixelCircle Fills a circle around the pixel (cx, cy) without smooth edges: for pixel art.
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(const ref Image image, Rect source, int x, int y)

Draws the part source of an image with its top left corner at (x, y), unscaled (a tile of a tile set).

void DrawImage(const ref Image image, Rect source, Rect target)

Draws the part source of an image scaled to target (nearest neighbour: every pixel of the image becomes a block of pixels), clipped to Clip and blended by the alpha of its pixels. Opaque pixels at a whole zoom factor (16 x 16 to 32 x 32) are copied without any arithmetic per pixel.

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.

void DrawImage(const ref Image image, Rect source, Rect target, Flip flip)

Draws the part source of an image scaled to target (nearest neighbour), mirrored by flip: a sprite that looks the other way. Blended by the alpha of its pixels.

void DrawImage(const ref Image image, Rect source, int x, int y, Flip flip)

Draws the part source of an image unscaled at (x, y), mirrored by flip.

void Plot(int x, int y, Color color)

Paints one pixel (blended by the alpha of the color).

void PixelLine(int x0, int y0, int x1, int y1, Color color)

Draws a line one pixel wide from (x0, y0) to (x1, y1), both ends included, without smooth edges (Bresenham): for pixel art. Canvas.Line draws smooth lines of any width.

void PixelCircle(int cx, int cy, int radius, Color color)

Draws the outline of a circle one pixel wide around the pixel (cx, cy), without smooth edges: for pixel art.

void FillPixelCircle(int cx, int cy, int radius, Color color)

Fills a circle around the pixel (cx, cy) without smooth edges: for pixel art. Canvas.FillCircle draws a smooth one.