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
drawElementImageToTexture(source, destination)
Parameters
source-
An object describing the texture source. Properties can include:
element-
A
drawableelement reference or anElementImageobject representing the DOM element to use in the texture. sxOptional-
A float representing the horizontal coordinate of the top-left corner of the source
elementarea to use in the texture. If omitted, defaults to0. syOptional-
A float representing the vertical coordinate of the top-left corner of the source
elementarea to use in the texture. If omitted, defaults to0. swidthOptional-
A float representing the width of the source
elementarea to use in the texture. If omitted, defaults to the element's width. sheightOptional-
A float representing the height of the source
elementarea to use in the texture. If omitted, defaults to the element's height.
destination-
An object describing the texture destination. Properties can include:
widthOptional-
An integer representing the width of the source when included in the texture, in texels.
heightOptional-
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.
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;
}