Note: HTML <canvas> tag is newly introduced in HTML5.
var canvas = document.getElementById('drawCanvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, 100, 120);
ctx.fillStyle = '#F15A23';
ctx.fillRect(50, 20, 120, 140);
Tag Specific Attributes Support
Following are few canvas tag attributes that are supported by <canvas> tag.
|height||pixel||Represents the height of the canvas|
|width||pixel||Represents the width of the canvas|
Standard Attributes Support
HTML <canvas> tag supports mostly all HTML Attributes. Following are some most common attributes that are used by <canvas> tag. You can get more details about available HTML Attributes here.
|class||user_defined||Represents a style for an element which is used in CSS style sheet|
|id||user_defined||Represents unique id for an element|
|lang||lang_code||Represents the language of the element for it’s content|
|style||css_properties||Represents an inline style for an element for e.g style=”color:red;text-decoration:underline;”|
|title||user_defined||Represents a tooltip information for an elements like img, a, abbr etc|
Event Attributes Support
The “script” is invoked when a certain “event” occurs. Each event handler content attribute deals with a different event. HTML <canvas> tag supports mostly all HTML Event Attributes. Following are some most common event attributes that are used by <canvas> tag. You can get more details about available HTML Event Attributes here.
|onclick||script||Event Fires immediately when mouse click on the specified element|
|ondblclick||script||Event Fires immediately when mouse double-click on the specified element|
|onkeydown||script||Event Fires immediately when user is pressing a down key from keyboard|
|onkeypress||script||Event Fires immediately when user presses a key from keyboard|
|onkeyup||script||Event Fires immediately when user releases a key from keyboard|
|onmousedown||script||Event Fires immediately when mouse pointer gets down from an element|
|onmousemove||script||Event Fires immediately when mouse pointer moves over an element|
|onmouseout||script||Event Fires immediately when mouse pointer gets out from an element|
|onmouseover||script||Event Fires immediately when mouse pointer moves over an element|
|onmouseup||script||Event Fires immediately when a mouse pointer is released over an element|