Canvas

Trait Canvas 

Source
pub trait Canvas {
Show 13 methods // Required methods fn width(&self) -> u32; fn height(&self) -> u32; fn set_range(&mut self, range: Range<usize>, color: &[Color]); fn get_range(&self, range: Range<usize>) -> &[Color]; // Provided methods fn blit<C: Canvas>( &mut self, src_canvas: &C, dst_x: i64, dst_y: i64, tint: Option<&Color>, ) { ... } fn blit_rect<C: Canvas>( &mut self, src_canvas: &C, src_x: u32, src_y: u32, width: u32, height: u32, dst_x: i64, dst_y: i64, tint: Option<&Color>, ) { ... } fn get(&self, x: u32, y: u32) -> &Color { ... } fn maybe_get(&self, x: i64, y: i64) -> Option<&Color> { ... } fn is_empty_or_color(&self, x: i64, y: i64, color: &Color) -> bool { ... } fn set(&mut self, x: u32, y: u32, color: &Color) { ... } fn clip_rect( &self, x: i64, y: i64, width: u32, height: u32, ) -> Option<(u32, u32, u32, u32)> { ... } fn clear_screen(&mut self, color: &Color) { ... } fn filled_rect( &mut self, sx: i64, sy: i64, width: u32, height: u32, color: &Color, ) { ... }
}
Expand description

Trait representing a basic canvas that can be drawn to.

A canvas provides basic pixel manipulation operations and blitting capabilities for 2D graphics rendering as well as convenience methods for drawing shapes.

Different implementations of this trait exist to utilize different rendering backends like an ansi terminal (CrosstermCanvas), or a window (PixelsCanvas).

Required Methods§

Source

fn width(&self) -> u32

Get the width of the canvas in pixels

Source

fn height(&self) -> u32

Get the height of the canvas in pixels

Source

fn set_range(&mut self, range: Range<usize>, color: &[Color])

Set a range of pixels to a given Color

Source

fn get_range(&self, range: Range<usize>) -> &[Color]

Get a range of pixels as a slice of Colors

Provided Methods§

Source

fn blit<C: Canvas>( &mut self, src_canvas: &C, dst_x: i64, dst_y: i64, tint: Option<&Color>, )

Blit a full input canvas to this canvas instance at a given position, optionally tinting the input canvas with a color.

§Arguments
  • src_canvas - The source canvas to blit from
  • dst_x - The x position to blit the source canvas to
  • dst_y - The y position to blit the source canvas to
  • tint - An optional color to tint the source canvas with
Source

fn blit_rect<C: Canvas>( &mut self, src_canvas: &C, src_x: u32, src_y: u32, width: u32, height: u32, dst_x: i64, dst_y: i64, tint: Option<&Color>, )

Blit only a rectangular region of the input canvas to this canvas instance at a given position, optionally tinting the input canvas with a color. If the source rectangle is partially out of view, only the visible part will be blitted. If the destination rectangle is partially out of view, only the visible part will be blitted.

See also: blit_rect

Source

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

Get the color of a specific pixel at a given position

Source

fn maybe_get(&self, x: i64, y: i64) -> Option<&Color>

Get the color of a specific pixel at a given position, if it is in bounds of the canvas.

§Returns
  • Some(&Color) - If the position is in bounds
  • None - If the position is out of bounds
Source

fn is_empty_or_color(&self, x: i64, y: i64, color: &Color) -> bool

Check if a specific pixel at a given position is out of bounds (empty) or has the specified color.

This method primarily can be used for pixel based collision detection.

Source

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

Set the color of a specific pixel at a given position

Source

fn clip_rect( &self, x: i64, y: i64, width: u32, height: u32, ) -> Option<(u32, u32, u32, u32)>

Clip a rectangle to the bounds of the canvas.

§Returns
  • Some((u32, u32, u32, u32)) - If the rectangle is partially or fully in view
  • None - If the rectangle is completely out of view
Source

fn clear_screen(&mut self, color: &Color)

Clear (fill) the whole canvas with a specific color

Source

fn filled_rect( &mut self, sx: i64, sy: i64, width: u32, height: u32, color: &Color, )

Draw a filled rectangle at a given position with a given width and height

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§