HTMLCanvasElement: clearElementGeometry() 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 clearElementGeometry() method of the HTMLCanvasElement interface clears any previous geometry applied to a drawable element (or ElementImage)'s underlying DOM presence.
Syntax
clearElementGeometry(element)
Parameters
element-
A reference to a
drawableelement or anElementImage.
Return value
None (undefined).
Description
The clearElementGeometry() method clears geometry applied to a drawable element or ElementImage. This includes both geometry applied automatically by drawElementImage() calls, and geometry applied manually by updateElementGeometry() calls.
Calling clearElementGeometry() causes the referenced element to be un-drawn, removing its interactivity and marking it as offscreen in the accessibility tree, without actually removing it from the DOM. You could re-draw it again with a subsequent updateElementGeometry() call.
It doesn't un-render the element from a <canvas> context; to do this, you'd have to redraw the canvas scene without rendering the element.
Examples
>Basic usage with 2D canvas rendering
In the following snippet, we add a paint event listener to a <canvas> element. Inside, we draw an element onto a 2D canvas context using drawElementImage(). Because the call includes the preserveElementGeometry: true option, we manually update the element's geometry using updateElementGeometry(). Finally, we clear the element's geometry using clearElementGeometry().
canvas.addEventListener("paint", () => {
ctx.drawElementImage(element, 100, 100, { preserveElementGeometry: true });
const currentTransform = new DOMMatrix();
currentTransform.e = 100;
currentTransform.f = 100;
canvas.updateElementGeometry(element, { canvasTransform: currentTransform });
canvas.clearElementGeometry(element);
});
Usage with 3D canvas rendering
For examples of clearElementGeometry() being used to clear the geometry of elements rendered on 3D canvases when the rendered scene isn't in an interactive state, see the following demos: