Skip to content

CameraSource

Manages webcam capture via navigator.mediaDevices.getUserMedia and renders frames into an unbuffered 2D canvas context with willReadFrequently: true.

ts
import { CameraSource } from 'vigilo-wasm';

Static Methods

CameraSource.open(options?: CameraOptions): Promise<CameraSource>

Requests webcam access from the browser, plays the stream into an internal or provided <video> element, and waits until the first frame is ready.

ts
const camera = await CameraSource.open({
  width: 1280,
  height: 720,
  frameRate: 30,
  facingMode: 'user',
});

CameraOptions

FieldTypeDefaultDescription
widthnumber1280Ideal capture width.
heightnumber720Ideal capture height.
frameRatenumber30Ideal frames per second.
facingMode'user' | 'environment''user'Camera facing mode ('user' = front-facing selfie).
deviceIdstringundefinedSpecific device ID from navigator.mediaDevices.enumerateDevices().
videoHTMLVideoElementundefinedExisting <video> element to render into. If omitted, a detached video element is created.

Instance Methods

grab(): ImageData

Samples the latest frame from the video stream without buffering or queuing. Returns a standard browser ImageData object containing raw RGBA 8-bit pixels.

ts
const image = camera.grab();
console.log(image.width, image.height, image.data.byteLength);

close(): void

Stops all tracks on the underlying MediaStream, pauses the video element, and releases the webcam hardware.


Properties

video: HTMLVideoElement

The HTML <video> element playing the live webcam stream.

stream: MediaStream

The active MediaStream returned by getUserMedia.

Released under the AGPL-3.0 License.