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!

Required Attributes

NONE

Optional Attributes

height specifies the height of the canvas in pixels - default value is 150.


<!-- Non-negative integer -->
<canvas height="300"></canvas>

width specifies the width of the canvas in pixels - default value is 300.


<!-- Non-negative integer -->
<canvas width="500"></canvas>

Global Attributes

Common

class - specifies a classname for the element allowing you to apply the style of the predefined class to the content.


<canvas class="classname">Fallback text here</canvas>

id - specifies a unique id for the element allowing you to apply the style of the predefined id to the content.


<canvas id="idname">Fallback text here</canvas>

style - specifies an inline style for the element allowing you to apply the style to the content.


<canvas style="color:red;text-align:left">Fallback text here</canvas>

title - specifies extra information about the content.


<canvas title="information about the content">Fallback text here</canvas>

Keyboard

accesskey - specifies a keyboard shortcut to associate with the element.


<canvas accesskey="aKeyValue">Fallback text here</canvas>

tabindex - specifies a tab order for the element.


<canvas tabindex="someNumber">Fallback text here</canvas>

Language

dir - specifies the directional flow of the content.


<canvas dir="ltr">Fallback text here</canvas>   <!-- The text will flow from left to right -->
<canvas dir="rtl">Fallback text here</canvas>   <!-- The text will flow from right to left -->

lang - specifies a language code for the content of the element.


<canvas lang="en">Fallback text here</canvas>

spellcheck - specifies whether the element is to have its spelling checked.


<!-- Valid values are true and  false - default is inherited / browser specific. -->
<canvas spellcheck="true">Fallback text here</canvas>

translate - specifies whether the content of the element can be translated.


<!-- Valid values are yes and  no - default is yes. -->
<canvas translate="no">Fallback text here</canvas> 

Miscellaneous

contenteditable - specifies whether the content of the element is editable.


<!-- Valid values are true and  false - default is inherited. -->
<canvas contenteditable="false">Fallback text here</canvas> 

draggable - specifies whether the the element is draggable.

Only text selections, images, and links can be dragged.


<!-- Valid values are true and  false - default is browser specific. -->
<canvas draggable="true">Fallback text here</canvas>

hidden - specifies whether the element is not yet, or no longer, relevant.


<!-- Valid values are an empty string or hidden -->
<canvas hidden>Fallback text here</canvas> 
<canvas hidden="hidden">Fallback text here</canvas> 

Event Attributes

Document

onabort - The JavaScript code to be run on abort.


<canvas onabort="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onerror - The JavaScript code to be run when an error occurs when the file is being loaded.


<canvas onerror="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onload - The JavaScript code to be run when the document is loaded.


<canvas onload="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onresize - The JavaScript code to be run when the browser window is resized.


<canvas onresize="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onscroll - The JavaScript code to be run when the scrollbar of an element is being scrolled.


<canvas onscroll="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onunload - The JavaScript code to be run once a page has unloaded or the browser window has been closed.


<canvas onunload="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

Document Element

oncopy - The JavaScript code to be run when the user copies the content of an element.


<canvas oncopy="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

oncut - The JavaScript code to be run when the user cuts the content of an element.


<canvas oncut="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onpaste - The JavaScript code to be run when the user pastes some content into an element.


<canvas onpaste="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

Drag and Drop

ondrag - The JavaScript code to be run when an element is dragged.


<canvas ondrag="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

ondragend - The JavaScript code to be run when an element has stopped being dragged.


<canvas ondragend="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

ondragenter - The JavaScript code to be run when an element has been dragged to a valid drop target.


<canvas ondragenter="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

ondragleave - The JavaScript code to be run when an element leaves a valid drop target.


<canvas ondragleave="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

ondragover - The JavaScript code to be run when an element is being dragged over a valid drop target.


<canvas ondragover="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

ondragstart - The JavaScript code to be run at the start of a drag operation.


<canvas ondragstart="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

ondrop - The JavaScript code to be run when dragged element is being dropped.


<canvas ondrop="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

Form

onblur - The JavaScript code to be run on abort.


<canvas onblur="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onchange - The JavaScript code to be run when object changed and attempt to leave field.


<canvas onchange="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

oncontextmenu - The JavaScript code to be run when a context menu is triggered.


<canvas oncontextmenu="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onfocus - The JavaScript code to be run when the element gets focus.


<canvas onfocus="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

oninput - The JavaScript code to be run when an element gets user input.


<canvas oninput="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

oninvalid - The JavaScript code to be run when an element is invalid.


<canvas oninvalid="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onreset - The JavaScript code to be run when the reset button of a form is clicked.


<canvas onreset="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onselect - The JavaScript code to be run when some or all of the contents of an object are selected.


<canvas onselect="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onsubmit - The JavaScript code to be run when a form is submitted.


<canvas onsubmit="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

Keyboard

onkeydown - The JavaScript code to be run when an element is in focus and keyboard key is pressed.


<canvas onkeydown="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onkeypress - The JavaScript code to be run when an element is in focus and keyboard key is pressed down and released.


<canvas onkeypress="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onkeyup - The JavaScript code to be run when an element is in focus and keyboard key is released.


<canvas onkeyup="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

Media

oncanplay - The JavaScript code to be run when a file has buffered enough so it is ready to start playing.


<canvas oncanplay="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

oncanplaythrough - The JavaScript code to be run when a file can be played all the way to the end without further need of buffering.


<canvas oncanplaythrough="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

oncuechange - The JavaScript code to be run when the cue changes when using the track element.


<canvas oncuechange="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

ondurationchange - The JavaScript code to be run when the length of the media is changed.


<canvas ondurationchange="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onemptied - The JavaScript code to be run when a media resource element suddenly becomes empty, usually due to an error.


<canvas onemptied="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onended - The JavaScript code to be run when the media has reach the end.


<canvas onended="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onloadeddata - The JavaScript code to be run when media data is loaded and playback can start.


<canvas onloadeddata="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onloadedmetadata - The JavaScript code to be run when metadata has been loaded.


<canvas onloadedmetadata="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onloadstart - The JavaScript code to be run when the media resource has started loading.


<canvas onloadstart="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onpause - The JavaScript code to be run when the media resource has been paused.


<canvas onpause="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onplay - The JavaScript code to be run when the media resource starts playback.


<canvas onplay="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onplaying - The JavaScript code to be run when playback has already begun.


<canvas onplaying="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onprogress - The JavaScript code to be run when the browser is fetching the media data.


<canvas onprogress="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onratechange - The JavaScript code to be run when the playback rate changes.


<canvas onratechange="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onseeked - The JavaScript code to be run when the seeking attribute is set to false indicating that seeking has finished.


<canvas onseeked="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onseeking - The JavaScript code to be run hen the seeking attribute is set to true indicating that seeking is currently active.


<canvas onseeking="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onstalled - The JavaScript code to be run when the browser is unable to continue fetching media data.


<canvas onstalled="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onsuspend - The JavaScript code to be run when media data has stopped before being completely loaded.


<canvas onsuspend="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

ontimeupdate - The JavaScript code to be run when the media resources current playback position has changed.


<canvas ontimeupdate="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onvolumechange - The JavaScript code to be run when the volume has changed or been muted.


<canvas onvolumechange="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onwaiting - The JavaScript code to be run when the media resource has paused but is expected to resume.


<canvas onwaiting="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

Mouse

onclick - The JavaScript code to be run when a mouse is clicked on the element.


<canvas onclick="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

ondblclick - The JavaScript code to be run when a mouse is double clicked on the element.


<canvas ondblclick="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onmousedown - The JavaScript code to be run when the mouse button is pressed down while the cursor is over the element.


<canvas onmousedown="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onmousemove - The JavaScript code to be run when the mouse button is moved.


<canvas onmousemove="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onmouseout - The JavaScript code to be run when the mouse cursor moves off an element.


<canvas onmouseout="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onmouseover - The JavaScript code to be run when the mouse cursor moves over an element.


<canvas onmouseover="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onmouseup - The JavaScript code to be run when the mouse button is released while the cursor is over the element.


<canvas onmouseup="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

onwheel - The JavaScript code to be run when the mouse wheel rolls up or down over an element.


<canvas onwheel="go()">Fallback text here</canvas>   <!-- Executes the go() function -->

Window

NONE

go to home page Homepage go to top of page Top