Canvas

Struct Canvas 

Source
pub struct Canvas { /* private fields */ }
Expand description

A canvas object that can be used to draw to the terminal using Braille characters.

Implementations§

Source§

impl Canvas

Source

pub fn new(width: u32, height: u32) -> Canvas

Creates a new Canvas with the given width and height.

Note that the Canvas can still draw outside the given dimensions (expanding the canvas) if a pixel is set outside the dimensions.

Examples found in repository?
examples/draw_triangle.rs (line 6)
5fn main() {
6    let mut canvas = Canvas::new(100, 100);
7    canvas.line(2, 2, 80, 80);
8    canvas.line(2, 80, 80, 80);
9    canvas.line(2, 2, 2, 80);
10    println!("{}", canvas.frame());
11}
More examples
Hide additional examples
examples/draw_rgb_triangles.rs (line 7)
6fn main() {
7    let mut canvas = Canvas::new(100, 100);
8    canvas.line_colored(2, 2, 80, 80, PixelColor::Red);
9    canvas.line_colored(2, 80, 80, 80, PixelColor::Green);
10    canvas.line_colored(2, 2, 2, 80, PixelColor::Blue);
11
12    canvas.line_colored(
13        2 + 5,
14        2 + 15,
15        80 + 5,
16        80 + 15,
17        PixelColor::TrueColor { r: 255, g: 0, b: 0 },
18    );
19    canvas.line_colored(
20        2 + 5,
21        80 + 15,
22        80 + 5,
23        80 + 15,
24        PixelColor::TrueColor { r: 0, g: 255, b: 0 },
25    );
26    canvas.line_colored(
27        2 + 5,
28        2 + 15,
29        2 + 5,
30        80 + 15,
31        PixelColor::TrueColor { r: 0, g: 0, b: 255 },
32    );
33    println!("{}", canvas.frame());
34}
Source

pub fn clear(&mut self)

Clears the canvas.

Source

pub fn set(&mut self, x: u32, y: u32)

Sets a pixel at the specified coordinates.

Source

pub fn set_colored(&mut self, x: u32, y: u32, color: PixelColor)

Sets a pixel at the specified coordinates. specifying the color of the braille char

Source

pub fn set_char(&mut self, x: u32, y: u32, c: char)

Sets a letter at the specified coordinates.

Source

pub fn text(&mut self, x: u32, y: u32, max_width: u32, text: &str)

Draws text at the specified coordinates (top-left of the text) up to max_width length

Source

pub fn unset(&mut self, x: u32, y: u32)

Deletes a pixel at the specified coordinates.

Source

pub fn toggle(&mut self, x: u32, y: u32)

Toggles a pixel at the specified coordinates.

Source

pub fn get(&self, x: u32, y: u32) -> bool

Detects whether the pixel at the given coordinates is set.

Source

pub fn rows(&self) -> Vec<String>

Returns a Vec of each row of the Canvas.

Note that each row is actually four pixels high due to the fact that a single Braille character spans two by four pixels.

Source

pub fn frame(&self) -> String

Draws the canvas to a String and returns it.

Examples found in repository?
examples/draw_triangle.rs (line 10)
5fn main() {
6    let mut canvas = Canvas::new(100, 100);
7    canvas.line(2, 2, 80, 80);
8    canvas.line(2, 80, 80, 80);
9    canvas.line(2, 2, 2, 80);
10    println!("{}", canvas.frame());
11}
More examples
Hide additional examples
examples/draw_rgb_triangles.rs (line 33)
6fn main() {
7    let mut canvas = Canvas::new(100, 100);
8    canvas.line_colored(2, 2, 80, 80, PixelColor::Red);
9    canvas.line_colored(2, 80, 80, 80, PixelColor::Green);
10    canvas.line_colored(2, 2, 2, 80, PixelColor::Blue);
11
12    canvas.line_colored(
13        2 + 5,
14        2 + 15,
15        80 + 5,
16        80 + 15,
17        PixelColor::TrueColor { r: 255, g: 0, b: 0 },
18    );
19    canvas.line_colored(
20        2 + 5,
21        80 + 15,
22        80 + 5,
23        80 + 15,
24        PixelColor::TrueColor { r: 0, g: 255, b: 0 },
25    );
26    canvas.line_colored(
27        2 + 5,
28        2 + 15,
29        2 + 5,
30        80 + 15,
31        PixelColor::TrueColor { r: 0, g: 0, b: 255 },
32    );
33    println!("{}", canvas.frame());
34}
Source

pub fn line(&mut self, x1: u32, y1: u32, x2: u32, y2: u32)

Draws a line from (x1, y1) to (x2, y2) onto the Canvas.

Examples found in repository?
examples/draw_triangle.rs (line 7)
5fn main() {
6    let mut canvas = Canvas::new(100, 100);
7    canvas.line(2, 2, 80, 80);
8    canvas.line(2, 80, 80, 80);
9    canvas.line(2, 2, 2, 80);
10    println!("{}", canvas.frame());
11}
Source

pub fn line_colored( &mut self, x1: u32, y1: u32, x2: u32, y2: u32, color: PixelColor, )

Draws a line from (x1, y1) to (x2, y2) onto the Canvas specifying the color of the line

Examples found in repository?
examples/draw_rgb_triangles.rs (line 8)
6fn main() {
7    let mut canvas = Canvas::new(100, 100);
8    canvas.line_colored(2, 2, 80, 80, PixelColor::Red);
9    canvas.line_colored(2, 80, 80, 80, PixelColor::Green);
10    canvas.line_colored(2, 2, 2, 80, PixelColor::Blue);
11
12    canvas.line_colored(
13        2 + 5,
14        2 + 15,
15        80 + 5,
16        80 + 15,
17        PixelColor::TrueColor { r: 255, g: 0, b: 0 },
18    );
19    canvas.line_colored(
20        2 + 5,
21        80 + 15,
22        80 + 5,
23        80 + 15,
24        PixelColor::TrueColor { r: 0, g: 255, b: 0 },
25    );
26    canvas.line_colored(
27        2 + 5,
28        2 + 15,
29        2 + 5,
30        80 + 15,
31        PixelColor::TrueColor { r: 0, g: 0, b: 255 },
32    );
33    println!("{}", canvas.frame());
34}

Trait Implementations§

Source§

impl Clone for Canvas

Source§

fn clone(&self) -> Canvas

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Canvas

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Canvas

Source§

fn eq(&self, other: &Canvas) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Canvas

Source§

impl StructuralPartialEq for Canvas

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.