pub struct Canvas {
pub context: CanvasRenderingContext2d,
/* private fields */
}
Expand description
A Canvas is an object on which you can draw. Only the main Canvas is displayed (returned by Window::init()).
§Example
use wasm_game_lib::graphics::window::Window;
use wasm_game_lib::graphics::image::Image;
use wasm_game_lib::graphics::sprite::Sprite;
use wasm_game_lib::system::sleep;
use std::time::Duration;
// Create a sprite to demonstrate how to draw a sprite on the canvas
let texture = Image::load("https://www.gravatar.com/avatar/419218774d04a581476ea1887a0921e0?s=128&d=identicon&r=PG").await.unwrap();
let sprite = Sprite::<u32>::new((0,0), &texture, (150, 150));
// create the main canvas
let (window, mut canvas) = Window::init();
loop {
canvas.clear(); // clear the canvas at each iteration
canvas.draw(&sprite); // draw a sprite on the canvas
// note that canvas.display() is not needed unlike a lot of graphics libraries
// you may want to slow down the loop to keep your game at 60fps
sleep(Duration::from_millis(16)).await;
}
Fields§
§context: CanvasRenderingContext2d
Implementations§
Source§impl Canvas
impl Canvas
Sourcepub fn new() -> Canvas
pub fn new() -> Canvas
Create a canvas which will not be displayed. To create a displayed canvas, see Window::init(). Creating a undisplayed canvas can be useful because a canvas is drawable on another canvas.
Sourcepub fn clear_rect(&mut self, (x, y): (f64, f64), (w, h): (f64, f64))
pub fn clear_rect(&mut self, (x, y): (f64, f64), (w, h): (f64, f64))
Clear a part of the canvas.
Sourcepub fn clear_with_black(&mut self)
pub fn clear_with_black(&mut self)
Clear all the canvas with a visible black.
Sourcepub fn clear_with_color(&mut self, color: Color)
pub fn clear_with_color(&mut self, color: Color)
Clear all the canvas with a Color.
Sourcepub fn draw(&mut self, object: &impl Drawable)
pub fn draw(&mut self, object: &impl Drawable)
Draw an object implementing the Drawable trait on the canvas.
§Example
// create a sprite to draw it on the canvas
let texture = Image::load("https://www.gravatar.com/avatar/419218774d04a581476ea1887a0921e0?s=128&d=identicon&r=PG").await.unwrap();
let sprite = Sprite::<u32>::new((0,0), &texture, (150, 150));
// create the main canvas
let (window, mut canvas) = Window::init();
// draw the sprite on the canvas
canvas.draw(&sprite);
See above for a more complete example.
Sourcepub fn draw_image(&mut self, (x, y): (f64, f64), image: &Image)
pub fn draw_image(&mut self, (x, y): (f64, f64), image: &Image)
Draw an image at a specific position. This method is intended to be used inside the Drawable trait. In the main code of your game, you should use a Sprite and the draw method.
Sourcepub fn draw_canvas(&mut self, (x, y): (f64, f64), canvas: &Canvas)
pub fn draw_canvas(&mut self, (x, y): (f64, f64), canvas: &Canvas)
Draw a canvas at a specific position.
Sourcepub fn get_2d_canvas_rendering_context(
&mut self,
) -> &mut CanvasRenderingContext2d
pub fn get_2d_canvas_rendering_context( &mut self, ) -> &mut CanvasRenderingContext2d
You can use the canvas rendering context to make advanced drawing
Sourcepub fn get_canvas_element(&self) -> &HtmlCanvasElement
pub fn get_canvas_element(&self) -> &HtmlCanvasElement
You can use the html element to do advanced things
Sourcepub fn fill_rect(
&mut self,
(x, y): (f64, f64),
(w, h): (f64, f64),
color: Color,
)
pub fn fill_rect( &mut self, (x, y): (f64, f64), (w, h): (f64, f64), color: Color, )
Fill a part of the canvas with a Color.
Sourcepub fn fill_text(
&mut self,
(x, y): (usize, usize),
text: &str,
max_width: Option<usize>,
)
pub fn fill_text( &mut self, (x, y): (usize, usize), text: &str, max_width: Option<usize>, )
Print text on the canvas. The Text struct is a better way to print text.
Sourcepub fn set_height(&mut self, height: u32)
pub fn set_height(&mut self, height: u32)
Set the canvas height in pixels
Sourcepub fn get_height(&self) -> u32
pub fn get_height(&self) -> u32
Return the actual canvas height in pixels