GPU:getPreferredCanvasFormat() 方法

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

GPU 接口的 getPreferredCanvasFormat() 方法返回用于当前系统上显示 8 位色深、标准动态范围(SDR)内容的最佳 canvas 纹理格式。

这通常用于为当前系统提供 GPUCanvasContext.configure() 调用的最佳 format 值。这是推荐的——如果你没有在配置 canvas 上下文时使用最佳的格式,可能会产生开销,例如根据平台而异的额外纹理的复制。

语法

js
getPreferredCanvasFormat()

参数

无。

返回值

一个字符串,表示 canvas 的纹理格式。该值可以是 rgba8unormbgra8unorm

异常

无。

示例

js
const canvas = document.querySelector("#gpuCanvas");
const context = canvas.getContext("webgpu");

context.configure({
  device: device,
  format: navigator.gpu.getPreferredCanvasFormat(),
  alphaMode: "premultiplied",
});

规范

Specification
WebGPU
# dom-gpu-getpreferredcanvasformat

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
getPreferredCanvasFormat
Experimental

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
In development. Supported in a pre-release version.
In development. Supported in a pre-release version.
No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.

参见