Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML5 Canvas

HTML5 Canvas


The tag defines graphics, such as charts and other images; you must use scripts to draw the graphics.

Draw a red rectangle, a gradient rectangle, a colorful rectangle, and some colorful text on the canvas.

Your browser does not support HTML5 element.


What is canvas?

HTML5 The element is used for drawing graphics, which is done through scripts (usually JavaScript).

The tag is only a graphics container; you must use scripts to draw the graphics.

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.

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 element.

Note: By default The element has no border and no content.

A simple example is as follows:

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 element.

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 element:

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:

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:

Your browser does not support HTML5 element.

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:

Your browser does not support HTML5 element.

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:

Using fillText():

Example

Draw filled text with a height of 30px on the canvas using the "Arial" font:

Your browser does not support HTML5 element.

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:

Your browser does not support HTML5 element.

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:

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:

Your browser does not support HTML5 element.

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:

Your browser does not support HTML5 element.

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:

Using an image:

The Scream

Example

Place an image on the canvas:

Your browser does not support HTML5 element.

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