Canvas

Struct Canvas 

Source
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: usize

The width of the canvas in terminal character columns.

§height: usize

The height of the canvas in terminal character rows.

§final_width: usize

The final width of the canvas in terminal character columns.

§final_height: usize

The final height of the canvas in terminal character rows.

§default_color: Color

The default color used for clearing the canvas and for transparent pixels.

Implementations§

Source§

impl Canvas

Source

pub fn new(width: usize, height: usize, default_color: Color) -> Canvas

Creates a new Canvas with the given width, height, and default color.

§Arguments
  • width - The width of the canvas in terminal character columns.
  • height - The height of the canvas in terminal character rows.
  • default_color - The default background color for the canvas.
§Returns

A new Canvas instance.

Source

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.

Source

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 = 0 corresponds to the top half of the first terminal cell row. - y = 1 corresponds to the bottom half of the first terminal cell row. - y = 2 corresponds to the top half of the second terminal cell row, and so on.
  • z - The z-layer (depth) of the pixel. Higher z values are drawn on top of lower z values.
  • color - The Color to set for the pixel.
Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.