HTMLCanvasElement: captureElementImage() 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.

The captureElementImage() method of the HTMLCanvasElement interface captures a snapshot of a drawable element's current rendered state, which can be rendered later on using the HTML-in-Canvas API.

Syntax

js
captureElementImage(element)

Parameters

element

A reference to the drawable element you wish to capture.

Return value

An ElementImage object.

Exceptions

InvalidStateError DOMException

Thrown if the element has not yet been painted, meaning there is currently no rendered state to capture.

Examples

Rendering the initial state of a drawable element

This example shows how to capture the initial rendered state of a canvas-rendered HTML element and then restore that state when wished.

HTML

We include a <canvas> element with its content attribute set to drawable. We also include a <div> element with the drawable attribute set, so it can be rendered onto the <canvas>. We also include two <button> elements, one to update the <div>'s background color, and one to restore its initial rendered state.

html
<p>
  <button id="update">Update box background</button>
  <button id="restore">Restore initial render</button>
</p>
<canvas width="640" height="320" content="drawable">
  <div drawable>Initial render status</div>
</canvas>

CSS

We include some rudimentary styles for the <div> element:

css
div {
  font-size: 1.4rem;
  font-family: Arial, Helvetica, sans-serif;
  border: 4px solid #576490;
  background: #d1d2f9;
  border-radius: 5px;
  padding: 10px;
}

JavaScript

First, we grab references to the <canvas>, <div>, and two <button> elements.

js
const canvas = document.querySelector("canvas");
const divElem = document.querySelector("div");
const updateBtn = document.getElementById("update");
const restoreBtn = document.getElementById("restore");

Next, we set a click event handler on the update <button> so that when clicked, its background-color is updated and its textContent is changed to "Background updated".

js
updateBtn.addEventListener("click", () => {
  divElem.style.backgroundColor = randomColor();
  divElem.textContent = "Background updated";
});

We define a main() function to handle the main rendering for 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, we declare an initialView variable and define a paint event handler. Inside the 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> onto the canvas. If initialView is undefined (only during the first paint), we set it to equal a ElementImage snapshot of the <div> using captureElementImage(), then set an event handler on the restore <button> so that this view is restored when it is clicked. This involves passing initialView to drawElementImage() instead of a reference to the <div>.

js
function main() {
  const ctx = canvas.getContext("2d");

  if (!ctx.drawElementImage) {
    document.body.classList.add("no-html-in-canvas");
    return;
  }

  let initialView;

  canvas.addEventListener("paint", () => {
    ctx.fillStyle = "goldenrod";
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    ctx.drawElementImage(divElem, 100, 100);
    if (!initialView) {
      initialView = canvas.captureElementImage(divElem);
      restoreBtn.addEventListener("click", () => {
        ctx.fillStyle = "goldenrod";
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.drawElementImage(initialView, 100, 100);
      });
    }
  });
}

Finally, we run the main() function to start eveything off.

js
main();

Result

Click the update button to change the <div>'s background color and enable the restore button. This will cause the paint event to fire, meaning that the <div>'s updated color will be rendered and the initial render snapshot will be captured. Pressing the restore button will restore the initial rendered state captured in the snapshot.

Specifications

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

Browser compatibility

See also