# recordFrames

```ts
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](/reference/types/frame-recording/#pibblframerecordingoptions)
and returns [PibblFrameRecordingSession](/reference/types/frame-recording/#pibblframerecordingsession).
Its result is [PibblFrameRecording](/reference/types/frame-recording/#pibblframerecording),
with [PibblRecordedFrameInfo](/reference/types/frame-recording/#pibblrecordedframeinfo) metadata
and [PibblRecordedFrame](/reference/types/frame-recording/#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](/reference/types/frame-recording/#pibblgifexportoptions).
Completion callbacks use [PibblRenderCompletion](/reference/types/frame-recording/#pibblrendercompletion).

## API details from source

<span id="api-recordFrames"></span>

### recordFrames

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.

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

Related API: [recordFrames](/reference/functions/record-frames/), [Canvas](/reference/types/canvas-runtime/#canvas), [PibblFrameRecordingOptions](/reference/types/frame-recording/#pibblframerecordingoptions), [PibblFrameRecordingSession](/reference/types/frame-recording/#pibblframerecordingsession).

#### Parameters

- **`canvas`** — The canvas belonging to a live Pibbl root mount.

- **`options`** — Capture duration, maximum frame rate, allocation headroom, and cancellation.

#### Returns

Stable controls and promises for one recording session.

#### See also

[PibblFrameRecordingOptions](/reference/types/frame-recording/#pibblframerecordingoptions)

[PibblFrameRecordingSession](/reference/types/frame-recording/#pibblframerecordingsession)

[View source — packages/core/src/lib/recording/recording.ts:253](/source/packages/core/src/lib/recording/recording-ts/#L253)

<span id="api-exportGif"></span>

### exportGif

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.

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

Related API: [exportGif](/reference/functions/record-frames/), [PibblFrameRecording](/reference/types/frame-recording/#pibblframerecording), [PibblGifExportOptions](/reference/types/frame-recording/#pibblgifexportoptions).

#### Parameters

- **`recording`** — Retained frames returned by recordFrames.

- **`options`** — Output dimensions, palette, dithering, sampling, looping, background, progress, and cancellation.

#### Returns

Encoded GIF bytes as a caller-owned Blob.

#### See also

[PibblGifExportOptions](/reference/types/frame-recording/#pibblgifexportoptions)

[PibblFrameRecording](/reference/types/frame-recording/#pibblframerecording)

[View source — packages/core/src/lib/recording/recording.ts:538](/source/packages/core/src/lib/recording/recording-ts/#L538)

## Implementation guidance for agents

Read the [Authoring, signals, and lifecycle companion](/agents/topics/lifecycle/) for ownership, adaptation, failure modes, and verification. [Agent start](/agents/) provides the version-selection workflow.

## Complete minimal examples

- [Record committed frames and export a GIF](/minimal-examples/recording/record-frames/): Capture a live Canvas color change, read frame metadata and pixels, export a GIF and release owned resources. [Plain source](/minimal/recording/record-frames.ts)
## Documentation version

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