Home > Web Front-end > H5 Tutorial > body text

Detailed explanation of HTML5 canvas (4)

黄舟
Release: 2017-03-17 15:46:18
Original
2290 people have browsed it

arcTo usage

Usage: arcTo(x1, y1, x2, y2, radius)
Description: The arcTo() method will use the current endpoint, endpoint 1 (x1, y1) and endpoint 2 ( x2, y2), and then draw an arc on a circle that is tangent to both sides of the angle and has a radius of radius. The starting point of the arc is the tangent point between the side where the current endpoint is and the circle. The end point of the arc is the tangent point between the side where endpoint 2 (x2, y2) is and the circle, and the arc drawn is the shortest length between the two tangent points. That arc. In addition, if the current endpoint is not the starting point of the arc, the arcTo() method will also add a straight line segment from the current endpoint to the starting point of the arc.

<!DOCTYPE html>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>arcTo()绘制</title>  
</head>  
<body>  
<canvas id="drawing" width="400" height="300" style="border:#0F0 solid 1px">A Drawing of something</canvas>  
<script>  
var drawing=document.getElementById("drawing");  
//确定浏览器支持<canvas>元素  
if(drawing.getContext){  
//取得绘图上下文对象的引用,“2d”是取得2D上下文对象  
var context=drawing.getContext("2d");
//设置canvas的onmouse事件
//路径指定
context.beginPath();
//单独使用arcTo()方法必须指定绘图开始的基点

context.moveTo(20,20);

//创建两切线之间的弧/曲线
context.arcTo(200,150,20,280,50);
//起始端点(20,20)、端点1(290,150)、端点2(20,280)三点组成的夹角相切并且半径为50的圆弧,并且只保留两切点之间圆弧部分以及起始端点到第一个切点之间部分

context.lineTo(200,280);
//绘图
context.strokeStyle="red";
//指定路径线的粗细
context.lineWidth=3;
//绘制路径
context.stroke();

//绘制文字标注
context.fillText("当前端点(20,20)", 20,20);
context.fillText("端点1(200,150)", 200,150);
context.fillText("端点2(20,280)", 20,280);
context.fillText("直线结束点(200,280)", 200,280);
//绘制辅助线
context.lineWidth = 1;
context.strokeStyle="blue";
context.moveTo(20,20);
context.lineTo(200,150);
context.lineTo(20,280);
context.stroke();
}  
</script>  
</body>  
</html>
Copy after login

Detailed explanation of HTML5 canvas (4)
As shown in the picture above, the current endpoint (20,20), endpoint 1 (200,150), and endpoint 2 (20,280) form an angle to draw an arc. The radius is 50 and tangent to the included angle. If the current endpoint is not the starting point of the arc, the arcTo() method will also add a straight line segment from the current endpoint to the starting point of the arc.
The last straight line is drawn by context.lineTo(200,280); and has nothing to do with the arc drawn by arcTo.

stroke() and fill() usage

stroke(): Draw a defined path
fill(): Fill the current drawing path
I used the following code For the two canvases, the drawing methods are drawCanvas1 and drawCanvas2 respectively, to facilitate comparison of the differences between the two.

<!Doctype html>
<html>
<head>
<title>stroke()、fill()用法</title>
<meta charset="utf-8"/>
<style>
    .myCanvas{        
    border:2px solid #f00;    
    }
    </style>
    <script>
    window.onload = function(){     
        drawConvas1();
        drawConvas2();
    };    function drawConvas1(){
        var canvas = document.getElementById("myCanvas1");        
        var context = canvas.getContext("2d");        
        //1、绘制三解形路径
        context.beginPath();
        context.moveTo(20,20);
        context.lineTo(20,100);
        context.lineTo(100,100);
        context.lineTo(20,20);

        context.strokeStyle = "red";
        context.stroke();   


        //2、绘制矩形80*80
        context.beginPath();
        context.moveTo(20,200);
        context.lineTo(100,200);
        context.lineTo(100,280);
        context.lineTo(20,280);
        context.lineTo(20,200);
        context.stroke();        //绘制坐标点标注,以方便看其原理
        context.fillText("(20,20)",20,20);
        context.fillText("(20,100)",20,100);
        context.fillText("(100,100)",100,100);      
        context.fillText("(20,200)",20,200);
        context.fillText("(100,200)",100,200);
        context.fillText("(100,280)",100,280);
        context.fillText("(20,280)",20,280);
    }   
     function drawConvas2(){
        var canvas = document.getElementById("myCanvas2");        
        var context = canvas.getContext("2d");        
        //1、绘制三解形填充
        context.beginPath();
        context.moveTo(20,20);
        context.lineTo(20,100);
        context.lineTo(100,100);
        context.lineTo(20,20);

        context.fillStyle = "red";
        context.fill(); 

        //2、绘制填充矩形80*80,先绘制路径再用fill()方法填充
        context.beginPath();
        context.moveTo(20,200);
        context.lineTo(100,200);
        context.lineTo(100,280);
        context.lineTo(20,280);
        context.lineTo(20,200);
        context.fill();        //上面的代码相当于
        //context.fillRect(20,200,80,80);

        //3、使用方法fillRect(x,y,width,height)绘制矩形填充
        context.fillRect(120,200,80,80);
    }</script></head><body>
    <p>
        <canvas class="myCanvas" id="myCanvas1" width="400" height="400">
            您的浏览器不支持canvas        
            </canvas>
        <canvas class="myCanvas" id="myCanvas2" width="400" height="400">
            您的浏览器不支持canvas        
            </canvas>
    </p>
    </body>
    </html>
Copy after login

Detailed explanation of HTML5 canvas (4)

createPattern() usage

Syntax: createPattern(image, repetitionStyle)
Description: The createPattern() method is used to create a representation of the image How to repeatedly tile the CanvasPattern object on the current graphic. The parameter image specifies the image used for tiling. This parameter can be an Image object or a Canvas object. The parameter repetitionStyle indicates how the image is repeatedly tiled. Possible values ​​are repeat (repeat tiles in both horizontal and vertical directions, which is also the default value), repeat-x (repeat tiles only in the horizontal direction), repeat- y (repeat tiling only in the vertical direction), no-repeat (tile only once, no repeated tiling).
Look at the example below:

<!Doctype html>
<html>
<head>
<title>createPattern()用法</title>
<meta charset="utf-8"/>
<style>
    .myCanvas{
        border:2px solid #f00;
    }
</style>
<script>
    window.onload = function(){     
        drawConvas1();
        drawConvas2();
    };

    function drawConvas1(){
        var canvas = document.getElementById("myCanvas1");
        var context = canvas.getContext("2d");

        //1、创建一个图片对象
        var img = new Image();
        img.src = "html5.png";
        ///当图片加载完毕后再设置对应的图像平铺模式并填充矩形
        img.onload = function(){
            var pattern = context.createPattern(img, "repeat");
            context.fillStyle = pattern;
            context.fillRect(0,0,200,200);
        }

    }

    function drawConvas2(){
        var canvas = document.getElementById("myCanvas2");
        var context = canvas.getContext("2d");

        //1、创建一个图片对象
        var img = new Image();
        img.src = "html5.png";
        ///当图片加载完毕后再设置对应的图像平铺模式并填充矩形
        img.onload = function(){
            var pattern = context.createPattern(img, "repeat");
            context.fillStyle = pattern;
            context.fillRect(30,30,200,200);
        }
    }
</script>
</head>

<body>
    <div>
        <canvas class="myCanvas" id="myCanvas1" width="400" height="400">
            您的浏览器不支持canvas
        </canvas>
        <canvas class="myCanvas" id="myCanvas2" width="400" height="400">
            您的浏览器不支持canvas
        </canvas>
    </div>
</body>
</html>
Copy after login

Detailed explanation of HTML5 canvas (4)
Note that the difference between the two lies only in the starting point coordinates of the rectangular area. The first is the starting point coordinate of the canvas (0,0), and the second starting point is (30,30). From the effect point of view, the first image of the second picture is not complete.
Note: The reference starting point for the CanvasPattern object to start tiling the image is not the upper left corner of the graphics we are currently drawing, but the upper left corner of the entire canvas canvas. To be precise, the starting point coordinates of the canvas (0,0 ).

createImageData and putImageData

Syntax:

  • createImageData(width,height); Create an ImageData object with width and height respectively. Returns an ImageData object.

  • createImageData(imageData); Create a new ImageData object that is the same as the specified imageData object. Returns an ImageData object.

  • putImageData(imgData,x,y,dirtyX,dirtyY,dirtyWidth,dirtyHeight);
    Function: Put the image data (from the specified ImageData object ) placed back on the canvas.
    imgData: is the ImageData object that needs to be placed on the canvas.
    x: The x coordinate of the upper left corner of the canvas
    y: The y coordinate of the upper left corner of the canvas
    The four parameters dirtyX, dirtyY, dirtyWidth, and dirtyHeight are optional. If there are no four parameters, the imageData will be changed from x , the y coordinate is completely drawn on the canvas; if there are these four parameters, the dirtyX, dirtyY starting coordinates, width dirtyWidth, and height dirtyHeight of the partial image of the imageData are retained.

  • getImageData(x,y,width,height);x,y are the x coordinates of the upper left corner where copying starts, width and height are the width and height of the area to be copied. Returns an ImageData object.

<!Doctype html>
<html>
<head>
<title>putImageData()用法</title>
<meta charset="utf-8"/>
<style>
    .myCanvas{
        border:2px solid #f00;
    }
</style>
<script>
    window.onload = function(){     
        drawConvas1();
        drawConvas2();
    };

    //画布1使用createImageData()、putImageData()画图
    function drawConvas1(){
        var canvas = document.getElementById("myCanvas1");
        var context = canvas.getContext("2d");

        var imgData=context.createImageData(100,100);
        for (var i=0;i<imgData.data.length;i+=4)
        {
            imgData.data[i+0]=255;
            imgData.data[i+1]=0;
            imgData.data[i+2]=0;
            imgData.data[i+3]=255;
        }
        context.putImageData(imgData,10,10);        
    }

    //画布2使用getImageData()、putImageData()画图
    function drawConvas2(){
        //获取canvas1中的ImageData,并绘制在canvas2中
        var canvas1 = document.getElementById("myCanvas1");
        var context1 = canvas1.getContext("2d");        
        var imgData = context1.getImageData(10,10,100,100);

        var canvas2 = document.getElementById("myCanvas2");
        var context2 = canvas2.getContext("2d");
        context2.putImageData(imgData,10,10);       
    }
</script>
</head>

<body>
    <div>
        <canvas class="myCanvas" id="myCanvas1" width="400" height="400">
            您的浏览器不支持canvas
        </canvas>
        <canvas class="myCanvas" id="myCanvas2" width="400" height="400">
            您的浏览器不支持canvas
        </canvas>
    </div>
</body>
</html>
Copy after login

Detailed explanation of HTML5 canvas (4)

toDataURL()

Syntax: canvas.toDataURL("image/png"); Note that the object is canvas instead of context
Function: Export the canvas canvas as a picture and return the picture data.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>canvas绘图</title>
    <script>
    window.onload = function()
    {
        drawCanvas1();
        drawCanvas2();
    }
    function drawCanvas1()
    {
        var canvas=document.getElementById("myCanvas1");
        var context=canvas.getContext("2d");
        //绘制红色矩形
        context.fillStyle="red";
        context.fillRect(10,10,200,200);        
    }

    function drawCanvas2()
    {
        //取得画布1图像的数据URL
        var canvas1 = document.getElementById("myCanvas1");
        var imgURL = canvas1.toDataURL("image/png");

        //把画布1的图像显示在画布2上
        var canvas2=document.getElementById("myCanvas2");
        var context2=canvas2.getContext("2d");

        var image = new Image();
        image.src = imgURL;
        context2.drawImage(image, 10, 10);
    }

    </script>
</head>
<body>
    <canvas id="myCanvas1" width="400" height="400" style="border:#F00 solid 1px">
        您的浏览器不支持canvas
    </canvas>
    <canvas id="myCanvas2" width="400" height="400" style="border:#F00 solid 1px">
        您的浏览器不支持canvas
    </canvas>
    <br />
    <h3>canvas2为使用toDataURL()方法,导出在canvas1元素上绘制的图像。</h3>
</body>
</html>
Copy after login

Detailed explanation of HTML5 canvas (4)
Notice why the rectangular position of canvas two is different from canvas one?
Because toDataURL() exports the entire canvas, and canvas two is drawn starting from (10, 10).

The above is the detailed content of Detailed explanation of HTML5 canvas (4). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template