HTMLCanvasElement: captureStream() method
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
ThecaptureStream()
method of theHTMLCanvasElement
interface returns aMediaStream
which includes aCanvasCaptureMediaStreamTrack
containing a real-time video capture of the canvas's contents.
In this article
Syntax
captureStream()captureStream(frameRate)
Parameters
frameRate
OptionalA double-precision floating-point value that indicates the rate of capture of eachframe. If not set, a new frame will be captured each time the canvas changes; if setto
0
, frames will not be captured automatically; instead, they will onlybe captured when the returned track'srequestFrame()
method iscalled.
Return value
A reference to aMediaStream
object, which has a singleCanvasCaptureMediaStreamTrack
in it.
Exceptions
NotSupportedError
DOMException
Thrown if the value of
frameRate
is negative.SecurityError
DOMException
The canvas's bitmap is not origin clean;at least some of its contents have or may have been loaded from a site other than the one from which the document itself was loaded.
Example
// Find the canvas element to captureconst canvasElt = document.querySelector("canvas");// Get the streamconst 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 elsewherestream.getTracks().forEach((track) => pc.addTrack(track, stream));
Specifications
Specification |
---|
Media Capture from DOM Elements> # dom-htmlcanvaselement-capturestream> |
Browser compatibility
Loading…
See also
HTMLMediaElement.captureStream()
, which allows capturing a streamfrom a media element.MediaStream
- Media Capture and Streams API