Skip to content

recordFrames

Read as Markdown

import { recordFrames, exportGif } from "@pibbl/core";
const canvas = document.querySelector<HTMLCanvasElement>("canvas")!;
// This canvas must already have a live await pibbl() mount.
const capture = recordFrames(canvas, { duration: 5000, frameRate: 60 });
await capture.ready;
// capture.stop() finishes early and keeps frames.
// capture.cancel() discards active capture and rejects with AbortError.
const recording = await capture.finished;
try {
const gif = await exportGif(recording, {
repeatCount: 0,
background: "white",
});
// gif is a caller-owned image/gif Blob.
} finally {
recording.dispose();
}

Capture observes successful, fully composed Pibbl root renders, with the current committed bitmap at timestamp zero. onRenderComplete receives the committed generation after render-owned inspection state publishes; failed attempts are reported as failed and reject the recording without being labeled as pixel evidence. A recording frame’s renderGeneration identifies the committed bitmap that was copied. onFrameCaptured runs after that frame’s RGBA bytes have been copied and reports the same generation; it may run after one or more completion callbacks when capture coalesces renders. Timestamps are wall-clock capture times: they are not logical animation times or generation IDs. Recording does not schedule rendering or fabricate frames while the app does not render. Renders inside the capture-rate interval coalesce into one pending capture so the final static bitmap is preserved. The maximum capture rate defaults to 60; output GIF sampling defaults to 25. Dimensions are physical backing pixels, including device pixel ratio.

Stop and cancel are idempotent. Stop during initialization captures frame zero then finishes. Disposing the mount cancels active capture; completed recordings remain independently owned until recording.dispose(). A cancelled or failed capture releases its worker, pixels, render subscription, timers, and listeners.

Recording requires a readable Canvas 2D bitmap, WebAssembly, Worker, and CSP permission for worker-src blob:. Canvas origin taint, resize, render failure, allocation failure, or a full three-frame transfer queue rejects capture and releases it. The estimate uses backing width × height × 4 × expected frame count × memory headroom (default 1.15); storage can grow. It is raw RGBA memory, not the much smaller final GIF size. No workers or WASM memory initialize on import.

GIF export runs in the recording’s worker, keeps the original frames, and allows one export at a time. Its signal cancels conversion without discarding the recording; disposing the recording cancels pending exports and reads. Progress is reported between bounded batches. A shared sampled palette limits colors to 256, delays round cumulatively to 10ms, and output sampling selects the most recent captured frame. GIF is lossy. An opaque CSS background flattens alpha; without one, alpha below 128 is transparent and other pixels become opaque. Canvas CSS backgrounds and HtmlBox DOM content are not part of its bitmap.

Capture uses PibblFrameRecordingOptions and returns PibblFrameRecordingSession. Its result is PibblFrameRecording, with PibblRecordedFrameInfo metadata and PibblRecordedFrame pixel copies. Export dimensions, maximum colors, dithering, automatic palette sampling effort, and an optional exact palette are all controlled through the export options. They leave retained pixels intact, allowing repeated exports with different settings. Encoding accepts PibblGifExportOptions. Completion callbacks use PibblRenderCompletion.

Record committed bitmap frames from any live Pibbl root canvas.

Capture uses the existing render lifecycle; it never starts a render loop. Stop retains frames; cancel discards them. Dispose the completed recording after reading or exporting it. Resizing, failed renders, unreadable pixels, allocation failure, and capture queue overflow reject and release the session.

recordFrames: (canvas: Canvas, options: PibblFrameRecordingOptions) => PibblFrameRecordingSession

Related API: recordFrames, Canvas, PibblFrameRecordingOptions, PibblFrameRecordingSession.

  • canvas — The canvas belonging to a live Pibbl root mount.

  • options — Capture duration, maximum frame rate, allocation headroom, and cancellation.

Stable controls and promises for one recording session.

PibblFrameRecordingOptions

PibblFrameRecordingSession

View source — packages/core/src/lib/recording/recording.ts:253

Encode a retained recording to an image/gif Blob in its worker’s WASM encoder.

Pixels remain worker-owned. Cancellation stops encoding and preserves the recording; disposing the recording cancels all pending reads and exports. GIF timing is rounded cumulatively to 10ms units. Without an opaque background, alpha below 128 is transparent and other pixels are opaque. Only one export per recording may run at a time. Requires workers, WebAssembly, and blob-worker CSP permission.

exportGif: (recording: PibblFrameRecording, options?: PibblGifExportOptions) => Promise<Blob>

Related API: exportGif, PibblFrameRecording, PibblGifExportOptions.

  • recording — Retained frames returned by recordFrames.

  • options — Output dimensions, palette, dithering, sampling, looping, background, progress, and cancellation.

Encoded GIF bytes as a caller-owned Blob.

PibblGifExportOptions

PibblFrameRecording

View source — packages/core/src/lib/recording/recording.ts:538

Read the Authoring, signals, and lifecycle companion for ownership, adaptation, failure modes, and verification. Agent start provides the version-selection workflow.

Documentation built with @pibbl/core 0.0.2, revision a1dcf8e. ALPHA — NOT FOR PRODUCTION USE.