TermUI.Widgets.Canvas (TermUI v0.2.0)
View SourceCanvas widget for custom drawing with direct buffer access.
Canvas provides a drawing surface with primitives for lines, rectangles, text, and Braille graphics. Useful for custom visualizations, charts, diagrams, and other graphics that don't fit standard widget patterns.
Usage
Canvas.new(
width: 40,
height: 20,
on_draw: fn canvas ->
canvas
|> Canvas.draw_rect(0, 0, 10, 5, "─", "│", "┌", "┐", "└", "┘")
|> Canvas.draw_text(2, 2, "Hello")
end
)Features
- Direct character buffer access
- Drawing primitives: line, rect, text
- Braille graphics for sub-character resolution
- Clear and fill operations
- Custom render callback
Braille Graphics
Each character cell contains a 2x4 Braille dot matrix, providing higher resolution for plotting and charts.
Summary
Functions
Gets the Braille resolution (dots) for the canvas.
Clears the canvas with the default character.
Clears all Braille dots.
Clears a Braille dot at sub-character position.
Converts dots to a Braille character.
Creates a canvas and draws on it with a function.
Draws a Braille line between two points.
Draws a horizontal line.
Draws a line between two points using Bresenham's algorithm.
Draws a rectangle outline.
Draws text at a position.
Draws a vertical line.
Returns empty Braille character.
Fills the canvas with a character.
Fills a rectangle with a character.
Returns full Braille character (all dots set).
Gets a character at a position.
Creates new Canvas widget props.
Updates the canvas dimensions.
Sets a character at a position.
Sets a Braille dot at sub-character position.
Renders the canvas state to a list of strings.
Functions
Gets the Braille resolution (dots) for the canvas.
Clears the canvas with the default character.
Clears all Braille dots.
Clears a Braille dot at sub-character position.
Converts dots to a Braille character.
Takes a list of {x, y} coordinates within a 2x4 cell.
Creates a canvas and draws on it with a function.
Draws a Braille line between two points.
Coordinates are in sub-character (dot) space:
- X resolution: width * 2
- Y resolution: height * 4
Draws a horizontal line.
Draws a line between two points using Bresenham's algorithm.
Draws a rectangle outline.
Border Options
The border map can contain:
:h- Horizontal character (default: "─"):v- Vertical character (default: "│"):tl- Top-left corner (default: "┌"):tr- Top-right corner (default: "┐"):bl- Bottom-left corner (default: "└"):br- Bottom-right corner (default: "┘")
Draws text at a position.
Draws a vertical line.
@spec empty_braille() :: String.t()
Returns empty Braille character.
Fills the canvas with a character.
Fills a rectangle with a character.
@spec full_braille() :: String.t()
Returns full Braille character (all dots set).
Gets a character at a position.
Creates new Canvas widget props.
Options
:width- Canvas width in characters (default: 40):height- Canvas height in characters (default: 20):default_char- Character to fill canvas (default: " "):on_draw- Callback function to draw on canvas
Updates the canvas dimensions.
Sets a character at a position.
Sets a Braille dot at sub-character position.
Each character cell is 2 dots wide and 4 dots high.
Renders the canvas state to a list of strings.