Skip to content

Canvas Rendering ​

Mermaid Native drives every output from one renderer-agnostic LayoutScene: an ordered stream of DrawCommands (rect, ellipse, line, polyline, polygon, text). The Kotlin/Wasm engine can serialise that scene to a device-independent draw-script instead of SVG, which a host page replays onto an HTML <canvas> 2D context.

The demo below renders live in your browser and supports zoom and pan. It uses the exact same layout scene that produces the SVG output and the native Kuikly Canvas output — so all three are geometrically identical.

Canvas2D rendering

The Kotlin/Wasm engine emits a device-independent draw-script that this page replays onto an HTML <canvas> — the same layout scene that drives the SVG and native Kuikly Canvas outputs.

Initializing engine…

Canvas Preview

Why a canvas path? ​

PathOutputConsumer
SVGdeterministic markupdocs, accessibility, print
Canvas2D{width,height,ops:[…]} draw-scriptweb apps that composite or animate
Native Kuikly Canvasimperative draw callsAndroid / iOS / HarmonyOS hosts

The canvas path is useful when a page needs to composite the diagram with other canvas content, animate it, or avoid a large SVG DOM. Because the script is generated by the same deterministic layout, it stays in lockstep with the SVG golden files.

Host integration ​

The Wasm module exports renderMermaidCanvasJson(source). Parse the result and replay it:

js
const script = JSON.parse(window.mermaidNative.renderMermaidCanvasJson(mermaidSource))
if (script.ops) {
  drawMermaidCanvas(canvasElement, script)
} else {
  // script.diagnostics carries typed failures (code, message, line, column)
}

A dependency-free reference executor ships with the repository at acceptance/mermaid-canvas.js. Hosts own sizing, device-pixel-ratio scaling and accessibility — exactly the boundary the SVG adapter already keeps.

Mermaid-compatible, not Mermaid official.