Skip to main content

euv_engine/renderer/canvas/
struct.rs

1use super::*;
2
3/// A 2D camera that defines the viewport into the game world.
4#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
5pub struct Camera2D {
6    /// The world-space position of the camera center.
7    #[get(type(copy))]
8    #[get_mut(pub(crate))]
9    pub position: Vector2D,
10    /// The zoom factor (1.0 = no zoom, 2.0 = 2x magnification).
11    #[get(type(copy))]
12    #[get_mut(pub(crate))]
13    pub zoom: f64,
14    /// The rotation angle in radians.
15    #[get(type(copy))]
16    #[get_mut(pub(crate))]
17    pub rotation: f64,
18    /// The viewport width in screen pixels.
19    #[get(type(copy))]
20    #[get_mut(pub(crate))]
21    #[set(pub(crate))]
22    pub viewport_width: f64,
23    /// The viewport height in screen pixels.
24    #[get(type(copy))]
25    #[get_mut(pub(crate))]
26    #[set(pub(crate))]
27    pub viewport_height: f64,
28}
29
30/// A 3D camera that defines the viewport into a 3D world using perspective
31/// or orthographic projection.
32#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
33pub struct Camera3D {
34    /// The world-space position of the camera (eye).
35    #[get(type(copy))]
36    #[get_mut(pub(crate))]
37    pub position: Vector3D,
38    /// The point the camera is looking at (target).
39    #[get(type(copy))]
40    #[get_mut(pub(crate))]
41    pub target: Vector3D,
42    /// The up direction for the camera.
43    #[new(skip)]
44    #[get(type(copy))]
45    #[get_mut(pub(crate))]
46    pub up: Vector3D,
47    /// The vertical field of view in radians.
48    #[new(skip)]
49    #[get(type(copy))]
50    #[get_mut(pub(crate))]
51    pub fov: f64,
52    /// The near clipping plane distance.
53    #[new(skip)]
54    #[get(type(copy))]
55    #[get_mut(pub(crate))]
56    pub near: f64,
57    /// The far clipping plane distance.
58    #[new(skip)]
59    #[get(type(copy))]
60    #[get_mut(pub(crate))]
61    pub far: f64,
62    /// The viewport width in pixels.
63    #[get(pub(crate), type(copy))]
64    #[get_mut(pub(crate))]
65    #[set(pub(crate))]
66    pub(crate) viewport_width: f64,
67    /// The viewport height in pixels.
68    #[get(pub(crate), type(copy))]
69    #[get_mut(pub(crate))]
70    #[set(pub(crate))]
71    pub(crate) viewport_height: f64,
72}
73
74/// A wrapper around `CanvasRenderingContext2d` providing convenience
75/// drawing methods and camera management for the game engine.
76#[derive(Clone, Data, New)]
77pub struct CanvasRenderer {
78    /// The underlying canvas 2D rendering context.
79    #[get(pub(crate))]
80    #[get_mut(pub(crate))]
81    #[set(pub(crate))]
82    pub(crate) context: CanvasRenderingContext2d,
83    /// The active camera controlling the viewport.
84    #[get(pub(crate), type(copy))]
85    #[get_mut(pub(crate))]
86    #[set(pub(crate))]
87    pub(crate) camera: Camera2D,
88    /// The active rendering quality preset.
89    ///
90    /// Controls `imageSmoothingEnabled`, `imageSmoothingQuality`, and
91    /// `textRendering` on the underlying context. Defaults to `Medium`.
92    #[get(pub(crate), type(copy))]
93    #[get_mut(pub(crate))]
94    #[set(pub(crate))]
95    pub(crate) quality: RenderQuality,
96}
97
98/// A linear gradient defined by two endpoints and a list of color stops.
99///
100/// Used to create smooth color transitions along a straight line
101/// for fill or stroke operations on the canvas.
102#[derive(Clone, Data, Debug, New, PartialEq)]
103pub struct LinearGradient {
104    /// The starting point of the gradient in world space.
105    #[get(type(copy))]
106    #[get_mut(pub(crate))]
107    #[set(pub(crate))]
108    pub start: Vector2D,
109    /// The ending point of the gradient in world space.
110    #[get(type(copy))]
111    #[get_mut(pub(crate))]
112    #[set(pub(crate))]
113    pub end: Vector2D,
114    /// The ordered list of color stops, each containing a position (0.0 to 1.0) and a CSS color string.
115    #[get_mut(pub(crate))]
116    #[set(pub(crate))]
117    pub stops: Vec<(f64, String)>,
118}
119
120/// A radial gradient defined by inner and outer circles and a list of color stops.
121///
122/// Used to create smooth color transitions radiating outward from a center point
123/// for fill or stroke operations on the canvas.
124#[derive(Clone, Data, Debug, New, PartialEq)]
125pub struct RadialGradient {
126    /// The center of the inner circle of the gradient.
127    #[get(type(copy))]
128    #[get_mut(pub(crate))]
129    #[set(pub(crate))]
130    pub inner_center: Vector2D,
131    /// The radius of the inner circle.
132    #[get(type(copy))]
133    #[get_mut(pub(crate))]
134    #[set(pub(crate))]
135    pub inner_radius: f64,
136    /// The center of the outer circle of the gradient.
137    #[get(type(copy))]
138    #[get_mut(pub(crate))]
139    #[set(pub(crate))]
140    pub outer_center: Vector2D,
141    /// The radius of the outer circle.
142    #[get(type(copy))]
143    #[get_mut(pub(crate))]
144    #[set(pub(crate))]
145    pub outer_radius: f64,
146    /// The ordered list of color stops, each containing a position (0.0 to 1.0) and a CSS color string.
147    #[get_mut(pub(crate))]
148    #[set(pub(crate))]
149    pub stops: Vec<(f64, String)>,
150}
151
152/// Shadow rendering configuration for drop shadow effects on canvas primitives.
153///
154/// When applied, all subsequent fill, stroke, and draw operations will cast
155/// a shadow with the specified color, blur radius, and offset.
156#[derive(Clone, Data, Debug, New, PartialEq, PartialOrd)]
157pub struct ShadowConfig {
158    /// The CSS color string of the shadow (e.g., `"rgba(0,0,0,0.5)"`).
159    #[get(pub(crate), type(clone))]
160    #[get_mut(pub(crate))]
161    #[set(pub(crate))]
162    pub(crate) color: String,
163    /// The blur radius of the shadow in pixels.
164    #[get(pub(crate), type(copy))]
165    #[get_mut(pub(crate))]
166    #[set(pub(crate))]
167    pub(crate) blur: f64,
168    /// The horizontal offset of the shadow in pixels.
169    #[get(pub(crate), type(copy))]
170    #[get_mut(pub(crate))]
171    #[set(pub(crate))]
172    pub(crate) offset_x: f64,
173    /// The vertical offset of the shadow in pixels.
174    #[get(pub(crate), type(copy))]
175    #[get_mut(pub(crate))]
176    #[set(pub(crate))]
177    pub(crate) offset_y: f64,
178}
179
180/// Represents the rendering priority layer for draw call ordering.
181///
182/// Higher z-index values are drawn on top of lower values,
183/// enabling correct visual layering of game objects.
184#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
185pub struct RenderLayer {
186    /// The z-index determining draw order. Higher values draw later (on top).
187    #[get(type(copy))]
188    #[get_mut(pub(crate))]
189    #[set(pub(crate))]
190    pub z_index: i32,
191    /// Whether objects in this layer should be rendered.
192    #[get(type(copy))]
193    #[get_mut(pub(crate))]
194    #[set(pub(crate))]
195    pub visible: bool,
196}
197
198/// An ordered buffer of deferred draw commands recorded during a frame.
199///
200/// Scenes and components push `DrawCommand`s into the list during `on_render`
201/// instead of drawing immediately. The engine then replays the whole list once
202/// per frame via `CanvasRenderer::replay`, which batches consecutive same-style
203/// shapes into a single path and skips redundant canvas state changes. The
204/// backing `Vec` is reused across frames via `clear()` to avoid reallocation.
205#[derive(Clone, Data, Debug, Default, New)]
206pub struct DrawList {
207    /// The recorded draw commands for the current frame.
208    #[get_mut(pub(crate))]
209    #[set(pub(crate))]
210    pub commands: Vec<DrawCommand>,
211}
212
213/// A supersampling anti-aliasing (SSAA) canvas wrapper that renders at a higher
214/// resolution on an offscreen canvas and downscales to the display canvas for
215/// smoother polygon edges in software-rendered 3D scenes.
216///
217/// The offscreen context is scaled by the supersampling factor so that all
218/// drawing code can use logical pixel coordinates without modification. After
219/// rendering, call `present()` to draw the high-resolution buffer onto the
220/// visible canvas with high-quality image smoothing.
221#[derive(Clone, Data, New)]
222pub struct SsaaCanvas {
223    /// The display canvas element visible to the user.
224    #[get_mut(pub(crate))]
225    #[set(pub(crate))]
226    pub display_canvas: HtmlCanvasElement,
227    /// The 2D rendering context of the display canvas used for final presentation.
228    #[get(pub(crate))]
229    #[get_mut(pub(crate))]
230    #[set(pub(crate))]
231    pub(crate) display_context: CanvasRenderingContext2d,
232    /// The offscreen canvas used for high-resolution rendering.
233    #[get(pub(crate))]
234    #[get_mut(pub(crate))]
235    #[set(pub(crate))]
236    pub(crate) offscreen_canvas: HtmlCanvasElement,
237    /// The 2D rendering context of the offscreen canvas, pre-scaled by the
238    /// supersampling factor.
239    #[get_mut(pub(crate))]
240    #[set(pub(crate))]
241    pub offscreen_context: CanvasRenderingContext2d,
242    /// The rendering quality preset for the downscaling present step.
243    ///
244    /// Controls the smoothing strategy when the offscreen buffer is
245    /// downscaled onto the display canvas. Defaults to `Medium`.
246    #[new(skip)]
247    #[get(pub(crate), type(copy))]
248    #[get_mut(pub(crate))]
249    #[set(pub(crate))]
250    pub(crate) quality: RenderQuality,
251    /// The logical display width in CSS pixels.
252    #[get(type(copy))]
253    #[get_mut(pub(crate))]
254    #[set(pub(crate))]
255    pub width: f64,
256    /// The logical display height in CSS pixels.
257    #[get(type(copy))]
258    #[get_mut(pub(crate))]
259    #[set(pub(crate))]
260    pub height: f64,
261}