Skip to main content

Crate plotkit_render_wasm

Crate plotkit_render_wasm 

Source
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.
DashPattern
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 PathEl elements.
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.
TextStyle
Controls how text is rendered: size, color, weight, font, and alignment.

Enums§

FontWeight
Font weight selector.
HAlign
Horizontal text alignment.
PathEl
A single element within a Path.
StrokeCap
The shape applied at the endpoints of an open sub-path.
StrokeJoin
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 Affine transform into the six parameters expected by CanvasRenderingContext2d.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 Color to a CSS rgba(...) 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 textAlign property value for a given HAlign.
stroke_cap_to_canvas
Returns the Canvas2D lineCap property value for a given StrokeCap.
stroke_join_to_canvas
Returns the Canvas2D lineJoin property value for a given StrokeJoin.
valign_to_canvas
Returns the Canvas2D textBaseline property value for a given VAlign.