euv_engine/renderer/canvas/trait.rs
1use super::*;
2
3/// A trait for objects that can record themselves into a draw list.
4pub trait Renderable {
5 /// Records this object's draw commands into the given draw list.
6 ///
7 /// # Arguments
8 ///
9 /// - `&mut DrawList` - The draw list to record commands into.
10 /// - `&Transform2D` - The world-space transform to apply.
11 fn draw(&self, draw_list: &mut DrawList, 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` - 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 fn fill_rect(&self, position: Vector2D, width: f64, height: f64);
90
91 /// Strokes the outline of a rectangle at the given world-space position and dimensions.
92 ///
93 /// # Arguments
94 ///
95 /// - `Vector2D` - The top-left position in world space.
96 /// - `f64` - The width.
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 fn draw_line(&self, start: Vector2D, end: Vector2D);
121
122 /// Fills text at the given world-space position.
123 ///
124 /// # Arguments
125 ///
126 /// - `&str` - The text to draw.
127 /// - `Vector2D` - The position in world space.
128 fn fill_text(&self, text: &str, position: Vector2D);
129
130 /// Sets the font for subsequent text rendering.
131 ///
132 /// # Arguments
133 ///
134 /// - `&str` - The CSS font string (e.g., `"16px sans-serif"`).
135 fn set_font(&self, font: &str);
136
137 /// Draws an image element at the given world-space position and dimensions.
138 ///
139 /// # Arguments
140 ///
141 /// - `&HtmlImageElement` - The image element to draw.
142 /// - `Vector2D` - The top-left position in world space.
143 /// - `f64` - The destination width.
144 fn draw_image(&self, image: &HtmlImageElement, position: Vector2D, width: f64, height: f64);
145
146 /// Applies a linear gradient as the fill style for subsequent operations.
147 ///
148 /// # Arguments
149 ///
150 /// - `&LinearGradient` - The linear gradient to use as fill style.
151 fn set_linear_gradient_fill(&self, gradient: &LinearGradient);
152
153 /// Applies a radial gradient as the fill style for subsequent operations.
154 ///
155 /// # Arguments
156 ///
157 /// - `&RadialGradient` - The radial gradient to use as fill style.
158 fn set_radial_gradient_fill(&self, gradient: &RadialGradient);
159}