Canvas

Struct Canvas 

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

Implementations§

Source§

impl Canvas

Source

pub fn height(&self) -> usize

Source

pub fn width(&self) -> usize

Source

pub fn set_pixel(&mut self, x: usize, y: usize, r: u8, g: u8, b: u8)

Examples found in repository?
examples/rotating_square.rs (lines 49-55)
22fn main() {
23    let config: RGBMatrixConfig = argh::from_env();
24    let rows = config.rows as isize;
25    let cols = config.cols as isize;
26    let (mut matrix, mut canvas) = RGBMatrix::new(config, 0).expect("Matrix initialization failed");
27
28    let [center_x, center_y] = [cols / 2, rows / 2];
29
30    let rotate_square = (rows.min(cols) as f64 * 1.41) as isize;
31    let min_rotate = center_x - rotate_square / 2;
32    let max_rotate = center_x + rotate_square / 2;
33
34    let display_square = (rows.min(cols) as f64 * 0.7) as isize;
35    let min_display = center_x - display_square / 2;
36    let max_display = center_x + display_square / 2;
37
38    for step in 0.. {
39        let rotation_deg = step as f64 / 2.0;
40        for x in min_rotate..max_rotate {
41            for y in min_rotate..max_rotate {
42                let [rot_x, rot_y] =
43                    rotate([x - center_x, y - center_x], rotation_deg.to_radians());
44                let canvas_x = rot_x + center_x as f64;
45                let canvas_y = rot_y + center_y as f64;
46                if (min_display..max_display).contains(&x)
47                    && (min_display..max_display).contains(&y)
48                {
49                    canvas.set_pixel(
50                        canvas_x as usize,
51                        canvas_y as usize,
52                        scale_col(x, min_display, max_display),
53                        255 - scale_col(y, min_display, max_display),
54                        scale_col(y, min_display, max_display),
55                    )
56                } else {
57                    canvas.set_pixel(canvas_x as usize, canvas_y as usize, 0, 0, 0)
58                }
59            }
60        }
61
62        canvas = matrix.update_on_vsync(canvas);
63
64        if step % 120 == 0 {
65            print!("\r{:>100}\rFramerate: {}", "", matrix.get_framerate());
66            std::io::stdout().flush().unwrap();
67        }
68    }
69}
Source

pub fn fill(&mut self, r: u8, g: u8, b: u8)

Examples found in repository?
examples/image.rs (line 30)
14fn main() {
15    let config: RGBMatrixConfig = argh::from_env();
16    let rows = config.rows;
17    let cols = config.cols;
18    let (mut matrix, mut canvas) = RGBMatrix::new(config, 0).expect("Matrix initialization failed");
19
20    let image_data = ImageRawBE::<Rgb888>::new(IMAGE_DATA, IMAGE_SIZE as u32);
21    let image = Image::new(
22        &image_data,
23        Point::new(
24            (cols / 2 - IMAGE_SIZE / 2) as i32,
25            (rows / 2 - IMAGE_SIZE / 2) as i32,
26        ),
27    );
28
29    for step in 0.. {
30        canvas.fill(0, 0, 0);
31        image.draw(canvas.as_mut()).unwrap();
32        canvas = matrix.update_on_vsync(canvas);
33
34        if step % 120 == 0 {
35            print!("\r{:>100}\rFramerate: {}", "", matrix.get_framerate());
36            std::io::stdout().flush().unwrap();
37        }
38    }
39}
More examples
Hide additional examples
examples/drawing.rs (line 49)
13fn main() {
14    let config: RGBMatrixConfig = argh::from_env();
15    let rows = config.rows as i32;
16    let cols = config.cols as i32;
17
18    let (mut matrix, mut canvas) = RGBMatrix::new(config, 0).expect("Matrix initialization failed");
19
20    let circle = {
21        let thin_stroke = PrimitiveStyle::with_stroke(Rgb888::CSS_GRAY, 1);
22        Circle::with_center(
23            Point::new(rows / 2 - 1, cols / 2 - 1),
24            rows.min(cols) as u32 - 2,
25        )
26        .into_styled(thin_stroke)
27    };
28    let top = Line::new(Point::new(0, 0), Point::new(cols - 1, 0))
29        .into_styled(PrimitiveStyle::with_stroke(Rgb888::GREEN, 1));
30    let bottom = Line::new(Point::new(0, rows - 1), Point::new(cols - 1, rows - 1))
31        .into_styled(PrimitiveStyle::with_stroke(Rgb888::CYAN, 1));
32    let left = Line::new(Point::new(0, 0), Point::new(0, rows - 1))
33        .into_styled(PrimitiveStyle::with_stroke(Rgb888::RED, 1));
34    let right = Line::new(Point::new(cols - 1, 0), Point::new(cols - 1, rows - 1))
35        .into_styled(PrimitiveStyle::with_stroke(Rgb888::BLUE, 1));
36    let diagonal1 = Line::new(Point::new(0, 0), Point::new(cols - 1, rows - 1))
37        .into_styled(PrimitiveStyle::with_stroke(Rgb888::YELLOW, 1));
38    let diagonal2 = Line::new(Point::new(cols - 1, 0), Point::new(0, rows - 1))
39        .into_styled(PrimitiveStyle::with_stroke(Rgb888::MAGENTA, 1));
40
41    let text = Text::with_alignment(
42        "Hello\nWorld",
43        Point::new(cols / 2, rows / 2),
44        MonoTextStyle::new(&FONT_6X10, Rgb888::WHITE),
45        Alignment::Center,
46    );
47
48    for step in 0.. {
49        canvas.fill(0, 0, 0);
50        circle.draw(canvas.as_mut()).unwrap();
51        [diagonal1, diagonal2, top, bottom, left, right]
52            .iter()
53            .for_each(|line| line.draw(canvas.as_mut()).unwrap());
54        if (step / 100) % 2 == 0 {
55            text.draw(canvas.as_mut()).unwrap();
56        }
57        canvas = matrix.update_on_vsync(canvas);
58
59        if step % 120 == 0 {
60            print!("\r{:>100}\rFramerate: {}", "", matrix.get_framerate());
61            std::io::stdout().flush().unwrap();
62        }
63    }
64}
Source

pub fn set_pwm_bits(&mut self, pwm_bits: usize)

Source

pub fn set_brightness(&mut self, brightness: u8)

Set the canvas’ brightness in percent.

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 DrawTarget for Canvas

Source§

type Color = Rgb888

The pixel color type the targetted display supports.
Source§

type Error = Infallible

Error type to return when a drawing operation fails. Read more
Source§

fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where I: IntoIterator<Item = Pixel<Self::Color>>,

Draw individual pixels to the display without a defined order. Read more
Source§

fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error>

Fill the entire display with a solid color. Read more
Source§

fn fill_contiguous<I>( &mut self, area: &Rectangle, colors: I, ) -> Result<(), Self::Error>
where I: IntoIterator<Item = Self::Color>,

Fill a given area with an iterator providing a contiguous stream of pixel colors. Read more
Source§

fn fill_solid( &mut self, area: &Rectangle, color: Self::Color, ) -> Result<(), Self::Error>

Fill a given area with a solid color. Read more
Source§

impl OriginDimensions for Canvas

Source§

fn size(&self) -> Size

Returns the size of the bounding box.

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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> Dimensions for T

Source§

fn bounding_box(&self) -> Rectangle

Returns the bounding box.
Source§

impl<T> DrawTargetExt for T
where T: DrawTarget,

Source§

fn translated(&mut self, offset: Point) -> Translated<'_, T>

Creates a translated draw target based on this draw target. Read more
Source§

fn cropped(&mut self, area: &Rectangle) -> Cropped<'_, T>

Creates a cropped draw target based on this draw target. Read more
Source§

fn clipped(&mut self, area: &Rectangle) -> Clipped<'_, T>

Creates a clipped draw target based on this draw target. Read more
Source§

fn color_converted<C>(&mut self) -> ColorConverted<'_, T, C>
where C: PixelColor + Into<<T as DrawTarget>::Color>,

Creates a color conversion draw target. 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> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
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.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.