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.
impl CanvasRenderer
Implements static font and color utility methods for CanvasRenderer.
Sourcepub fn build_font_string(size: f64, family: &str) -> String
pub fn build_font_string(size: f64, family: &str) -> String
Sourcepub fn default_font_string() -> String
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.
Sourcepub fn enable_context_anti_aliasing(context: &CanvasRenderingContext2d)
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.
impl CanvasRenderer
Implements drawing and camera management methods for CanvasRenderer.
Sourcepub fn from_selector(
canvas_selector: &str,
viewport_width: f64,
viewport_height: f64,
) -> Option<CanvasRenderer>
pub fn from_selector( canvas_selector: &str, viewport_width: f64, viewport_height: f64, ) -> Option<CanvasRenderer>
Sourcepub fn enable_anti_aliasing(&self)
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.
Sourcepub fn clear_with_color(&self, color: &str)
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").
Sourcepub fn apply_camera(&self)
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.
Sourcepub fn set_fill_color(&self, color: &str)
pub fn set_fill_color(&self, color: &str)
Sourcepub fn set_stroke_color(&self, color: &str)
pub fn set_stroke_color(&self, color: &str)
Sourcepub fn set_line_width(&self, width: f64)
pub fn set_line_width(&self, width: f64)
Sourcepub fn set_global_alpha(&self, alpha: f64)
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.
Sourcepub fn fill_rect(&self, position: Vector2D, width: f64, height: f64)
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.
Sourcepub fn stroke_rect(&self, position: Vector2D, width: f64, height: f64)
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.
Sourcepub fn fill_circle(&self, center: Vector2D, radius: f64)
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.
Sourcepub fn stroke_circle(&self, center: Vector2D, radius: f64)
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.
Sourcepub fn draw_line(&self, start: Vector2D, end: Vector2D)
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.
Sourcepub fn fill_text(&self, text: &str, position: Vector2D)
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.
Sourcepub fn set_font(&self, font: &str)
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").
Sourcepub fn draw_image(
&self,
image: &HtmlImageElement,
position: Vector2D,
width: f64,
height: f64,
)
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.
Sourcepub fn draw_image_subregion(
&self,
image: &HtmlImageElement,
source: Rect,
dest_position: Vector2D,
dest_width: f64,
dest_height: f64,
)
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
Implements blend mode, shadow, and gradient rendering methods for CanvasRenderer.
impl CanvasRenderer
Implements blend mode, shadow, and gradient rendering methods for CanvasRenderer.
Sourcepub fn set_blend_mode(&self, mode: BlendMode)
pub fn set_blend_mode(&self, mode: BlendMode)
Sets the blend mode for compositing subsequent draw operations.
§Arguments
BlendMode- The blend mode to apply.
Sourcepub fn set_shadow(&self, config: &ShadowConfig)
pub fn set_shadow(&self, config: &ShadowConfig)
Applies a shadow configuration for subsequent draw operations.
§Arguments
&ShadowConfig- The shadow configuration to apply.
Sourcepub fn clear_shadow(&self)
pub fn clear_shadow(&self)
Clears any previously applied shadow, disabling shadow rendering.
Sourcepub fn set_linear_gradient_fill(&self, gradient: &LinearGradient)
pub fn set_linear_gradient_fill(&self, gradient: &LinearGradient)
Applies a linear gradient as the fill style for subsequent operations.
§Arguments
&LinearGradient- The linear gradient to use as fill style.
Sourcepub fn set_radial_gradient_fill(&self, gradient: &RadialGradient)
pub fn set_radial_gradient_fill(&self, gradient: &RadialGradient)
Applies a radial gradient as the fill style for subsequent operations.
§Arguments
&RadialGradient- The radial gradient to use as fill style.
Sourcepub fn set_linear_gradient_stroke(&self, gradient: &LinearGradient)
pub fn set_linear_gradient_stroke(&self, gradient: &LinearGradient)
Applies a linear gradient as the stroke style for subsequent operations.
§Arguments
&LinearGradient- The linear gradient to use as stroke style.
Sourcepub fn set_radial_gradient_stroke(&self, gradient: &RadialGradient)
pub fn set_radial_gradient_stroke(&self, gradient: &RadialGradient)
Applies a radial gradient as the stroke style for subsequent operations.
§Arguments
&RadialGradient- The radial gradient to use as stroke style.
Source§impl CanvasRenderer
impl CanvasRenderer
pub fn get_context(&self) -> &CanvasRenderingContext2d
pub fn get_mut_context(&mut self) -> &mut CanvasRenderingContext2d
pub fn set_context(&mut self, val: CanvasRenderingContext2d) -> &mut Self
pub fn get_camera(&self) -> Camera2D
pub fn get_mut_camera(&mut self) -> &mut Camera2D
pub fn set_camera(&mut self, val: Camera2D) -> &mut Self
Source§impl CanvasRenderer
impl CanvasRenderer
pub fn new(context: CanvasRenderingContext2d, camera: Camera2D) -> Self
Trait Implementations§
Source§impl Clone for CanvasRenderer
impl Clone for CanvasRenderer
Source§fn clone(&self) -> CanvasRenderer
fn clone(&self) -> CanvasRenderer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl RenderBackend for CanvasRenderer
Implements the RenderBackend trait for CanvasRenderer, providing
a backend-agnostic rendering interface.
impl RenderBackend for CanvasRenderer
Implements the RenderBackend trait for CanvasRenderer, providing
a backend-agnostic rendering interface.