In-Web Video Generation
A finished round becomes a shareable MP4, encoded in the browser. Part of the Box of Cards work.
TS · WebCodecs · Mediabunny · Canvas 2D · Web Audio API · React · html-to-image · Vite
Clip from a round
When a round is won, a 'Save clip' button builds a 720x1280 portrait MP4 of it — cards, winner reveal, confetti — framed for TikTok and Shorts. Everything happens in the browser; no server, no upload, nothing leaves the device. A small chip shows progress and turns into a download button; the encode runs in the background, so it survives the round moving on.
Encoding in the browser
Screen recording produces blurry output, the wrong aspect ratio, and conflicts with the browser over frame timing. Instead the browser encodes the video itself with WebCodecs, selecting a codec at runtime with a hardware-then-software fallback since support varies by browser — every frame drawn by hand and muxed to MP4 with Mediabunny. No recording, no ffmpeg, no server.
Capturing cards as bitmaps
The cards are richly styled React DOM; redrawing them pixel-perfect in canvas would be impractical. So the game's real card components are mounted off-screen with the UI hidden and captured there — what ends up in the clip is the actual React scene, not a canvas re-implementation of it. Each one is snapshotted to a bitmap once with html-to-image, then painted onto the canvas across every frame of the clip, alongside the hand-drawn background, throws and confetti. The frame count is not fixed — it follows the clip length, which scales with how many cards and how much text the round has to show.
Mixing the audio
There is no audio library: one music track is looped and the effects (toss, landing, laugh, confetti) are mixed in sample by sample into a single stereo buffer. Each cue fires on the same timing as the visuals, so the landing sounds exactly when the card lands.
The debug editor
A single config flag turns the same encoder into a full frame-by-frame editor — play, pause, step, scrub, re-encode, download, and even preview a single card toss in isolation. Production runs that exact code path off-screen with the UI hidden; the card throws are pure closed-form math, no keyframes.
Firefox limitations
I love Firefox — it just needs some development attention here: its hardware encoding is roughly 4x slower than Chrome, with no way for a developer to force or fix it. A memory and cache leak accumulates until the encode fails outright, and on modern tablets it does not run at all — so Firefox gets a warning instead of a clip.