HTML5 CanvasLOGIN

HTML5 Canvas

<canvas></canvas> is a new tag that appears in HTML5. Like all DOM objects, it has its own properties, methods and events. Among them is the drawing method, which js can call for drawing. .

What is Canvas?

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

<canvas> tag is just Graphics container, you must 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.

Basically all browsers support the <canvas> element except IE8 and previous versions


There are two methods for drawing images using the canvas element. They are

context.fill()//Fill

context.stroke()//Draw the border

style: Before drawing graphics, the drawing style must be set

    context.fillStyle//Filled style

##                 context. strokeStyle//Border style

context.lineWidth//Graphic border width

Color representation:

                                                                                                                                                                                                                                                 Use the color name directly: "red"                 with with "blue" to 1-255,1-255)

rgba(1-255,1-255,1-255, transparency)

Use Canvas Draw a straight line:


<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>php.cn</title>
    </head>
    <style type="text/css">
        canvas{border:dashed 2px #CCC}
    </style>
    <script type="text/javascript">
        function $$(id){
            return document.getElementById(id);
        }
        function pageLoad(){
            var can = $$('can');
            var cans = can.getContext('2d');
            cans.moveTo(20,30);//第一个起点
            cans.lineTo(120,90);//第二个点
            cans.lineTo(220,60);//第三个点(以第二个点为起点)
            cans.lineWidth=3;
            cans.strokeStyle = 'red';
            cans.stroke();
        }
    </script>
    <body onload="pageLoad();">
        <canvas id="can" width="400px" height="300px">4</canvas>
    </body>
</html>

Canvas coordinates

canvas is a two-dimensional grid.

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

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <style type="text/css">
        canvas{border:dashed 2px #CCC}
    </style>
    <script type="text/javascript">
        function $$(id){
            return document.getElementById(id);
        }
        function pageLoad(){
            var can = $$('can');
            var cans = can.getContext('2d');
            cans.fillStyle = 'yellow';
            cans.fillRect(30,30,340,240);
        }
    </script>
    <body onload="pageLoad();">
        <canvas id="can" width="400px" height="300px">4</canvas>
    </body>
</html>

Note: A method is used here - fillRect(). From the literal meaning, you can also know that this is to fill a rectangle. The parameters here are worth explaining fillRect(X,Y,Width,Height) , this is different from the coordinates in mathematics. The X and Y here start from the starting point relative to the upper left corner of the Canvas. Remember! !


##Round

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <style type="text/css">
        canvas{border:dashed 2px #CCC}
    </style>
    <script type="text/javascript">
        function $$(id){
            return document.getElementById(id);
        }
        function pageLoad(){
            var can = $$('can');
            var cans = can.getContext('2d');
            cans.beginPath();
            cans.arc(200,150,100,0,Math.PI*2,false);
            cans.closePath();
            cans.lineWidth = 5;
            cans.strokeStyle = 'red';
            cans.stroke();
        }
    </script>
    <body onload="pageLoad();">
        <canvas id="can" width="400px" height="300px">4</canvas>
    </body>
</html>

Note: The usage of the arc method here is arc(X,Y,Radius,startAngle,endAngle,anticlockwise), which means (X coordinate of the center of the circle, Y coordinate of the center of the circle, radius, start angle (radians), end angle radian, whether according to Draw clockwise);


Canvas - Gradient

Gradient can fill rectangles, circles, Lines, text, etc., various shapes can have different colors defined by themselves.

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 more stop colors.

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

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

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <style type="text/css">
        canvas{border:dashed 2px #CCC}
    </style>
    <script type="text/javascript">
        function $$(id){
            return document.getElementById(id);
        }
        function pageLoad(){
            var can = $$('can');
            var cans = can.getContext('2d');
            var gnt1 = cans.createLinearGradient(10,0,390,0);
            gnt1.addColorStop(0,'red');
            gnt1.addColorStop(0.5,'green');
            gnt1.addColorStop(1,'blue');
            cans.fillStyle = gnt1;
            cans.fillRect(10,10,380,280);
        }
    </script>
    <body onload="pageLoad();">
        <canvas id="can" width="400px" height="300px">4</canvas>
    </body>
</html>

Canvas - Text

text: The text to be drawn

x: The x-coordinate axis of the starting point of the text

y: The y-coordinate axis of the starting point of the text

context.font: Set font style

context.textAlign: Horizontal alignment

start, end, right, center

context.textBaseline: Vertical alignment

top, hanging, middle, alphabetic, ideographic、bottom

<!DOCTYPE html>
<html>
 <head>
    <meta charset="utf-8">
    <title>Canvas</title>
 </head>
 <style type="text/css">
    body{margin:20px auto; padding:0; width:800px; }
    canvas{border:dashed 2px #CCC}
 </style>
 <script type="text/javascript">
    function $$(id){
        return document.getElementById(id);
    }
    function pageLoad(){
        var can = $$('can');
        var cans = can.getContext('2d');
        cans.font = 'bold 144px consolas';
        cans.textAlign = 'left';
        cans.textBaseline = 'top';
        cans.strokeStyle = '#DF5326';
        cans.strokeText('Hello', 100, 100);
        cans.font = 'bold 144px arial';
        cans.fillStyle = 'red';
        cans.fillText('World', 300,300);
    }
    function img_click(){
        var can = $$('can');
        var cans = can.getContext('2d');
        cans.clearRect(0,0,800,600);
    }
 </script>
<body onload="pageLoad();">
    <canvas id="can" width="800px" height="600px" onclick="img_click(this);"></canvas>
</body>
</html>


Next Section
<!doctype html> <html> <head> <meta charset="UTF-8"> </head> <style type="text/css"> canvas{border:dashed 2px #CCC} </style> <script type="text/javascript"> function $$(id){ return document.getElementById(id); } function pageLoad(){ var can = $$('can'); var cans = can.getContext('2d'); cans.moveTo(0,0); cans.lineTo(400,300); var gnt1 = cans.createLinearGradient(0,0,400,300);//线性渐变的起止坐标 gnt1.addColorStop(0,'red');//创建渐变的开始颜色,0表示偏移量,个人理解为直线上的相对位置,最大为1,一个渐变中可以写任意个渐变颜色 gnt1.addColorStop(1,'yellow'); cans.lineWidth=3; cans.strokeStyle = gnt1; cans.stroke(); } </script> <body onload="pageLoad();"> <canvas id="can" width="400px" height="300px">4</canvas> </body> </html>
submitReset Code
ChapterCourseware