HTML5 Canvas
Draw a red rectangle, a gradient rectangle, a colorful rectangle, and some colorful text on the canvas.
What is canvas?
HTML5
You can use canvas in many ways to draw paths, boxes, circles, characters, and add images.
Browser support
The numbers in the table indicate the first browser version that supports the
| Element | |||||
|---|---|---|---|---|---|
| 4.0 | 9.0 | 2.0 | 3.1 | 9.0 |
Creating a Canvas
A canvas is a rectangular area in a web page, drawn through the
Note: By default
Note: The tag usually needs an id attribute specified (often referenced in scripts), and the width and height attributes define the size of the canvas.
**Tip:** You can use multiple
Use the style attribute to add a border:
Example
<
canvas
id
=
"
myCanvas
"
width
=
"
200
"
height
=
"
100
"
style
=
"border:1px solid #000000;
"
canvas
Try it »
Drawing Images with JavaScript
The canvas element itself has no drawing capability. All drawing work must be done inside JavaScript:
Example
var
c
=
document
.
getElementById
(
"
myCanvas
"
)
;
var
ctx
=
c
.
getContext
(
"
2d
"
)
;
ctx
.
fillStyle
=
"
#FF0000
"
;
ctx
.
fillRect
(
0
,
0
,
150
,
75
)
;
Try it »
Example explanation:
First, find the
var c=document.getElementById("myCanvas");
Then, create the context object:
var ctx=c.getContext("2d");
The getContext("2d") object is a built-in HTML5 object with various methods for drawing paths, rectangles, circles, characters, and adding images.
The following two lines of code draw a red rectangle:
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);
The fillStyle property can be set to a CSS color, a gradient, or a pattern. The default setting of fillStyle is #000000 (black).
fillRect(x,y,width,height) method defines the current fill style of the rectangle.
Canvas Coordinates
A canvas is a two-dimensional grid.
The top-left corner of the canvas has coordinates (0,0)
The fillRect method above has the parameters (0,0,150,75).
This means: draw a 150x75 rectangle on the canvas, starting from the top-left corner (0,0).
Coordinate Example
As shown below, the X and Y coordinates of the canvas are used to position drawings on the canvas. When the mouse moves over the rectangle, the positioning coordinates are displayed.
X
Y
Canvas - Paths
To draw lines on a canvas, we will use the following two methods:
- moveTo(x,y) defines the starting coordinates of the line
- lineTo(x,y) defines the ending coordinates of the line
To draw lines we must use "ink" methods, such as stroke().
Example
Define the starting coordinates (0,0) and the ending coordinates (200,100), then use the stroke() method to draw the line:
JavaScript:
var
c
=
document
.
getElementById
(
"
myCanvas
"
)
;
var
ctx
=
c
.
getContext
(
"
2d
"
)
;
ctx
.
moveTo
(
0
,
0
)
;
ctx
.
lineTo
(
200
,
100
)
;
ctx
.
stroke
(
)
;
Try it »
To draw a circle in canvas, we will use the following method:
arc(x,y,r,start,stop)
Actually, when drawing circles we use "ink" methods, such as stroke() or fill().
Example
Use the arc() method to draw a circle:
JavaScript:
var
c
=
document
.
getElementById
(
"
myCanvas
"
)
;
var
ctx
=
c
.
getContext
(
"
2d
"
)
;
ctx
.
beginPath
(
)
;
ctx
.
arc
(
95
,
50
,
40
,
0
,
2
Math
.
PI
)
;
ctx
.
stroke
(
)
;
Try it »
Canvas - Text
When drawing text with canvas, the important properties and methods are as follows:
- font - defines the font
- fillText(text,x,y) - draws filled text on the canvas
- strokeText(text,x,y) - draws hollow text on the canvas
Using fillText():
Example
Draw filled text with a height of 30px on the canvas using the "Arial" font:
JavaScript:
var
c
=
document
.
getElementById
(
"
myCanvas
"
)
;
var
ctx
=
c
.
getContext
(
"
2d
"
)
;
ctx
.
font
=
"
30px Arial
"
;
ctx
.
fillText
(
"
Hello World
"
,
10
,
50
)
;
Try it »
Using strokeText():
Example
Draw hollow text with a height of 30px on the canvas using the "Arial" font:
JavaScript:
var
c
=
document
.
getElementById
(
"
myCanvas
"
)
;
var
ctx
=
c
.
getContext
(
"
2d
"
)
;
ctx
.
font
=
"
30px Arial
"
;
ctx
.
strokeText
(
"
Hello World
"
,
10
,
50
)
;
Try it »
Canvas - Gradients
Gradients can fill rectangles, circles, lines, text, and more; you can define different colors for various shapes.
There are two different ways to set a Canvas gradient:
- createLinearGradient(x,y,x1,y1) - creates a linear gradient
- createRadialGradient(x,y,r,x1,y1,r1) - creates a radial/circular gradient
When using a gradient object, you must use two or more color stops.
The addColorStop() method specifies color stops, with parameters described using coordinates that can range from 0 to 1.
To use a gradient, set the value of fillStyle or strokeStyle to the gradient, then draw a shape such as a rectangle, text, or a line.
Using createLinearGradient():
Example
Create a linear gradient. Use the gradient to fill a rectangle:
JavaScript:
var
c
=
document
.
getElementById
(
"
myCanvas
"
)
;
var
ctx
=
c
.
getContext
(
"
2d
"
)
;
//
Create a gradient
var
grd
=
ctx
.
createLinearGradient
(
0
,
0
,
200
,
0
)
;
grd
.
addColorStop
(
0
,
"
red
"
)
;
grd
.
addColorStop
(
1
,
"
white
"
)
;
//
Fill Gradient
ctx
.
fillStyle
=
grd
;
ctx
.
fillRect
(
10
,
10
,
150
,
80
)
;
Try it »
Using createRadialGradient():
Example
Create a radial/circular gradient. Use the gradient to fill a rectangle:
JavaScript:
var
c
=
document
.
getElementById
(
"
myCanvas
"
)
;
var
ctx
=
c
.
getContext
(
"
2d
"
)
;
//
Create a gradient
var
grd
=
ctx
.
createRadialGradient
(
75
,
50
,
5
,
90
,
60
,
100
)
;
grd
.
addColorStop
(
0
,
"
red
"
)
;
grd
.
addColorStop
(
1
,
"
white
"
)
;
//
Fill Gradient
ctx
.
fillStyle
=
grd
;
ctx
.
fillRect
(
10
,
10
,
150
,
80
)
;
Try it »
Canvas - Images
To place an image on the canvas, use the following method:
- drawImage(image,x,y)
Using an image:

Example
Place an image on the canvas:
JavaScript:
var
c
=
document
.
getElementById
(
"
myCanvas
"
)
;
var
ctx
=
c
.
getContext
(
"
2d
"
)
;
var
img
=
document
.
getElementById
(
"
scream
"
)
;
ctx
.
drawImage
(
img
,
10
,
10
)
;
Try it »
HTML Canvas Reference Manual
The complete attributes of the tag can be found in theCanvas Reference Manual.
HTML tag
| Tag | Description |
|---|---|
| The HTML5 canvas element uses JavaScript to draw images on web pages. |
For more information, refer to:This single article is enough to learn HTML5 Canvas