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.
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
Hello, text
Keystrokes and the spinning cube become patched frames, then characters.
starting…
Loading the stream plugin…
How it works
stream()watches the cardThe 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.
asciiFromCanvas()does the pixel workOn 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 thetoAscii()export uses, exported on its own so it runs on any drawable: a stream frame, a video, your own canvas.One handler joins them
onFramereceives every repaint with its canvas; the handler runsasciiFromCanvas()on it and writes the string. The handle exposes the livecanvas, areadypromise,statsandstop().The
<pre>is replacedtextContentfor plain text,innerHTMLfor 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.