HTML Canvas Element

Use the HTML <canvas> element with either the canvas scripting API or the WebGL API to draw graphics and animations.

Attributes

height The height of the coordinate space in CSS pixels. Defaults to 150.

moz-opaque Lets the canvas know whether or not translucency will be a factor. If the canvas knows there's no translucency, painting performance can be optimized. This is only supported by Mozilla-based browsers; use the standardized canvas.getContext('2d', { alpha: false }) instead.

width The width of the coordinate space in CSS pixels. Defaults to 300.

Usage Notes

Alternative contentSection You may (and should) provide alternate content inside the <canvas> block. That content will be rendered both on older browsers that don't support canvas and in browsers with JavaScript disabled.

Required </canvas> tagSection Unlike the <img> element, the <canvas> element requires the closing tag (</canvas>).

Sizing the canvas using CSS versus HTMLSection The displayed size of the canvas can be changed using CSS, but if you do this the image is scaled during rendering to fit the styled size, which can make the final graphics rendering end up being distorted. It is better to specify your canvas dimensions by setting the width and height attributes directly on the <canvas> attributes, either directly in the HTML or by using JavaScript.

Examples

This code snippet adds a canvas element to your HTML document. A fallback text is provided if a browser is unable to render the canvas, or if can't read a canvas. Providing a useful fallback text or sub DOM helps to make the the canvas more accessible.

<canvas id="canvas" width="300" height="300">

An alternative text describing what your canvas displays.

</canvas>

Then in the JavaScript code, call HTMLCanvasElement.getContext() to get a drawing context and start drawing onto the canvas:

<script>

var canvas = document.getElementById('canvas');

var ctx = canvas.getContext('2d');

ctx.fillStyle = 'green';

ctx.fillRect(10, 10, 100, 100);

</script>

Accessibility concerns

Alternative content The <canvas> element on its own is just a bitmap and does not provide information about any drawn objects. Canvas content is not exposed to accessibility tools like semantic HTML is. In general, you should avoid using canvas in an accessible website or app. The following guides can help to make it more accessible.

More Examples

This code snippet adds a canvas element to your HTML document. A fallback text is provided if a browser is unable to render the canvas, or if can't read a canvas. Providing a useful fallback text or sub DOM helps to make the the canvas more accessible.

<canvas id="canvas" width="300" height="300">

More placeholder text describing what your canvas displays.

</canvas>

You may try slight variations, and it's worth to try mixing colors such as:

<script>

var canvas = document.getElementById('canvas');

var ctx = canvas.getContext('2d');

ctx.fillStyle = 'blue';

ctx.fillRect(10, 10, 100, 100);

</script>

Or perhaps:

<script>

var canvas = document.getElementById('canvas');

var ctx = canvas.getContext('2d');

ctx.fillStyle = 'red';

ctx.fillRect(10, 10, 100, 100);

</script>