[−][src]Struct wasm_game_lib::graphics::canvas::Canvas
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
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 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_with_black(&mut self)
[src]
Clear all the canvas with a visible black.
pub fn clear_with_color(&mut self, color: Color)
[src]
Clear all the canvas with a Color.
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 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 get_2d_canvas_rendering_context(
&mut self
) -> &mut CanvasRenderingContext2d
[src]
&mut self
) -> &mut CanvasRenderingContext2d
You can use the canvas rendering context to make advanced drawing
pub fn get_canvas_element(&self) -> &HtmlCanvasElement
[src]
You can use the html element to do advanced things
pub fn fill_rect(
&mut self,
(x, y): (f64, f64),
(w, h): (f64, f64),
color: Color
)
[src]
&mut self,
(x, y): (f64, f64),
(w, h): (f64, f64),
color: Color
)
Fill a part of the canvas with a Color.
pub fn fill_text(
&mut self,
(x, y): (usize, usize),
text: &str,
max_width: Option<usize>
)
[src]
&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.
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
pub fn get_size(&self) -> (u32, u32)
[src]
Return the width and the height of a canvas.
Trait Implementations
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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,