Struct Canvas2D

Source
pub struct Canvas2D {
    pub camera: Camera2D,
}
Expand description

Fixed size 2D canvas

§Description

Canvas2D is basicaly a wrapper around Camera2D with convinience methods to make life easier.

§Note

Deref and DerefMut traits are implemented; this means you can access camera’s fields and methods directly from canvas like so:

let canvas = Canvas2D::new(800_f32, 600_f32);

// These lines do the same thing
println!("{}", canvas.zoom);
println!("{}", canvas.camera.zoom);

§Implementation Detail

There’s a bug that mirrors render target on the Y axis, as a workaround, the render target gets flipped vertically.

Fields§

§camera: Camera2D

The wrapped Camera2D necessary for all the calculations

Implementations§

Source§

impl Canvas2D

Source

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

Creates a new canvas.

§Why does it take floats instead of integers?

The reason it takes floats and not integers is because Macroquad uses floats in (almost) all of its functions.

Source

pub fn draw(&self)

Draws canvas to the screen.

Source

pub fn draw_ex(&self, target_width: f32, target_height: f32)

Draws canvas with target width/height.

Source

pub fn width(&self) -> f32

Returns canvas width.

Source

pub fn height(&self) -> f32

Returns canvas height.

Source

pub fn mouse_position(&self) -> (f32, f32)

Returns mouse position on the canvas

Source

pub fn mouse_position_ex( &self, target_width: f32, target_height: f32, ) -> (f32, f32)

Returns mouse position with target width/height.

Source

pub fn get_texture(&self) -> &Texture2D

Returns a reference to the canvas texture.

Source

pub fn get_texture_mut(&mut self) -> &mut Texture2D

Returns a mutable reference to the canvas texture.

Source

pub fn get_size(&self, target_width: f32, target_height: f32) -> Vec2

Calculate size of the canvas so it can fit inside of the target.

Source

pub fn get_padding(&self, target_width: f32, target_height: f32) -> (f32, f32)

Returns padding of the canvas.

§Note

Internally it uses Canvas2D::get_size_and_padding and simply drops the size, so if you also need size, consider just using Canvas2D::get_size_and_padding

Source

pub fn get_size_and_padding( &self, target_width: f32, target_height: f32, ) -> (f32, f32, Vec2)

Returns size and padding of the canvas. Used in Canvas2D::draw_ex for fitting the canvas on the screen and for centering.

Source

pub fn get_scale_factor( &self, target_width: f32, target_height: f32, ) -> (f32, f32)

Returns scale factors.

Source

pub fn get_min_scale_factor(&self, target_width: f32, target_height: f32) -> f32

Returns scale factors’ minimum value.

Methods from Deref<Target = Camera2D>§

Source

pub fn world_to_screen(&self, point: Vec2) -> Vec2

Returns the screen space position for a 2d camera world space position.

Screen position in window space - from (0, 0) to (screen_width, screen_height()).

Source

pub fn screen_to_world(&self, point: Vec2) -> Vec2

Returns the world space position for a 2d camera screen space position.

Point is a screen space position, often mouse x and y.

Trait Implementations§

Source§

impl Deref for Canvas2D

Source§

type Target = Camera2D

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for Canvas2D

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

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> 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.