<div align="center"><img id="_aimg_28844" aid="28844" zoomfile="data/attachment/forum/201507/23/101516lmddi4216rze6r1d.jpg" src="data/attachment/forum/201507/23/101516lmddi4216rze6r1d.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" style="max-width:90%" alt="1.jpg" title="1.jpg" w="640"></div> <br> <font size="3"> 一条HTML5 canvas路径是通过绘制指令来连接一系列的点,由这一系列的点构成直线或曲线。路径可以用于在HTML5 canvas上绘制各种类型的图形:直线、圆形、多边形等等。路径的绘制是canvas的核心,必须很好的理解和掌握。</font><br> <br> <font size="3"> <strong>开始和关闭一条路径</strong></font><br> <br> <font size="3"> 要开始和关闭一条路径可以使用2D上下文的beginPath()和closePath()函数。例如下面的例子:</font><br> <div class="blockcode"> <div id="code_j1H"><ol> <li>var canvas = document.getElementById("ex1");<br> </li> <li>var context = canvas.getContext("2d");<br> </li> <li> <br> </li> <li>context.beginPath();<br> </li> <li> <br> </li> <li>//... 绘制路径<br> </li> <li> <br> </li> <li>context.closePath(); </li> </ol></div> <em onclick="copycode($('code_j1H'));">复制代码</em> </div> <br> <font size="3"> <strong>moveTo()函数</strong></font><br> <br> <font size="3"> 当你在canvas中绘制一条路径的时候,你可以想象自己正在使用一支“虚拟笔”。这支虚拟笔总是位于某个位置,你可以使用2D上下文的moveTo(x, y)函数来移动这支虚拟笔。例如下面的代码:</font><br> <div class="blockcode"> <div id="code_l7t"><ol><li>context.moveTo(10,10); </li></ol></div> <em onclick="copycode($('code_l7t'));">复制代码</em> </div> <br> <font size="3"> 这个例子将“虚拟笔”移动到(10,10)这个坐标点上。</font><br> <br> <font size="3"> <strong>lineTo()函数</strong></font><br> <br> <font size="3"> lineTo函数用于从虚拟笔的当前位置绘制一条直线到lineTo()函数中指定的点。下面是一个例子:</font><br> <div class="blockcode"> <div id="code_zSH"><ol> <li>context.beginPath();<br> </li> <li> <br> </li> <li>context.moveTo(10,10);<br> </li> <li>context.lineTo(50,50);<br> </li> <li> <br> </li> <li>context.closePath(); </li> </ol></div> <em onclick="copycode($('code_zSH'));">复制代码</em> </div> <br> <font size="3"> 这个例子中首先移动虚拟笔到(10,10)坐标点位置,然后从这个点绘制一条直线到(50,50)坐标点。</font><br> <br> <font size="3"> lineTo()函数还会将虚拟笔移动到执行的结束点位置。上面的例子中是移动到(50,50)的位置。</font><br> <br> <font size="3"> <strong>stroke()函数 + fill()函数</strong></font><br> <br> <font size="3"> 在你没有通知2D上下文绘制路径之前,实际是不会在画布上绘制任何东西的。你可以通过stroke()或fill()函数来通知2D上下文。</font><br> <br> <font size="3"> stroke()函数用于路径操作指定的图形的外轮廓。</font><br> <br> <font size="3"> fill()函数用于填充有路径操作指定的图形。</font><br> <br> <font size="3"> 下面的例子展示了stroke()或fill()函数的用法。</font><br> <div class="blockcode"> <div id="code_lDe"><ol> <li>context.beginPath();<br> </li> <li>context.moveTo(10,10);<br> </li> <li>context.lineTo(60,50);<br> </li> <li>context.lineTo(110,50);<br> </li> <li>context.lineTo(10,10);<br> </li> <li>context.stroke();<br> </li> <li>context.closePath();<br> </li> <li> <br> </li> <li>context.beginPath();<br> </li> <li>context.moveTo(100,10);<br> </li> <li>context.lineTo(150,50);<br> </li> <li>context.lineTo(200,50);<br> </li> <li>context.lineTo(100,10);<br> </li> <li>context.fill();<br> </li> <li>context.closePath(); </li> </ol></div> <em onclick="copycode($('code_lDe'));">复制代码</em> </div> <br> <font size="3"> 上面代码的返回结果如下:</font><br> <br> <div align="center"><img id="_aimg_28845" aid="28845" zoomfile="data/attachment/forum/201507/23/101720h8i81gzp8d4zmdz6.jpg" src="data/attachment/forum/201507/23/101720h8i81gzp8d4zmdz6.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" style="max-width:90%" alt="2.jpg" title="2.jpg" w="500"></div> <br> <font size="3"> <strong>线条的宽度</strong></font><br> <br> <font size="3"> 你可以使用2D上下文的lineWidth属性来设置绘制线条的宽度。下面是一个例子:</font><br> <div class="blockcode"> <div id="code_jCZ"><ol><li>context.lineWidth = 10; </li></ol></div> <em onclick="copycode($('code_jCZ'));">复制代码</em> </div> <br> <font size="3"> 上面的例子设置绘制线条的宽度为10像素。</font><br> <br> <font size="3"> 下面的例子绘制3条直线,它们的宽度分别为1,5和10。</font><br> <br> <div align="center"><img id="_aimg_28846" aid="28846" zoomfile="data/attachment/forum/201507/23/101757l848ymu153qe9981.jpg" src="data/attachment/forum/201507/23/101757l848ymu153qe9981.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" style="max-width:90%" alt="3.jpg" title="3.jpg" w="500"></div> <br> <font size="3"> 当你绘制的线条宽度大于1的时候,扩展的线条宽度将平均分配在线条中心线的两侧。距离来说,如果你从(10,10)这个点绘制一条直线到(100,10)这个点,线条的宽度为10,那么,实际上是从(10,5)这个点开始绘制,然后扩展到(10,15)这个点,在水平绘制到(100,5)和(100,15)这两个点,就像是绘制一个矩形。</font><br> <br> <font size="3"> <strong>线条的线条(LINE CAP)</strong></font><br> <br> <font size="3"> 在使用路径来绘制线条的时候,你可以设置线条的线头样式。线头的样式通过2D上下文的lineCap属性来设置。它有三个可选值:</font><font size="3"><br> </font><br> <ul> <li><font size="3"> butt</font></li> <li><font size="3"> round</font></li> <li> <font size="3"> square</font><br> </li> </ul> <br> <font size="3"> butt样式的线头是扁平且和线正交的样式。</font><br> <br> <font size="3"> round样式的线头是一个圆角的线头,圆的半径等于线条宽度的一半。</font><br> <br> <font size="3"> square样式的线头会在线的末端绘制一个矩形。矩形的大小为:线条的宽度 X 线条的宽/2。</font><br> <br> <font size="3"> 下面是几个不同线头样式的线条的例子。所有的线条的宽度都是10。最总版的一组线条的lineCap的取值为butt,中间的一组线条的lineCap的取值为round,最右边的一组线条的lineCap的取值为square。</font><br> <br> <div align="center"><img id="_aimg_28847" aid="28847" zoomfile="data/attachment/forum/201507/23/101757larrafruvqrurc55.jpg" src="data/attachment/forum/201507/23/101757larrafruvqrurc55.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" style="max-width:90%" alt="4.jpg" title="4.jpg" w="500"></div> <br> <font size="3"> lineCap的取值butt和square非常相似。有时难以区别。这里制作了几个小例子,从这些例子中你可以看出它们之间的微小差别。下面又三组线条,每一组左边(或上边)的线条的lineCap属性取值为butt,右边(或下边)的线条的lineCap属性取值为square。</font><br> <br> <div align="center"><img id="_aimg_28848" aid="28848" zoomfile="data/attachment/forum/201507/23/101758p4h7cwazccktutak.jpg" src="data/attachment/forum/201507/23/101758p4h7cwazccktutak.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" style="max-width:90%" alt="5.jpg" title="5.jpg" w="500"></div> <br> <font size="3"> 正如上面的结果所示,square线头的线条要比butt线头的线条要长。</font><br> <br> <font size="3"> <strong>线条的连接</strong></font><br> <br> <font size="3"> 2D上下文的lineJoin属性用于定义两条线条连接处的点如何绘制。两条线条连接处的点被称为“连接点”。lineJoin属性有下面的三种取值:</font><br> <br> <ul> <li><font size="3"> miter</font></li> <li><font size="3"> bevel</font></li> <li> <font size="3"> round</font><br> </li> </ul> <br> <font size="3"> 下面是这三种取值的示例代码:</font><br> <div class="blockcode"> <div id="code_GYs"><ol> <li>context.lineJoin = "miter";<br> </li> <li>context.lineJoin = "bevel";<br> </li> <li>context.lineJoin = "round"; </li> </ol></div> <em onclick="copycode($('code_GYs'));">复制代码</em> </div> <br> <font size="3"> miter的连接点是一个三角形的连接点。</font><font size="3"><br> </font><br> <font size="3"> bevel的连接点是一个平头的连接点。</font><br> <br> <font size="3"> round的连接点是一个圆角的连接点。</font><br> <br> <font size="3"> 下面分别是三种线条连接点的例子,从左到右的lineJoin属性分别是:miter,bevel和round。</font><br> <br> <div align="center"><img id="_aimg_28849" aid="28849" zoomfile="data/attachment/forum/201507/23/102023glie1ki36mm44wzi.jpg" src="data/attachment/forum/201507/23/102023glie1ki36mm44wzi.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" style="max-width:90%" alt="6.jpg" title="6.jpg" w="500"></div> <br> <font size="3"> <strong>绘制曲线</strong></font><br> <br> <font size="3"> 2D上下文的arc()函数可以用于绘制一条曲线。arc()函数有6个参数:</font><br> <br> <ul> <li><font size="3"> x:圆弧的中心点的X坐标位置。</font></li> <li><font size="3"> y:圆弧的中心点的Y坐标位置。</font></li> <li><font size="3"> radius:圆弧的半径。</font></li> <li><font size="3"> startAngle:圆弧开始的角弧度。</font></li> <li><font size="3"> endAngle:圆弧结束的角弧度。</font></li> <li> <font size="3"> anticlockwise:设置是以顺时针还是逆时针绘制圆弧,false为顺时针。</font><br> </li> </ul> <br> <font size="3"> 下面是一段示例代码:</font><br> <div class="blockcode"> <div id="code_Src"><ol> <li>context.lineWidth = 3;<br> </li> <li> <br> </li> <li>var x = 50;<br> </li> <li>var y = 50;<br> </li> <li>var radius = 25;<br> </li> <li>var startAngle = (Math.PI / 180) * 45;<br> </li> <li>var endAngle = (Math.PI / 180) * 180;<br> </li> <li>var anticlockwise = false;<br> </li> <li> <br> </li> <li>context.beginPath();<br> </li> <li>context.arc(x, y, radius, startAngle, endAngle, anticlockwise);<br> </li> <li>context.stroke();<br> </li> <li>context.closePath(); </li> </ol></div> <em onclick="copycode($('code_Src'));">复制代码</em> </div> <br> <font size="3"> 上面的代码绘制了一条弧线,它的中心点位于(50,50)坐标点,半径为25,从45度开始到180度结束。</font><br> <br> <font size="3"> 下面是上面代码的返回结果:</font><br> <br> <div align="center"><img id="_aimg_28850" aid="28850" zoomfile="data/attachment/forum/201507/23/102024pw66tl6rbtn6fnl8.jpg" src="data/attachment/forum/201507/23/102024pw66tl6rbtn6fnl8.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" style="max-width:90%" alt="7.jpg" title="7.jpg" w="500"></div> <br> <font size="3"> 上面的例子如果将anticlockwise设置为true,会得到下面的结果:</font><br> <br> <div align="center"><img id="_aimg_28851" aid="28851" zoomfile="data/attachment/forum/201507/23/102024rio12a8esaa4oa82.jpg" src="data/attachment/forum/201507/23/102024rio12a8esaa4oa82.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" style="max-width:90%" alt="8.jpg" title="8.jpg" w="500"></div> <br> <font size="3"> 如果你要花一个完整的圆,可以简单的设置startAngle为0,endAngle设置为2 * Math.PI,它相当于(Math.PI / 180) * 360。</font><br> <br> <font size="3"> <strong>arcTo()函数</strong></font><br> <br> <font size="3"> 2D上下文中有一个arcTo()函数,它用于从当前的点绘制一条曲线到参数指定的点,曲线的半径也由参数指定。它的语法为:arcTo(x1, y1, x2, y2, radius)。注意:参数中x1, y1, x2, y2指的是这个点的控制点。arcTo()函数可以使用lineTo()和arc函数来模仿。</font><br> <br> <font size="3"> <strong>quadraticCurveTo()函数</strong></font><br> <br> <font size="3"> quadraticCurveTo()函数用于绘制一条二次贝兹曲线。这条曲线由一个控制点来控制,它的语法为:quadraticCurveTo(cp1x, cp1y, x, y)。下面是一个示例代码:</font><br> <div class="blockcode"> <div id="code_uU9"><ol> <li>context.lineWidth = 3;<br> </li> <li> <br> </li> <li>var fromX = 50;<br> </li> <li>var fromY = 50;<br> </li> <li>var toX = 100;<br> </li> <li>var toY = 50;<br> </li> <li>var cpX = 75;<br> </li> <li>var cpY = 100;<br> </li> <li> <br> </li> <li>context.beginPath();<br> </li> <li>context.moveTo(fromX, fromY);<br> </li> <li>context.quadraticCurveTo(cpX, cpY, toX, toY);<br> </li> <li>context.stroke();<br> </li> <li>context.closePath(); </li> </ol></div> <em onclick="copycode($('code_uU9'));">复制代码</em> </div> <br> <font size="3"> 上面的代码绘制一条从(50,50)开始到(100,50)的二次贝兹曲线,这条曲线的控制点为(75,100)。得到的结果如下所示:</font><br> <br> <div align="center"><img id="_aimg_28852" aid="28852" zoomfile="data/attachment/forum/201507/23/102025dooxkdd7k0hhwxxh.jpg" src="data/attachment/forum/201507/23/102025dooxkdd7k0hhwxxh.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" style="max-width:90%" alt="9.jpg" title="9.jpg" w="500"></div> <br> <font size="3"> 仔细看,在曲线下方有一个小点,那是这条曲线的控制点。(这个点是专门绘制出来让大家看的)</font><br> <font size="3"><br> </font><br> <font size="3"> 关于控制点,可以参考下面的图像:</font><br> <font size="3"><br> </font><br> <div align="center"><img id="_aimg_28853" aid="28853" zoomfile="data/attachment/forum/201507/23/102025fbadj5jay75ehb8e.jpg" src="data/attachment/forum/201507/23/102025fbadj5jay75ehb8e.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" style="max-width:90%" alt="10.jpg" title="10.jpg" w="190"></div> <br> <font size="3"> <strong>bezierCurveTo()函数</strong></font><br> <br> <font size="3"> bezierCurveTo()函数用于从一个点到另一个点绘制一条三次贝兹曲线。三次贝兹曲线有两个控制点,而二次贝兹曲线只有一个控制点。它的语法为:bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)。下面是一个例子:</font><br> <div class="blockcode"> <div id="code_d7E"><ol> <li>context.lineWidth = 3;<br> </li> <li> <br> </li> <li>var fromX = 50;<br> </li> <li>var fromY = 50;<br> </li> <li>var toX = 300;<br> </li> <li>var toY = 50;<br> </li> <li>var cp1X = 100;<br> </li> <li>var cp1Y = 100;<br> </li> <li>var cp2X = 250;<br> </li> <li>var cp2Y = 100;<br> </li> <li> <br> </li> <li>context.beginPath();<br> </li> <li>context.moveTo(fromX, fromY);<br> </li> <li>context.bezierCurveTo(cp1X, cp1Y, cp2X, cp2Y, toX, toY);<br> </li> <li>context.stroke();<br> </li> <li>context.closePath(); </li> </ol></div> <em onclick="copycode($('code_d7E'));">复制代码</em> </div> <br> <font size="3"> 这段代码从(50,50)绘制一条三次贝兹曲线到(300,50),两个控制点分别为:(100,100)和(250,100)。得到的结果如下:</font><br> <br> <div align="center"><img id="_aimg_28856" aid="28856" zoomfile="data/attachment/forum/201507/23/103753pr9errg9mmr4y7ln.jpg" src="data/attachment/forum/201507/23/103753pr9errg9mmr4y7ln.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" style="max-width:90%" alt="0.jpg" title="0.jpg" w="500"></div> <br> <font size="3"> 曲线下方的两个小圆点是两个控制点,他们并不是曲线的一部分。</font><br> <br> <font size="3"> 下面是一个不同的例子,它的开始点和结束点于上面的例子相同,但是控制点和上面的例子不相同。</font><br> <div class="blockcode"> <div id="code_r1g"><ol> <li>context.lineWidth = 3;<br> </li> <li> <br> </li> <li>var fromX = 50;<br> </li> <li>var fromY = 50;<br> </li> <li>var toX = 300;<br> </li> <li>var toY = 50;<br> </li> <li>var cp1X = 100;<br> </li> <li>var cp1Y = 10;<br> </li> <li>var cp2X = 250;<br> </li> <li>var cp2Y = 100;<br> </li> <li> <br> </li> <li>context.beginPath();<br> </li> <li>context.moveTo(fromX, fromY);<br> </li> <li>context.bezierCurveTo(cp1X, cp1Y, cp2X, cp2Y, toX, toY);<br> </li> <li>context.stroke();<br> </li> <li>context.closePath(); </li> </ol></div> <em onclick="copycode($('code_r1g'));">复制代码</em> </div> <br> <font size="3"> 上面的代码得到的结果如下:</font><font size="3"><br> </font><br> <div align="center"><img id="_aimg_28854" aid="28854" zoomfile="data/attachment/forum/201507/23/102426oitmfmuco0yvuucr.jpg" src="data/attachment/forum/201507/23/102426oitmfmuco0yvuucr.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" style="max-width:90%" alt="11.jpg" title="11.jpg" w="500"></div> <br> <font size="3"> </font><font size="3">文章的最后,我们引用MDN上的一个例子,它用Canvas路径绘制出“吃豆人”游戏的一个小场景:</font><br> <br> <div align="center"><img id="_aimg_28855" aid="28855" zoomfile="data/attachment/forum/201507/23/102426t1lwi9it2a5lztw2.jpg" src="data/attachment/forum/201507/23/102426t1lwi9it2a5lztw2.jpg" class="zoom" onclick="zoom(this, this.src, 0, 0, 0)" style="max-width:90%" alt="12.jpg" title="12.jpg" w="150"></div> <br> <font size="3"> 实现的代码如下:</font><br> <div class="blockcode"> <div id="code_dzo"><ol> <li>function draw() {<br> </li> <li> var canvas = document.getElementById('canvas');<br> </li> <li> if (canvas.getContext){<br> </li> <li> var ctx = canvas.getContext('2d');<br> </li> <li> <br> </li> <li> roundedRect(ctx,12,12,150,150,15);<br> </li> <li> roundedRect(ctx,19,19,150,150,9);<br> </li> <li> roundedRect(ctx,53,53,49,33,10);<br> </li> <li> roundedRect(ctx,53,119,49,16,6);<br> </li> <li> roundedRect(ctx,135,53,49,33,10);<br> </li> <li> roundedRect(ctx,135,119,25,49,10);<br> </li> <li> <br> </li> <li> ctx.beginPath();<br> </li> <li> ctx.arc(37,37,13,Math.PI/7,-Math.PI/7,false);<br> </li> <li> ctx.lineTo(31,37);<br> </li> <li> ctx.fill();<br> </li> <li> <br> </li> <li> for(var i=0;i </li> <li> ctx.fillRect(51+i*16,35,4,4);<br> </li> <li> }<br> </li> <li> <br> </li> <li> for(i=0;i </li> <li> ctx.fillRect(115,51+i*16,4,4);<br> </li> <li> }<br> </li> <li> <br> </li> <li> for(i=0;i </li> <li> ctx.fillRect(51+i*16,99,4,4);<br> </li> <li> }<br> </li> <li> <br> </li> <li> ctx.beginPath();<br> </li> <li> ctx.moveTo(83,116);<br> </li> <li> ctx.lineTo(83,102);<br> </li> <li> ctx.bezierCurveTo(83,94,89,88,97,88);<br> </li> <li> ctx.bezierCurveTo(105,88,111,94,111,102);<br> </li> <li> ctx.lineTo(111,116);<br> </li> <li> ctx.lineTo(106.333,111.333);<br> </li> <li> ctx.lineTo(101.666,116);<br> </li> <li> ctx.lineTo(97,111.333);<br> </li> <li> ctx.lineTo(92.333,116);<br> </li> <li> ctx.lineTo(87.666,111.333);<br> </li> <li> ctx.lineTo(83,116);<br> </li> <li> ctx.fill();<br> </li> <li> <br> </li> <li> ctx.fillStyle = "white";<br> </li> <li> ctx.beginPath();<br> </li> <li> ctx.moveTo(91,96);<br> </li> <li> ctx.bezierCurveTo(88,96,87,99,87,101);<br> </li> <li> ctx.bezierCurveTo(87,103,88,106,91,106);<br> </li> <li> ctx.bezierCurveTo(94,106,95,103,95,101);<br> </li> <li> ctx.bezierCurveTo(95,99,94,96,91,96);<br> </li> <li> ctx.moveTo(103,96);<br> </li> <li> ctx.bezierCurveTo(100,96,99,99,99,101);<br> </li> <li> ctx.bezierCurveTo(99,103,100,106,103,106);<br> </li> <li> ctx.bezierCurveTo(106,106,107,103,107,101);<br> </li> <li> ctx.bezierCurveTo(107,99,106,96,103,96);<br> </li> <li> ctx.fill();<br> </li> <li> <br> </li> <li> ctx.fillStyle = "black";<br> </li> <li> ctx.beginPath();<br> </li> <li> ctx.arc(101,102,2,0,Math.PI*2,true);<br> </li> <li> ctx.fill();<br> </li> <li> <br> </li> <li> ctx.beginPath();<br> </li> <li> ctx.arc(89,102,2,0,Math.PI*2,true);<br> </li> <li> ctx.fill();<br> </li> <li> }<br> </li> <li>}<br> </li> <li> <br> </li> <li>// A utility function to draw a rectangle with rounded corners.<br> </li> <li> <br> </li> <li>function roundedRect(ctx,x,y,width,height,radius){<br> </li> <li> ctx.beginPath();<br> </li> <li> ctx.moveTo(x,y+radius);<br> </li> <li> ctx.lineTo(x,y+height-radius);<br> </li> <li> ctx.quadraticCurveTo(x,y+height,x+radius,y+height);<br> </li> <li> ctx.lineTo(x+width-radius,y+height);<br> </li> <li> ctx.quadraticCurveTo(x+width,y+height,x+width,y+height-radius);<br> </li> <li> ctx.lineTo(x+width,y+radius);<br> </li> <li> ctx.quadraticCurveTo(x+width,y,x+width-radius,y);<br> </li> <li> ctx.lineTo(x+radius,y);<br> </li> <li> ctx.quadraticCurveTo(x,y,x,y+radius);<br> </li> <li> ctx.stroke();<br> </li> <li>} </li> </ol></div> <em onclick="copycode($('code_dzo'));">复制代码</em> </div> <br> <font size="3"> </font><font size="2"><font color="#808080">本文版权属于jQuery之家,转载请注明出处:<a href="http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/201507122217.html" target="_blank">http://www.htmleaf.com/ziliaoku/ ... g/201507122217.html</a></font></font><br> <br> <br>