Mithun Sreedharan
Mithun Sreedharan

Reputation: 51282

How to divide a circle into three equal parts with HTML5 canvas?

enter image description here

How can I divide a circle into three equal parts with HTML5 canvas 2D context API like above figure?

I was trying this

Can somebody suggest a better way? probably with percentages (or in degrees) instead of hard-coded coordinates?

var can = document.getElementById('mycanvas');
var ctx = can.getContext('2d');

ctx.fillStyle = "#BD1981";
ctx.beginPath();
ctx.arc(200, 200, 150, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();

ctx.strokeStyle = "#FFC8B2";
ctx.lineWidth = "2";
ctx.beginPath();
ctx.moveTo(200, 200);
ctx.lineTo(100, 100);
ctx.closePath();
ctx.stroke();

ctx.beginPath();
ctx.moveTo(200, 200);
ctx.lineTo(350, 200);
ctx.closePath();
ctx.stroke();

ctx.beginPath();
ctx.moveTo(200, 200);
ctx.lineTo(100, 300);
ctx.closePath();
ctx.stroke();

Upvotes: 2

Views: 4794

Answers (3)

Amber York
Amber York

Reputation: 1

I know you probably got your answer but I found Wayne's jsfiddle helpful so I'm adding my contribution which lets you set a custom number of sections you want to divide the circle into.

http://jsfiddle.net/yorksea/3ef0y22c/2/

(also using @phant0m's drawAngledLine)

var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
var RADIUS = 300;
var num_sections = 19; //set this for number of divisions

function drawCircle(x, y, r) {
  ctx.beginPath();
  ctx.arc(x, y, r, 0, 2 * Math.PI);
  ctx.stroke();
}

function drawAngledLine(x, y, length, angle) {
  var radians = angle / 180 * Math.PI;
  var endX = x + length * Math.cos(radians);
  var endY = y - length * Math.sin(radians);
  ctx.beginPath();
  ctx.moveTo(x, y)
  ctx.lineTo(endX, endY);
  ctx.closePath();
  ctx.stroke();
}

//draw circle outline
drawCircle(320, 320, RADIUS);

//loop the number of sections to draw each
for (i = 1; i <= num_sections; i++) {
  drawAngledLine(320, 320, RADIUS, i * (360 / num_sections));
}
<canvas id="canvas" width="650" height="650"></canvas>

Upvotes: 0

Wayne
Wayne

Reputation: 60414

Putting it all together (using @phant0m's drawAngledLine):

var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
var RADIUS = 70;

function drawCircle(x, y, r) {
    ctx.beginPath();
    ctx.arc(x, y, r, 0, 2 * Math.PI);
    ctx.stroke();
}

function drawAngledLine(x, y, length, angle) {
    var radians = angle / 180 * Math.PI;
    var endX = x + length * Math.cos(radians);
    var endY = y - length * Math.sin(radians);
    ctx.beginPath();
    ctx.moveTo(x, y)
    ctx.lineTo(endX, endY);
    ctx.closePath();
    ctx.stroke();
}

drawCircle(140, 140, RADIUS);
drawAngledLine(140, 140, RADIUS, 1 * (360 / 3));
drawAngledLine(140, 140, RADIUS, 2 * (360 / 3));
drawAngledLine(140, 140, RADIUS, 3 * (360 / 3));

Demo here:

Upvotes: 2

phant0m
phant0m

Reputation: 16905

Here is a function (demo) that allows you to specify a starting point, the length and the angle in degrees:

var drawAngledLine = function(x, y, length, angle) {
    var radians = angle / 180 * Math.PI;
    var endX = x + length * Math.cos(radians);
    var endY = y - length * Math.sin(radians);

    ctx.beginPath();
    ctx.moveTo(x, y)
    ctx.lineTo(endX, endY);
    ctx.closePath();
    ctx.stroke();
}

Upvotes: 7

Related Questions