Skip to content
SnapDOMTry SnapStudioGitHub★ 8K
Labs Experiment

The text you see isn't text

Click into the box below and type. Every glyph is a WebGL texture rendered from the last SnapDOM capture. The editable DOM is invisible, and updates cross-fade in.

TL;DR

A transparent contenteditable proxy gives the browser a real caret and keystrokes; a hidden source element mirrors the text and is what SnapDOM captures. A defineExports plugin feeds each capture into a WebGL renderer that double-buffers two textures and cross-fades between them.

What you're looking at

You get a blinking caret and text selection. The glyphs are pixels in a WebGL texture captured from a hidden DOM element and mixed into the canvas under your cursor. The shader dropdown changes only what the fragment shader does with the finished frame.

Try it

Type, paste or press Enter. The browser handles the caret and selection; WebGL paints the captured letters.

Live DOM signal

Type here

This text is real DOM, but you're seeing it as a WebGL texture kept in sync by SnapDOM.

LIVE GPU
Hidden DOM double-buffered texturestarting…

How it works

  1. A transparent proxy owns the caret

    The .input-layer sits on top of the WebGL canvas with transparent text and background. It gives the browser somewhere to place a caret and receives keyboard and paste events; the caret and selection stay visible.

  2. Every edit is mirrored to a hidden element

    On every input event, paste, and Enter (a beforeinput override inserts a literal newline instead of letting contenteditable create a new <div>), the proxy's text is copied into the hidden #dom-source element and refresh() runs.

  3. refresh() coalesces bursts

    Same dirty/working loop as the live-mirror demo: input during a capture is flagged dirty and picked up by the running loop. It captures #dom-source with the seamlessWebgl plugin.

  4. The plugin exports into the renderer

    The plugin's defineExports returns a webgl export that hands the capture's URL to renderer.update(ctx.export.url) instead of returning an image.

  5. The idle texture is loaded, not the visible one

    SeamlessRenderer.update() loads the new capture into whichever of its two textures (current or next) is off screen, then starts a roughly 110ms blend toward it.

  6. The shader mixes, then the effect applies

    The fragment shader mixes beforeImage and afterImage by a blend uniform that ramps 0 1 over that window; the selected shader runs on the result.

Why this needed a plugin

SnapDOM's core exports return a finished image. defineExports lets a plugin register a named export (here webgl) that does something else with the render, such as pushing it into one of two alternating GPU textures.

The difference from the live-mirror demo is architectural. Splitting the caret owner from the capture source makes the transition seamless: the canvas never waits on a capture, it is always mid-blend between two textures it already has while the hidden source is captured again.

Copy-paste example

The plugin factory forwards a capture's URL to the renderer you hand it:

function seamlessWebgl (renderer) {
  let started = 0
  let elapsed = 0
  return {
    name: 'seamless-webgl',
    beforeSnap () { started = performance.now() },
    afterRender () { elapsed = performance.now() - started },
    defineExports () {
      return {
        webgl: async ctx => {
          const canvas = await renderer.update(ctx.export.url)
          perfLabel.textContent = `${elapsed.toFixed(0)} ms · texture swapped`
          return canvas
        }
      }
    }
  }
}

The usage calls the export through the generic capture.to('webgl') rather than capture.toWebgl(). Both work; .to(name) invokes a plugin-defined export without SnapDOM knowing its name ahead of time:

const plugin = seamlessWebgl(renderer)
let dirty = false
let working = false

async function refresh () {
  dirty = true
  if (working) return
  working = true
  while (dirty) {
    dirty = false
    const capture = await snapdom(source, { plugins: [plugin] })
    await capture.to('webgl')
  }
  working = false
}
input sync #dom-source refresh() capture.to('webgl') renderer.update() blend 0 1 shader

Limits and things to watch

The transparent-proxy trick works for plain text only; rich text and IME composition edge cases need more care. This proxy reduces Enter to a literal newline and paste to plain text. Double-buffering costs one extra WebGL texture of GPU memory over the live-mirror demo, worth it only when the crossfade is the point.

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