Skip to main content

CanvasRenderer

Struct CanvasRenderer 

Source
pub struct CanvasRenderer { /* private fields */ }
Expand description

A wrapper around CanvasRenderingContext2d providing convenience drawing methods and camera management for the game engine.

Implementations§

Source§

impl CanvasRenderer

Implements static font and color utility methods for CanvasRenderer.

Source

pub fn build_font_string(size: f64, family: &str) -> String

Builds a CSS font string from font size and family.

§Arguments
  • f64 - The font size in pixels.
  • &str - The font family name.
§Returns
  • String - The CSS font string (e.g., "16px sans-serif").
Source

pub fn default_font_string() -> String

Creates a default font string using the default font size and family.

§Returns
  • String - The default CSS font string.
Source

pub fn enable_context_anti_aliasing(context: &CanvasRenderingContext2d)

Enables high-quality anti-aliasing on an arbitrary canvas 2D context.

Sets imageSmoothingEnabled to true and imageSmoothingQuality to "high" on the given context. This is a static utility for code that manages its own CanvasRenderingContext2d without using a CanvasRenderer instance.

§Arguments
  • &CanvasRenderingContext2d - The canvas context to configure.
Source§

impl CanvasRenderer

Implements drawing and camera management methods for CanvasRenderer.

Source

pub fn from_selector( canvas_selector: &str, viewport_width: f64, viewport_height: f64, ) -> Option<CanvasRenderer>

Creates a new renderer from a canvas element selector and viewport dimensions.

§Arguments
  • &str - The CSS selector for the canvas element.
  • f64 - The viewport width.
  • f64 - The viewport height.
§Returns
  • Option<CanvasRenderer> - The renderer, or None if the canvas was not found.
Source

pub fn enable_anti_aliasing(&self)

Enables high-quality anti-aliasing on the canvas context by setting imageSmoothingEnabled to true and imageSmoothingQuality to "high".

This improves the quality of image scaling operations (e.g., draw_image) but does not affect vector primitives like lines and fills, which are already anti-aliased by the browser’s 2D canvas implementation.

Source

pub fn clear(&self)

Clears the entire canvas viewport.

Source

pub fn clear_with_color(&self, color: &str)

Clears the canvas and fills it with the given CSS color string.

§Arguments
  • &str - The CSS color string (e.g., "#000000").
Source

pub fn save(&self)

Saves the current canvas state (transform, styles) onto the state stack.

Source

pub fn restore(&self)

Restores the most recently saved canvas state.

Source

pub fn apply_camera(&self)

Applies the camera transform to the canvas context.

Translates to the screen center, applies zoom and rotation, then offsets by the negative camera position.

Source

pub fn set_fill_color(&self, color: &str)

Sets the fill color for subsequent fill operations.

§Arguments
  • &str - The CSS color string.
Source

pub fn set_stroke_color(&self, color: &str)

Sets the stroke color for subsequent stroke operations.

§Arguments
  • &str - The CSS color string.
Source

pub fn set_line_width(&self, width: f64)

Sets the line width for subsequent stroke operations.

§Arguments
  • f64 - The line width in pixels.
Source

pub fn set_global_alpha(&self, alpha: f64)

Sets the global alpha (opacity) for all subsequent drawing operations.

§Arguments
  • f64 - The alpha value in the range 0.0 to 1.0.
Source

pub fn fill_rect(&self, position: Vector2D, width: f64, height: f64)

Fills a rectangle at the given world-space position and dimensions.

§Arguments
  • Vector2D - The top-left position in world space.
  • f64 - The width.
  • f64 - The height.
Source

pub fn stroke_rect(&self, position: Vector2D, width: f64, height: f64)

Strokes the outline of a rectangle at the given world-space position and dimensions.

§Arguments
  • Vector2D - The top-left position in world space.
  • f64 - The width.
  • f64 - The height.
Source

pub fn fill_circle(&self, center: Vector2D, radius: f64)

Fills a circle at the given world-space center with the specified radius.

§Arguments
  • Vector2D - The center in world space.
  • f64 - The radius.
Source

pub fn stroke_circle(&self, center: Vector2D, radius: f64)

Strokes the outline of a circle at the given world-space center.

§Arguments
  • Vector2D - The center in world space.
  • f64 - The radius.
Source

pub fn draw_line(&self, start: Vector2D, end: Vector2D)

Draws a line segment between two world-space points.

§Arguments
  • Vector2D - The start point.
  • Vector2D - The end point.
Source

pub fn fill_text(&self, text: &str, position: Vector2D)

Fills text at the given world-space position.

§Arguments
  • &str - The text to draw.
  • Vector2D - The position in world space.
Source

pub fn set_font(&self, font: &str)

Sets the font for subsequent text rendering.

§Arguments
  • &str - The CSS font string (e.g., "16px sans-serif").
Source

pub fn draw_image( &self, image: &HtmlImageElement, position: Vector2D, width: f64, height: f64, )

Draws an image element at the given world-space position and dimensions.

§Arguments
  • &HtmlImageElement - The image element to draw.
  • Vector2D - The top-left position in world space.
  • f64 - The destination width.
  • f64 - The destination height.
Source

pub fn draw_image_subregion( &self, image: &HtmlImageElement, source: Rect, dest_position: Vector2D, dest_width: f64, dest_height: f64, )

Draws a sub-region of an image element at the given world-space position.

§Arguments
  • &HtmlImageElement - The image element to draw.
  • Rect - The source rectangle within the image.
  • Vector2D - The destination top-left position in world space.
  • f64 - The destination width.
  • f64 - The destination height.
Source§

impl CanvasRenderer

Source

pub fn get_context(&self) -> &CanvasRenderingContext2d

Source

pub fn get_mut_context(&mut self) -> &mut CanvasRenderingContext2d

Source

pub fn set_context(&mut self, val: CanvasRenderingContext2d) -> &mut Self

Source

pub fn get_camera(&self) -> Camera2D

Source

pub fn get_mut_camera(&mut self) -> &mut Camera2D

Source

pub fn set_camera(&mut self, val: Camera2D) -> &mut Self

Source§

impl CanvasRenderer

Source

pub fn new(context: CanvasRenderingContext2d, camera: Camera2D) -> Self

Trait Implementations§

Source§

impl Clone for CanvasRenderer

Source§

fn clone(&self) -> CanvasRenderer

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more