大网数据广告活动
Web 标准与前端开发

CaptureController: getSupportedZoomLevels() method

· 52CE 我爱测

The CaptureController interface's **`getSupportedZoomLevels()`** method returns the different zoom levels that the captured display surface supports.

Screen Capture API

The CaptureController interface's getSupportedZoomLevels() method returns the different zoom levels that the captured display surface supports.

Syntax

getSupportedZoomLevels()

Parameters

None.

Return value

An array of numbers representing the different zoom levels that the captured display surface supports.

Exceptions

  • InvalidStateError DOMException
    • : The capturing MediaStream returned by the originating MediaDevices.getDisplayMedia() call is no longer capturing, for example because the associated MediaStreamTrack objects have had MediaStreamTrack.stop called on them.
  • NotSupportedError DOMException
    • : The surface type being captured is not a browser tab.

Examples

Basic getSupportedZoomLevels() usage

In our live demo, shown in Using the Captured Surface Control API, we grab the supported zoom levels of the captured display surface by running getSupportedZoomLevels(), storing the resulting array in a variable called zoomLevels:

const zoomLevels = controller.getSupportedZoomLevels();

This is later used in a function called updateZoomButtonState(). The problem this solves is that, if you try to zoom out below the minimum supported zoom level, or zoom in above the maximum supported zoom level, CaptureController.decreaseZoomLevel/CaptureController.increaseZoomLevel will throw an InvalidStateError DOMException.

[!NOTE] It is generally a best practice to call decreaseZoomLevel() and increaseZoomLevel() from within a try...catch block because the zoom level could be changed asynchronously by an entity other than the application, which might lead to an error being thrown. For example, the user might directly interact with the captured surface to zoom in or out.

The updateZoomButtonState() function avoids this issue by first making sure both the "Zoom out" and "Zoom in" buttons are enabled. It then does two checks:

  • If the current zoom level is equal to the minimum supported zoom level (stored in the first value of the zoomLevels array), we disable the "Zoom out" button so the user can't zoom out any further.
  • If the current zoom level is equal to the maximum supported zoom level (stored in the last value of the zoomLevels array), we disable the "Zoom in" button so the user can't zoom in any further.
function updateZoomButtonState() {
  decBtn.disabled = false;
  incBtn.disabled = false;
  if (controller.zoomLevel === zoomLevels[0]) {
    decBtn.disabled = true;
  } else if (controller.zoomLevel === zoomLevels[zoomLevels.length - 1]) {
    incBtn.disabled = true;
  }
}

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。