files/en-us/web/api/htmlcanvaselement/capturestream/index.md
{{APIRef("Media Capture and Streams")}}
The captureStream() method of the {{domxref("HTMLCanvasElement")}} interface returns a {{domxref("MediaStream")}}
which includes a {{domxref("CanvasCaptureMediaStreamTrack")}} containing a real-time video capture of the canvas's contents.
captureStream()
captureStream(frameRate)
frameRate {{optional_inline}}
0, frames will not be captured automatically; instead, they will only
be captured when the returned track's
{{domxref("CanvasCaptureMediaStreamTrack.requestFrame", "requestFrame()")}} method is
called.A reference to a {{domxref("MediaStream")}} object, which has a single {{domxref("CanvasCaptureMediaStreamTrack")}} in it.
NotSupportedError {{domxref("DOMException")}}
frameRate is negative.SecurityError {{domxref("DOMException")}}
// Find the canvas element to capture
const canvasElt = document.querySelector("canvas");
// Get the stream
const 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
stream.getTracks().forEach((track) => pc.addTrack(track, stream));
{{Specifications}}
{{Compat}}