Struct drawing_stuff::canvas::Canvas
source · pub struct Canvas { /* private fields */ }Expand description
A Canvas is just a glorified pixel buffer with some usefull functionality.
Implementations§
source§impl Canvas
impl Canvas
sourcepub fn width(&self) -> usize
pub fn width(&self) -> usize
Returns the width of the canvas.
§Examples
use drawing_stuff::canvas::Canvas;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
assert_eq!(WIDTH, canvas.width());sourcepub fn height(&self) -> usize
pub fn height(&self) -> usize
Returns the height of the canvas.
§Examples
use drawing_stuff::canvas::Canvas;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
assert_eq!(HEIGHT, canvas.height());sourcepub fn buffer(&self) -> &Vec<RGB>
pub fn buffer(&self) -> &Vec<RGB>
Returns a reference to the pixel buffer of the canvas.
§Examples
use drawing_stuff::canvas::Canvas;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let buffer = canvas.buffer();sourcepub fn buffer_mut(&mut self) -> &mut Vec<RGB>
pub fn buffer_mut(&mut self) -> &mut Vec<RGB>
Returns a mutabel reference to the pixel buffer of the canvas.
§Examples
use drawing_stuff::canvas::Canvas;
use drawing_stuff::color::RGB;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let buffer = canvas.buffer_mut();
for pixel in buffer {
*pixel = RGB { r: 255, g: 255, b: 255 };
}sourcepub fn buffer_u32(&self) -> Vec<u32>
pub fn buffer_u32(&self) -> Vec<u32>
Returns the pixel buffer as a 32-bit buffer in the format 0RGB.
§Examples
use drawing_stuff::canvas::Canvas;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let buffer = canvas.buffer_u32();sourcepub fn pixel_inside(&self, x: isize, y: isize) -> bool
pub fn pixel_inside(&self, x: isize, y: isize) -> bool
Checks if the pixel specified lays inside of the canvas.
§Examples
use drawing_stuff::canvas::Canvas;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let in_bound = canvas.pixel_inside(200, 100);
assert_eq!(true, in_bound);sourcepub fn get(&self, x: usize, y: usize) -> Option<&RGB>
pub fn get(&self, x: usize, y: usize) -> Option<&RGB>
Returns the color of the pixel at the specified position.
Returns None if position is not inside the canvas.
§Examples
use drawing_stuff::canvas::Canvas;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let pixel = canvas.get(200, 100);
assert_eq!(true, pixel.is_some());sourcepub fn set(&mut self, x: usize, y: usize, color: RGB) -> Option<()>
pub fn set(&mut self, x: usize, y: usize, color: RGB) -> Option<()>
Sets the color of the pixel at the specified position.
Returns None if position is not inside the canvas.
§Examples
use drawing_stuff::canvas::Canvas;
use drawing_stuff::color::RGB;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let color = RGB { r: 255, g: 255, b: 255 };
let success = canvas.set(200, 100, color);
assert_eq!(true, success.is_some());sourcepub fn fill(&mut self, color: RGB)
pub fn fill(&mut self, color: RGB)
Fills the whole canvas with a given color.
§Examples
use drawing_stuff::canvas::Canvas;
use drawing_stuff::color::RGB;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let color = RGB { r: 255, g: 255, b: 255 };
canvas.fill(color);source§impl Canvas
impl Canvas
sourcepub fn draw<T>(&mut self, drawable: &T)where
T: Draw,
pub fn draw<T>(&mut self, drawable: &T)where
T: Draw,
Draws anything arbitrary implementing the Draw trait onto the canvas.
§Examples
use drawing_stuff::canvas::{Canvas, Draw};
use drawing_stuff::color::RGBA;
pub struct Circle {
pub center: (isize, isize),
pub radius: usize,
pub solid: bool,
pub color: RGBA,
}
impl Draw for Circle {
fn draw(&self, canvas: &mut Canvas) {
match self.solid {
true => canvas.draw_circle_solid(self.center.0, self.center.1, self.radius, self.color),
false => canvas.draw_circle(self.center.0, self.center.1, self.radius, self.color),
}
}
}
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let color = RGBA { r: 255, g: 255, b: 255, a: 255 };
let circle = Circle {
center: (200, 100),
radius: 50,
solid: true,
color,
};
canvas.draw(&circle);
// or
circle.draw(&mut canvas);sourcepub fn draw_pixel(&mut self, x: isize, y: isize, color: RGBA) -> Option<()>
pub fn draw_pixel(&mut self, x: isize, y: isize, color: RGBA) -> Option<()>
Draws a single pixel onto the canvas.
Returns None if position is not inside the canvas.
§Examples
use drawing_stuff::canvas::Canvas;
use drawing_stuff::color::RGBA;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let color = RGBA { r: 255, g: 255, b: 255, a: 255 };
let success = canvas.draw_pixel(200, 100, color);
assert_eq!(true, success.is_some());sourcepub fn draw_line(
&mut self,
x1: isize,
y1: isize,
x2: isize,
y2: isize,
color: RGBA
)
pub fn draw_line( &mut self, x1: isize, y1: isize, x2: isize, y2: isize, color: RGBA )
Draws a line onto the canvas.
§Examples
use drawing_stuff::canvas::Canvas;
use drawing_stuff::color::RGBA;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let color = RGBA { r: 255, g: 255, b: 255, a: 255 };
canvas.draw_line(200, 100, 500, 700, color);sourcepub fn draw_circle(&mut self, x: isize, y: isize, r: usize, color: RGBA)
pub fn draw_circle(&mut self, x: isize, y: isize, r: usize, color: RGBA)
Draws a circle onto the canvas.
§Examples
use drawing_stuff::canvas::Canvas;
use drawing_stuff::color::RGBA;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let color = RGBA { r: 255, g: 255, b: 255, a: 255 };
canvas.draw_circle(200, 100, 15, color);sourcepub fn draw_circle_solid(&mut self, x: isize, y: isize, r: usize, color: RGBA)
pub fn draw_circle_solid(&mut self, x: isize, y: isize, r: usize, color: RGBA)
Draws a solid circle onto the canvas.
§Examples
use drawing_stuff::canvas::Canvas;
use drawing_stuff::color::RGBA;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let color = RGBA { r: 255, g: 255, b: 255, a: 255 };
canvas.draw_circle_solid(200, 100, 15, color);sourcepub fn draw_polygon(&mut self, vertices: &Vec<(isize, isize)>, color: RGBA)
pub fn draw_polygon(&mut self, vertices: &Vec<(isize, isize)>, color: RGBA)
Draws a polygon onto the canvas.
§Examples
use drawing_stuff::canvas::Canvas;
use drawing_stuff::color::RGBA;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let color = RGBA { r: 255, g: 255, b: 255, a: 255 };
let vertices = vec![(200, 100), (500, 700), (300, 800)];
canvas.draw_polygon(&vertices, color);sourcepub fn draw_polygon_solid(
&mut self,
vertices: &Vec<(isize, isize)>,
clockwise: bool,
color: RGBA
)
pub fn draw_polygon_solid( &mut self, vertices: &Vec<(isize, isize)>, clockwise: bool, color: RGBA )
Draws a solid polygon onto the canvas.
The vertices of the polygon have to be given in the specified order (clockwise / anti-clockwise).
§Examples
use drawing_stuff::canvas::Canvas;
use drawing_stuff::color::RGBA;
const WIDTH: usize = 1080;
const HEIGHT: usize = 720;
let mut canvas = Canvas::new(WIDTH, HEIGHT);
let color = RGBA { r: 255, g: 255, b: 255, a: 255 };
let clockwise = true;
let vertices = vec![(200, 100), (500, 700), (300, 800)]; // clockwise
canvas.draw_polygon_solid(&vertices, clockwise, color);