- : The Graphics Canvas element
- Attributes
- Usage notes
- Alternative content
- Closing tag
- Sizing the canvas using CSS versus HTML
- Maximum canvas size
- Using an offscreen canvas
- Examples
- HTML
- JavaScript
- Result
- Accessibility concerns
- Alternative content
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- HTML Canvas Graphics
- What is HTML Canvas?
- Browser Support
- Canvas Examples
- Example
- Add a JavaScript
- Draw a Line
- Example
- Draw a Circle
- Example
- Draw a Text
- Example
- Stroke Text
- Example
- Draw Linear Gradient
- Example
- Draw Circular Gradient
- Example
- Draw Image
- HTML Canvas Tutorial
- HTMLCanvasElement: width property
- Value
- Examples
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
: The Graphics Canvas element
Use the HTML element with either the canvas scripting API or the WebGL API to draw graphics and animations.
Content categories | Flow content, phrasing content, embedded content, palpable content. |
---|---|
Permitted content | Transparent but with no interactive content descendants except for elements, elements, elements whose type attribute is checkbox , radio , or button . |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parents | Any element that accepts phrasing content. |
Implicit ARIA role | No corresponding role |
Permitted ARIA roles | Any |
DOM interface | HTMLCanvasElement |
Attributes
This element’s attributes include the global attributes.
The height of the coordinate space in CSS pixels. Defaults to 150.
moz-opaque Non-standard Deprecated
Lets the canvas know whether 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.
The width of the coordinate space in CSS pixels. Defaults to 300.
Usage notes
Alternative content
You should provide alternate content inside the block. That content will be rendered both on older browsers that don’t support canvas and in browsers with JavaScript disabled.
Closing tag
Sizing the canvas using CSS versus HTML
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 elements, either directly in the HTML or by using JavaScript.
Maximum canvas size
The maximum size of a element is very large, but the exact size depends on the browser. The following is some data we’ve collected from various tests and other sources (e.g. Stack Overflow):
Browser | Maximum height | Maximum width | Maximum area |
---|---|---|---|
Chrome | 32,767 pixels | 32,767 pixels | 268,435,456 pixels (i.e., 16,384 x 16,384) |
Firefox | 32,767 pixels | 32,767 pixels | 472,907,776 pixels (i.e., 22,528 x 20,992) |
Safari | 32,767 pixels | 32,767 pixels | 268,435,456 pixels (i.e., 16,384 x 16,384) |
IE | 8,192 pixels | 8,192 pixels | ? |
Note: Exceeding the maximum dimensions or area renders the canvas unusable — drawing commands will not work.
Using an offscreen canvas
A canvas can be rendered using the OffscreenCanvas API where the document and canvas are decoupled. The benefit is that a worker thread can handle canvas rendering and the main thread of your web application is not blocked by canvas operations. By parallelizing work, other UI elements of your web application will remain responsive even if you are running complex graphics on an offscreen canvas. For more information, see the OffscreenCanvas API documentation.
Examples
HTML
This code snippet adds a canvas element to your HTML document. A fallback text is provided if a browser is unable to read or render the canvas.
canvas width="120" height="120"> An alternative text describing what your canvas displays. canvas>
JavaScript
Then in the JavaScript code, call HTMLCanvasElement.getContext() to get a drawing context and start drawing onto the canvas:
const canvas = document.querySelector("canvas"); const ctx = canvas.getContext("2d"); ctx.fillStyle = "green"; // Add a rectangle at (10, 10) with size 100x100 pixels ctx.fillRect(10, 10, 100, 100);
Result
Accessibility concerns
Alternative content
The 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 as 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.
Specifications
Browser compatibility
BCD tables only load in the browser
See also
- Canvas API
- Canvas tutorial
- Canvas-related demos
- OffscreenCanvas
- Canvas cheat sheet (2009)
- Canvas cheat sheet (pdf) (2015)
- Canvas cheat sheet (pdf) (2020)
- Canvas introduction by Apple (2013)
Found a content problem with this page?
This page was last modified on Jun 27, 2023 by MDN contributors.
Your blueprint for a better internet.
HTML Canvas Graphics
The HTML element is used to draw graphics on a web page.
The graphic to the left is created with . It shows four elements: a red rectangle, a gradient rectangle, a multicolor rectangle, and a multicolor text.
What is HTML Canvas?
The HTML element is used to draw graphics, on the fly, via JavaScript.
The element is only a container for graphics. You must use JavaScript to actually draw the graphics.
Canvas has several methods for drawing paths, boxes, circles, text, and adding images.
Browser Support
The numbers in the table specify the first browser version that fully supports the element.
Element | |||||
---|---|---|---|---|---|
4.0 | 9.0 | 2.0 | 3.1 | 9.0 |
Canvas Examples
A canvas is a rectangular area on an HTML page. By default, a canvas has no border and no content.
The markup looks like this:
Note: Always specify an id attribute (to be referred to in a script), and a width and height attribute to define the size of the canvas. To add a border, use the style attribute.
Here is an example of a basic, empty canvas:
Your browser does not support the canvas element.
Example
Add a JavaScript
After creating the rectangular canvas area, you must add a JavaScript to do the drawing.
Draw a Line
Example
Draw a Circle
Example
Draw a Text
Example
Stroke Text
Example
Draw Linear Gradient
Example
// Create gradient
var grd = ctx.createLinearGradient(0, 0, 200, 0);
grd.addColorStop(0, «red»);
grd.addColorStop(1, «white»);
// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 80);
Draw Circular Gradient
Example
// Create gradient
var grd = ctx.createRadialGradient(75, 50, 5, 90, 60, 100);
grd.addColorStop(0, «red»);
grd.addColorStop(1, «white»);
// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 80);
Draw Image
HTML Canvas Tutorial
To learn more about , please read our HTML Canvas Tutorial.
HTMLCanvasElement: width property
This is one of the two properties, the other being HTMLCanvasElement.height , that controls the size of the canvas.
Value
Examples
canvas id="canvas" width="300" height="300">canvas>
You can get the width of the canvas with the following code:
const canvas = document.getElementById("canvas"); console.log(canvas.width); // 300
Specifications
Browser compatibility
BCD tables only load in the browser
See also
- HTMLCanvasElement : Interface used to define the HTMLCanvasElement.width property
- HTMLCanvasElement.height : Other property used to control the size of the canvas
Found a content problem with this page?
This page was last modified on Apr 7, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.