HTML5 <canvas>
Tag
Topic: HTML5 Tags ReferencePrev|Next
Description
The <canvas>
element defines a region in the document, which can be used to draw graphics on the fly via scripting (usually JavaScript). For example, it can be used to draw path and shapes, graphs or even perform animations.
The following table summarizes the usages context and the version history of this tag.
Placement: | Inline |
---|---|
Content: | Inline, and text |
Start/End Tag: | Start tag: required, End tag: required |
Version: | New in HTML5 |
Syntax
The basic syntax of the <canvas>
tag is given with:
The example below shows the <canvas>
tag in action.
Example
Try this code »<canvas id="myCanvas" width="300" height="200"></canvas>
<script type="text/javascript">
window.onload = function(){
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.moveTo(50, 150);
context.lineTo(250, 50);
context.stroke();
};
</script>
Note: You may (and should) provide alternate content inside the canvas element. That content will be rendered both on older browsers that don't support canvas and in browsers with JavaScript disabled.
Tag-Specific Attributes
The following table shows the attributes that are specific to the <canvas>
tag.
Attribute | Value | Description |
---|---|---|
width |
pixels | Sets the width of the canvas. |
height |
pixels | Sets the height of the canvas. |
Global Attributes
Like all other HTML tags, the <canvas>
tag supports the global attributes in HTML5.
Event Attributes
The <canvas>
tag also supports the event attributes in HTML5.
Browser Compatibility
The <canvas>
tag is supported in all major modern browsers.
Basic Support—
|
Further Reading
See tutorial on: HTML5 Canvas.
Related tags: <script>
.