HTML <canvas> (Canvas for Drawing) Tag
Example
<canvas id="myCanvas">
Your browser does not support the canvas tag.
</canvas>
<script>
let canvas = document.getElementById("myCanvas");
let context = canvas.getContext("2d");
context.fillStyle = "red";
context.fillRect(0, 0, 100, 100);
</script>
Meaning
The <canvas> (Canvas for Drawing) element defines a region in the document to be used as a bitmap canvas where script (usually JavaScript) code can be used to render graphics interactively.
Note:
- Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers.
- Prior to 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.
- 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. Versions of Safari prior to version 2 will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it.
Maximum canvas size:
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) |
Edge | 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) |
IE | 8,192 pixels | 8,192 pixels | N/A |
Safari | 32,767 pixels | 32,767 pixels | 268,435,456 pixels (i.e., 16,384 x 16,384) |
Version: HTML5
Standard Syntax
<canvas> </canvas>
Browser Support
Status
Attributes
Attribute | Value | Description |
---|---|---|
height | pixels | Sets the height of the canvas. (Default value is 150) |
width | pixels | Sets the width of the canvas. (Default value is 300) |
Global Attributes
<canvas> element also supports the Global Attributes in HTML.
Event Attributes
<canvas> element also supports the Event Attributes in HTML.
More Examples
Create transparency
Example
<canvas id="myCanvas">
Your browser does not support the canvas tag.
</canvas>
<script>
let c = document.getElementById("myCanvas");
let context = c.getContext("2d");
context.fillStyle = "red";
context.fillRect(40, 40, 100, 50);
/*Transparency*/
context.globalAlpha = 0.2;
context.fillStyle = "green";
context.fillRect(70, 70, 100, 50);
context.fillStyle = "yellow";
context.fillRect(100, 100, 100, 50);
</script>
Create stroke
Example
<canvas id="myCanvas">
Your browser does not support the canvas tag.
</canvas>
<script type="text/javascript">
let canvas = document.getElementById("myCanvas");
let context = canvas.getContext("2d");
context.moveTo(0, 100);
context.lineTo(100, 0);
context.stroke();
</script>
By Default CSS Value(s)
Most of the browsers will display the <canvas> element with the following by default value(s)
canvas {
height: 150px;
width: 300px;
}