This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The Canvas API provides a means for drawing graphics via JavaScript and the HTML <canvas> element. Among other things, it can be used for animation, game graphics, data visualization, photo manipulation, and real-time video processing.
The Canvas API largely focuses on 2D graphics. The WebGL API, which also uses the <canvas> element, draws hardware-accelerated 2D and 3D graphics.
This simple example draws a green rectangle onto a canvas.
<canvas id="canvas"></canvas>
The Document.getElementById() method gets a reference to the HTML <canvas> element. Next, the HTMLCanvasElement.getContext() method gets that element's context—the thing onto which the drawing will be rendered.
The actual drawing is done using the CanvasRenderingContext2D interface. The fillStyle property makes the rectangle green. The fillRect() method places its top-left corner at (10, 10), and gives it a size of 150 units wide by 100 tall.
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "green";
ctx.fillRect(10, 10, 150, 100);
HTMLCanvasElementCanvasRenderingContext2DCanvasGradientCanvasPatternImageBitmapImageDataTextMetricsOffscreenCanvasPath2D Experimental
ImageBitmapRenderingContext Experimental
Note: The interfaces related to the WebGLRenderingContext are referenced under WebGL.
Note: OffscreenCanvas is also available in web workers.
CanvasCaptureMediaStreamTrack is a related interface.
A comprehensive tutorial covering both the basic usage of the Canvas API and its advanced features.
A hands-on, book-length introduction to the Canvas API and WebGL.
A handy reference for the Canvas API.
Combining <video> and <canvas> to manipulate video data in real time.
The Canvas API is extremely powerful, but not always simple to use. The libraries listed below can make the creation of canvas-based projects faster and easier.
Note: See the WebGL API for 2D and 3D libraries that use WebGL.
| Specification |
|---|
| HTML> # the-canvas-element> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
Canvas_API |
1 | 12 | 1.5["Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers.", "Before Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.", "Before Firefox 12, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead."] |
9 | 2Although early versions of Apple's Safari browser don't require the closing tag, the specification indicates that it is required, so you should be sure to include it for broadest compatibility. Before version 2, Safari will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it. |
18 | 4["Before Firefox for Android 5, the canvas width and height were signed integers instead of unsigned integers.", "Before Firefox for Android 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.", "Before Firefox for Android 14, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead."] |
10.1 | 1 | 1.0 | 37 | 1 |
height |
1 | 12 | 1.5["Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers.", "Before Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.", "Before Firefox 12, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead."] |
9 | 2Although early versions of Apple's Safari browser don't require the closing tag, the specification indicates that it is required, so you should be sure to include it for broadest compatibility. Before version 2, Safari will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it. |
18 | 4["Before Firefox for Android 5, the canvas width and height were signed integers instead of unsigned integers.", "Before Firefox for Android 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.", "Before Firefox for Android 14, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead."] |
10.1 | 1 | 1.0 | 37 | 1 |
moz-opaque |
No | No | 3.5 | No | No | No | 4 | No | No | No | No | No |
width |
1 | 12 | 1.5["Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers.", "Before Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.", "Before Firefox 12, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead."] |
9 | 2Although early versions of Apple's Safari browser don't require the closing tag, the specification indicates that it is required, so you should be sure to include it for broadest compatibility. Before version 2, Safari will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it. |
18 | 4["Before Firefox for Android 5, the canvas width and height were signed integers instead of unsigned integers.", "Before Firefox for Android 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.", "Before Firefox for Android 14, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead."] |
10.1 | 1 | 1.0 | 37 | 1 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API