pub struct Canvas { /* private fields */ }
Implementations§
Source§impl Canvas
impl Canvas
pub fn height(&self) -> usize
pub fn width(&self) -> usize
Sourcepub fn set_pixel(&mut self, x: usize, y: usize, r: u8, g: u8, b: u8)
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}
Sourcepub fn fill(&mut self, r: u8, g: u8, b: u8)
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
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}
pub fn set_pwm_bits(&mut self, pwm_bits: usize)
Sourcepub fn set_brightness(&mut self, brightness: u8)
pub fn set_brightness(&mut self, brightness: u8)
Set the canvas’ brightness in percent.
Trait Implementations§
Source§impl DrawTarget for Canvas
impl DrawTarget for Canvas
Source§type Error = Infallible
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>
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
Draw individual pixels to the display without a defined order. Read more
Source§fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error>
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>,
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
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
Mutably borrows from an owned value. Read more
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Dimensions for Twhere
T: OriginDimensions,
impl<T> Dimensions for Twhere
T: OriginDimensions,
Source§fn bounding_box(&self) -> Rectangle
fn bounding_box(&self) -> Rectangle
Returns the bounding box.
Source§impl<T> DrawTargetExt for Twhere
T: DrawTarget,
impl<T> DrawTargetExt for Twhere
T: DrawTarget,
Source§fn translated(&mut self, offset: Point) -> Translated<'_, T>
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>
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>
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>
fn color_converted<C>(&mut self) -> ColorConverted<'_, T, C>
Creates a color conversion draw target. Read more
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Casts the value.
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
Source§fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
Source§fn unwrapped_cast_from(src: Src) -> Dst
fn unwrapped_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> WrappingAs for T
impl<T> WrappingAs for T
Source§fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
Source§fn wrapping_cast_from(src: Src) -> Dst
fn wrapping_cast_from(src: Src) -> Dst
Casts the value.