pub struct Canvas2D { /* private fields */ }
Implementations§
Source§impl Canvas2D
impl Canvas2D
Sourcepub fn new(width: f32, height: f32) -> Self
pub fn new(width: f32, height: f32) -> Self
Create a new canvas with the given width and height.
Sourcepub fn width_height(&self) -> (f32, f32)
pub fn width_height(&self) -> (f32, f32)
Get width and height.
Sourcepub fn get_texture(&self) -> &Texture2D
pub fn get_texture(&self) -> &Texture2D
Get a reference of the canvas texture.
Sourcepub fn get_texture_mut(&mut self) -> &mut Texture2D
pub fn get_texture_mut(&mut self) -> &mut Texture2D
Get a mutable reference of the canvas texture.
Sourcepub fn set_camera(&self)
pub fn set_camera(&self)
Set the canvas as te default camera to draw
if you want to draw to the screen you should call.
macroquad::camera::set_default_camera()
Sourcepub fn calculate_size_and_padding(
&self,
target_width: f32,
target_height: f32,
) -> (f32, f32, Vec2)
pub fn calculate_size_and_padding( &self, target_width: f32, target_height: f32, ) -> (f32, f32, Vec2)
Calculate size and padding of the canvas so it can fit inside of the target and its position is in the center.
Sourcepub fn calculate_size(&self, target_width: f32, target_height: f32) -> Vec2
pub fn calculate_size(&self, target_width: f32, target_height: f32) -> Vec2
Calculate size of the canvas so it can fit inside of the target respecting the aspect ratio.
Sourcepub fn calculate_min_scale_factor(
&self,
target_width: f32,
target_height: f32,
) -> f32
pub fn calculate_min_scale_factor( &self, target_width: f32, target_height: f32, ) -> f32
Calculate the minimum scale factor so the canvas can fit inside of the target respecting the aspect ratio of the canvas.
Sourcepub fn calculate_scale_factor(
&self,
target_width: f32,
target_height: f32,
) -> (f32, f32)
pub fn calculate_scale_factor( &self, target_width: f32, target_height: f32, ) -> (f32, f32)
Calculate scale factor so the canvas can fit inside of the target.
Sourcepub fn parent_coordinates_to_canvas_coordinates(
&self,
parent_width: f32,
parent_height: f32,
screen_x: f32,
screen_y: f32,
offset_x: f32,
offset_y: f32,
) -> (f32, f32)
pub fn parent_coordinates_to_canvas_coordinates( &self, parent_width: f32, parent_height: f32, screen_x: f32, screen_y: f32, offset_x: f32, offset_y: f32, ) -> (f32, f32)
Convert from the parent coordinates to canvas coordinates.
Warning it can return negative numbers or values grater than the canvas when the mouse is outside of the canvas.
Sourcepub fn canvas_coordinates_to_parent_coordinates(
&self,
parent_width: f32,
parent_height: f32,
canvas_x: f32,
canvas_y: f32,
offset_x: f32,
offset_y: f32,
) -> (f32, f32)
pub fn canvas_coordinates_to_parent_coordinates( &self, parent_width: f32, parent_height: f32, canvas_x: f32, canvas_y: f32, offset_x: f32, offset_y: f32, ) -> (f32, f32)
Convert from the canvas coordinates to parent coordinates.
Warning do to float division it can be a small margin of error.
Sourcepub fn screen_coordinates_to_canvas_coordinates(
&self,
screen_x: f32,
screen_y: f32,
offset_x: f32,
offset_y: f32,
) -> (f32, f32)
pub fn screen_coordinates_to_canvas_coordinates( &self, screen_x: f32, screen_y: f32, offset_x: f32, offset_y: f32, ) -> (f32, f32)
A wrapper around the parent_to_canvas
for better ergonomic.
Convert from the screen coordinates to canvas coordinates.
Warning it can return negative numbers or values grater than the canvas when the mouse is outside of the canvas.
Sourcepub fn canvas_coordinates_to_screen_coordinates(
&self,
canvas_x: f32,
canvas_y: f32,
offset_x: f32,
offset_y: f32,
) -> (f32, f32)
pub fn canvas_coordinates_to_screen_coordinates( &self, canvas_x: f32, canvas_y: f32, offset_x: f32, offset_y: f32, ) -> (f32, f32)
A wrapper around the canvas_to_parent
for better ergonomic.
Convert from the canvas coordinates to screen coordinates.
Warning do to float division it can be a small margin of error.
Sourcepub fn screen_mouse_position_to_canvas(
&self,
offset_x: f32,
offset_y: f32,
) -> (f32, f32)
pub fn screen_mouse_position_to_canvas( &self, offset_x: f32, offset_y: f32, ) -> (f32, f32)
Get the mouse position in canvas coordinates.
Warning it can return negative numbers or values grater than the canvas
Sourcepub fn draw_to_screen(&self)
pub fn draw_to_screen(&self)
Draws the canvas to the middle of the screen, keeping the aspect ratio.
It calls set_default_camera
before drawing.
Sourcepub fn set_rotation(&mut self, angle: f32)
pub fn set_rotation(&mut self, angle: f32)
Set the camara rotation. The angle is in degrees.
Sourcepub fn move_camera_by(&mut self, x_amount: f32, y_amount: f32)
pub fn move_camera_by(&mut self, x_amount: f32, y_amount: f32)
Move the camera by a amount in pixels.
Sourcepub fn move_camera_to(&mut self, x_position: f32, y_position: f32)
pub fn move_camera_to(&mut self, x_position: f32, y_position: f32)
Move the camera to the given position.