A capture, shattered
Five WebGL shaders, one starting point: a SnapDOM capture of a live, editable card, turned into a WebGL texture.
A defineExports plugin (shatterExport) adds result.toShatter(), which gets a canvas through the silent ctx.exports.canvas() facade (no re-triggered beforeExport/afterExport hooks) and slices it into a grid of small canvases used as WebGL textures. Ripple, glitch, dissolve and metal need no plugin: the whole card is one texture via plain snapdom.toCanvas().
What you're looking at
The card's heading and paragraph are contenteditable. SnapDOM captures the card as it renders (your text, font metrics, gradient, shadow), so the effects run on your own words.
Try it
Edit the card, pick an effect, press the button (it relabels per effect). Ripple and Glitch settle on their own; Dissolve ends empty and needs Reset. Metal tracks your pointer until you exit it.
Try me!
This text is editable HTML. SnapDOM captures it as it renders, font, gradient and shadow included, and turns it into a WebGL texture.
Edit the card's text before applying an effect: you'll see your own text, not a canned image.
How it works
Ripple, Glitch, Dissolve, Metal: one texture, one shader
snapdom.toCanvas(card, { scale: 2 })captures the card as one canvas, uploaded withtexImage2D. A per-effect fragment shader distorts or reveals it over a fixed duration (Metal: indefinitely, tracking the pointer).The card hides while the effect plays
The card's CSS
visibilityis set tohiddenafter the capture and restored when the effect finishes. Metal stays hidden until you exit it.Shatter needs more than one texture
shatterExport'sdefineExports(ctx)returns ashatterexport.result.toShatter({ cols, rows })first gets a canvas viactx.exports.canvas(opts), the core exporter called directly.That canvas is sliced into a grid
The canvas is cut into
cols × rowssmall canvases withdrawImage; each becomes its own WebGL texture at its origin cell.Physics takes over
Each piece spawns with a random velocity and rotation; gravity applies every frame and pieces fade out until none are left.
Why this needed a plugin
SnapDOM's built-in exports (toPng, toCanvas, toSvg, toBlob) return one complete image. Slicing into tiles is a shatter concern, not a capture concern.
defineExports adds the custom shatter output. The shatter export still needs canvas pixels, so it calls the silent ctx.exports.{img,svg,canvas,blob,png,jpeg,webp} facade, which reuses the core exporters without re-running the plugin's beforeExport/afterExport hooks: no recursion, no duplicate firing.
The other four effects use no plugin; most creative uses of a capture need none. defineExports is for a new output shape; reach for toCanvas() first.
Copy-paste example
The plugin factory. cols/rows set the grid:
function shatterExport (options = {}) {
const { cols: defaultCols = 14, rows: defaultRows = 9 } = options
return {
name: 'shatter',
defineExports (ctx) {
return {
shatter: async (_ctx2, opts = {}) => {
const cols = opts.cols || defaultCols
const rows = opts.rows || defaultRows
// Reuse the core canvas exporter directly — this does NOT
// re-run beforeExport/afterExport, so it's safe to call from
// inside another export.
const canvas = await ctx.exports.canvas(opts)
const pw = canvas.width / cols
const ph = canvas.height / rows
const pieces = []
for (let ry = 0; ry < rows; ry++) {
for (let rx = 0; rx < cols; rx++) {
const piece = document.createElement('canvas')
piece.width = pw
piece.height = ph
piece.getContext('2d').drawImage(canvas, rx * pw, ry * ph, pw, ph, 0, 0, pw, ph)
pieces.push({ u: rx / cols, v: ry / rows, du: 1 / cols, dv: 1 / rows, canvas: piece })
}
}
return { pieces }
}
}
}
}
}
Usage. The plugin is local to this capture; toShatter() appears on the result beside toPng/toCanvas:
const result = await snapdom(card, { plugins: [shatterExport({ cols: 16, rows: 11 })] })
const { pieces } = await result.toShatter()
// pieces[i] = { u, v, du, dv, canvas } — grid position (0..1) plus a
// small <canvas>, ready to hand to texImage2D as its own WebGL texture
The other four effects skip the plugin: one capture, one texture:
const canvas = await snapdom.toCanvas(card, { scale: 2 })
const tex = makeTexture(canvas) // gl.texImage2D(..., canvas)
Limits and things to watch
The ctx.exports.canvas() facade is documented in PLUGIN_SPEC.md. A 16×11 grid makes 176 pieces; finer grids add draw calls, textures and memory.
Hook contracts and custom exports: plugin API and PLUGIN_SPEC.md.