HTML <canvas> tagS2C Home   « HTML <canvas> tag

Definition

The <canvas> tag is used for creating graphics dynamically in a HTML document using a scripting language such as JavaScript.

Alternative text should be provided within the <canvas> element as fallback content and to aid accessibility; this will be rendered within browsers where JavaScript is disabled and also on older browsers not supporting the <canvas> element.

The <canvas> tag was introduced in HTML5.

Example


<canvas width="200" height="200">The canvas tag is not supported in your browser!</canvas>
<script>
  const canvas = document.querySelector('canvas'); 
  const ctx = canvas.getContext('2d'); 
  ctx.fillStyle = 'blue'; 
  ctx.fillRect(20, 20, 100, 100);
</script>
The canvas tag is not supported in your browser!

Attributes

The following attributes can be used with the <canvas> tag.


<canvas> Specific Attributes
Attribute Value Description Example
heightNon-negative integerSpecifies the height of the canvas in pixels - Default value is 150

<canvas height="300">Fallback Text</canvas>
widthNon-negative integerSpecifies the height of the canvas in pixels - Default value is 300

<canvas width="400">Fallback Text</canvas>
<canvas> Global Attributes
Attribute Description Example
Common
classSpecifies a classname for the element allowing you to apply the style of the predefined class to the content.

<canvas class="name">Fallback Text</canvas>
idSpecifies a unique id for the element allowing you to apply the style of the predefined id to the content.

<canvas id="name">Fallback Text</canvas>
styleSpecifies an inline style for the element allowing you to apply the style to the contentt.

<canvas style="color:red;">Fallback Text</canvas>
titleSpecifies an inline style for the element allowing you to apply the style to the content.

<canvas title="Content info">Fallback Text</canvas>
Keyboard
accesskeySpecifies a keyboard shortcut to associate with the element.

<canvas accesskey="a">Fallback Text</canvas>
tabindexSpecifies a tab order for the element.

<canvas tabindex="1">Fallback Text</canvas>
Language
dirSpecifies the directional flow of the content.

<!-- The text will flow from left to right -->
<canvas dir="ltr">Fallback Text</canvas>
<!-- The text will flow from right to left -->
<canvas dir="rtl">Fallback Text</canvas>
langSpecifies a language code for the content of the element.

<canvas lang="en">Fallback Text</canvas>
spellcheckSpecifies an inline style for the element allowing you to apply the style to the contentt.

<!-- Valid values true and false. -->
<!-- Default inherited / browser specific. -->
<canvas spellcheck="true">Fallback Text</canvas> 
translateSpecifies an inline style for the element allowing you to apply the style to the content.

<!-- Valid values yes and no. -->
<!-- Default yes. -->
<canvas translate="no">Fallback Text</canvas> 
Miscellaneous
contenteditableSpecifies whether the content of the element is editable.

<!-- Valid values true and false. -->
<!-- Default inherited. -->
<canvas contenteditable="true">Fallback Text</canvas> 
draggableSpecifies whether the element is draggable.

<!-- Valid values true and false. -->
<!-- Default browser specific. -->
<canvas draggable="true">Fallback Text</canvas>
hiddenSpecifies whether the element is not yet, or no longer, relevant.

<!-- Valid values an empty string or hidden -->
<canvas hidden>Fallback Text</canvas> 
<canvas hidden="hidden">Fallback Text</canvas> 
<canvas> Event Attributes
Attribute Description Example
Document Element
oncopyThe script to be run when the user copies the content of an element.

 <!-- Executes go() function -->
<canvas oncopy="go()">Fallback Text</canvas> 
oncutThe script to be run when the user cuts the content of an element.

 <!-- Executes go() function -->
<canvas oncut="go()">Fallback Text</canvas>
onpasteThe script to be run when the user pastes some content into an element.

 <!-- Executes go() function -->
<canvas onpaste="go()">Fallback Text</canvas>
Drag and Drop
ondragThe script to be run when an element is dragged.

 <!-- Executes go() function -->
<canvas ondrag="go()">Fallback Text</canvas>
ondragendThe script to be run when an element has stopped being dragged.

 <!-- Executes go() function -->
<canvas ondragend="go()">Fallback Text</canvas>
ondragenterThe script to be run when an element has been dragged to a valid drop target.

 <!-- Executes go() function -->
<canvas ondragenter="go()">Fallback Text</canvas>
ondragleaveThe script to be run when an element leaves a valid drop target.

 <!-- Executes go() function -->
<canvas ondragleave="go()">Fallback Text</canvas>
ondragoverThe script to be run when an element is being dragged over a valid drop target.

 <!-- Executes go() function -->
<canvas ondragover="go()">Fallback Text</canvas>
ondragstartThe script to be run at the start of a drag operation.

 <!-- Executes go() function -->
<canvas ondragstart="go()">Fallback Text</canvas>
ondropThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<canvas ondrop="go()">Fallback Text</canvas>
Form
onblurThe script to be run when the element loses focus.

 <!-- Executes go() function -->
<canvas onblur="go()">Fallback Text</canvas>
onchangeThe script to be run when object changed and attempt to leave field.

 <!-- Executes go() function -->
<canvas onchange="go()">Fallback Text</canvas>
oncontextmenuThe script to be run when a context menu is triggered.

 <!-- Executes go() function -->
<canvas oncontextmenu="go()">Fallback Text</canvas>
onfocusThe script to be run when the element gets focus.

 <!-- Executes go() function -->
<canvas onfocus="go()">Fallback Text</canvas>
oninputThe script to be run when an element gets user input.

 <!-- Executes go() function -->
<canvas oninput="go()">Fallback Text</canvas>
oninvalidThe script to be run when an element is invalid.

 <!-- Executes go() function -->
<canvas oninvalid="go()">Fallback Text</canvas>
onresetThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<canvas onreset="go()">Fallback Text</canvas>
onselectThe script to be run when some or all of the contents of an object are selected.

 <!-- Executes go() function -->
<canvas onselect="go()">Fallback Text</canvas>
onsubmitThe script to be run when a form is submitted.

 <!-- Executes go() function -->
<canvas onsubmit="go()">Fallback Text</canvas>
Keyboard
onkeydownThe script to be run when an element is in focus and keyboard key is pressed down.

 <!-- Executes go() function -->
<canvas onkeydown="go()">Fallback Text</canvas>
onkeypressThe script to be run when an element is in focus and keyboard key is pressed down and released.

 <!-- Executes go() function -->
<canvas onkeypress="go()">Fallback Text</canvas>
onkeyupThe script to be run when an element is in focus and keyboard key is released.

 <!-- Executes go() function -->
<canvas onkeyup="go()">Fallback Text</canvas>
Media
onabortThe script code to be run on abort.

 <!-- Executes go() function -->
<canvas onabort="go()">Fallback Text</canvas>
oncanplayThe script to be run when a file has buffered enough so it is ready to start playing.

 <!-- Executes go() function -->
<canvas oncanplay="go()">Fallback Text</canvas>
oncanplaythroughThe script to be run when a file can be played all the way to the end without further need of buffering.

 <!-- Executes go() function -->
<canvas oncanplaythrough="go()">Fallback Text</canvas>
oncuechangeThe script to be run when the cue changes when using the track element.

 <!-- Executes go() function -->
<canvas oncuechange="go()">Fallback Text</canvas>
ondurationchangeThe script to be run when the length of the media is changed.

 <!-- Executes go() function -->
<canvas ondurationchange="go()">Fallback Text</canvas>
onemptiedThe script to be run when a media resource element suddenly becomes empty, usually due to an error.

 <!-- Executes go() function -->
<canvas onemptied="go()">Fallback Text</canvas>
onendedThe script to be run when the media has reach the end.

 <!-- Executes go() function -->
<canvas onended="go()">Fallback Text</canvas>
onloadeddataThe script to be run when media data is loaded and playback can start.

 <!-- Executes go() function -->
<canvas onloadeddata="go()">Fallback Text</canvas>
onloadedmetadataThe script to be run when metadata has been loaded.

 <!-- Executes go() function -->
<canvas onloadedmetadata="go()">Fallback Text</canvas>
onloadstartThe script to be run whenthe media resource has started loading.

 <!-- Executes go() function -->
<canvas onloadstart="go()">Fallback Text</canvas>
onpauseThe script to be run when the media resource has been paused.

 <!-- Executes go() function -->
<canvas onpause="go()">Fallback Text</canvas>
onplayThe script to be run when the media resource starts playback.

 <!-- Executes go() function -->
<canvas onplay="go()">Fallback Text</canvas>
onplayingThe script to be run when when playback has already begun.

 <!-- Executes go() function -->
<canvas onplaying="go()">Fallback Text</canvas>
onprogressThe script to be run when the browser is fetching the media data.

 <!-- Executes go() function -->
<canvas onprogress="go()">Fallback Text</canvas>
onratechangeThe script to be run when the playback rate changes.

 <!-- Executes go() function -->
<canvas onratechange="go()">Fallback Text</canvas>
onseekedThe script to be run when the seeking attribute is set to false indicating that seeking has finished.

 <!-- Executes go() function -->
<canvas onseeked="go()">Fallback Text</canvas>
onseekingThe script to be run when the seeking attribute is set to true indicating that seeking is currently active.

 <!-- Executes go() function -->
<canvas onseeking="go()">Fallback Text</canvas>
onstalledThe script to be run when the browser is unable to continue fetching media data.

 <!-- Executes go() function -->
<canvas onstalled="go()">Fallback Text</canvas>
onsuspendThe script to be run when media data has stopped before being completely loaded.

 <!-- Executes go() function -->
<canvas onsuspend="go()">Fallback Text</canvas>
ontimeupdateThe script to be run when the media resources current playback position has changed.

 <!-- Executes go() function -->
<canvas ontimeupdate="go()">Fallback Text</canvas>
onvolumechangeThe script to be run when the volume has changed or been muted.

 <!-- Executes go() function -->
<canvas onvolumechange="go()">Fallback Text</canvas>
onwaitingThe script to be run when the media resource has paused but is expected to resume.

 <!-- Executes go() function -->
<canvas onwaiting="go()">Fallback Text</canvas>
Mouse
onclickThe script to be run when when a mouse is clicked on an element.

 <!-- Executes go() function -->
<canvas onclick="go()">Fallback Text</canvas>
ondblclickThe script to be run when a mouse is double clicked on an element.

 <!-- Executes go() function -->
<canvas ondblclick="go()">Fallback Text</canvas>
onmousedownThe script to be run when he mouse button is pressed down while the cursor is over an element.

 <!-- Executes go() function -->
<canvas onmousedown="go()">Fallback Text</canvas>
onmousemoveThe script to be run when the mouse button is moved.

 <!-- Executes go() function -->
<canvas onmousemove="go()">Fallback Text</canvas>
onmouseoutThe script to be run when the mouse cursor moves off an element.

 <!-- Executes go() function -->
<canvas onmouseout="go()">Fallback Text</canvas>
onmouseoverThe script to be run when the mouse cursor moves over an element.

 <!-- Executes go() function -->
<canvas onmouseover="go()">Fallback Text</canvas>
onmouseupThe script to be run when the mouse button is released while the cursor is over the element.

 <!-- Executes go() function -->
<canvas onmouseup="go()">Fallback Text</canvas>
onwheelThe script to be run when the mouse wheel rolls up or down over an element.

 <!-- Executes go() function -->
<canvas onwheel="go()">Fallback Text</canvas>
Scroll
onscrollThe script code to be run when the scrollbar of an element is being scrolled.

 <!-- Executes go() function -->
<canvas onscroll="go()">Fallback Text</canvas> 
Window - NONE

Relevant HTML Tutorials

HTML Advanced - Graphics & Visuals