Screen Capture API
{{DefaultAPISidebar("Screen Capture API")}}
The Screen Capture API introduces additions to the existing Media Capture and Streams API to let the user select a screen or portion of a screen (such as a window) to capture as a media stream. This stream can then be recorded or shared with others over the network.
Screen Capture API concepts and usage
The Screen Capture API is relatively simple to use. Its main method is {{domxref("MediaDevices.getDisplayMedia()")}}
, whose job is to ask the user to select a screen or portion of a screen to capture in the form of a {{domxref("MediaStream")}}
.
To start capturing video from the screen, you call getDisplayMedia()
on navigator.mediaDevices
:
captureStream =
await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
The {{jsxref("Promise")}}
returned by getDisplayMedia()
resolves to a {{domxref("MediaStream")}}
which streams the captured display surface.
See the article Using the Screen Capture API for a more in-depth look at how to use the API to capture screen contents as a stream.
Screen capture extensions
The Screen Capture API has additional features that extend its capabilities:
Limiting the screen area captured in the stream
- The Element Capture API restricts the captured region to a specified rendered DOM element and its descendants.
- The Region Capture API crops the captured region to the area of the screen in which a specified DOM element is rendered.
See Using the Element Capture and Region Capture APIs to learn more.
Controlling the captured screen area
The Captured Surface Control API allows the capturing application to provide limited control over the captured display surface, for example zooming and scrolling its contents.
See Using the Captured Surface Control API to learn more.
Interfaces
{{domxref("BrowserCaptureMediaStreamTrack")}}
- : Represents a single video track; extends the
{{domxref("MediaStreamTrack")}}
class with methods to limit the part of a self-capture stream (for example, a user’s screen or window) that is captured.
- : Represents a single video track; extends the
{{domxref("CaptureController")}}
- : Provides methods that can be used to further manipulate a captured display surface (captured via
{{domxref("MediaDevices.getDisplayMedia()")}}
). ACaptureController
object is associated with a captured display surface by passing it into agetDisplayMedia()
call as the value of the options object’scontroller
property.
- : Provides methods that can be used to further manipulate a captured display surface (captured via
{{domxref("CropTarget")}}
- : Provides a static method,
{{domxref("CropTarget.fromElement_static", "fromElement()")}}
, which returns a{{domxref("CropTarget")}}
instance that can be used to crop a captured video track to the area in which a specified element is rendered.
- : Provides a static method,
{{domxref("RestrictionTarget")}}
- : Provides a static method,
{{domxref("RestrictionTarget.fromElement_static", "fromElement()")}}
, which returns a{{domxref("RestrictionTarget")}}
instance that can be used to restrict a captured video track to a specified DOM element.
- : Provides a static method,
Additions to the MediaDevices interface
{{domxref("MediaDevices.getDisplayMedia()")}}
- : The
getDisplayMedia()
method is added to theMediaDevices
interface. Similar to{{domxref("MediaDevices.getUserMedia", "getUserMedia()")}}
, this method creates a promise that resolves with a{{domxref("MediaStream")}}
containing the display area selected by the user, in a format that matches the specified options.
- : The
Additions to existing dictionaries
The Screen Capture API adds properties to the following dictionaries defined by other specifications.
MediaTrackConstraints
{{domxref("MediaTrackConstraints.displaySurface")}}
- : A
ConstrainDOMString
indicating what type of display surface is to be captured. The value is one ofbrowser
,monitor
, orwindow
.
- : A
{{domxref("MediaTrackConstraints.logicalSurface")}}
- : Indicates whether or not the video in the stream represents a logical display surface (that is, one which may not be entirely visible onscreen, or may be completely offscreen). A value of
true
indicates a logical display surface is to be captured.
- : Indicates whether or not the video in the stream represents a logical display surface (that is, one which may not be entirely visible onscreen, or may be completely offscreen). A value of
{{domxref("MediaTrackConstraints.suppressLocalAudioPlayback")}}
- : Controls whether the audio playing in a tab will continue to be played out of a user’s local speakers when the tab is captured, or whether it will be suppressed. A value of
true
indicates that it will be suppressed.
- : Controls whether the audio playing in a tab will continue to be played out of a user’s local speakers when the tab is captured, or whether it will be suppressed. A value of
MediaTrackSettings
{{domxref("MediaTrackSettings.cursor")}}
- : A string which indicates whether or not the display surface currently being captured includes the mouse cursor, and if so, whether it’s only visible while the mouse is in motion or if it’s always visible. The value is one of
always
,motion
, ornever
.
- : A string which indicates whether or not the display surface currently being captured includes the mouse cursor, and if so, whether it’s only visible while the mouse is in motion or if it’s always visible. The value is one of
{{domxref("MediaTrackSettings.displaySurface")}}
- : A string indicating what type of display surface is currently being captured. The value is one of
browser
,monitor
, orwindow
.
- : A string indicating what type of display surface is currently being captured. The value is one of
{{domxref("MediaTrackSettings.logicalSurface")}}
- : A boolean value, which is
true
if the video being captured doesn’t directly correspond to a single onscreen display area.
- : A boolean value, which is
{{domxref("MediaTrackSettings.suppressLocalAudioPlayback")}}
- : A boolean value, which is
true
if the audio being captured is not played out of the user’s local speakers.
- : A boolean value, which is
{{domxref("MediaTrackSettings.screenPixelRatio")}}
- : A number representing the ratio of the physical size of a pixel on the captured display surface (displayed at its physical resolution) to the logical size of a CSS pixel on the capturing screen (displayed at its logical resolution). It cannot be used as a constraint or capability.
MediaTrackSupportedConstraints
{{domxref("MediaTrackSupportedConstraints.displaySurface")}}
- : A boolean, which is
true
if the current environment supports the{{domxref("MediaTrackConstraints.displaySurface")}}
constraint.
- : A boolean, which is
{{domxref("MediaTrackSupportedConstraints.logicalSurface")}}
- : A boolean, which is
true
if the current environment supports the constraint{{domxref("MediaTrackConstraints.logicalSurface")}}
.
- : A boolean, which is
{{domxref("MediaTrackSupportedConstraints.suppressLocalAudioPlayback")}}
- : A boolean, which is
true
if the current environment supports the constraint{{domxref("MediaTrackConstraints.suppressLocalAudioPlayback")}}
.
- : A boolean, which is
Security considerations
Websites that support Permissions Policy (either using the HTTP {{HTTPHeader("Permissions-Policy")}}
header or the {{HTMLElement("iframe")}}
attribute allow
) can specify a desire to use the Screen Capture API using the directive {{HTTPHeader("Permissions-Policy/display-capture", "display-capture")}}
:
<iframe allow="display-capture" src="/some-other-document.html">…</iframe>
A site can also specify a desire to use the Captured Surface Control API via the {{HTTPHeader("Permissions-Policy/captured-surface-control", "captured-surface-control")}}
directive. Specifically, the {{domxref("CaptureController.forwardWheel", "forwardWheel()")}}
, {{domxref("CaptureController.increaseZoomLevel", "increaseZoomLevel()")}}
, {{domxref("CaptureController.decreaseZoomLevel", "decreaseZoomLevel()")}}
, and {{domxref("CaptureController.resetZoomLevel", "resetZoomLevel()")}}
methods are controlled by this directive.
The default allowlist for both directives is self
, which permits any content within the same origin use Screen Capture.
These methods are considered powerful features, which means that even if permission is allowed via a Permissions-Policy
, the user will still be prompted for permission to use them. The Permissions API can be used to query the aggregate permission (from both the website and the user) for using the listed features.
In addition, the specification requires that a user has recently interacted with the page to use these features — this means that transient activation is required. See the individual method pages for more details.
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- Using the Screen Capture API
- Using the Element Capture and Region Capture APIs
- Using the Captured Surface Control API
{{domxref("MediaDevices.getDisplayMedia()")}}