Expand description
WASM Canvas2D rendering backend for plotkit.
This crate provides WasmRenderer, a rendering backend that translates
plotkit drawing primitives into HTML5 Canvas2D API calls via web-sys.
It is designed to run in WebAssembly environments inside a browser.
§Architecture
The renderer wraps a CanvasRenderingContext2d obtained from an
HtmlCanvasElement. Each plotkit primitive (path fill, path stroke,
text draw, clipping, image blit) maps directly to the corresponding
Canvas2D method calls.
Helper functions for color conversion, font string construction, and affine transform decomposition are exposed as pure functions so they can be unit-tested without a browser environment.
§Example (browser-side)
ⓘ
use plotkit_render_wasm::WasmRenderer;
use web_sys::CanvasRenderingContext2d;
let ctx: CanvasRenderingContext2d = /* obtain from canvas element */;
let mut renderer = WasmRenderer::new(ctx, 800, 600);
// ... use renderer via the Renderer trait ...
let _ = renderer.finalize();Structs§
- Affine
- A 2D affine transform.
- Color
- An RGBA color with 8 bits per channel.
- Dash
Pattern - A repeating dash pattern for stroked paths.
- Image
- A raster image stored as raw RGBA pixel data.
- Paint
- Describes how a filled region should be painted.
- Path
- A vector path composed of a sequence of
PathElelements. - Point
- A 2D point in device-independent coordinates.
- Rect
- An axis-aligned rectangle defined by its top-left corner and dimensions.
- Stroke
- Describes the visual style of a stroked path.
- Text
Style - Controls how text is rendered: size, color, weight, font, and alignment.
Enums§
- Font
Weight - Font weight selector.
- HAlign
- Horizontal text alignment.
- PathEl
- A single element within a
Path. - Stroke
Cap - The shape applied at the endpoints of an open sub-path.
- Stroke
Join - The shape applied at the join between two path segments.
- VAlign
- Vertical text alignment.
Traits§
- Renderer
- A rendering backend that can produce raster or vector output.
Functions§
- affine_
to_ canvas_ params - Decomposes a plotkit
Affinetransform into the six parameters expected byCanvasRenderingContext2d.setTransform(a, b, c, d, e, f). - build_
font_ string - Builds a CSS font shorthand string from a
TextStyle. - color_
to_ css - Converts a plotkit
Colorto a CSSrgba(...)string. - count_
path_ elements - Counts the number of each path element type in a
Path. - dash_
pattern_ values - Computes a dash pattern array string suitable for Canvas2D
setLineDash. - estimate_
text_ width - Estimates the width of a text string for a given
TextStyle. - halign_
to_ canvas - Returns the Canvas2D
textAlignproperty value for a givenHAlign. - stroke_
cap_ to_ canvas - Returns the Canvas2D
lineCapproperty value for a givenStrokeCap. - stroke_
join_ to_ canvas - Returns the Canvas2D
lineJoinproperty value for a givenStrokeJoin. - valign_
to_ canvas - Returns the Canvas2D
textBaselineproperty value for a givenVAlign.