API REFERENCE · 2.2.0

wgpu-kit/media

Canvas recording, format negotiation, and Blob downloads.

Auxiliary inputs, dispatch counts, managed batches and typed kernels are released in 2.1.0. The original API retains the published npm 2.0.0 declarations and implementation as its historical audit baseline; Additions in 2.1.0 and 2.2.0 are identified on affected APIs and cite src files, without claiming a fresh audit of the entire API. Only actual public exports of this entry point are listed.

Source reference: 1 ↗

pickMimefunction

Select the first supported recording MIME type, without starting capture.

pickMime(): string | null;

Parameters

This call takes no arguments.

Returns

string | null Supported MIME string; null if MediaRecorder is absent or none of the candidates is supported.

Behavior & constraints

  • Actual 2.0.0 order: video/webm;codecs=vp9 → video/webm;codecs=vp8 → video/webm → video/mp4;codecs=avc1.42E01E → video/mp4. The declaration comment suggesting MP4 first is stale.
  • isTypeSupported is capability negotiation, not a guarantee that a recording will produce bytes.

Examples

Check recording support.
import { pickMime } from 'wgpu-kit/media';
console.log(pickMime() ?? 'Recording unavailable');

CanvasRecorderclass

Records a canvas video stream through the browser MediaRecorder API.

class CanvasRecorder

constructorconstructor

Negotiate and retain the preferred MIME type.

new CanvasRecorder();

Parameters

This call takes no arguments.

Returns

CanvasRecorder An idle recorder; capture has not started.

Errors & failure cases

  • Throws Error('MediaRecorder is not supported in this environment') when pickMime() returns null.

mimeTypegetter

Chosen MIME type, fixed at construction.

get mimeType(): string;

Returns

string Selected codec/container string.

recordinggetter

Whether the underlying recorder is currently in state recording.

get recording(): boolean;

Returns

boolean False before start and after stop; true while recording.

startmethod

Begin capturing a canvas at a requested 60 frames per second.

start(canvas: HTMLCanvasElement, videoBitsPerSecond?: number): void;

Parameters

NameTypeRequiredDefaultMeaning
canvasHTMLCanvasElementYes—Canvas to record; its changing pixels become the video. No audio track is added.
videoBitsPerSecondnumberNo12_000_000Requested video bitrate in bits/second; the browser may choose the actual bitrate.

Returns

void No return value.

Behavior & constraints

  • Uses canvas.captureStream(60), MediaRecorder.start(250), and collects nonempty data chunks. It does not advance or redraw the canvas.
  • Wait for the previous stop() promise before starting again. The recorder reference remains set until the stop event.

Errors & failure cases

  • Throws Error('Recording already in progress') if an internal recorder exists. Browser captureStream/MediaRecorder errors also propagate, including an origin-tainted canvas or unsupported encoder.

stopmethod

Stop recording and join collected video chunks into a Blob.

stop(): Promise<RecordingResult>;

Parameters

This call takes no arguments.

Returns

Promise<RecordingResult> Resolves after the MediaRecorder stop event with the recording data and elapsed wall-clock duration.

Behavior & constraints

  • No MediaRecorder error listener, cancellation, or explicit stream-track cleanup is exposed. Call stop once per started session and handle rejection.

Errors & failure cases

  • Rejects Error('没有进行中的录制') if no recorder is recording, and rejects if the final Blob has zero bytes. Native stop errors may reject too.

Examples

Start capture and save it from a later stop action.
import { CanvasRecorder, downloadBlob } from 'wgpu-kit/media';
const recorder = new CanvasRecorder();
const canvas = document.querySelector<HTMLCanvasElement>('#canvas')!;
// Invoke start from a user action; keep rendering your canvas.
recorder.start(canvas, 8_000_000);
// Invoke this from a later Stop action.
async function finishRecording() {
  const result = await recorder.stop();
  const extension = result.mimeType.startsWith('video/mp4') ? 'mp4' : 'webm';
  downloadBlob(result.blob, `capture.${extension}`);
}

RecordingResultinterface

Metadata and bytes returned by CanvasRecorder.stop().

interface RecordingResult {
  blob: Blob;
  mimeType: string;
  seconds: number;
  bytes: number;
}

Parameters

NameTypeRequiredDefaultMeaning
blobBlobYes—Recorded video chunks combined into one Blob.
mimeTypestringYes—MIME selected by the recorder.
secondsnumberYes—Elapsed performance.now() time from start to stop event, in seconds; not measured media timeline duration.
bytesnumberYes—Blob.size in bytes.

downloadBlobfunction

Ask the browser to download a Blob using an object URL.

downloadBlob(blob: Blob, filename: string): void;

Parameters

NameTypeRequiredDefaultMeaning
blobBlobYes—Data to download.
filenamestringYes—Suggested download filename; browser policies may alter it.

Returns

void No return value.

Behavior & constraints

  • Requires a browser document. Creates and clicks an anchor, then revokes the object URL after 5000 ms. This is a download request, not a completion receipt.

Examples

Request a browser download.
import { downloadBlob } from 'wgpu-kit/media';
downloadBlob(new Blob(['hello'], { type: 'text/plain' }), 'hello.txt');

Search concepts, APIs, and examples