Struct 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

Source

pub fn new(width: usize, height: usize) -> Self

Creates a new black canvas.

§Examples
use drawing_stuff::canvas::Canvas;

const WIDTH: usize = 1080;
const HEIGHT: usize = 720;

let mut canvas = Canvas::new(WIDTH, HEIGHT);
Source§

impl Canvas

Source

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());
Source

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());
Source

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();
Source

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 };
}
Source

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();
Source

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);
Source

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());
Source

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());
Source

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

Source

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: u32,
    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);
Source

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());
Source

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);
Source

pub fn draw_polyline( &mut self, x1: isize, y1: isize, x2: isize, y2: isize, width: u32, color: RGBA, )

Draws a line with specified width onto the canvas. Drawing the line as a filled polygon.

§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_polyline(200, 100, 500, 700, 5, color);
Source

pub fn draw_polyline_capped( &mut self, x1: isize, y1: isize, x2: isize, y2: isize, width: u32, color: RGBA, )

Draws a line with specified width and capped ends onto the canvas. Drawing the line as a filled polygon with circles on both ends.

§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_polyline_capped(200, 100, 500, 700, 5, color);
Source

pub fn draw_circle(&mut self, x: isize, y: isize, r: u32, 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);
Source

pub fn draw_circle_solid(&mut self, x: isize, y: isize, r: u32, 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);
Source

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);
Source

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);

Trait Implementations§

Source§

impl Clone for Canvas

Source§

fn clone(&self) -> Canvas

Returns a copy 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

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.