W3Schools.com

HTML5 <canvas> Tag

Example

Draw a red square, on the fly, and show it inside the <canvas> element:

<canvas id="myCanvas"></canvas>

<script type="text/javascript">
var canvas=document.getElementById('myCanvas');
var ctx=canvas.getContext('2d');
ctx.fillStyle='#FF0000';
ctx.fillRect(0,0,80,100);
</script>

Try it yourself »

Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The <canvas> tag is supported in all major browsers.


Definition and Usage

The <canvas> tag is used to draw graphics, on the fly, via scripting (usually JavaScript).

The <canvas> tag is only a container for graphics, you must use a script to actually draw the graphics.


Differences Between HTML 4.01 and HTML5

The <canvas> tag is new in HTML5.


Tips and Notes

Note: Any text inside the <canvas> element will be displayed in browsers that does not support <canvas>.

Tip:For a complete reference of all the attributes and methods that can be used with the canvas object, go to our complete canvas 2d reference.


Attributes

New : New in HTML5.

Attribute Value Description
heightNew pixels Specifies the height of the canvas
widthNew pixels Specifies the width of the canvas

Global Attributes

The <canvas> tag also supports the Global Attributes in HTML5.


Event Attributes

The <canvas> tag also supports the Event Attributes in HTML5.


WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$7.95/mo SEO Hosting
Premium Website Design
WEB BUILDING
Download XML Editor
FREE Website BUILDER
Free Website Templates Free CSS Templates
Make Your Own Website
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE