<canvas>
Tags define graphics, such as charts and other images, which you must draw using scripts.
Draw a red rectangle, a gradient rectangle, a colored rectangle, and some colored text on the canvas (Canvas).
Your browser does not support HTML5 HTML5 You can use it in a variety of ways The numbers in the table indicate support. element < canvas > 4.0 9.0 2.0 3.1 9.0 A canvas is a rectangular frame on a web page, through the Note: by default Note: tags usually need to specify an id attribute (often referenced in scripts) Tip: you can use more than one in the HTML page Use Instance resolution: First, find Then, create the The getContext (“2d”) object is a built-in HTML5 object with a variety of ways to draw paths, rectangles, circles, characters, and add images. The following two lines of code draw a red rectangle: Set up Draw a rectangle of 150x75 on the canvas, starting at the upper left corner Coordinate instance As shown in the following figure, 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 box moved by the mouse. In We have to use the method of “ink” to draw lines, like Define start coordinates Your browser does not support HTML5 In In fact, we use the “ink” method when drawing circles, such as Use Your browser does not support HTML5 Using canvas to draw text, the important properties and methods are as follows: Use Use the “Arial” font to draw a high-30px text (solid) on the canvas: Your browser does not support HTML5 Use Use the “Arial” font to draw a high-30px text (void) on the canvas: Your browser does not support HTML5 Gradients can be filled in rectangles, circles, lines, text, etc., and various shapes can define different colors. There are two different ways to set Canvas gradients: When we use a gradient object, we must use two or more stop colors. Using gradients, settin Use Create a linear gradient. Fill the rectangle with a gradient: Your browser does not support HTML5 Use Create a radial / circular gradient. Fill the rectangle with a gradient: Your browser does not support HTML5 To place an image on the canvas, use the following methods: Place an image on the canvas: Your browser does not support HTML5 The complete attributes of the tag can be found in the Canvas reference manual. Tag Description < canvas > The canvas element of HTML5 uses JavaScript to draw images on a web page. For more information, it is enough to learn the article HTML5 Canvas.
<canvas>
element。 11.33.1. What is canvas? ¶
<canvas>
elements are used for drawing graphics, which are done through scripts (usually JavaScript).
<canvas>
labels are just graphics containers, and you must use scripts to draw graphics.
canvas
draw paths, boxes, circles, characters, and add images. 11.33.2. Browser support ¶
<canvas>
the first browser version number of the element. 11.33.3. Create a canvas ¶
<canvas>
elements to draw.
<canvas>
the element has no borders and content.
<canvas>
a simple example is as follows:<canvas id="myCanvas" width="200" height="100"></canvas>
width
and
height
the size of the canvas defined by the.
<canvas>
element。
style
property to add a border:Example ¶
<canvasid="myCanvas"width="200"height="100"style="border:1px solid
#000000;"></canvas>
11.33.4. Use JavaScript to draw an image ¶
canvas
the element itself does not have the ability to draw. All drawing work must be done within JavaScript:Example ¶
varc=document.getElementById("myCanvas");varctx=c.getContext("2d");ctx.fillStyle="#FF0000";ctx.fillRect(0,0,150,75);
<canvas>
elements:var c=document.getElementById("myCanvas");
context
object:var ctx=c.getContext("2d");
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);
fillStyle
properties can be CSS colors, gradients, or patterns.
fillStyle
the default setting is # 000000 (black).
fillRect(x,y,width,height)
method defines the current filling method of the rectangle. 11.33.5. Canvas coordinates ¶
canvas
is a two-dimensional grid.
canvas
The coordinates of the upper-left corner of the
(0,0)
fillRect
method has parameters
(0,0,150,75)
.
(0,0)
. 11.33.6. Canvas-path ¶
Canvas
to draw lines, we will use the following two methods:
moveTo(
x,y)
define line start coordinates
lineTo(
x,y)
define line end coordinates
stroke()
.Example ¶
(0,0)
, and end coordinates
(200,100)
. And then use the
stroke()
method to draw lines:
<canvas>
element. JavaScript:varc=document.getElementById("myCanvas");varctx=c.getContext("2d");ctx.moveTo(0,0);ctx.lineTo(200,100);ctx.stroke();
canvas
to draw a circle in, we will use the following methods:arc(x,y,r,start,stop)
stroke()
or
fill()
.Example ¶
arc()
method to draw a circle:
<canvas>
element. JavaScript:varc=document.getElementById("myCanvas");varctx=c.getContext("2d");ctx.beginPath();ctx.arc(95,50,40,0,2\*Math.PI);ctx.stroke();
11.33.7. Canvas-text ¶
font
-define fonts
fillText(
text,x,y)
-in
canvas
draw solid text on the
strokeText(
text,x,y)
-in
canvas
draw hollow text on the
fillText()
:Example ¶
<canvas>
element. JavaScript:varc=document.getElementById("myCanvas");varctx=c.getContext("2d");ctx.font="30px
Arial";ctx.fillText("Hello World",10,50);
strokeText()
:Example ¶
<canvas>
element. JavaScript:varc=document.getElementById("myCanvas");varctx=c.getContext("2d");ctx.font="30px
Arial";ctx.strokeText("Hello World",10,50);
11.33.8. Canvas-gradient ¶
createLinearGradient(
x,y,x1,y1)
-create a line gradient
createRadialGradient(
x,y,r,x1,y1,r1)
-create a radial / circular gradient
addColorStop()
the method specifies the color stop, and the parameters are described by coordinates, which can be 0 to 1.
fillStyle
or
strokeStyle
is a gradient, and then draw a shape such as a rectangle, text, or a line.
createLinearGradient()
:Example ¶
<canvas>
element. JavaScript:varc=document.getElementById("myCanvas");varctx=c.getContext("2d");
//Create gradient vargrd=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);
createRadialGradient()
Example ¶
<canvas>
element. JavaScript:varc=document.getElementById("myCanvas");varctx=c.getContext("2d");
//Create gradient vargrd=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);
11.33.9. Canvas-Ima ¶
drawImage(
image,x,y)
Work with images: ¶

Example ¶
<canvas>
element. JavaScript:varc=document.getElementById("myCanvas");varctx=c.getContext("2d");
varimg=document.getElementById("scream");ctx.drawImage(img,10,10);
11.33.10. HTML Canvas reference manual ¶
11.33.11. HTML < canvas > tag ¶