Skip to main content

euv_engine/renderer/
enum.rs

1use super::*;
2
3/// Defines how new pixels are composited with existing pixels on the canvas.
4///
5/// Maps directly to the CSS `globalCompositeOperation` property.
6#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
7pub enum BlendMode {
8    /// The source is drawn over the destination (default alpha blending).
9    #[default]
10    Normal,
11    /// The source color is multiplied with the destination, producing a darker result.
12    Multiply,
13    /// The source and destination are inverted, multiplied, then inverted again.
14    Screen,
15    /// The source and destination colors are added together, clamped to maximum brightness.
16    Lighter,
17    /// Combines `Multiply` and `Screen` based on the destination color.
18    Overlay,
19    /// Keeps the darker of the source and destination per channel.
20    Darken,
21    /// Keeps the lighter of the source and destination per channel.
22    Lighten,
23    /// Dodges the destination color brightening it based on the source.
24    ColorDodge,
25    /// Burns the destination color darkening it based on the source.
26    ColorBurn,
27    /// A harsher version of `Overlay` using the source color as the filter.
28    HardLight,
29    /// A softer version of `Overlay` using the source color as the filter.
30    SoftLight,
31    /// Subtracts the darker color from the lighter color per channel.
32    Difference,
33    /// Similar to `Difference` but with lower contrast.
34    Exclusion,
35    /// Uses the hue of the source with the saturation and luminosity of the destination.
36    Hue,
37    /// Uses the saturation of the source with the hue and luminosity of the destination.
38    Saturation,
39    /// Uses the hue and saturation of the source with the luminosity of the destination.
40    Color,
41    /// Uses the luminosity of the source with the hue and saturation of the destination.
42    Luminosity,
43}
44
45/// A single deferred draw operation recorded into a `DrawList`.
46///
47/// Commands carry the resolved style for the operation (fill/stroke color, line
48/// width) so that replay can group consecutive same-style shapes into a single
49/// path and skip redundant canvas state changes. Colors are stored as `Color`
50/// and converted to CSS strings only at replay time.
51#[derive(Clone, Debug, PartialEq)]
52pub enum DrawCommand {
53    /// Fills a rectangle. Carries the fill color.
54    FillRect {
55        /// The top-left position in world space.
56        position: Vector2D,
57        /// The width in pixels.
58        width: f64,
59        /// The height in pixels.
60        height: f64,
61        /// The fill color.
62        color: Color,
63    },
64    /// Strokes the outline of a rectangle. Carries stroke color and line width.
65    StrokeRect {
66        /// The top-left position in world space.
67        position: Vector2D,
68        /// The width in pixels.
69        width: f64,
70        /// The height in pixels.
71        height: f64,
72        /// The stroke color.
73        color: Color,
74        /// The stroke line width in pixels.
75        line_width: f64,
76    },
77    /// Fills a circle. Carries the fill color.
78    FillCircle {
79        /// The center in world space.
80        center: Vector2D,
81        /// The radius in pixels.
82        radius: f64,
83        /// The fill color.
84        color: Color,
85    },
86    /// Strokes the outline of a circle. Carries stroke color and line width.
87    StrokeCircle {
88        /// The center in world space.
89        center: Vector2D,
90        /// The radius in pixels.
91        radius: f64,
92        /// The stroke color.
93        color: Color,
94        /// The stroke line width in pixels.
95        line_width: f64,
96    },
97    /// Draws a line segment. Carries stroke color and line width.
98    Line {
99        /// The start point in world space.
100        start: Vector2D,
101        /// The end point in world space.
102        end: Vector2D,
103        /// The stroke color.
104        color: Color,
105        /// The stroke line width in pixels.
106        line_width: f64,
107    },
108    /// Fills text at a position. Carries the fill color and font.
109    FillText {
110        /// The text to draw.
111        text: String,
112        /// The position in world space.
113        position: Vector2D,
114        /// The fill color.
115        color: Color,
116        /// The CSS font string.
117        font: String,
118    },
119    /// Draws a transformed sprite sub-region (image, source rect, TRS transform).
120    DrawSprite {
121        /// The image to draw.
122        image: HtmlImageElement,
123        /// The source rectangle within the image.
124        source: Rect,
125        /// The world-space transform (position, rotation, scale). Scale signs flip.
126        transform: Transform2D,
127    },
128    /// Draws an image sub-region at a destination rect (no rotation).
129    DrawImageRect {
130        /// The image to draw.
131        image: HtmlImageElement,
132        /// The source rectangle within the image.
133        source: Rect,
134        /// The destination top-left position in world space.
135        dest_position: Vector2D,
136        /// The destination width in pixels.
137        dest_width: f64,
138        /// The destination height in pixels.
139        dest_height: f64,
140    },
141    /// Applies a global alpha to all subsequent commands until changed.
142    SetGlobalAlpha {
143        /// The alpha value in the range 0.0 to 1.0.
144        alpha: f64,
145    },
146    /// Applies a blend mode to all subsequent commands until changed.
147    SetBlendMode {
148        /// The blend mode to apply.
149        mode: BlendMode,
150    },
151}
152
153/// Rendering quality preset controlling anti-aliasing smoothing strategy.
154///
155/// Maps to the canvas `imageSmoothingQuality` value plus an explicit
156/// `imageSmoothingEnabled` toggle. Combined with a CSS `image-rendering:
157/// pixelated` rule on the consumer side, `Low` produces crisp pixel-art
158/// rendering while `High` produces smooth vector-style rendering.
159#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
160pub enum RenderQuality {
161    /// Fastest rendering, pixelated scaling.
162    ///
163    /// Disables `imageSmoothingEnabled` on the canvas context and sets
164    /// `imageSmoothingQuality = "low"`. Pair with CSS `image-rendering:
165    /// pixelated` for sharp nearest-neighbour scaling.
166    Low,
167    /// Balanced rendering with default smoothing quality.
168    ///
169    /// Sets `imageSmoothingQuality = "medium"`.
170    Medium,
171    /// Highest fidelity rendering with smooth edges and high-quality scaling.
172    ///
173    /// Sets `imageSmoothingQuality = "high"`. Best for vector-style content
174    /// on HiDPI displays. This is the default — when no explicit quality is
175    /// requested, the engine errs on the side of visual fidelity rather than
176    /// performance, since users typically notice aliasing artifacts before
177    /// they notice a few extra milliseconds of GPU time.
178    #[default]
179    High,
180}
181
182/// Errors that can occur while asynchronously initializing a `WebGpuRenderer`.
183///
184/// Each variant maps to one specific failure mode that the WebGPU init
185/// pipeline can encounter when calling into the browser's GPU API. The
186/// underlying JS error (when available) is carried as a `JsValue` so callers
187/// can surface the exact diagnostic string without losing fidelity.
188///
189/// Instead of logging diagnostics inside the engine, `WebGpuRenderer::init`
190/// returns `Result<WebGpuRenderer, WebGpuInitError>` and lets the caller
191/// decide how to react — typically via `Console::error` on the example side
192/// or by falling back to the Canvas 2D backend.
193#[derive(Clone, Debug)]
194pub enum WebGpuInitError {
195    /// `Reflect::get(navigator, "webgpu")` threw an exception.
196    ///
197    /// Surfaced when the JavaScript binding lookup itself fails rather than
198    /// simply returning `undefined`/`null`. Carries the original JS error.
199    NavigatorLookup(JsValue),
200    /// `navigator.gpu` is `undefined` or `null`.
201    ///
202    /// The browser does not expose WebGPU on the current origin. The most
203    /// common causes are serving over an insecure origin (must be HTTPS or
204    /// `localhost`) or running in a browser that lacks the WebGPU feature.
205    NavigatorGpuMissing,
206    /// `Reflect::get(gpu, "requestAdapter")` threw an exception.
207    ///
208    /// Carries the original JS error returned by the reflect call.
209    RequestAdapterLookup(JsValue),
210    /// `gpu.requestAdapter()` threw an exception synchronously.
211    ///
212    /// Carries the thrown JS error or value.
213    RequestAdapterCall(JsValue),
214    /// The adapter promise rejected, or the `INIT_PROMISE_TIMEOUT_MILLIS`
215    /// race timer fired before the adapter was produced.
216    ///
217    /// Carries the rejection value, which may be a string, an error object,
218    /// or `undefined` when the timeout won the race.
219    AdapterPromise(JsValue),
220    /// `requestAdapter()` resolved to `null` or `undefined`.
221    ///
222    /// No compatible GPU adapter exists for the requested `powerPreference`.
223    AdapterUnavailable,
224    /// `Reflect::get(adapter, "requestDevice")` threw an exception.
225    RequestDeviceLookup(JsValue),
226    /// `adapter.requestDevice()` threw an exception synchronously.
227    RequestDeviceCall(JsValue),
228    /// The device promise rejected, or the `INIT_PROMISE_TIMEOUT_MILLIS`
229    /// race timer fired before the device was produced.
230    DevicePromise(JsValue),
231    /// `requestDevice()` resolved to `null` or `undefined`.
232    ///
233    /// The adapter could not allocate a device, typically because the
234    /// adapter is in a `device-lost` state.
235    DeviceUnavailable,
236    /// `document.querySelector(canvas_selector)` returned `None`.
237    ///
238    /// The canvas element is not in the DOM yet (or its selector is wrong).
239    /// Carries the selector string that was queried.
240    CanvasNotFound(String),
241    /// `document.querySelector(canvas_selector)` threw an exception.
242    CanvasQuery(JsValue),
243    /// `canvas.get_context("webgpu")` returned `None`.
244    ///
245    /// The canvas is already using a different context type, or WebGPU is
246    /// disabled for this canvas.
247    CanvasContextUnavailable,
248    /// `Reflect::get(gpu, "getPreferredCanvasFormat")` threw an exception.
249    PreferredFormatLookup(JsValue),
250    /// `gpu.getPreferredCanvasFormat()` threw an exception synchronously.
251    PreferredFormatCall(JsValue),
252    /// `getPreferredCanvasFormat()` resolved to a value that is not a string.
253    ///
254    /// Carries the offending JS value so callers can log its type/name.
255    PreferredFormatType(JsValue),
256    /// `Reflect::get(context, "configure")` threw an exception.
257    ConfigureLookup(JsValue),
258    /// `Reflect::get(device, "queue")` threw an exception.
259    QueueLookup(JsValue),
260}