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    /// - `C: AsRef<str>` - The CSS color string (e.g., `"#000000"`).
28    fn clear_color<C>(&self, color: C)
29    where
30        C: AsRef<str>;
31
32    /// Saves the current rendering state onto the state stack.
33    fn save(&self);
34
35    /// Restores the most recently saved rendering state.
36    fn restore(&self);
37
38    /// Sets the fill color for subsequent fill operations.
39    ///
40    /// # Arguments
41    ///
42    /// - `&str` - The CSS color string.
43    fn set_fill_color(&self, color: &str);
44
45    /// Sets the stroke color for subsequent stroke operations.
46    ///
47    /// # Arguments
48    ///
49    /// - `&str` - The CSS color string.
50    fn set_stroke_color(&self, color: &str);
51
52    /// Sets the line width for subsequent stroke operations.
53    ///
54    /// # Arguments
55    ///
56    /// - `f64` - The line width in pixels.
57    fn set_line_width(&self, width: f64);
58
59    /// Sets the global alpha (opacity) for all subsequent drawing operations.
60    ///
61    /// # Arguments
62    ///
63    /// - `f64` - The alpha value in the range 0.0 to 1.0.
64    fn set_global_alpha(&self, alpha: f64);
65
66    /// Sets the blend mode for compositing subsequent draw operations.
67    ///
68    /// # Arguments
69    ///
70    /// - `BlendMode` - The blend mode to apply.
71    fn set_blend_mode(&self, mode: BlendMode);
72
73    /// Applies a shadow configuration for subsequent draw operations.
74    ///
75    /// # Arguments
76    ///
77    /// - `&ShadowConfig` - The shadow configuration to apply.
78    fn set_shadow(&self, config: &ShadowConfig);
79
80    /// Clears any previously applied shadow, disabling shadow rendering.
81    fn clear_shadow(&self);
82
83    /// Fills a rectangle at the given world-space position and dimensions.
84    ///
85    /// # Arguments
86    ///
87    /// - `Vector2D` - The top-left position in world space.
88    /// - `f64` - The width.
89    /// - `f64` - The height.
90    fn fill_rect(&self, position: Vector2D, width: f64, height: f64);
91
92    /// Strokes the outline of a rectangle at the given world-space position and dimensions.
93    ///
94    /// # Arguments
95    ///
96    /// - `Vector2D` - The top-left position in world space.
97    /// - `f64` - The width.
98    /// - `f64` - The height.
99    fn stroke_rect(&self, position: Vector2D, width: f64, height: f64);
100
101    /// Fills a circle at the given world-space center with the specified radius.
102    ///
103    /// # Arguments
104    ///
105    /// - `Vector2D` - The center in world space.
106    /// - `f64` - The radius.
107    fn fill_circle(&self, center: Vector2D, radius: f64);
108
109    /// Strokes the outline of a circle at the given world-space center.
110    ///
111    /// # Arguments
112    ///
113    /// - `Vector2D` - The center in world space.
114    /// - `f64` - The radius.
115    fn stroke_circle(&self, center: Vector2D, radius: f64);
116
117    /// Draws a line segment between two world-space points.
118    ///
119    /// # Arguments
120    ///
121    /// - `Vector2D` - The start point.
122    /// - `Vector2D` - The end point.
123    fn draw_line(&self, start: Vector2D, end: Vector2D);
124
125    /// Fills text at the given world-space position.
126    ///
127    /// # Arguments
128    ///
129    /// - `&str` - The text to draw.
130    /// - `Vector2D` - The position in world space.
131    fn fill_text(&self, text: &str, position: Vector2D);
132
133    /// Sets the font for subsequent text rendering.
134    ///
135    /// # Arguments
136    ///
137    /// - `&str` - The CSS font string (e.g., `"16px sans-serif"`).
138    fn set_font(&self, font: &str);
139
140    /// Draws an image element at the given world-space position and dimensions.
141    ///
142    /// # Arguments
143    ///
144    /// - `&HtmlImageElement` - The image element to draw.
145    /// - `Vector2D` - The top-left position in world space.
146    /// - `f64` - The destination width.
147    /// - `f64` - The destination height.
148    fn draw_image(&self, image: &HtmlImageElement, position: Vector2D, width: f64, height: f64);
149
150    /// Applies a linear gradient as the fill style for subsequent operations.
151    ///
152    /// # Arguments
153    ///
154    /// - `&LinearGradient` - The linear gradient to use as fill style.
155    fn set_linear_gradient_fill(&self, gradient: &LinearGradient);
156
157    /// Applies a radial gradient as the fill style for subsequent operations.
158    ///
159    /// # Arguments
160    ///
161    /// - `&RadialGradient` - The radial gradient to use as fill style.
162    fn set_radial_gradient_fill(&self, gradient: &RadialGradient);
163}