HTML <canvas> (Canvas for Drawing) Tag

❮ Previous Reference Next ❯

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:

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;
}



Related Tags:

<noscript> and <script>

See Canvas Reference.

❮ Previous Reference Next ❯