Canvas How to - Draw Semicircle








Question

We would like to know how to draw Semicircle.

Answer


<!--   ww  w  . j a va  2s . c  o  m-->
<!DOCTYPE html>
<html>
<body>
  <canvas id="myCanvas" width="578" height="200"></canvas>
  <script>
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');
      context.beginPath();
      context.arc(288, 75, 70, 0, Math.PI, false);
      context.closePath();
      context.lineWidth = 5;
      context.fillStyle = 'red';
      context.fill();
      context.strokeStyle = '#550000';
      context.stroke();
    </script>
</body>
</html>

The code above is rendered as follows: