Divide circle SVG into 12 parts

Asked

Viewed 236 times

9

I’m using an SVG circle and I want to share it with a line dynamically created into 12 parts. I can only divide into 4 parts, through its radius. I know it is possible through the sin and cos.

Example jsfiddle: https://jsfiddle.net/k3epungk/

Source code:

//raio circulo
r=50;

var circle = document.getElementById("circle");
circle.setAttribute("cx",100);
circle.setAttribute("cy",100);
circle.setAttribute("r",r);

//posiçao
x=100;
y=100;

var array_y1=[(y - r)+10,y,(y + r)+10,y]; 
var array_y2=[(y - r)-10,y,(y + r)-10,y]; 

var array_x1=[x,(x + r)+10,x,(x - r)+10]; 
var array_x2=[x,(x + r)-10,x,(x - r)-10]; 

var svg = document.getElementById('svg');
		
for(var i=0;i<=3;i++){
    
    var newLine = document.createElementNS('http://www.w3.org/2000/svg','line');
    newLine.setAttributeNS(null,'id','line'+i);
    newLine.setAttributeNS(null,'x1',array_x1[i]);
    newLine.setAttributeNS(null,'y1',array_y1[i]);
    newLine.setAttributeNS(null,'x2',array_x2[i]);
    newLine.setAttributeNS(null,'y2',array_y2[i]);
    newLine.setAttributeNS(null,"stroke","black");
    newLine.setAttributeNS(null,"stroke-width","1");
    svg.appendChild(newLine);
}
<svg width=200 height=200 id=svg>
<circle id="circle" title="horloge" cx="100" cy="100" r="50" stroke-width="2px" fill="Aqua" stroke="black" />
</svg>

1 answer

13


I got it. Here’s the fiddle: https://jsfiddle.net/m5ojvsbe/

And here’s the source code. It can be executed right here with the blue button Execute that’s down there:

// Raio do círculo.
var r = 50;

// Raio das linhas internas e externas
var r1 = 40;
var r2 = 60;

// Calcula os senos e co-senos.
var a = Math.sqrt(3) / 2;
var b = 0.5;
var sins = [0, b, a, 1,  a,  b,  0, -b, -a, -1, -a, -b];
var coss = [1, a, b, 0, -b, -a, -1, -a, -b,  0,  b,  a];

// Posição.
var x = 100;
var y = 100;

// Desenha o círculo.
var circle = document.getElementById("circle");
circle.setAttribute("cx", x);
circle.setAttribute("cy", y);
circle.setAttribute("r", r);

var svg = document.getElementById('svg');

// Desenha as doze linhas.
for (var i = 0; i < 12; i++) {
    var newLine = document.createElementNS('http://www.w3.org/2000/svg', 'line');
    newLine.setAttributeNS(null, 'id', 'line' + i);
    newLine.setAttributeNS(null, 'x1', x + coss[i] * r1);
    newLine.setAttributeNS(null, 'y1', y + sins[i] * r1);
    newLine.setAttributeNS(null, 'x2', x + coss[i] * r2);
    newLine.setAttributeNS(null, 'y2', y + sins[i] * r2);
    newLine.setAttributeNS(null, "stroke", "black");
    newLine.setAttributeNS(null, "stroke-width", i % 3 == 0 ? "2" : "1");
    svg.appendChild(newLine);
}
<svg width=200 height=200 id=svg>
<circle id="circle" title="horloge" cx="100" cy="100" r="50" stroke-width="2px" fill="Aqua" stroke="black" />
</svg>

  • Thank you, it works perfectly. I wanted only the 4 central strokes to be slightly larger than the others.

  • @akm Edited. Is that how you want it? Or would it be larger in a matter of length rather than thickness?

  • That’s right, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.