Last active
July 26, 2020 13:29
-
-
Save Ashkanph/a396d4636d65bc8a86afb7d7d7c7ae25 to your computer and use it in GitHub Desktop.
Circle svg stroke arc
This file contains 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
<!DOCTYPE html> | |
<!-- ref: https://jsbin.com/woquxetufe/edit?html,js,output --> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<svg> | |
<path id="arc1" fill="none" stroke="#446688" stroke-width="20" /> | |
<path id="arc2" fill="none" stroke="red" stroke-width="20" /> | |
</svg> | |
<script> | |
function polarToCartesian(centerX, centerY, radius, angleInDegrees) { | |
var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0; | |
return { | |
x: centerX + (radius * Math.cos(angleInRadians)), | |
y: centerY + (radius * Math.sin(angleInRadians)) | |
}; | |
} | |
function describeArc(x, y, radius, startAngle, endAngle){ | |
var start = polarToCartesian(x, y, radius, endAngle); | |
var end = polarToCartesian(x, y, radius, startAngle); | |
var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1"; | |
var d = [ | |
"M", start.x, start.y, | |
"A", radius, radius, 0, largeArcFlag, 0, end.x, end.y | |
].join(" "); | |
return d; | |
} | |
window.onload = function() { | |
document.getElementById("arc1").setAttribute("d", describeArc(170, 170, 100, 0, 90)); | |
document.getElementById("arc2").setAttribute("d", describeArc(170, 170, 100, 90, 120)); | |
}; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment