Created
December 2, 2012 19:14
-
-
Save buschtoens/4190516 to your computer and use it in GitHub Desktop.
Generate the path for an annular sector svg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deg2rad(deg) { | |
return deg * Math.PI / 180; | |
} | |
function annularSector(centerX, centerY, startAngle, endAngle, innerRadius, outerRadius) { | |
startAngle = deg2rad(startAngle + 180); | |
endAngle = deg2rad(endAngle + 180); | |
var p = [ | |
[centerX + innerRadius * Math.cos(startAngle), centerY + innerRadius * Math.sin(startAngle)] | |
, [centerX + outerRadius * Math.cos(startAngle), centerY + outerRadius * Math.sin(startAngle)] | |
, [centerX + outerRadius * Math.cos(endAngle), centerY + outerRadius * Math.sin(endAngle)] | |
, [centerX + innerRadius * Math.cos(endAngle), centerY + innerRadius * Math.sin(endAngle)] | |
]; | |
var angleDiff = endAngle - startAngle | |
, largeArc = (angleDiff % (Math.PI * 2)) > Math.PI ? 1 : 0; | |
var commands = []; | |
commands.push("M" + p[0].join()); | |
commands.push("L" + p[1].join()); | |
commands.push("A" + [outerRadius, outerRadius].join() + " 0 " + largeArc + " 1 " + p[2].join()); | |
commands.push("L" + p[3].join()); | |
commands.push("A" + [innerRadius, innerRadius].join() + " 0 " + largeArc + " 0 " + p[0].join()); | |
commands.push("z"); | |
return commands.join(" "); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment