HTMLCanvasElement: getElementImageDefaultSize() 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 getElementImageDefaultSize() method of the HTMLCanvasElement interface returns an object containing the default size, in canvas units, of an element (or ElementImage) when drawn with drawElementImage() (specifying no destination size).
Syntax
getElementImageDefaultSize(element)
Parameters
element-
A reference to a
drawableelement or anElementImage.
Return value
An object containing the following properties:
height-
The default height of the element, in canvas units.
width-
The default width of the element, in canvas units.
Note: These are live values that will change if, for example, the canvas resolution changes.
Exceptions
InvalidStateErrorDOMException-
Thrown if the
elementhas not yet been painted, meaning there is currently no rendered state to capture the size of.
Examples
>Recording the default size and using it to render an element
This example shows how to extract the default size of an element and then render it on a canvas at various different sizes relative to that default size.
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 a range <input> with a <label> and an <output>, which we will use to control the rendered size of the <div> (varying it between 50% and 150% of its default size). Finally, we include a <p> to output the rendered element size into.
<div>
<label for="range">Adjust element size:</label>
<input type="range" id="range" step="0.1" min="0.5" value="1" max="1.5" />
<output>100%</output>
</div>
<canvas width="640" height="200" content="drawable">
<div drawable>Initial render status</div>
</canvas>
<p></p>
CSS
We include some rudimentary styles for the <div> element:
div {
font-size: 1.4rem;
border: 4px solid #576490;
background: #d1d2f9;
border-radius: 5px;
padding: 10px;
}
JavaScript
First, we grab references to the <canvas>, <div>, <p>, <input>, and <output> elements.
const canvas = document.querySelector("canvas");
const divElem = document.querySelector("canvas div");
const sizeOutput = document.querySelector("p");
const rangeInput = document.querySelector("input");
const rangeOutput = document.querySelector("output");
Next, we set an input event handler on the update <input> so that when its value is changed, we print the new value to the <output> element and invoke HTMLCanvasElement.requestPaint() to fire a paint event (which causes the <div> element to re-render).
rangeInput.addEventListener("input", () => {
rangeOutput.textContent = `${Math.floor(rangeInput.value * 100)}%`;
canvas.requestPaint();
});
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 initialSize 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:
- Set
initialSizeto the intial rendered size of the<div>element usinggetElementImageDefaultSize(divElem). - Use
drawElementImage()to draw the<div>onto the canvas, setting its width and height to the initial rendered size multiplied by the<input>value. - Set the
<p>element'stextContentto report the rendered size.
function main() {
const ctx = canvas.getContext("2d");
if (!ctx.drawElementImage) {
document.body.classList.add("no-html-in-canvas");
return;
}
let initialSize;
canvas.addEventListener("paint", () => {
ctx.fillStyle = "goldenrod";
ctx.fillRect(0, 0, canvas.width, canvas.height);
if (!initialSize) {
initialSize = canvas.getElementImageDefaultSize(divElem);
}
ctx.drawElementImage(
divElem,
100,
50,
initialSize.width * Number(rangeInput.value),
initialSize.height * Number(rangeInput.value),
);
sizeOutput.textContent = `Element rendered size (canvas units): ${Math.ceil(initialSize.width * Number(rangeInput.value))}, ${Math.ceil(initialSize.height * Number(rangeInput.value))}`;
});
}
Finally, we run the main() function to start eveything off.
main();