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.
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.
Type here
Every edit becomes a WebGL texture through SnapDOM.
How it works
Typing fires an
inputlistenerEach keystroke in the
contenteditableelement fires a nativeinputevent, whose one listener callsupdateMirror().updateMirror()sets adirtyflagIf a capture is running, it marks the work
dirtyand returns. Otherwise it enters awhile (dirty)loop and captures the element with{ plugins: [mirrorPlugin] }.The plugin's
defineExports(ctx)adds awebglexportSnapDOM wires it up as
capture.toWebgl(), which passes the capture's context, includingctx.export.url, the rendered SVG data URL.The export loads that SVG into the existing texture
It creates an
Imagefromctx.export.url. Once decoded, theWebGLMirrorinstance'supdate()uploads it as a newTEXTURE_2Dand returns the canvas.An independent render loop keeps drawing it
The mirror's own
requestAnimationFrameloop, started once outside SnapDOM, redraws the texture every frame with the selected shader.The
dirtyre-check collapses bursts of keystrokesdirtyis 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.