Canvas study notes two: draw a triangle with path way

effect

Here Insert Picture Description

The complete code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    #test {
      box-shadow: 0 0 10px #333;
    }
  </style>
</head>
<body>
  <canvas id="test" width="500" height="400"></canvas>
  <script>
    //画布
    let canvas = document.getElementById('test');
    // 画笔
    let ctx = canvas.getContext('2d');

    //路径1、起始点;2、画图命令画出路径;3、路径闭合4、通过描边或填充绘制图形

    // 绘制三角形
    ctx.beginPath();
    // 1、起始点
    ctx.moveTo(100,100);
    // 2、描绘
    ctx.lineTo(200,100);
    ctx.lineTo(200,200);
    // 3、绘画

    // 填充
    // ctx.fillStyle = 'red';
    // ctx.fill();
    // 描边线
    ctx.strokeStyle = 'red';
    // 闭合
    ctx.closePath();
    ctx.stroke();
  </script>
</body>
</html>
Published 96 original articles · won praise 15 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_35958891/article/details/104078053