HTML <canvas> Tag

The HTML <canvas> tag is used for creating graphics on the fly in an HTML document. It can be used for rendering graphs, game graphics, fancy text, or other images.

The tag itself renders a blank canvas. To draw on the canvas, the <canvas> tag is used in conjunction with JavaScript (using the getContext() method).

Any content between the <canvas></canvas> tags is "fallback content"- meaning, it will be displayed only if the canvas cannot be displayed.

The <canvas> tag was introduced in HTML 5.

Syntax

The <canvas> tag is written as <canvas id=""></canvas> with any fallback content inserted between the start and end tag. This fallback content will only be displayed if the browser doesn't support the <canvas> element.

You can also specify the canvas height and width using the height and width attributes respectively. If you don't use these attributes, the canvas will be rendered at the default size, which is a width of 300 and a height of 150.

To make the <canvas> actually display something, you will need to add the id attribute and reference the <canvas>'s id with some JavaScript.

Like this:

<canvas id="myCanvas">
Fallback content...
</canvas>

<script>
function displayCanvas()
	{
      var canvas = document.getElementById("myCanvas");
...
</script>

Examples

Shapes

Rectangles

Here's an example of using the <canvas> element to draw rectangles with various fills and opacity levels.

Bézier Curves

You can also use the <canvas> element to draw Bézier curves. To do this, use bezierCurveTo, which works similar to the bezier and path tools in computer drawing packages such as Adobe Illustrator, GIMP, and others.

Text

Here's an example of using the <canvas> element being used to draw text.

Images

You can even use the <canvas> element to draw an image. To draw an image, use the drawImage() method. This requires that you supply the URL of an image.

Attributes

Attributes can be added to an HTML element to provide more information about how the element should appear or behave.

There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.

The <canvas> element accepts the following attributes.

Element-Specific Attributes

This table shows the attributes that are specific to the <canvas> tag/element.

AttributeDescription
widthSpecifies the canvas width in pixels. The default value is 300.

Possible values:

[Non-negative integer] (for example, 300)

heightSpecifies the canvas height in pixels. The default value is 150.

Possible values:

[Non-negative integer] (for example, 150)

Global Attributes

The following attributes are standard across all HTML5 elements. Therefore, you can use these attributes with the <canvas> tag , as well as with all other HTML tags.

  • accesskey
  • class
  • contenteditable
  • contextmenu
  • dir
  • draggable
  • dropzone
  • hidden
  • id
  • inert
  • itemid
  • itemprop
  • itemref
  • itemscope
  • itemtype
  • lang
  • spellcheck
  • style
  • tabindex
  • title
  • translate

For a full explanation of these attributes, see HTML 5 global attributes.

Event Handler Content Attributes

Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event.

Below are the standard HTML5 event handler content attributes.

Again, you can use any of these with the <canvas> element, as well as any other HTML5 element.

  • onabort
  • oncancel
  • onblur
  • oncanplay
  • oncanplaythrough
  • onchange
  • onclick
  • oncontextmenu
  • ondblclick
  • ondrag
  • ondragend
  • ondragenter
  • ondragexit
  • ondragleave
  • ondragover
  • ondragstart
  • ondrop
  • ondurationchange
  • onemptied
  • onended
  • onerror
  • onfocus
  • onformchange
  • onforminput
  • oninput
  • oninvalid
  • onkeydown
  • onkeypress
  • onkeyup
  • onload
  • onloadeddata
  • onloadedmetadata
  • onloadstart
  • onmousedown
  • onmousemove
  • onmouseout
  • onmouseover
  • onmouseup
  • onmousewheel
  • onpause
  • onplay
  • onplaying
  • onprogress
  • onratechange
  • onreadystatechange
  • onscroll
  • onseeked
  • onseeking
  • onselect
  • onshow
  • onstalled
  • onsubmit
  • onsuspend
  • ontimeupdate
  • onvolumechange
  • onwaiting

For a full explanation of these attributes, see HTML 5 event handler content attributes.

Differences Between HTML 4 & HTML 5

The <canvas> element was introduced in HTML5.

To see more detail see HTML5 <canvas> Tag. Also check out the links to the official specifications below.

Template

Here's a template for the <canvas> tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.

For more information on attributes for this tag, see HTML5 <canvas> Tag.

<canvas  
 width=""
 height=""
 
 accesskey=""
 class=""
 contenteditable=""
 contextmenu=""
 dir=""
 draggable=""
 dropzone=""
 hidden=""
 id=""
 itemid=""
 itemprop=""
 itemref=""
 itemscope=""
 itemtype=""
 lang=""
 spellcheck=""
 style=""
 tabindex=""
 title=""
 translate=""

 onabort=""
 onautocomplete=""
 onautocompleteerror=""
 onblur=""
 oncancel=""
 oncanplay=""
 oncanplaythrough=""
 onchange=""
 onclick=""
 onclose=""
 oncontextmenu=""
 oncuechange=""
 ondblclick=""
 ondrag=""
 ondragend=""
 ondragenter=""
 ondragexit=""
 ondragleave=""
 ondragover=""
 ondragstart=""
 ondrop=""
 ondurationchange=""
 onemptied=""
 onended=""
 onerror=""
 onfocus=""
 oninput=""
 oninvalid=""
 onkeydown=""
 onkeypress=""
 onkeyup=""
 onload=""
 onloadeddata=""
 onloadedmetadata=""
 onloadstart=""
 onmousedown=""
 onmouseenter=""
 onmouseleave=""
 onmousemove=""
 onmouseout=""
 onmouseover=""
 onmouseup=""
 onmousewheel=""
 onpause=""
 onplay=""
 onplaying=""
 onprogress=""
 onratechange=""
 onreset=""
 onresize=""
 onscroll=""
 onseeked=""
 onseeking=""
 onselect=""
 onshow=""
 onsort=""
 onstalled=""
 onsubmit=""
 onsuspend=""
 ontimeupdate=""
 ontoggle=""
 onvolumechange=""
 onwaiting=""
	>
</canvas>

Tag Details

For more details about the <canvas> tag, see HTML5 <canvas> Tag.

Specifications

Here are the official specifications for the <canvas> element.

What's the Difference?

W3C creates "snapshot" specifications that don't change once defined. So the HTML5 specification won't change once it becomes an official recommendation. WHATWG on the other hand, develops a "living standard" that is updated on a regular basis. In general, you will probably find that the HTML living standard will be more closely aligned to the current W3C draft than to the HTML5 specification.