The PNG is the canvas
A paint app with no <canvas> element. The board is an <img> that SnapDOM re-exports as a PNG after every stroke.
A plugin's afterRender hook saves the rendered clone; afterClone swaps it back in on the next capture and appends the newest stroke. SnapDOM uses Canvas only for the raster export.
What you're looking at
Pick a color and brush size, then drag across the board. The page keeps no bitmap buffer; the drawing state lives in the plugin.
Try it
Draw on the board below. The line under the cursor is a local SVG path; the PNG underneath catches up about every 120ms.
How it works
A hidden source element
A 420×420
<div>off-screen holds one empty<svg class="strokes">. SnapDOM captures this element, not the visible board.The visible line is free
A local SVG
<path>on the visible board follows the pointer, sampled withgetCoalescedEvents(), with no capture latency.The stroke goes to the plugin
About every 120ms the stroke's path data goes to
plugin.recordStroke()andsnapdom.toPng()runs on the hidden source.afterClonerestores and applies the strokeThe plugin swaps in the clone saved from the previous render. It finds the stroke's
<path>bydata-stroke-idand updates it, or appends one for a new stroke. One drag stays one<path>.afterRendercheckpoints the resultThe plugin clones the rendered result as the baseline for the next capture.
The output
<img>replaces the boardThe PNG goes into the board. The download button points at the same
<img>.src.
Why this needed a plugin
SnapDOM 3 can reuse eligible captures, but stateful hooks run on fresh captures and the accumulated drawing belongs to the plugin. afterClone and afterRender share one closure: afterClone adds the new mark to savedClone, afterRender saves the result for the next call, independent of capture reuse.
Copy-paste example
The plugin factory. Swap in your own persistence logic for any "keep building on the last capture" case:
function createPaintPlugin (sourceEl) {
let savedClone = null
let skipNext = false
let pendingStroke = null
function makePath (stroke) {
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
path.setAttribute('data-stroke-id', stroke.id)
path.setAttribute('d', stroke.d)
path.setAttribute('fill', 'none')
path.setAttribute('stroke', stroke.color)
path.setAttribute('stroke-width', stroke.size)
path.setAttribute('stroke-linecap', 'round')
path.setAttribute('stroke-linejoin', 'round')
return path
}
return {
name: 'paint',
recordStroke (stroke) { pendingStroke = stroke },
afterClone (ctx) {
if (skipNext) { skipNext = false; pendingStroke = null; return }
if (savedClone) ctx.clone = savedClone.cloneNode(true)
if (!pendingStroke) return
const layer = ctx.clone.querySelector('.strokes')
const existing = layer.querySelector(`[data-stroke-id="${pendingStroke.id}"]`)
if (existing) existing.setAttribute('d', pendingStroke.d)
else layer.append(makePath(pendingStroke))
},
afterRender (ctx) {
savedClone = ctx.clone.cloneNode(true)
pendingStroke = null
},
clear () { skipNext = true }
}
}
The usage: capture again on every stroke update, plugin included:
const plugin = createPaintPlugin(canvasSource)
async function render (stroke) {
if (stroke) plugin.recordStroke(stroke)
const img = await snapdom.toPng(canvasSource, { plugins: [plugin] })
board.replaceChildren(img)
}
// on clear: plugin.clear() then render() again — skipNext lets SnapDOM
// clone the pristine live element once, instead of hand-building an
// empty replacement clone that might miss pipeline setup.
Limits and things to watch
Every stroke update is a full capture, so the render loop is throttled (120ms here) rather than running on every pointermove. The live path keeps the drawing instant while the PNG catches up. The skipNext flag lets clear() hand the first render after a clear to SnapDOM's own cloning pipeline; a hand-built empty clone would miss pipeline behavior added later (pseudo-elements, SVG defs inlining).
Hook contracts and custom exports: plugin API and PLUGIN_SPEC.md.