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
impl Canvas2D
Sourcepub fn new(width: f32, height: f32) -> Self
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.
Sourcepub fn draw_ex(&self, target_width: f32, target_height: f32)
pub fn draw_ex(&self, target_width: f32, target_height: f32)
Draws canvas with target width/height.
Sourcepub fn mouse_position(&self) -> (f32, f32)
pub fn mouse_position(&self) -> (f32, f32)
Returns mouse position on the canvas
Sourcepub fn mouse_position_ex(
&self,
target_width: f32,
target_height: f32,
) -> (f32, f32)
pub fn mouse_position_ex( &self, target_width: f32, target_height: f32, ) -> (f32, f32)
Returns mouse position with target width/height.
Sourcepub fn get_texture(&self) -> &Texture2D
pub fn get_texture(&self) -> &Texture2D
Returns a reference to the canvas texture.
Sourcepub fn get_texture_mut(&mut self) -> &mut Texture2D
pub fn get_texture_mut(&mut self) -> &mut Texture2D
Returns a mutable reference to the canvas texture.
Sourcepub fn get_size(&self, target_width: f32, target_height: f32) -> Vec2
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.
Sourcepub fn get_padding(&self, target_width: f32, target_height: f32) -> (f32, f32)
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
Sourcepub fn get_size_and_padding(
&self,
target_width: f32,
target_height: f32,
) -> (f32, f32, Vec2)
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.
Sourcepub fn get_scale_factor(
&self,
target_width: f32,
target_height: f32,
) -> (f32, f32)
pub fn get_scale_factor( &self, target_width: f32, target_height: f32, ) -> (f32, f32)
Returns scale factors.
Sourcepub fn get_min_scale_factor(&self, target_width: f32, target_height: f32) -> f32
pub fn get_min_scale_factor(&self, target_width: f32, target_height: f32) -> f32
Returns scale factors’ minimum value.
Methods from Deref<Target = Camera2D>§
Sourcepub fn world_to_screen(&self, point: Vec2) -> Vec2
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()).
Sourcepub fn screen_to_world(&self, point: Vec2) -> Vec2
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.