Canvas

Struct Canvas 

Source
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

Source

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.

Source

pub fn clear_rect(&mut self, (x, y): (f64, f64), (w, h): (f64, f64))

Clear a part of the canvas.

Source

pub fn clear(&mut self)

Clear all the canvas with a transparent black (white).

Source

pub fn clear_with_black(&mut self)

Clear all the canvas with a visible black.

Source

pub fn clear_with_color(&mut self, color: Color)

Clear all the canvas with a Color.

Source

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.

Source

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.

Source

pub fn draw_canvas(&mut self, (x, y): (f64, f64), canvas: &Canvas)

Draw a canvas at a specific position.

Source

pub fn get_2d_canvas_rendering_context( &mut self, ) -> &mut CanvasRenderingContext2d

You can use the canvas rendering context to make advanced drawing

Source

pub fn get_canvas_element(&self) -> &HtmlCanvasElement

You can use the html element to do advanced things

Source

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.

Source

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.

Source

pub fn set_width(&mut self, width: u32)

Set the canvas width in pixels

Source

pub fn set_height(&mut self, height: u32)

Set the canvas height in pixels

Source

pub fn get_width(&self) -> u32

Return the actual canvas width in pixels

Source

pub fn get_height(&self) -> u32

Return the actual canvas height in pixels

Source

pub fn get_size(&self) -> (u32, u32)

Return the width and the height of a canvas.

Trait Implementations§

Source§

impl Default for Canvas

Source§

fn default() -> Self

Returns the “default value” for a type. 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> 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, 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.