HTMLCanvasElement: requestPaint() 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 requestPaint() method of the HTMLCanvasElement schedules a single paint event even if no repaints occurred, when rendering elements on a <canvas> using the HTML-in-Canvas API. This can be useful for example if you want to redraw the element at a different size on the <canvas> without changing its rendered DOM state.

The requestPaint() method and the paint event operate similarly to requestAnimationFrame(), although the requestAnimationFrame() callback is invoked at the start of the document lifecycle, and the paint event is fired at the end.

Syntax

js
requestPaint()

Parameters

None.

Return value

None (undefined).

Examples

Requesting a paint event to redraw an element

This example shows how to use the requestPaint() method to redraw an element at double its original rendered size on a <canvas>.

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>. Finally, we include a <button> that can be pressed to change the element's rendered size.

html
<button>Double render size</button>
<canvas width="640" height="200" 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;
  border: 4px solid #576490;
  background: #d1d2f9;
  border-radius: 5px;
  padding: 10px;
}

JavaScript

First, we grab references to the <canvas>, <div>, and <button> elements. We also define renderWidth and renderHeight variables to store the width and height to render the <div> element at.

js
const canvas = document.querySelector("canvas");
const divElem = document.querySelector("canvas div");
const btn = document.querySelector("button");
let renderWidth;
let renderHeight;

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 define a paint event handler. Inside the handler, we retrieve the initial rendered size of the <div> element using the HTMLCanvasElement.getElementImageDefaultSize() method and set the the renderWidth and renderHeight variables to the retrieved values. We then fill the entire canvas with a solid color using the fillStyle property and fillRect() method, and use drawElementImage() to draw the <div> onto the canvas, setting its width and height to renderWidth and renderHeight.

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

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

  canvas.addEventListener("paint", () => {
    if (!renderWidth) {
      const initialSize = canvas.getElementImageDefaultSize(divElem);
      renderWidth = initialSize.width;
      renderHeight = initialSize.height;
    }

    ctx.fillStyle = "goldenrod";
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    ctx.drawElementImage(divElem, 100, 50, renderWidth, renderHeight);
  });
}

We set a click event handler on the <button>. Inside, we double the renderWidth and renderHeight values and call requestPaint() to fire the paint event and run its handler again, re-rendering the <div> element at the new size. After a delay of 2 seconds, we use the same technique to render the <div> back at its original size.

js
btn.addEventListener("click", () => {
  btn.disabled = true;
  renderWidth *= 2;
  renderHeight *= 2;
  canvas.requestPaint();

  setTimeout(() => {
    btn.disabled = false;
    renderWidth *= 0.5;
    renderHeight *= 0.5;
    canvas.requestPaint();
  }, 2000);
});

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

js
main();

Result

Specifications

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

Browser compatibility

See also