HTMLCanvasElement.captureStream()

The HTMLCanvasElement captureStream() method returns a MediaStream which includes a CanvasCaptureMediaStreamTrack containing a real-time video capture of the canvas's contents.

Syntax

MediaStream = canvas.captureStream(frameRate);

Parameters

frameRate Optional
A double-precision floating-point value that indicates the rate of capture of each frame. If not set, a new frame will be captured each time the canvas changes; if set to 0, frames will not be captured automatically; instead, they will only be captured when the returned track's requestFrame() method is called.

Return value

A reference to a MediaStream object, which has a single CanvasCaptureMediaStreamTrack in it.

Exceptions

NotSupportedError
The value of frameRate is negative.

Example

// Find the canvas element to capture
var canvasElt = document.querySelector('canvas');

// Get the stream
var stream = canvasElt.captureStream(25); // 25 FPS

// Do things to the stream
// E.g. Send it to another computer using an RTCPeerConnection
//      pc is an RTCPeerConnection created elsewhere
pc.addStream(stream);

Specifications

Specification Status Comment
Media Capture from DOM Elements
The definition of 'HTMLCanvasElement.captureStream()' in that specification.
Working Draft Initial definition

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
captureStream
Experimental
Chrome Full support 51Edge Full support ≤79Firefox Full support 43IE No support NoOpera Full support 36Safari Full support 11WebView Android Full support 51Chrome Android Full support 51Firefox Android Full support 43Opera Android Full support 36Safari iOS Full support 11Samsung Internet Android Full support 5.0

Legend

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.

See also