Skip to content
SnapDOMTry SnapStudioGitHub★ 8K
Labs Experiment

A capture, shattered

Five WebGL shaders, one starting point: a SnapDOM capture of a live, editable card, turned into a WebGL texture.

TL;DR

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.

Live DOM

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.

100%SnapDOM

Edit the card's text before applying an effect: you'll see your own text, not a canned image.

How it works

  1. Ripple, Glitch, Dissolve, Metal: one texture, one shader

    snapdom.toCanvas(card, { scale: 2 }) captures the card as one canvas, uploaded with texImage2D. A per-effect fragment shader distorts or reveals it over a fixed duration (Metal: indefinitely, tracking the pointer).

  2. The card hides while the effect plays

    The card's CSS visibility is set to hidden after the capture and restored when the effect finishes. Metal stays hidden until you exit it.

  3. Shatter needs more than one texture

    shatterExport's defineExports(ctx) returns a shatter export. result.toShatter({ cols, rows }) first gets a canvas via ctx.exports.canvas(opts), the core exporter called directly.

  4. That canvas is sliced into a grid

    The canvas is cut into cols × rows small canvases with drawImage; each becomes its own WebGL texture at its origin cell.

  5. 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)
card snapdom(plugins) result.toShatter() ctx.exports.canvas() slice into pieces WebGL textures

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.