ElementImage
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.
The ElementImage interface represents a snapshot of a drawable element's rendered state, captured using HTMLCanvasElement.captureElementImage(), which can be rerendered later on, including via an OffscreenCanvas.
An ElementImage can be rendered:
- On a 2D canvas context using
CanvasRenderingContext2D.drawElementImage(). - In a WebGL texture using
WebGLRenderingContext.texElementSubImage2D(). - In a WebGPU texture using
GPUQueue.drawElementImageToTexture().
An ElementImage can also be transferred to a web worker and drawn to an OffscreenCanvas using OffscreenCanvasRenderingContext2D.drawElementImage().
Instance methods
close()-
Removes an
ElementImage, freeing up the memory used to store it.
Examples
>Reporting the element initial size using an ElementImage
This example shows how to record the initial size of an canvas-rendered element from an ElementImage.
HTML
We include a <canvas> element with its content attribute set to drawable, containing a <div> element with the drawable attribute set. We also include a <p> element to output the initial size into.
<canvas width="640" height="200" content="drawable">
<div drawable>Happy happy, joy joy</div>
</canvas>
<p></p>
CSS
We set some rudimentary styles on the drawable <div> element.
div {
font-size: 1.4rem;
border: 4px solid #576490;
background: #d1d2f9;
border-radius: 5px;
padding: 10px;
}
JavaScript
In our script, we first grab references to the <canvas>, <div>, and <p> elements.
const canvas = document.querySelector("canvas");
const divElem = document.querySelector("div");
const output = document.querySelector("p");
Next, we define a main() function before running it to start the example. Inside, we first get a 2D canvas context using the getContext() method. Next, we check if the drawElementImage() method exists on the context; if not, we add a no-html-in-canvas class to the <body> element that is used to display a "no support" banner.
Next, inside a paint event handler, we fill the entire canvas with a solid color using the fillStyle property and fillRect() method. We then use drawElementImage() to draw the <div> element onto the canvas. Finally, we capture the initial rendered view of the <div> as an ElementImage using HTMLCanvasElement.captureElementImage(), return an object containing its size using HTMLCanvasElement.getElementImageDefaultSize(), write the size to the <p> element, then close() the ElementImage after we have finished with it.
function main() {
const ctx = canvas.getContext("2d");
if (!ctx.drawElementImage) {
document.body.classList.add("no-html-in-canvas");
return;
}
canvas.addEventListener("paint", () => {
ctx.fillStyle = "goldenrod";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawElementImage(divElem, 100, 50);
const initialView = canvas.captureElementImage(divElem);
const initialViewSize = canvas.getElementImageDefaultSize(initialView);
output.textContent = `Elment initial size (canvas units): ${Math.ceil(initialViewSize.width)}, ${Math.ceil(initialViewSize.height)}`;
initialView.close();
});
}
main();
Result
The element's returned size is given in canvas units. It can be useful to query the element's initial size so that you can get the element's size in canvas dimensions rather than CSS pixels. This can be used to size texture storage up front, or check whether an element's rendered size has changed.