Canvas, SVG and MathML in HTML Documents
Canvas draws pixels
canvas is an HTML element with a bitmap drawing surface. Set width and height attributes for its coordinate dimensions. getContext("2d") returns a drawing context when available. fillRect draws a rectangle at x, y with a width and height. Provide equivalent information outside the canvas as well as fallback content inside it.
<canvas id="chart" width="120" height="60" role="img" aria-label="A green bar">A green bar.</canvas><p>Chart: 40 units.</p><script> const ctx = document.getElementById("chart").getContext("2d"); if (ctx) { ctx.fillStyle = "green"; ctx.fillRect(0, 0, 40, 20); }</script>A green rectangle occupies the first 40 by 20 canvas pixels.
SVG describes vector shapes
SVG is a separate markup vocabulary that can be embedded in HTML. A viewBox defines a coordinate rectangle; a circle uses cx, cy and r for center and radius. Unlike painted canvas pixels, inline SVG shapes are DOM nodes. Give informative graphics an accessible name and retain meaningful text alternatives.
<svg viewBox="0 0 100 100" width="100" height="100" role="img" aria-labelledby="svg-title"> <title id="svg-title">Blue circle</title> <circle cx="50" cy="50" r="35" fill="navy" /></svg>The circle remains a vector shape in the SVG namespace.
MathML represents mathematical structure
MathML is another markup vocabulary. A fraction uses mfrac with two children: numerator and denominator. mn denotes a number and mi an identifier. This preserves mathematical structure rather than drawing an image of the formula. Actual speech and braille depend on the browser and assistive technology.
<p>One half:</p><math> <mfrac> <mn>1</mn> <mn>2</mn> </mfrac></math>The fraction contains two number nodes in the MathML namespace.
Common mistakes and practice
CSS dimensions alone scale the canvas bitmap; changing width or height attributes resets its drawing buffer. SVG and MathML have their own standards, not ordinary HTML element interfaces. Do not use canvas for text-heavy content or assume graphical fallback provides a fully accessible interactive experience. This lesson introduces integration boundaries, not complete graphics programming.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Canvas, SVG and MathML in HTML Documents — Knowledge Check
Apply the integration concepts taught in this lesson.
Which element provides a bitmap drawing surface?
Which vocabulary describes vector shapes as nodes?
Which MathML element describes a fraction?
What happens when the canvas width attribute changes?
Does a canvas graphic automatically provide all accessible data?