扩展 @wdebeaum 的精彩答案,这是一种生成弧形路径的方法:
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;
}
to use
document.getElementById("arc1").setAttribute("d", describeArc(200, 400, 100, 0, 180));
并在你的html中
<path id="arc1" fill="none" stroke="#446688" stroke-width="20" />
现场演示 http://jsbin.com/quhujowota/1/edit?html,js,output