GPUCanvasContext: getCurrentTexture() 方法

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

注意:此功能在 Web Workers 中可用。

GPUCanvasContext 接口的 getCurrentTexture() 方法返回下一个将由 canvas 上下文合成到文档中的 GPUTexture

语法

js
getCurrentTexture()

参数

无。

返回值

一个 GPUTexture 对象实例。

异常

InvalidStateError DOMException

如果在配置 canvas 上下文之前(即在调用 GPUCanvasContext.configure() 之前)调用 getCurrentTexture(),则会抛出此错误。

示例

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

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

// …
// Later on
const commandEncoder = device.createCommandEncoder();

const renderPassDescriptor = {
  colorAttachments: [
    {
      clearValue: [0, 0, 0, 1], // Opaque black
      loadOp: "clear",
      storeOp: "store",
      view: context.getCurrentTexture().createView(),
    },
  ],
};

const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);

规范

规范
WebGPU
# dom-gpucanvascontext-getcurrenttexture

浏览器兼容性

另见