Skip to main content

RenderBackend

Trait RenderBackend 

Source
pub trait RenderBackend {
Show 21 methods // Required methods fn clear(&self); fn clear_with_color(&self, color: &str); fn save(&self); fn restore(&self); fn set_fill_color(&self, color: &str); fn set_stroke_color(&self, color: &str); fn set_line_width(&self, width: f64); fn set_global_alpha(&self, alpha: f64); fn set_blend_mode(&self, mode: BlendMode); fn set_shadow(&self, config: &ShadowConfig); fn clear_shadow(&self); fn fill_rect(&self, position: Vector2D, width: f64, height: f64); fn stroke_rect(&self, position: Vector2D, width: f64, height: f64); fn fill_circle(&self, center: Vector2D, radius: f64); fn stroke_circle(&self, center: Vector2D, radius: f64); fn draw_line(&self, start: Vector2D, end: Vector2D); fn fill_text(&self, text: &str, position: Vector2D); fn set_font(&self, font: &str); fn draw_image( &self, image: &HtmlImageElement, position: Vector2D, width: f64, height: f64, ); fn set_linear_gradient_fill(&self, gradient: &LinearGradient); fn set_radial_gradient_fill(&self, gradient: &RadialGradient);
}
Expand description

An abstract rendering backend that decouples draw calls from the concrete canvas API, enabling future backends such as WebGL or WebGPU.

All methods mirror the CanvasRenderingContext2d interface so that CanvasRenderer can implement this trait with zero overhead.

Required Methods§

Source

fn clear(&self)

Clears the entire viewport.

Source

fn clear_with_color(&self, color: &str)

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

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

fn save(&self)

Saves the current rendering state onto the state stack.

Source

fn restore(&self)

Restores the most recently saved rendering state.

Source

fn set_fill_color(&self, color: &str)

Sets the fill color for subsequent fill operations.

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

fn set_stroke_color(&self, color: &str)

Sets the stroke color for subsequent stroke operations.

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

fn set_line_width(&self, width: f64)

Sets the line width for subsequent stroke operations.

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

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

fn set_blend_mode(&self, mode: BlendMode)

Sets the blend mode for compositing subsequent draw operations.

§Arguments
  • BlendMode - The blend mode to apply.
Source

fn set_shadow(&self, config: &ShadowConfig)

Applies a shadow configuration for subsequent draw operations.

§Arguments
  • &ShadowConfig - The shadow configuration to apply.
Source

fn clear_shadow(&self)

Clears any previously applied shadow, disabling shadow rendering.

Source

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

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

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

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

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

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

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

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

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.
Source

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.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl RenderBackend for CanvasRenderer

Implements the RenderBackend trait for CanvasRenderer, providing a backend-agnostic rendering interface.