pub struct Canvas {
pub width: usize,
pub height: usize,
pub final_width: usize,
pub final_height: usize,
pub default_color: Color,
/* private fields */
}Expand description
A canvas for drawing to the terminal, like a digital picasso.
The Canvas represents a 3D grid of TerminalPixels that can be drawn to.
Each terminal character cell is composed of two half-block pixels (top and bottom).
The z coordinate in set_pixel determines the layering (depth).
It uses a differential rendering approach to only update the parts of the screen that have changed.
Fields§
§width: usizeThe width of the canvas in terminal character columns.
height: usizeThe height of the canvas in terminal character rows.
final_width: usizeThe final width of the canvas in terminal character columns.
final_height: usizeThe final height of the canvas in terminal character rows.
default_color: ColorThe default color used for clearing the canvas and for transparent pixels.
Implementations§
Source§impl Canvas
impl Canvas
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the entire canvas to the default_color.
All half-block pixels across all z-layers are reset to the default_color.
Sourcepub fn set_pixel(&mut self, x: usize, y: usize, z: usize, color: Color)
pub fn set_pixel(&mut self, x: usize, y: usize, z: usize, color: Color)
Sets a half-block pixel at the specified (x, y) coordinate and z-layer with the given color.
§Arguments
x- The terminal column coordinate (0-indexed).y- The half-block row coordinate (0-indexed). -y = 0corresponds to the top half of the first terminal cell row. -y = 1corresponds to the bottom half of the first terminal cell row. -y = 2corresponds to the top half of the second terminal cell row, and so on.z- The z-layer (depth) of the pixel. Higherzvalues are drawn on top of lowerzvalues.color- TheColorto set for the pixel.
Sourcepub fn render(&mut self) -> String
pub fn render(&mut self) -> String
Renders the current state of the canvas to a string containing ANSI escape codes.
This function composites all z-layers for each terminal character cell to determine the final top and bottom half-block colors. It then compares this composited state with the previous frame’s state and returns a string containing only the necessary ANSI escape codes to update the terminal, optimizing for minimal output.
§Returns
A String containing ANSI escape codes to update the terminal.
Auto Trait Implementations§
impl Freeze for Canvas
impl RefUnwindSafe for Canvas
impl Send for Canvas
impl Sync for Canvas
impl Unpin for Canvas
impl UnwindSafe for Canvas
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more