[][src]Struct wasm_game_lib::graphics::canvas::Canvas

pub struct Canvas { /* fields omitted */ }

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; 
}

Methods

impl Canvas[src]

pub fn new() -> Canvas[src]

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.

pub fn fill_rect(&mut self, (x, y): (f64, f64), (w, h): (f64, f64), color: &str)[src]

Fill a part of the canvas with a color.

Example valid values for the color parameter:

  • "blue",
  • "#241F45",
  • "#aaa"

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

Clear a part of the canvas.

pub fn clear(&mut self)[src]

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

pub fn clear_black(&mut self)[src]

Clear all the canvas with a visible black.

pub fn draw(&mut self, object: &impl Drawable)[src]

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.

pub fn get_size(&self) -> (usize, usize)[src]

Return the width and the height of a canvas.

pub fn draw_image(&mut self, (x, y): (f64, f64), image: &Image)[src]

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.

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

Draw a canvas at a specific position.

pub fn fill_text(
    &mut self,
    (x, y): (usize, usize),
    text: &str,
    max_width: Option<usize>
)
[src]

Print text on the canvas. The Text struct is a better way to print text.

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

Set the canvas width in pixels

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

Set the canvas height in pixels

pub fn get_width(&self) -> u32[src]

Return the actual canvas width in pixels

pub fn get_height(&self) -> u32[src]

Return the actual canvas height in pixels

Auto Trait Implementations

impl RefUnwindSafe for Canvas

impl !Send for Canvas

impl !Sync for Canvas

impl Unpin for Canvas

impl UnwindSafe for Canvas

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.