HTML5 Canvas

What is Canvas?

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

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

tags are just graphics containers, you have to use a script to draw the graphics.

You can use Canva to draw paths, boxes, circles, characters and add images in a variety of ways.


Browser support

7.jpg


##Internet Explorer 9+, Firefox, Opera, Chrome, and Safari support the element.

Note: Internet Explorer 8 and earlier The IE version of the browser does not support the element.


Create a canvas (Canvas)

A canvas is placed on the web page is a rectangular box, drawn through the element.

Note: By default, the element has no border and content.

A simple example is as follows:

;

Note: Tags usually need to specify an id attribute (often referenced in scripts), the size of the canvas defined by the width and height attributes.

Tip: You can use multiple elements in an HTML page.

Use thestyle attribute to add borders:

    php.cn 
您的浏览器不支持 HTML5 canvas 标签。

Run the program and try it


Use JavaScript to draw images

The canvas element itself has no drawing capabilities. All drawing work must be done inside JavaScript:

Example

    php.cn 
您的浏览器不支持 HTML5 canvas 标签。

Program running result:

1.jpg

Example analysis:

First, find the element:

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

Then, create the context object:

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

##getContext("2d") object is a built-in HTML5 object that has a variety of drawing paths, rectangles, circles, characters, and added Image method.

The following two lines of code draw a red rectangle:

ctx.fillStyle="#FF0000";

ctx.fillRect(0, 0,150,75);

Set the fillStyle property to a CSS color, gradient, or pattern. The default setting for fillStyle is #000000 (black).

fillRect(x,y,width,height) method defines the current filling method of the rectangle.


Canvas coordinatescanvas is a two-dimensional grid.

The coordinates of the upper left corner of the canvas are (0,0)

The fillRect method above has parameters (0,0,150,75).

Means: Draw a 150x75 rectangle on the canvas, starting from the upper left corner (0,0).

Coordinate example

As shown in the figure below, the X and Y coordinates of the canvas are used to position the painting on the canvas. The positioning coordinates are displayed on the rectangular frame where the mouse moves.


Canvas - Path

To draw a line on the Canvas, we will use the following two methods:

moveTo(x ,y) Define the lineStart coordinate

lineTo(x,y) Define lineEnd coordinate

To draw lines we must use the "ink" method, just like stroke().

Example

Define the starting coordinates ( 0,0), and the end coordinate (200,100). Then use the stroke() method to draw the line:

    php.cn 
您的浏览器不支持 HTML5 canvas 标签。

Program running result:

8.jpg


To draw a circle in canvas, we will use the following method:

##arc(x,y,r,start,stop)

In fact, we use the "ink" method when drawing a circle, such as stroke() or fill().

Example

Use arc() method to draw a circle:

    php.cn 
您的浏览器不支持 HTML5 canvas 标签。

Program running result:

4.jpg


Canvas - Text

Use canvas to draw text. The important properties and methods are as follows:

font- Define font

fillText(text,x,y)- Draw solid text on canvas

strokeText(text,x,y)- Draw hollow text on canvas

Example

    php.cn 
您的浏览器不支持 HTML5 canvas 标签。

Program running result:

8.jpg


Canvas - Gradient

Gradient can be filled in rectangles, circles, lines, text, etc. Various shapes can be customized Define different colors.

There are two different ways to set the Canvas gradient:

createLinearGradient(x,y,x1,y1) - Create a line gradient

createRadialGradient(x,y,r,x1,y1,r1) - Create a radial/circular gradient

When we use gradient objects, we must use two or two Stop color above.

addColorStop()The method specifies the color stop. The parameters are described by coordinates, which can be 0 to 1.

Use gradient, set the value of fillStyle or strokeStyle to the gradient, Then draw a shape, such as a rectangle, text, or a line.


Example

Use createLinearGradient() to create a linear gradient. Fill the rectangle with gradient:

    php.cn 
您的浏览器不支持 HTML5 canvas 标签。

Program running result:

3.jpg


##Example

Use createRadialGradient() to create a radial/circular gradient. Fill the rectangle with gradient:

    php中文网(php.cn) 
您的浏览器不支持 HTML5 canvas 标签。

Program running result:

5.jpg


##Canvas - Image

You can put a picture on the canvas, so you can do some operations on the picture and add some materials you want, such as text.

Place an image on the canvasUse the following method:

drawImage(image,x,y)

Using images

7.jpg

Example

Place an image To the canvas:

    php中文网(php.cn) 

使用图像:

The Scream

画布:

您的浏览器不支持 HTML5 canvas 标签。

Program running result:

1.jpg


# #HTML Canvas Reference Manual

The complete attributes of the tag can be found in the Canvas Reference Manual.


The HTML tag

< ;canvas> The canvas element of HTML5 uses JavaScript to draw images on the web page.
## Tag Description


Continuing Learning
||
php.cn
您的浏览器不支持 HTML5 canvas 标签。
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!