W3Schools.com

HTML5 Canvas


The <canvas> element is used to draw graphics, on the fly, on a web page.

Your browser does not support the <canvas> element.

What is Canvas?

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

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

A canvas is a drawable region defined in HTML code with height and width attributes.

Canvas has several methods for drawing paths, boxes, circles, characters, and adding images.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the <canvas> element.

Note: Internet Explorer 8 and earlier versions, do not support the <canvas> element.


Create a Canvas

A canvas is specified with the <canvas> element.

Specify the id, width, and height of the <canvas> element:

<canvas id="myCanvas" width="200" height="100"></canvas>


Draw With JavaScript

The <canvas> element has no drawing abilities of its own.

All drawing must be done inside a JavaScript:

<script type="text/javascript">
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);
</script>

Try it yourself »

JavaScript uses the id to find the <canvas> element:

var c=document.getElementById("myCanvas");

Then, create a context object:

var ctx=c.getContext("2d");

The getContext("2d") object is a built-in HTML5 object, with many methods to draw paths, boxes, circles, characters, images and more.

The next two lines draws a red rectangle:

ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);

The fillStyle attribute makes it red, and the fillRect attribute specifies the shape, position, and size.


Understanding Coordinates

The fillRect property above had the parameters (0,0,150,75).

This means: Draw a 150x75 rectangle on the canvas, starting at the top left corner (0,0).

The canvas' X and Y coordinates are used to position drawings on the canvas.

Mouse over the rectangle below to see the coordinates:

X
Y

More Canvas Examples

Below are more examples of drawing on the <canvas> element:

Example - Line

Draw a line by specifying where to start, and where to stop:

Your browser does not support the <canvas> element.

Try it yourself »

Example - Circle

Draw a circle by specifying the size, color, and position:

Your browser does not support the <canvas> element.

Try it yourself »

Example - Gradient

Draw a gradient background with the colors you specify:

Your browser does not support the <canvas> element.

Try it yourself »

Example - Image

Put an image on the canvas:


Try it yourself »


HTML5 <canvas> Tag

Tag Description
<canvas> Used to draw graphics, on the fly, via scripting (usually JavaScript)

Complete Canvas 2d Reference

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.




W3Schools Certification

W3Schools' Online Certification

The perfect solution for professionals who need to balance work, family, and career building.

More than 10 000 certificates already issued!

Get Your Certificate »

The HTML Certificate documents your knowledge of HTML.

The CSS Certificate documents your knowledge of advanced CSS.

The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.

The jQuery Certificate documents your knowledge of jQuery.

The XML Certificate documents your knowledge of XML, XML DOM and XSLT.

The ASP Certificate documents your knowledge of ASP, SQL, and ADO.

The PHP Certificate documents your knowledge of PHP and SQL (MySQL).

WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$3.98 Unlimited Hosting
Premium Website Design
WEB BUILDING
Download XML Editor
FREE Website BUILDER
Best Website Templates Top CSS Templates
CREATE HTML Websites
EASY WEBSITE BUILDER
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