Skip to content
SnapDOMTry SnapStudioGitHub★ 8K
Labs Experiment

DOM, live on the GPU

Edit the card on the left. SnapDOM captures the latest state and uploads it to the WebGL texture on the right; rapid edits are combined into the next capture.

TL;DR

A defineExports plugin adds a webgl export that feeds the captured SVG into an existing WebGL texture. A dirty/working loop collapses fast typing into one trailing capture, so stale captures never queue up.

What you're looking at

The right panel updates after each capture. It is a SnapDOM capture of the live element, decoded into an <img> and uploaded as a WebGL TEXTURE_2D. The theme buttons change a CSS custom property the element reads, so a theme switch takes the same capture path. The shader dropdown only changes how the fragment shader samples the texture on the GPU.

Try it

Edit the text, switch the accent color or swap shaders while typing. The right pane is a WebGL canvas with its own render loop; SnapDOM only refreshes its texture.

Theme
Editable DOMcontenteditable
Live signal

Type here

Every edit becomes a WebGL texture through SnapDOM.

EDITABLE DOM_
WebGL textureidle
GPU texture
input updateMirror() snapdom() capture.toWebgl() GPU texture

How it works

  1. Typing fires an input listener

    Each keystroke in the contenteditable element fires a native input event, whose one listener calls updateMirror().

  2. updateMirror() sets a dirty flag

    If a capture is running, it marks the work dirty and returns. Otherwise it enters a while (dirty) loop and captures the element with { plugins: [mirrorPlugin] }.

  3. The plugin's defineExports(ctx) adds a webgl export

    SnapDOM wires it up as capture.toWebgl(), which passes the capture's context, including ctx.export.url, the rendered SVG data URL.

  4. The export loads that SVG into the existing texture

    It creates an Image from ctx.export.url. Once decoded, the WebGLMirror instance's update() uploads it as a new TEXTURE_2D and returns the canvas.

  5. An independent render loop keeps drawing it

    The mirror's own requestAnimationFrame loop, started once outside SnapDOM, redraws the texture every frame with the selected shader.

  6. The dirty re-check collapses bursts of keystrokes

    dirty is re-checked after each capture, so a fast-typed sentence yields one trailing capture of the latest DOM state.

Why this needed a plugin

SnapDOM's core exports (toPng, toCanvas and the rest) return a finished image or canvas. An existing WebGL texture is a destination the caller invents.

defineExports is the seam for that: a plugin returns named async functions that get the capture's context and are wired up as capture.to<Name>(). beforeSnap/afterRender here only time each round trip for the readout.

Copy-paste example

The plugin factory. It needs only ctx.export.url, so it fits any WebGL renderer with an update(url) method:

function webglMirror (renderer) {
  let started = 0
  let elapsed = 0

  return {
    name: 'webgl-live-mirror',
    beforeSnap () { started = performance.now() },
    afterRender () { elapsed = performance.now() - started },
    defineExports () {
      return {
        webgl: async ctx => {
          const canvas = await renderer.update(ctx.export.url)
          statsEl.textContent = `${elapsed.toFixed(0)} ms · GPU updated`
          return canvas
        }
      }
    }
  }
}

The usage. The dirty/working loop collapses a burst of edits into one trailing capture:

const mirrorPlugin = webglMirror(renderer)
let dirty = false
let working = false

async function updateMirror () {
  dirty = true
  if (working) return
  working = true
  while (dirty) {
    dirty = false
    const capture = await snapdom(editable, { plugins: [mirrorPlugin] })
    await capture.toWebgl()
  }
  working = false
}

editable.addEventListener('input', updateMirror)

Limits and things to watch

A keystroke can trigger a fresh capture and upload: keep the element small, or debounce the handler for heavier content. SnapDOM 3 reuses eligible work automatically, but this demo's timing hooks are stateful and force a fresh capture; resource caches are still reused.

Hook contracts and custom exports: plugin API and PLUGIN_SPEC.md.