Codectionary / Developer documentation / HTML

<canvas>

The <canvas> element provides a blank, resizable bitmap surface for drawing graphics, animations, and visualizations via JavaScript — typically using the Canvas 2D API or WebGL. Unlike SVG, canvas content is pixel-based rather than made of individually addressable DOM elements, which makes it well-suited for games, charts, and pixel-level image manipulation.

Syntax

<canvas id="myCanvas" width="400" height="300"></canvas>

Examples

Basic Canvas Setup

Creating a canvas and drawing a simple rectangle.

<canvas id="myCanvas" width="300" height="150" style="border:1px solid #ccc;"></canvas>
<script>
  const ctx = document.getElementById("myCanvas").getContext("2d");
  ctx.fillStyle = "#3b82f6";
  ctx.fillRect(20, 20, 100, 80);
</script>

Drawing a Circle

Using canvas arc methods to draw shapes.

<canvas id="circleCanvas" width="200" height="200"></canvas>
<script>
  const ctx = document.getElementById("circleCanvas").getContext("2d");
  ctx.beginPath();
  ctx.arc(100, 100, 60, 0, Math.PI * 2);
  ctx.fillStyle = "#10b981";
  ctx.fill();
</script>

Fallback Content

Providing fallback content for browsers or users without canvas/script support.

<canvas id="chart" width="400" height="200">
  Your browser does not support canvas. Here is a summary: Sales grew 20% in Q2.
</canvas>

Attributes

  • width (number): The canvas width in pixels (default 300)
  • height (number): The canvas height in pixels (default 150)

Best practices

  • Set width/height as HTML attributes rather than CSS to avoid blurry, stretched rendering
  • Provide meaningful fallback content between the tags for cases where canvas or JavaScript is unavailable
  • For charts, diagrams, or icons that need to stay interactive and accessible as individual elements, consider SVG instead of canvas
  • Canvas content is not part of the accessibility tree by default — add ARIA attributes or fallback text for accessibility when the content is meaningful

Browser compatibility

Support applies to the named feature, not every attribute or behaviour in this article. Versions refer to browsers, not operating systems.

<canvas>

Desktop

Chrome
Supported
From 1
Firefox
Supported
From 1.5
Safari
Supported
From 2
Edge
Supported
From 12
Opera
Supported
From 9

Mobile

Safari on iOS
Supported
From 1
Chrome for Android
Supported
From 18
Firefox for Android
Supported
From 4
Samsung Internet
Supported
From 1.0

MDN BCD record for this feature · MDN reference

Support history and limitations

Chrome

  • From 1

Firefox

  • From 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.

Safari

  • From 2 · Although 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.

Edge

  • From 12

Opera

  • From 9

Safari on iOS

  • From 1

Chrome for Android

  • From 18

Firefox for Android

  • From 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.

Samsung Internet

  • From 1.0
<canvas> width

Desktop

Chrome
Supported
From 1
Firefox
Supported
From 1.5
Safari
Supported
From 2
Edge
Supported
From 12
Opera
Supported
From 9

Mobile

Safari on iOS
Supported
From 1
Chrome for Android
Supported
From 18
Firefox for Android
Supported
From 4
Samsung Internet
Supported
From 1.0

MDN BCD record for this feature

Support history and limitations

Chrome

  • From 1

Firefox

  • From 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.

Safari

  • From 2 · Although 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.

Edge

  • From 12

Opera

  • From 9

Safari on iOS

  • From 1

Chrome for Android

  • From 18

Firefox for Android

  • From 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.

Samsung Internet

  • From 1.0
<canvas> height

Desktop

Chrome
Supported
From 1
Firefox
Supported
From 1.5
Safari
Supported
From 2
Edge
Supported
From 12
Opera
Supported
From 9

Mobile

Safari on iOS
Supported
From 1
Chrome for Android
Supported
From 18
Firefox for Android
Supported
From 4
Samsung Internet
Supported
From 1.0

MDN BCD record for this feature

Support history and limitations

Chrome

  • From 1

Firefox

  • From 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.

Safari

  • From 2 · Although 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.

Edge

  • From 12

Opera

  • From 9

Safari on iOS

  • From 1

Chrome for Android

  • From 18

Firefox for Android

  • From 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.

Samsung Internet

  • From 1.0
HTMLCanvasElement.getContext

Desktop

Chrome
Supported
From 1
Firefox
Supported
From 1.5
Safari
Supported
From 2
Edge
Supported
From 12
Opera
Supported
From 9

Mobile

Safari on iOS
Supported
From 1
Chrome for Android
Supported
From 18
Firefox for Android
Supported
From 4
Samsung Internet
Supported
From 1.0

MDN BCD record for this feature · MDN reference

Support history and limitations

Chrome

  • From 1

Firefox

  • From 1.5

Safari

  • From 2

Edge

  • From 12

Opera

  • From 9

Safari on iOS

  • From 1

Chrome for Android

  • From 18

Firefox for Android

  • From 4

Samsung Internet

  • From 1.0
CanvasRenderingContext2D.fillRect

Desktop

Chrome
Supported
From 1
Firefox
Supported
From 1.5
Safari
Supported
From 2
Edge
Supported
From 12
Opera
Supported
From 9

Mobile

Safari on iOS
Supported
From 1
Chrome for Android
Supported
From 18
Firefox for Android
Supported
From 4
Samsung Internet
Supported
From 1.0

MDN BCD record for this feature · MDN reference

Support history and limitations

Chrome

  • From 1

Firefox

  • From 1.5

Safari

  • From 2

Edge

  • From 12

Opera

  • From 9

Safari on iOS

  • From 1

Chrome for Android

  • From 18

Firefox for Android

  • From 4

Samsung Internet

  • From 1.0
CanvasRenderingContext2D.arc

Desktop

Chrome
Supported
From 1
Firefox
Supported
From 1.5
Safari
Supported
From 3
Edge
Supported
From 12
Opera
Supported
From 11.6

Mobile

Safari on iOS
Supported
From 1
Chrome for Android
Supported
From 18
Firefox for Android
Supported
From 4
Samsung Internet
Supported
From 1.0

MDN BCD record for this feature · MDN reference

Support history and limitations

Chrome

  • From 1

Firefox

  • From 1.5

Safari

  • From 3

Edge

  • From 12

Opera

  • From 11.6

Safari on iOS

  • From 1

Chrome for Android

  • From 18

Firefox for Android

  • From 4

Samsung Internet

  • From 1.0
CanvasRenderingContext2D.fillText

Desktop

Chrome
Supported
From 2
Firefox
Supported
From 3.5
Safari
Supported
From 4
Edge
Supported
From 12
Opera
Supported
By 12.1 (possibly earlier)

Mobile

Safari on iOS
Supported
From 3.2
Chrome for Android
Supported
From 18
Firefox for Android
Supported
From 4
Samsung Internet
Supported
From 1.0

MDN BCD record for this feature · MDN reference

Support history and limitations

Chrome

  • From 2

Firefox

  • From 3.5

Safari

  • From 4

Edge

  • From 12

Opera

  • By 12.1 (possibly earlier)

Safari on iOS

  • From 3.2

Chrome for Android

  • From 18

Firefox for Android

  • From 4

Samsung Internet

  • From 1.0

MDN BCD 8.1.5 · Dataset built 2026-10-08 · Mapping reviewed 2026-10-21. These dates are not individual browser-test or claim-verification dates. Unknown means the data does not establish support.

HTML Living Standard

Specifications & further reading