How can I draw a circle in HTML5 canvas using JavaScript?

How to draw a simple circle in HTML5 canvas using minimal JavaScript code?

+9


source to share


1 answer


Here's how to draw a circle using JavaScript in HTML5.

Here is the code:



   var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');
      var centerX = canvas.width / 2;
      var centerY = canvas.height / 2;
      var radius = 70;

      context.beginPath();
      context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
      context.fillStyle = 'green';
      context.fill();
      context.lineWidth = 5;
      context.strokeStyle = '#003300';
      context.stroke();
      

      body {
        margin: 0px;
        padding: 0px;
      }
      

    <canvas id="myCanvas" width="578" height="200"></canvas>
      

Run codeHide result


+15


source







All Articles