# Record committed frames and export a GIF
Capture a live Canvas color change, read frame metadata and pixels, export a GIF and release owned resources.

## APIs demonstrated

- [recordFrames](/reference/functions/record-frames/) from `@pibbl/core`
- [exportGif](/reference/functions/record-frames/) from `@pibbl/core`
- [PibblFrameRecordingOptions](/reference/types/frame-recording/) from `@pibbl/core`
- [PibblFrameRecordingSession](/reference/types/frame-recording/) from `@pibbl/core`
- [PibblFrameRecording](/reference/types/frame-recording/) from `@pibbl/core`
- [PibblRecordedFrameInfo](/reference/types/frame-recording/) from `@pibbl/core`
- [PibblRecordedFrame](/reference/types/frame-recording/) from `@pibbl/core`
- [PibblGifExportOptions](/reference/types/frame-recording/) from `@pibbl/core`
- [PibblRenderCompletion](/reference/types/frame-recording/) from `@pibbl/core`

## Complete example

[Plain source](/minimal/recording/record-frames.ts). Run as a browser module after the document body exists. TSX uses `jsx: "react-jsx"` and `jsxImportSource: "@pibbl/core"`.

```ts
import {
  createElement,

  Rectangle,
  signal,
  recordFrames,
  exportGif,
  type PibblFrameRecordingOptions,
  type PibblFrameRecordingSession,
  type PibblFrameRecording,
  type PibblRecordedFrameInfo,
  type PibblRecordedFrame,
  type PibblGifExportOptions,
  type PibblRenderCompletion,
} from "@pibbl/core";
import { pibblSync } from '@pibbl/core/sync';

const canvas = document.createElement("canvas");
canvas.style.cssText = "width:240px;height:160px";
const button = document.createElement("button");
button.textContent = "Record a color change";
const status = document.createElement("p");
const preview = document.createElement("img");
preview.alt = "GIF of the captured color change";
document.body.append(canvas, button, status, preview);
const fill = signal("#159447");
const root = pibblSync(
  canvas,
  createElement(Rectangle, { style: { width: 240, height: 160, fill } }),
  { viewport: { width: 240, height: 160 }, pixelRatio: 1 },
);
let latestCompletion: PibblRenderCompletion | undefined;
const options: PibblFrameRecordingOptions = {
  duration: 1000,
  frameRate: 30,
  onRenderComplete: (completion) => {
    latestCompletion = completion;
  },
  onFrameCaptured: (frame) => {
    status.textContent = `Copied render ${frame.renderGeneration}`;
  },
};
const gifOptions: PibblGifExportOptions = {
  width: 240,
  frameRate: 25,
  background: "white",
  repeatCount: 0,
};
let session: PibblFrameRecordingSession | undefined;
let retained: PibblFrameRecording | undefined;
let url: string | undefined;
let changeTimer: ReturnType<typeof setTimeout> | undefined;
let disposed = false;
button.onclick = async () => {
  button.disabled = true;
  if (url) URL.revokeObjectURL(url);
  fill.set("#159447");
  try {
    session = recordFrames(canvas, options);
    await session.ready;
    status.textContent = "Recording successful renders…";
    changeTimer = setTimeout(() => fill.set("#FF735E"), 300);
    retained = await session.finished;
    const metadata: readonly PibblRecordedFrameInfo[] = retained.frames;
    const first: PibblRecordedFrame = await retained.readFrame(0);
    const gif = await exportGif(retained, gifOptions);
    if (disposed) return;
    url = URL.createObjectURL(gif);
    preview.src = url;
    status.textContent = `${metadata.length} captured frames; ${first.width}×${first.height}; ${retained.duration} ms; render ${latestCompletion?.generation}`;
  } catch (error) {
    if (!disposed) status.textContent = String(error);
  } finally {
    if (changeTimer) clearTimeout(changeTimer);
    retained?.dispose();
    retained = undefined;
    session = undefined;
    button.disabled = false;
  }
};
window.addEventListener(
  "pagehide",
  () => {
    disposed = true;
    if (changeTimer) clearTimeout(changeTimer);
    session?.cancel();
    retained?.dispose();
    if (url) URL.revokeObjectURL(url);
    root.dispose();
  },
  { once: true },
);

```

## Usage notes

- Recording observes committed renders without scheduling application animation.
- The application owns and revokes its GIF Blob URL.

## Documentation version

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