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>
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>
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>
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<img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/194/983a040a3b3ad84ba9d1a2727acb7ea7-3.png?x-oss-process=image/resize,p_40" class="lazy" Data.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>
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 id="canvas-为使用toDataURL-方法-导出在canvas-元素上绘制的图像">canvas2为使用toDataURL()方法,导出在canvas1元素上绘制的图像。</h3> </body> </html>
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!

html5的div元素默认一行不可以放两个。div是一个块级元素,一个元素会独占一行,两个div默认无法在同一行显示;但可以通过给div元素添加“display:inline;”样式,将其转为行内元素,就可以实现多个div在同一行显示了。

html5中列表和表格的区别:1、表格主要是用于显示数据的,而列表主要是用于给数据进行布局;2、表格是使用table标签配合tr、td、th等标签进行定义的,列表是利用li标签配合ol、ul等标签进行定义的。

固定方法:1、使用header标签定义文档头部内容,并添加“position:fixed;top:0;”样式让其固定不动;2、使用footer标签定义尾部内容,并添加“position: fixed;bottom: 0;”样式让其固定不动。

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

html5中不支持的标签有:1、acronym,用于定义首字母缩写,可用abbr替代;2、basefont,可利用css样式替代;3、applet,可用object替代;4、dir,定义目录列表,可用ul替代;5、big,定义大号文本等等。

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

html5是指超文本标记语言(HTML)的第五次重大修改,即第5代HTML。HTML5是Web中核心语言HTML的规范,用户使用任何手段进行网页浏览时看到的内容原本都是HTML格式的,在浏览器中通过一些技术处理将其转换成为了可识别的信息。HTML5由不同的技术构成,其在互联网中得到了非常广泛的应用,提供更多增强网络应用的标准机。

3种取消方法:1、给td元素添加“border:none”无边框样式即可,语法“td{border:none}”。2、给td元素添加“border:0”样式,语法“td{border:0;}”,将td边框的宽度设置为0即可。3、给td元素添加“border:transparent”样式,语法“td{border:transparent;}”,将td边框的颜色设置为透明即可。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
