三角形加正方形

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <canvas id="canvas" width="300" height="300"></canvas>
  <script>
  var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

ctx.beginPath();
ctx.moveTo(100,100);
ctx.lineTo(200,100);
ctx.lineTo(200,200);
ctx.lineTo(100,200);
ctx.closePath();
  ctx.stroke();
ctx.fillStyle = 'rgb(230, 148, 249)';
ctx.fill();
ctx.beginPath();
    //设置路径起点坐标
    ctx.moveTo(100, 100);
    //绘制直线线段到坐标点
    ctx.lineTo(200, 100);
    //绘制直线线段到坐标点
   ctx.lineTo(150, 30);
    //先关闭绘制路径。注意,此时将会使用直线连接当前端点和起始端点。
    ctx.closePath();
    //最后,按照绘制路径画出直线
    ctx.stroke();
    // 进行绘图处理
     ctx.fillStyle = 'rgba(200,0,0,0.5)';
     // 填充路径
     ctx.fill();
  </script>
</body>
</html>

原文地址:https://www.cnblogs.com/xiaoyueyuedeboke/p/10053583.html