OffscreenCanvas: updateElementGeometry() 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 updateElementGeometry() method of the OffscreenCanvas interface updates the geometry applied to an ElementImage's underlying DOM presence.

Note: See the HTMLCanvasElement.updateElementGeometry() page for detailed descriptions of what all the method options do plus examples.

Syntax

js
updateElementGeometry(element)
updateElementGeometry(element, options)

Parameters

element

A reference to the ElementImage whose geometry you wish to update.

options Optional

An options object, the properties of which can include:

preserveHitTestOrder Optional

A boolean value that controls whether the element's underlying DOM presence is brought to the top of the hit test stack (false), or whether it will remain in the same position in the hit test stack (true). Defaults to false.

canvasClip Optional

A DOMRect that sets the area of the element that can receive pointer events. This is useful when you only render a portion of an element image on the canvas and want to limit hit testing to the rendered portion. Defaults to undefined. The rect is in the element's own coordinate space.

canvasTransform Optional

A DOMMatrix that updates the size, shape, and position of the element's underlying DOM presence so that it can be kept in sync with its canvas rendering position in terms of hit-testing area. Defaults to undefined. The matrix units are specified in CSS pixels.

Return value

None (undefined).

Exceptions

InvalidStateError DOMException

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

Examples

Updating the geometry of an element rendered via an offscreen canvas

Our Canvas 2D context offscreen canvas example (source code) shows how to render an element on an offscreen canvas using drawElementImage(). Below, we walk through the pertinent part that features updateElementGeometry(); see drawElementImage() for a full example walkthrough.

In our worker script's message event handler, if data contains an ElementImage of our <form>, we render it to the <canvas>. We start by getting references to the ElementImage and the OffscreenCanvas, then fill the entire canvas context with a solid color using the fillStyle property and fillRect() method.

We then draw the form to the canvas using drawElementImage(). Because we included preserveElementGeometry: true, we need to call updateElementGeometry() to update the element geometry so that it will be interactive as expected. Finally, we log the element's original size to the console, retrieved using getElementImageDefaultSize(), and call ElementImage.close() to free up the memory used to store the element image now we no longer need it:

js
  if (data.formImage) {
    const img = data.formImage;
    const offscreen = ctx.canvas;

    ctx.fillStyle = "#7D82B8";
    ctx.fillRect(0, 0, offscreen.width, offscreen.height);

    ctx.drawElementImage(img, x, y, {
      preserveElementGeometry: true,
    });

    offscreen.updateElementGeometry(img, {
      canvasTransform: new DOMMatrix().translate(x, y),
    });

    console.log(offscreen.getElementImageDefaultSize(img));

    img.close();
  }
});

Specifications

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

Browser compatibility

See also