HTML Canvas Introduction


Canvas

What is Canvas?

Canvas is a medium for oil painting. Canvas is typically stretched across a wooden frame.

On the HTML canvas, you can draw all kinds of graphics, from simple lines, to complex graphic objects:

Your browser does not support the <canvas> element.

What is HTML Canvas?

HTML canvas is about drawing graphics on an HTML canvas.

HTML canvas is a set of JavaScript methods (APIs) for drawing graphics (lines, boxes, circles, shapes).

HTML canvas is a rectangular area on an HTML page, specified with the <canvas> element.

The HTML <canvas> element (introduced in HTML5) is a container for HTML graphics.

Note By default, the <canvas> element has no border and no content.

HTML Canvas Can Draw Text

Canvas can draw colorful text, with or without animation.


HTML Canvas Can Draw Graphics

Canvas has great features for graphical data presentation with an imagery of graphs and charts.


HTML Canvas Can be Animated

Canvas objects can move. Everything is possible: from simple bouncing balls to complex animations.


HTML Canvas Can be Interactive

Canvas is drawn by JavaScript. Because of this, canvas can respond to JavaScript events.

Canvas can listen for, and respond to, any user action (key clicks, mouse clicks, button clicks, finger movement).


HTML Canvas Can Play Games

Canvas methods for animations, offer lots of possibilities for HTML gaming applications.


Canvas Example

In HTML, a <canvas> element looks like this:

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

The <canvas> element must have an id attribute so it can be referred to by JavaScript.

The width and height attribute is necessary to define the size of the canvas.

To add a border, use a style attribute:

Example

<canvas id="myCanvas" width="200" height="100"
style="border:1px solid #000000;">
</canvas>

Try it Yourself »
Note By default, the <canvas> element has no border and no content.

Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

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

Internet explorer 8 and earlier, does not support the <canvas> element.

If an HTML element is not supported by a browser, you can display a message instead.

Any text in an unknown HTML element will always be displayed by the browser.

You can test it yourself if you change <canvas> to something "unknown":

Example

<xcanvas id="myCanvas" width="200" height="100"
style="border:1px solid #000000;">
Your browser does not support the HTML5 canvas tag.
</xcanvas>

Try it Yourself »



Color Picker

colorpicker