目录

Drawing Lines

线方法

我们需要以下方法在画布上绘制线条 -

Sr.No. 方法和描述
1

beginPath()

此方法重置当前路径。

2

moveTo(x, y)

此方法使用给定点创建新的子路径。

3

closePath()

此方法将当前子路径标记为已关闭,并启动一个新的子路径,其点与新关闭的子路径的开始和结束相同。

4

fill()

此方法使用当前填充样式填充子路径。

5

stroke()

此方法使用当前笔触样式描绘子路径。

6

lineTo(x, y)

此方法将给定点添加到当前子路径,通过直线连接到前一个子路径。

例子 (Example)

以下是利用上述方法绘制三角形的简单示例。

<!DOCTYPE HTML>
<html>
   <head>
      <style>
         #test {
            width: 100px;
            height:100px;
            margin: 0px auto;
         }
      </style>
      <script type = "text/javascript">
         function drawShape() {
            // get the canvas element using the DOM
            var canvas = document.getElementById('mycanvas');
            // Make sure we don't execute when canvas isn't supported
            if (canvas.getContext) {
               // use getContext to use the canvas for drawing
               var ctx = canvas.getContext('2d');
               // Filled triangle
               ctx.beginPath();
               ctx.moveTo(25,25);
               ctx.lineTo(105,25);
               ctx.lineTo(25,105);
               ctx.fill();
               // Stroked triangle
               ctx.beginPath();
               ctx.moveTo(125,125);
               ctx.lineTo(125,45);
               ctx.lineTo(45,125);
               ctx.closePath();
               ctx.stroke();
            } else {
               alert('You need Safari or Firefox 1.5+ to see this demo.');
            }
         }
      </script>
   </head>
   <body id = "test" onload = "drawShape();">
      <canvas id = "mycanvas"></canvas>
   </body>
</html>

以上示例将绘制以下形状 -

新页面打开

线属性

有几个属性允许我们设置线条样式。

S.No. 属性和描述
1

lineWidth [ = value ]

此属性返回当前行宽并可以设置,以更改行宽。

2

lineCap [ = value ]

此属性返回当前线帽样式并可以设置,以更改线帽样式。 可能的线帽样式是butt, round,square

3

lineJoin [ = value ]

此属性返回当前行连接样式并可以设置,以更改行连接样式。 可能的线连接样式是bevel, round,miter

4

miterLimit [ = value ]

此属性返回当前的斜接限制比率并可以设置,以更改斜接限制比率。

例子 (Example)

下面是一个简单的例子,它利用lineWidth属性绘制不同宽度的线条。

<!DOCTYPE HTML>
<html>
   <head>
      <style>
         #test {
            width: 100px;
            height:100px;
            margin: 0px auto;
         }
      </style>
      <script type = "text/javascript">
         function drawShape() {
            // get the canvas element using the DOM
            var canvas = document.getElementById('mycanvas');
            // Make sure we don't execute when canvas isn't supported
            if (canvas.getContext) {
               // use getContext to use the canvas for drawing
               var ctx = canvas.getContext('2d');
               for (i=0;i<10;i++){
                  ctx.lineWidth = 1+i;
                  ctx.beginPath();
                  ctx.moveTo(5+i*14,5);
                  ctx.lineTo(5+i*14,140);
                  ctx.stroke();
               }
            } else {
               alert('You need Safari or Firefox 1.5+ to see this demo.');
            }
         }
      </script>
   </head>
   <body id = "test" onload = "drawShape();">
      <canvas id = "mycanvas"></canvas>
   </body>
</html>

以上示例将绘制以下形状 -

新页面打开
↑回到顶部↑
WIKI教程 @2018