Skip to main content

euv_engine/renderer/
trait.rs

1use crate::*;
2
3/// A trait for objects that can be rendered onto a canvas.
4pub trait Renderable {
5    /// Draws this object onto the given canvas rendering context.
6    ///
7    /// # Arguments
8    ///
9    /// - `&CanvasRenderingContext2d` - The canvas 2D rendering context.
10    /// - `&Transform2D` - The world-space transform to apply.
11    fn draw(&self, context: &CanvasRenderingContext2d, transform: &Transform2D);
12}
13
14/// An abstract rendering backend that decouples draw calls from the concrete
15/// canvas API, enabling future backends such as WebGL or WebGPU.
16///
17/// All methods mirror the `CanvasRenderingContext2d` interface so that
18/// `CanvasRenderer` can implement this trait with zero overhead.
19pub trait RenderBackend {
20    /// Clears the entire viewport.
21    fn clear(&self);
22
23    /// Clears the viewport and fills it with the given CSS color string.
24    ///
25    /// # Arguments
26    ///
27    /// - `&str` - The CSS color string (e.g., `"#000000"`).
28    fn clear_with_color(&self, color: &str);
29
30    /// Saves the current rendering state onto the state stack.
31    fn save(&self);
32
33    /// Restores the most recently saved rendering state.
34    fn restore(&self);
35
36    /// Sets the fill color for subsequent fill operations.
37    ///
38    /// # Arguments
39    ///
40    /// - `&str` - The CSS color string.
41    fn set_fill_color(&self, color: &str);
42
43    /// Sets the stroke color for subsequent stroke operations.
44    ///
45    /// # Arguments
46    ///
47    /// - `&str` - The CSS color string.
48    fn set_stroke_color(&self, color: &str);
49
50    /// Sets the line width for subsequent stroke operations.
51    ///
52    /// # Arguments
53    ///
54    /// - `f64` - The line width in pixels.
55    fn set_line_width(&self, width: f64);
56
57    /// Sets the global alpha (opacity) for all subsequent drawing operations.
58    ///
59    /// # Arguments
60    ///
61    /// - `f64` - The alpha value in the range 0.0 to 1.0.
62    fn set_global_alpha(&self, alpha: f64);
63
64    /// Sets the blend mode for compositing subsequent draw operations.
65    ///
66    /// # Arguments
67    ///
68    /// - `BlendMode` - The blend mode to apply.
69    fn set_blend_mode(&self, mode: BlendMode);
70
71    /// Applies a shadow configuration for subsequent draw operations.
72    ///
73    /// # Arguments
74    ///
75    /// - `&ShadowConfig` - The shadow configuration to apply.
76    fn set_shadow(&self, config: &ShadowConfig);
77
78    /// Clears any previously applied shadow, disabling shadow rendering.
79    fn clear_shadow(&self);
80
81    /// Fills a rectangle at the given world-space position and dimensions.
82    ///
83    /// # Arguments
84    ///
85    /// - `Vector2D` - The top-left position in world space.
86    /// - `f64` - The width.
87    /// - `f64` - The height.
88    fn fill_rect(&self, position: Vector2D, width: f64, height: f64);
89
90    /// Strokes the outline of a rectangle at the given world-space position and dimensions.
91    ///
92    /// # Arguments
93    ///
94    /// - `Vector2D` - The top-left position in world space.
95    /// - `f64` - The width.
96    /// - `f64` - The height.
97    fn stroke_rect(&self, position: Vector2D, width: f64, height: f64);
98
99    /// Fills a circle at the given world-space center with the specified radius.
100    ///
101    /// # Arguments
102    ///
103    /// - `Vector2D` - The center in world space.
104    /// - `f64` - The radius.
105    fn fill_circle(&self, center: Vector2D, radius: f64);
106
107    /// Strokes the outline of a circle at the given world-space center.
108    ///
109    /// # Arguments
110    ///
111    /// - `Vector2D` - The center in world space.
112    /// - `f64` - The radius.
113    fn stroke_circle(&self, center: Vector2D, radius: f64);
114
115    /// Draws a line segment between two world-space points.
116    ///
117    /// # Arguments
118    ///
119    /// - `Vector2D` - The start point.
120    /// - `Vector2D` - The end point.
121    fn draw_line(&self, start: Vector2D, end: Vector2D);
122
123    /// Fills text at the given world-space position.
124    ///
125    /// # Arguments
126    ///
127    /// - `&str` - The text to draw.
128    /// - `Vector2D` - The position in world space.
129    fn fill_text(&self, text: &str, position: Vector2D);
130
131    /// Sets the font for subsequent text rendering.
132    ///
133    /// # Arguments
134    ///
135    /// - `&str` - The CSS font string (e.g., `"16px sans-serif"`).
136    fn set_font(&self, font: &str);
137
138    /// Draws an image element at the given world-space position and dimensions.
139    ///
140    /// # Arguments
141    ///
142    /// - `&HtmlImageElement` - The image element to draw.
143    /// - `Vector2D` - The top-left position in world space.
144    /// - `f64` - The destination width.
145    /// - `f64` - The destination height.
146    fn draw_image(&self, image: &HtmlImageElement, position: Vector2D, width: f64, height: f64);
147
148    /// Applies a linear gradient as the fill style for subsequent operations.
149    ///
150    /// # Arguments
151    ///
152    /// - `&LinearGradient` - The linear gradient to use as fill style.
153    fn set_linear_gradient_fill(&self, gradient: &LinearGradient);
154
155    /// Applies a radial gradient as the fill style for subsequent operations.
156    ///
157    /// # Arguments
158    ///
159    /// - `&RadialGradient` - The radial gradient to use as fill style.
160    fn set_radial_gradient_fill(&self, gradient: &RadialGradient);
161}