HTMLCanvasElement: paint event

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 paint event of the Canvas API is fired when drawable elements have been updated and require redrawing to display the changes.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("paint", (event) => { })

onpaint = (event) => { }

Event type

A CanvasPaintEvent.

Description

When using the HTML-in-Canvas API to render a DOM element on a <canvas> element, the latest visual state of the element needs rendering to the canvas every time it is repainted, otherwise the user won't see any change. This includes the initial paint, and the repaint each time a text selection is made or an interactive descendent element is focused or hovered, for example.

To re-render the element on each repaint, you can put your rendering code inside a paint event handler.

If you want to re-render the element continuously even if it isn't repainted, for example if it is being moved around the <canvas>, you can request a paint event to be fired using the requestPaint() method, or you could use requestAnimationFrame(). You can see an example in our HTML-in-Canvas 2D context example:

js
canvas.addEventListener("paint", () => {
  ctx.fillStyle = "#7D82B8";
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  ctx.drawElementImage(form, 100, 100);
  fx.apply(performance.now());
  canvas.requestPaint();
});

The paint event object includes a changedElements property containing an array of references to drawable elements that were repainted. This can be used to detect which elements changed, when multiple drawable elements are rendered.

The rendering lifecycle

Canvas drawing commands executed inside a paint event handler appear in the current rendering update, but any DOM modifications made inside the same handler won't render until the next update:

js
canvas.addEventListener("paint", () => {
  // Appears in the current rendering update
  ctx.drawElementImage(element, 20, 20);

  // Triggers a new paint event; change renders in the next update:
  element.style.backgroundColor = "green";
});

Depending on what visual updates you are making and when, this may result in out of sync rendering updates. To avoid this, you could make such updates inside a requestAnimationFrame() callback instead, which will run before the paint event handler for each rendering update. You could for example combine the following with a paint event handler:

js
function update() {
  // Make DOM updates
  requestAnimationFrame(update);
}
requestAnimationFrame(update);

Nested canvas paint event order

With HTML-in-Canvas, it is possible to nest one <canvas> element inside another, set content="drawable" on both of them, and render drawable elements inside either or both of them. When a drawable element inside the child <canvas> is repainted, a paint event is fired on both the parent and child canvases. It is fired on the child canvas first, so its drawable element's visual representation is updated. It is fired on the parent canvas second, so its drawable element's visual representation can include the latest visual state of the nested <canvas>'s drawable element.

For example:

html
<canvas id="parent-canvas" width="640" height="480" content="drawable">
  <div id="parent-element" drawable>
    Parent element content
    <canvas id="child-canvas" width="300" height="200" content="drawable">
      <div id="child-element" drawable>Child element content</div>
    </canvas>
  </div>
</canvas>

When the drawable element on the child <canvas> is repainted (for example, by updating its background-color), the paint event is fired on the child canvas first and the parent canvas second. You can see this in action in our nested canvas demo.

Examples

Basic usage

This example shows how to render DOM elements onto a 2D canvas, and how the paint event is used to keep their rendered visual state updated.

HTML

We include a <canvas> element with its content attribute set to drawable, and two <div> elements, each containing a heading and a button. Each <div> has the drawable attribute set.

html
<canvas content="drawable" width="480" height="320">
  <div id="one" drawable>
    <h2>Heading</h2>
    <p>Content.</p>
    <button>Click me</button>
  </div>

  <div id="two" drawable>
    <h2>Heading</h2>
    <p>Content.</p>
    <button>Click me</button>
  </div>
</canvas>

CSS

We set some rudimentary styles on the drawable elements. As you'll see in the result, the HTML is rendered on the canvas exactly as styled by the CSS.

css
div {
  border: 3px solid black;
  border-radius: 10px;
  padding: 10px 20px;
}

h2 {
  margin: 0 0 10px;
}

p {
  margin: 10px 0;
}

#one {
  background-color: #b2b5e0;
}

#two {
  background-color: orange;
}

JavaScript

In our script, we define a main() function before running it to start the example. Inside, we first grab references to the <canvas> and both <div> elements, then we 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 fill the entire canvas with a solid color using the fillStyle property and fillRect() method. We then use drawElementImage() to draw both of the drawable elements onto the canvas, placing each one at different coordinates. We do this inside a paint event so that the drawable elements are redrawn on the canvas each time they are repainted. This includes the initial paint, and the repaint each time a text selection is made or a button is focused or hovered, for example.

js
function main() {
  const canvas = document.querySelector("canvas");
  const one = document.querySelector("#one");
  const two = document.querySelector("#two");
  const ctx = canvas.getContext("2d");

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

  canvas.addEventListener("paint", () => {
    ctx.fillStyle = "#7D82B8";
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    ctx.drawElementImage(one, 20, 20);
    ctx.drawElementImage(two, 100, 140);
  });
}

main();

Result

Specifications

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

Browser compatibility

See also