Table of Contents

Class CanvasGraphics

Namespace
SharpConsoleUI.Drawing
Assembly
SharpConsoleUI.dll

Drawing context that wraps a CharacterBuffer with local-coordinate translation. Used both for async painting (wrapping the internal canvas buffer at offset 0,0) and for event-driven painting (wrapping the window buffer with content-area offset). All drawing methods accept canvas-local coordinates and translate them automatically.

public sealed class CanvasGraphics
Inheritance
CanvasGraphics
Inherited Members
Extension Methods

Properties

Height

Canvas content height in local coordinates.

public int Height { get; }

Property Value

int

Width

Canvas content width in local coordinates.

public int Width { get; }

Property Value

int

Methods

CheckerFill(int, int, int, int, char, char, Color, Color, Color)

Fills a rectangle with alternating characters in a checkerboard pattern.

public void CheckerFill(int x, int y, int width, int height, char ch1, char ch2, Color fg1, Color fg2, Color bg)

Parameters

x int

The X coordinate of the rectangle.

y int

The Y coordinate of the rectangle.

width int

The width of the rectangle.

height int

The height of the rectangle.

ch1 char

The character for even cells.

ch2 char

The character for odd cells.

fg1 Color

The foreground color for even cells.

fg2 Color

The foreground color for odd cells.

bg Color

The background color.

Clear(Color)

Clears the entire canvas area with the specified background color.

public void Clear(Color bg)

Parameters

bg Color

The background color to fill with.

Clear(char, Color, Color)

Clears the entire canvas area with the specified character and colors.

public void Clear(char ch, Color fg, Color bg)

Parameters

ch char

The fill character.

fg Color

The foreground color.

bg Color

The background color.

DrawArc(int, int, int, double, double, char, Color, Color)

Draws an arc (portion of a circle) using parametric sampling. Angles are in radians, measured counter-clockwise from the positive X axis.

public void DrawArc(int cx, int cy, int radius, double startAngle, double endAngle, char ch, Color fg, Color bg)

Parameters

cx int

The center X coordinate.

cy int

The center Y coordinate.

radius int

The arc radius.

startAngle double

The starting angle in radians.

endAngle double

The ending angle in radians.

ch char

The character to draw with.

fg Color

The foreground color.

bg Color

The background color.

DrawBox(int, int, int, int, BoxChars, Color, Color)

Draws a box border using the specified box drawing characters.

public void DrawBox(int x, int y, int width, int height, BoxChars boxChars, Color fg, Color bg)

Parameters

x int

The X coordinate of the box's top-left corner.

y int

The Y coordinate of the box's top-left corner.

width int

The width of the box.

height int

The height of the box.

boxChars BoxChars

The box drawing character set.

fg Color

The foreground color.

bg Color

The background color.

DrawCircle(int, int, int, char, Color, Color)

Draws a circle outline using the midpoint circle algorithm.

public void DrawCircle(int cx, int cy, int radius, char ch, Color fg, Color bg)

Parameters

cx int

The center X coordinate.

cy int

The center Y coordinate.

radius int

The circle radius.

ch char

The character to draw with.

fg Color

The foreground color.

bg Color

The background color.

DrawEllipse(int, int, int, int, char, Color, Color)

Draws an ellipse outline using the midpoint ellipse algorithm.

public void DrawEllipse(int cx, int cy, int rx, int ry, char ch, Color fg, Color bg)

Parameters

cx int

The center X coordinate.

cy int

The center Y coordinate.

rx int

The horizontal radius.

ry int

The vertical radius.

ch char

The character to draw with.

fg Color

The foreground color.

bg Color

The background color.

DrawHorizontalLine(int, int, int, char, Color, Color)

Draws a horizontal line starting at the given position.

public void DrawHorizontalLine(int x, int y, int length, char ch, Color fg, Color bg)

Parameters

x int

The starting X coordinate.

y int

The Y coordinate.

length int

The length of the line in characters.

ch char

The character to draw with.

fg Color

The foreground color.

bg Color

The background color.

DrawLine(int, int, int, int, char, Color, Color)

Draws a line between two points using Bresenham's algorithm.

public void DrawLine(int x0, int y0, int x1, int y1, char ch, Color fg, Color bg)

Parameters

x0 int

The starting X coordinate.

y0 int

The starting Y coordinate.

x1 int

The ending X coordinate.

y1 int

The ending Y coordinate.

ch char

The character to draw with.

fg Color

The foreground color.

bg Color

The background color.

DrawPolygon((int X, int Y)[], char, Color, Color)

Draws a polygon outline by connecting consecutive points with lines.

public void DrawPolygon((int X, int Y)[] points, char ch, Color fg, Color bg)

Parameters

points (int X, int Y)[]

The polygon vertices in canvas-local coordinates.

ch char

The character to draw with.

fg Color

The foreground color.

bg Color

The background color.

DrawTriangle(int, int, int, int, int, int, char, Color, Color)

Draws a triangle outline by connecting three vertices with lines.

public void DrawTriangle(int x0, int y0, int x1, int y1, int x2, int y2, char ch, Color fg, Color bg)

Parameters

x0 int

First vertex X.

y0 int

First vertex Y.

x1 int

Second vertex X.

y1 int

Second vertex Y.

x2 int

Third vertex X.

y2 int

Third vertex Y.

ch char

The character to draw with.

fg Color

The foreground color.

bg Color

The background color.

DrawVerticalLine(int, int, int, char, Color, Color)

Draws a vertical line starting at the given position.

public void DrawVerticalLine(int x, int y, int length, char ch, Color fg, Color bg)

Parameters

x int

The X coordinate.

y int

The starting Y coordinate.

length int

The length of the line in characters.

ch char

The character to draw with.

fg Color

The foreground color.

bg Color

The background color.

FillCircle(int, int, int, char, Color, Color)

Draws a filled circle using the midpoint algorithm with horizontal scanlines.

public void FillCircle(int cx, int cy, int radius, char ch, Color fg, Color bg)

Parameters

cx int

The center X coordinate.

cy int

The center Y coordinate.

radius int

The circle radius.

ch char

The fill character.

fg Color

The foreground color.

bg Color

The background color.

FillEllipse(int, int, int, int, char, Color, Color)

Draws a filled ellipse using the midpoint algorithm with horizontal scanlines.

public void FillEllipse(int cx, int cy, int rx, int ry, char ch, Color fg, Color bg)

Parameters

cx int

The center X coordinate.

cy int

The center Y coordinate.

rx int

The horizontal radius.

ry int

The vertical radius.

ch char

The fill character.

fg Color

The foreground color.

bg Color

The background color.

FillPolygon((int X, int Y)[], char, Color, Color)

Draws a filled polygon using scanline rasterization with the even-odd rule.

public void FillPolygon((int X, int Y)[] points, char ch, Color fg, Color bg)

Parameters

points (int X, int Y)[]

The polygon vertices in canvas-local coordinates.

ch char

The fill character.

fg Color

The foreground color.

bg Color

The background color.

FillRect(int, int, int, int, Color)

Fills a rectangle at canvas-local coordinates with spaces and the specified background.

public void FillRect(int x, int y, int width, int height, Color bg)

Parameters

x int

The X coordinate of the rectangle's top-left corner.

y int

The Y coordinate of the rectangle's top-left corner.

width int

The width of the rectangle.

height int

The height of the rectangle.

bg Color

The background color.

FillRect(int, int, int, int, char, Color, Color)

Fills a rectangle at canvas-local coordinates with the specified character and colors.

public void FillRect(int x, int y, int width, int height, char ch, Color fg, Color bg)

Parameters

x int

The X coordinate of the rectangle's top-left corner.

y int

The Y coordinate of the rectangle's top-left corner.

width int

The width of the rectangle.

height int

The height of the rectangle.

ch char

The fill character.

fg Color

The foreground color.

bg Color

The background color.

FillTriangle(int, int, int, int, int, int, char, Color, Color)

Draws a filled triangle using scanline rasterization.

public void FillTriangle(int x0, int y0, int x1, int y1, int x2, int y2, char ch, Color fg, Color bg)

Parameters

x0 int

First vertex X.

y0 int

First vertex Y.

x1 int

Second vertex X.

y1 int

Second vertex Y.

x2 int

Third vertex X.

y2 int

Third vertex Y.

ch char

The fill character.

fg Color

The foreground color.

bg Color

The background color.

GetCell(int, int)

Gets the cell at the specified canvas-local position.

public Cell GetCell(int x, int y)

Parameters

x int

The X coordinate in canvas-local space.

y int

The Y coordinate in canvas-local space.

Returns

Cell

The cell at the specified position.

GradientFillHorizontal(int, int, int, int, char, Color, Color, Color)

Fills a rectangle with a horizontal foreground color gradient (left to right).

public void GradientFillHorizontal(int x, int y, int width, int height, char ch, Color fgStart, Color fgEnd, Color bg)

Parameters

x int

The X coordinate of the rectangle.

y int

The Y coordinate of the rectangle.

width int

The width of the rectangle.

height int

The height of the rectangle.

ch char

The fill character.

fgStart Color

The starting foreground color (left).

fgEnd Color

The ending foreground color (right).

bg Color

The background color.

GradientFillRect(int, int, int, int, Color, Color, bool)

Fills a rectangle with a background color gradient.

public void GradientFillRect(int x, int y, int width, int height, Color bgStart, Color bgEnd, bool horizontal)

Parameters

x int

The X coordinate of the rectangle.

y int

The Y coordinate of the rectangle.

width int

The width of the rectangle.

height int

The height of the rectangle.

bgStart Color

The starting background color.

bgEnd Color

The ending background color.

horizontal bool

True for left-to-right gradient, false for top-to-bottom.

GradientFillVertical(int, int, int, int, char, Color, Color, Color)

Fills a rectangle with a vertical foreground color gradient (top to bottom).

public void GradientFillVertical(int x, int y, int width, int height, char ch, Color fgStart, Color fgEnd, Color bg)

Parameters

x int

The X coordinate of the rectangle.

y int

The Y coordinate of the rectangle.

width int

The width of the rectangle.

height int

The height of the rectangle.

ch char

The fill character.

fgStart Color

The starting foreground color (top).

fgEnd Color

The ending foreground color (bottom).

bg Color

The background color.

PatternFill(int, int, int, int, string[], Color, Color)

Fills a rectangle with a repeating 2D character pattern.

public void PatternFill(int x, int y, int width, int height, string[] pattern, Color fg, Color bg)

Parameters

x int

The X coordinate of the rectangle.

y int

The Y coordinate of the rectangle.

width int

The width of the rectangle.

height int

The height of the rectangle.

pattern string[]

The pattern rows to tile.

fg Color

The foreground color.

bg Color

The background color.

SetNarrowCell(int, int, char, Color, Color)

Sets a single cell at the specified canvas-local position.

public void SetNarrowCell(int x, int y, char ch, Color fg, Color bg)

Parameters

x int

The X coordinate in canvas-local space.

y int

The Y coordinate in canvas-local space.

ch char

The character to display.

fg Color

The foreground color.

bg Color

The background color.

StippleFill(int, int, int, int, double, Color, Color)

Fills a rectangle with a density-based stipple pattern. Density ranges from 0.0 (empty) to 1.0 (fully filled).

public void StippleFill(int x, int y, int width, int height, double density, Color fg, Color bg)

Parameters

x int

The X coordinate of the rectangle.

y int

The Y coordinate of the rectangle.

width int

The width of the rectangle.

height int

The height of the rectangle.

density double

The fill density from 0.0 to 1.0.

fg Color

The foreground color.

bg Color

The background color.

WriteString(int, int, string, Color, Color)

Writes a string at the specified canvas-local position.

public void WriteString(int x, int y, string text, Color fg, Color bg)

Parameters

x int

The X coordinate.

y int

The Y coordinate.

text string

The text to write.

fg Color

The foreground color.

bg Color

The background color.

WriteStringCentered(int, string, Color, Color)

Writes horizontally centered text within the canvas at the specified Y position.

public void WriteStringCentered(int y, string text, Color fg, Color bg)

Parameters

y int

The Y coordinate in canvas-local space.

text string

The text to center.

fg Color

The foreground color.

bg Color

The background color.

WriteStringInBox(int, int, int, int, string, BoxChars, Color, Color, Color, Color)

Draws a box border and writes centered text inside it.

public void WriteStringInBox(int x, int y, int width, int height, string text, BoxChars boxChars, Color fg, Color bg, Color boxFg, Color boxBg)

Parameters

x int

The X coordinate of the box.

y int

The Y coordinate of the box.

width int

The width of the box.

height int

The height of the box.

text string

The text to center inside the box.

boxChars BoxChars

The box drawing character set.

fg Color

The text foreground color.

bg Color

The text background color.

boxFg Color

The box border foreground color.

boxBg Color

The box border background color.

WriteStringRight(int, string, Color, Color)

Writes right-aligned text within the canvas at the specified Y position.

public void WriteStringRight(int y, string text, Color fg, Color bg)

Parameters

y int

The Y coordinate in canvas-local space.

text string

The text to right-align.

fg Color

The foreground color.

bg Color

The background color.

WriteWrappedText(int, int, int, string, Color, Color)

Writes word-wrapped text starting at the given position within the specified width.

public void WriteWrappedText(int x, int y, int width, string text, Color fg, Color bg)

Parameters

x int

The X coordinate.

y int

The Y coordinate.

width int

The maximum line width for wrapping.

text string

The text to wrap and write.

fg Color

The foreground color.

bg Color

The background color.