canvas製作八卦圖源碼

 源碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <canvas id="canvas" width="400" height="400" style="background:#E1E1EF"></canvas>
  <script>
    let ctx = document.getElementById('canvas').getContext('2d');
    function draw() {
      ctx.clearRect(0, 0, 400, 400);
      ctx.strokeRect(0, 0, 400, 400);
      ctx.fillStyle = "#000000";
      ctx.beginPath();
      ctx.arc(200, 200, 200, Math.PI * .5, Math.PI * 1.5, true);
      ctx.closePath();
      ctx.fill();
      ctx.fillStyle = "#ffffff";
      ctx.beginPath();
      ctx.arc(200, 200, 200, Math.PI * 1.5, Math.PI * 2.5, true);
      ctx.closePath();
      ctx.fill();
      ctx.fillStyle = "#000000";
      ctx.beginPath();
      ctx.arc(200, 100, 100, 0, Math.PI * 2, true);
      ctx.closePath();
      ctx.fill();
      ctx.fillStyle = "#ffffff";
      ctx.beginPath();
      ctx.arc(200, 300, 100, 0, Math.PI * 2, true);
      ctx.closePath();
      ctx.fill();
      ctx.fillStyle = "#ffffff";
      ctx.beginPath();
      ctx.arc(200, 100, 5, 0, Math.PI * 2, true);
      ctx.closePath();
      ctx.fill();
      ctx.fillStyle = "#000000";
      ctx.beginPath();
      ctx.arc(200, 300, 5, 0, Math.PI * 2, true);
      ctx.closePath();
      ctx.fill();
    }
    draw();
  </script>
</body>
</html>

 

效果圖如下:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章