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 default_font() -> String
pub fn default_font() -> String
Creates a default font string using the default font size and family.
§Returns
String- The default CSS font string.
Sourcepub fn enable_smoothing_on(context: &CanvasRenderingContext2d)
pub fn enable_smoothing_on(context: &CanvasRenderingContext2d)
Enables high-quality anti-aliasing on an arbitrary canvas 2D context.
Applies the High rendering quality preset via apply_quality,
which sets imageSmoothingEnabled, imageSmoothingQuality = "high",
and textRendering = "geometricPrecision" on the given context.
Use this static helper when you manage your own CanvasRenderingContext2d
and don’t hold a CanvasRenderer instance. For instances, call
renderer.enable_smoothing() instead.
§Arguments
&CanvasRenderingContext2d- The canvas context to configure.
Sourcepub fn detect_dpr() -> f64
pub fn detect_dpr() -> f64
Detects the host device pixel ratio (HiDPI scale factor) via reflection.
Reads window.devicePixelRatio using Reflect::get because the
web-sys Window features currently in use do not expose a native
getter for this property. Falls back to
RENDERER_DEFAULT_DEVICE_PIXEL_RATIO (1.0) when the value is missing,
not a finite number, or below 1.0.
§Returns
f64- The detected device pixel ratio (clamped to>= 1.0).
Source§impl CanvasRenderer
impl CanvasRenderer
Sourcepub fn from_selector<S>(
canvas_selector: S,
viewport_width: f64,
viewport_height: f64,
) -> Option<CanvasRenderer>
pub fn from_selector<S>( canvas_selector: S, viewport_width: f64, viewport_height: f64, ) -> Option<CanvasRenderer>
Sourcepub fn enable_smoothing(&self)
pub fn enable_smoothing(&self)
Enables high-quality anti-aliasing on the canvas context by setting
imageSmoothingEnabled to true and imageSmoothingQuality to "high".
Applies the active quality preset via the shared apply_quality
helper so that all smoothing-related settings are kept in sync.
Sourcepub fn clear_color<C>(&self, color: C)
pub fn clear_color<C>(&self, color: C)
Clears the canvas and fills it with the given CSS color string.
§Arguments
C: AsRef<str>- The CSS color string (e.g.,"#000000").
Sourcepub fn replay(&self, list: &DrawList)
pub fn replay(&self, list: &DrawList)
Replays a recorded DrawList onto this renderer’s canvas.
Convenience wrapper around replay_context using this renderer’s context.
§Arguments
&DrawList- The recorded commands to replay.
Sourcepub fn replay_context(context: &CanvasRenderingContext2d, list: &DrawList)
pub fn replay_context(context: &CanvasRenderingContext2d, list: &DrawList)
Replays a recorded DrawList onto an arbitrary canvas 2D context in a
single batched pass.
Consecutive same-style shapes are merged into one path (one begin_path
plus one fill/stroke per style run), fill/stroke colors and line
widths are only re-applied when they change, and sprites are drawn with a
single set_transform rather than a save/restore pair. This collapses
the per-shape canvas state churn of immediate-mode drawing.
The canvas transform and global alpha are reset to identity / 1.0 when
replay finishes, so callers can sandwich the call between
save()/apply_camera() and restore() without leaking state.
§Arguments
&CanvasRenderingContext2d- The target canvas 2D context.&DrawList- The recorded commands to replay.
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<C>(&self, color: C)
pub fn set_fill_color<C>(&self, color: C)
Sets the fill color for subsequent fill operations.
§Arguments
C: AsRef<str>- The CSS color string.
Sourcepub fn set_stroke_color<C>(&self, color: C)
pub fn set_stroke_color<C>(&self, color: C)
Sets the stroke color for subsequent stroke operations.
§Arguments
C: AsRef<str>- The CSS color string.
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<T>(&self, text: T, position: Vector2D)
pub fn fill_text<T>(&self, text: T, position: Vector2D)
Fills text at the given world-space position.
§Arguments
T: AsRef<str>- The text to draw.Vector2D- The position in world space.
Sourcepub fn set_font<F>(&self, font: F)
pub fn set_font<F>(&self, font: F)
Sets the font for subsequent text rendering.
§Arguments
F: AsRef<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_rect(
&self,
image: &HtmlImageElement,
source: Rect,
dest_position: Vector2D,
dest_width: f64,
dest_height: f64,
)
pub fn draw_image_rect( &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
pub fn get_quality(&self) -> RenderQuality
pub fn get_mut_quality(&mut self) -> &mut RenderQuality
pub fn set_quality(&mut self, val: RenderQuality) -> &mut Self
Source§impl CanvasRenderer
impl CanvasRenderer
pub fn new( context: CanvasRenderingContext2d, camera: Camera2D, quality: RenderQuality, ) -> 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.
Each method forwards to the inherent CanvasRenderer method of the
same name, so the per-call documentation lives on the trait definition
in engine::renderer::trait — the inherent method is the source of
truth, this impl is the trait bridge.
Source§fn clear(&self)
fn clear(&self)
Forwards to CanvasRenderer::clear.
Source§fn clear_color<C>(&self, color: C)
fn clear_color<C>(&self, color: C)
Forwards to CanvasRenderer::clear_color.
Source§fn save(&self)
fn save(&self)
Forwards to CanvasRenderer::save.
Source§fn restore(&self)
fn restore(&self)
Forwards to CanvasRenderer::restore.
Source§fn set_fill_color(&self, color: &str)
fn set_fill_color(&self, color: &str)
Forwards to CanvasRenderer::set_fill_color.
Source§fn set_stroke_color(&self, color: &str)
fn set_stroke_color(&self, color: &str)
Forwards to CanvasRenderer::set_stroke_color.
Source§fn set_line_width(&self, width: f64)
fn set_line_width(&self, width: f64)
Forwards to CanvasRenderer::set_line_width.
Source§fn set_global_alpha(&self, alpha: f64)
fn set_global_alpha(&self, alpha: f64)
Forwards to CanvasRenderer::set_global_alpha.
Source§fn set_blend_mode(&self, mode: BlendMode)
fn set_blend_mode(&self, mode: BlendMode)
Forwards to CanvasRenderer::set_blend_mode.
Source§fn set_shadow(&self, config: &ShadowConfig)
fn set_shadow(&self, config: &ShadowConfig)
Forwards to CanvasRenderer::set_shadow.
Source§fn clear_shadow(&self)
fn clear_shadow(&self)
Forwards to CanvasRenderer::clear_shadow.
Source§fn fill_rect(&self, position: Vector2D, width: f64, height: f64)
fn fill_rect(&self, position: Vector2D, width: f64, height: f64)
Forwards to CanvasRenderer::fill_rect.
Source§fn stroke_rect(&self, position: Vector2D, width: f64, height: f64)
fn stroke_rect(&self, position: Vector2D, width: f64, height: f64)
Forwards to CanvasRenderer::stroke_rect.
Source§fn fill_circle(&self, center: Vector2D, radius: f64)
fn fill_circle(&self, center: Vector2D, radius: f64)
Forwards to CanvasRenderer::fill_circle.
Source§fn stroke_circle(&self, center: Vector2D, radius: f64)
fn stroke_circle(&self, center: Vector2D, radius: f64)
Forwards to CanvasRenderer::stroke_circle.
Source§fn set_font(&self, font: &str)
fn set_font(&self, font: &str)
Forwards to CanvasRenderer::set_font.
Source§fn draw_image(
&self,
image: &HtmlImageElement,
position: Vector2D,
width: f64,
height: f64,
)
fn draw_image( &self, image: &HtmlImageElement, position: Vector2D, width: f64, height: f64, )
Forwards to CanvasRenderer::draw_image.
Source§fn set_linear_gradient_fill(&self, gradient: &LinearGradient)
fn set_linear_gradient_fill(&self, gradient: &LinearGradient)
Forwards to CanvasRenderer::set_linear_gradient_fill.
Source§fn set_radial_gradient_fill(&self, gradient: &RadialGradient)
fn set_radial_gradient_fill(&self, gradient: &RadialGradient)
Forwards to CanvasRenderer::set_radial_gradient_fill.