drawable

The drawable attribute marks a MathML element as a paint root, meaning that it can be rendered independently on a parent <canvas> element with its content attribute set to drawable, using the HTML-in-Canvas API.

The drawable attribute is a boolean attribute; its presence marks a <canvas> descendant as a paint root.

Examples

Basic usage

This example shows how to render MathML onto a 2D canvas.

HTML

We include a <canvas> element with its content attribute set to drawable. Nested inside the canvas we include a <math> element containing some MathML content. The <math> element has the drawable attribute set.

html
<canvas width="240" height="180" content="drawable">
  <math display="block" drawable>
    <mrow>
      <mi>E</mi>
      <mo>=</mo>
      <mi>m</mi>
      <msup>
        <mi>c</mi>
        <mn>2</mn>
      </msup>
    </mrow>
  </math>
</canvas>

CSS

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

css
math {
  font-size: 20px;
}

math:hover,
math:focus {
  color: red;
}

JavaScript

In our script, we first grab references to our <canvas> and <math> elements.

js
const canvas = document.querySelector("canvas");
const mathElem = document.querySelector("math");

Next, we define a main() function. Inside, 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 use the drawElementImage() method to draw the <math> element onto the canvas. We do this inside a paint event so that the <math> element is redrawn on the canvas each time it is repainted. This includes the initial paint, and the repaint each time the MathML is focused or hovered.

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

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

  canvas.addEventListener("paint", () => {
    ctx.drawElementImage(mathElem, 10, 10);
  });
}

Finally, we run the main() function to start the example:

js
main();

Result

Specifications

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

Browser compatibility

See also