How to draw a circle in HTML5 Canvas using JavaScript?

后端 未结 3 1057
离开以前
离开以前 2020-12-17 08:05

How to draw a simple circle in HTML5 Canvas using minimum JavaScript code?

3条回答
  •  再見小時候
    2020-12-17 08:35

    Here is how to draw a circle using JavaScript in HTML5:

    const canvas = document.getElementById('myCanvas');
    const context = canvas.getContext('2d');
    const centerX = canvas.width / 2;
    const centerY = canvas.height / 2;
    const 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;
    }

提交回复
热议问题