GPUQueue: drawElementImageToTexture() method

Limited availability

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

Want more browser support for this feature? Tell us why.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

The drawElementImageToTexture() method of the GPUQueue interface draws a drawable element's state recorded during the current rendering update (or an ElementImage) into a texture. Part of the HTML-in-Canvas API.

Syntax

js
drawElementImageToTexture(source, destination)

Parameters

source

An object describing the texture source. Properties can include:

element

A drawable element reference or an ElementImage object representing the DOM element to use in the texture.

sx Optional

A float representing the horizontal coordinate of the top-left corner of the source element area to use in the texture. If omitted, defaults to 0.

sy Optional

A float representing the vertical coordinate of the top-left corner of the source element area to use in the texture. If omitted, defaults to 0.

swidth Optional

A float representing the width of the source element area to use in the texture. If omitted, defaults to the element's width.

sheight Optional

A float representing the height of the source element area to use in the texture. If omitted, defaults to the element's height.

destination

An object describing the texture destination. Properties can include:

width Optional

An integer representing the width of the source when included in the texture, in texels.

height Optional

An integer representing the height of the source when included in the texture, in texels.

Return value

None (undefined).

Examples

Typical usage

Our WebGPU example (source code) renders an HTML form as a texture on all six sides of a rotating 3D cube. The following snippet shows the code used to create and bind a texture, and upload the latest snapshot of the source form element into the texture each time it is repainted.

js
let elementTexture = null;
let bindGroup = null;
let texWidth = 0;
let texHeight = 0;

function updateElementTexture() {
  // Size in canvas-grid pixels.
  const scale = canvas.width / canvas.clientWidth;
  const w = Math.ceil(source.offsetWidth * scale);
  const h = Math.ceil(source.offsetHeight * scale);
  if (!(w > 0 && h > 0)) return false;

  if (!elementTexture || w !== texWidth || h !== texHeight) {
    elementTexture?.destroy();
    elementTexture = device.createTexture({
      size: [w, h],
      format: "rgba8unorm",
      usage:
        GPUTextureUsage.TEXTURE_BINDING |
        GPUTextureUsage.COPY_DST |
        GPUTextureUsage.RENDER_ATTACHMENT,
    });
    bindGroup = device.createBindGroup({
      layout: pipeline.getBindGroupLayout(0),
      entries: [
        { binding: 0, resource: { buffer: uniformBuffer } },
        { binding: 1, resource: sampler },
        { binding: 2, resource: elementTexture.createView() },
      ],
    });
    texWidth = w;
    texHeight = h;
  }

  device.queue.drawElementImageToTexture(
    { source },
    { texture: elementTexture, size: { width: w, height: h } },
  );
  return true;
}

Specifications

This feature does not appear to be defined in any specification.

Browser compatibility

See also