The canvas element is used to draw graphics on a web page.
The HTML5 canvas element uses JavaScript to draw graphics on a web page.
A canvas is a rectangular area, and you control every pixel of it.
The canvas element has several methods for drawing paths, boxes, circles, characters, and adding images.
Add a canvas element to the HTML5 page.
Specify the id, width, and height of the element:
The canvas element has no drawing abilities of its own. All drawing must be done inside a JavaScript:
JavaScript uses the id to find the canvas element:
Then, create a context object:
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:
The fillStyle method makes it red, and the fillRect method specifies the shape, position, and size.
The fillRect method 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:
Below are more examples of drawing on the canvas element:
Tag | Description |
---|---|
<canvas> | Defines graphics |
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.
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 |
---|