Skip to content
SnapDOMTry SnapStudioGitHub★ 8K
Labs Experiment

A live element, as text

A card with text and a spinning CSS cube. The right side is the same element redrawn as ASCII art on every frame, through the stream plugin.

TL;DR

Two calls. stream() keeps a canvas in step with the card, and asciiFromCanvas(), the function behind the ascii-export plugin, turns each frame into characters. No capture result in between: the canvas goes straight from one to the other.

What you're looking at

The left card is editable HTML on a white background with solid-colored boxes, the shape the stream plugin patches best. The right pane is a <pre>. On every frame the plugin hands over its canvas, the text is recomputed and replaced. Pick a character set, change the width, or switch to color to see each character carry the pixel it came from.

Try it

Editable DOMcontenteditable + CSS 3D

Hello, text

Keystrokes and the spinning cube become patched frames, then characters.

SNAPDO
ASCII outputstream() + asciiFromCanvas()
starting…

Loading the stream plugin…

How it works

  1. stream() watches the card

    The first frame is a full SnapDOM capture. After that, a keystroke is traced to the nearest box with a solid background and only that subtree is captured and drawn into its rectangle. The cube spins under a 3D transform, which no box of its own can hold, so each turn repaints the white card around it: still a patch, capped here at 15 fps, and the stats count it as one.

  2. asciiFromCanvas() does the pixel work

    On each repaint it downsamples the canvas to one pixel per character, maps luminance to the charset and returns a string, or colored HTML when format: 'html'. It is the same code the toAscii() export uses, exported on its own so it runs on any drawable: a stream frame, a video, your own canvas.

  3. One handler joins them

    onFrame receives every repaint with its canvas; the handler runs asciiFromCanvas() on it and writes the string. The handle exposes the live canvas, a ready promise, stats and stop().

  4. The <pre> is replaced

    textContent for plain text, innerHTML for the colored version. Nothing else on the page is touched.

Copy-paste example

import { stream } from '@zumer/snapdom-stream'
import { asciiFromCanvas } from '@zumer/snapdom-plugins/ascii-export'

const out = document.querySelector('pre')
const live = stream(document.querySelector('#card'), {
  fps: 15,
  onFrame: frame => { out.textContent = asciiFromCanvas(frame.canvas, { width: 90 }) }
})
await live.ready
// later: live.stop()

The same shape works for any pixel-level step on a frame, or for a video element instead of a stream: asciiFromCanvas() takes anything drawImage accepts.

Limits and things to watch

On a gradient card, the plugin can repaint a window of the enclosing card. Resizing, removing nodes, overlapping effects or large dirty regions can require a full frame. More columns increase conversion work; color mode also creates one <span> per character. Lower the width if updates lag.