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

瀏覽器相容性

另見