gpt4 book ai didi

javascript - 圆圈中的 SVG 圆弧区域

转载 作者:行者123 更新时间:2023-11-30 05:38:54 29 4
gpt4 key购买 nike

我想在 SVG 中得到类似的东西。到目前为止,我已经制作了圆圈,但我想正确定位周围的黑色区域。

API 返回四个值:

  • start_angle : 第一个 Angular (看起来是弧度)
  • end_angle : 最终 Angular (看起来是弧度)
  • inner_radius : 较小的半径
  • outer_radius : 较大的半径

这是我想要的方案: enter image description here

我正在用 Javascript 制作 SVG,所以我的代码是这样的:

    var myArc = document.createElementNS('http://www.w3.org/2000/svg', 'path');
myArc.setAttribute('fill', 'black');
myArc.setAttribute('d', 'M-'+outer_radius+',32A'+outer_radius+','+outer_radius+' 0 0,1 -'+outer_radius+',-32L-'+inner_radius+',-30A'+inner_radius+','+inner_radius+' 0 0,0 -'+inner_radius+',30Z');// TODO
arcs.appendChild(myArc);

这可以绘制一个区域,但我不知道要输入什么值。我试图确定要使用的点,但它不起作用:

var pointA = [outer_radius * Math.cos(start_angle * 180 / Math.PI), outer_radius * Math.sin(start_angle * 180 / Math.PI)];
var pointB = [outer_radius * Math.cos(end_angle * 180 / Math.PI), outer_radius * Math.sin(end_angle * 180 / Math.PI)];
var pointC = [inner_radius * Math.cos(end_angle * 180 / Math.PI), inner_radius * Math.sin(end_angle * 180 / Math.PI)];
var pointD = [inner_radius * Math.cos(start_angle * 180 / Math.PI), inner_radius * Math.sin(start_angle * 180 / Math.PI)];

你能帮我解决这个问题吗?

感谢您的帮助。

最佳答案

我假设您可以定义中心点。如果是这样,请尝试以下操作(它使用度数)并绘制两个单独的弧线,内部和外部。但是你可以得到每个的起点和终点。路径分为 4 个部分绘制:

1)外弧

2) 开始外弧和开始内弧之间的桥梁

3)内弧

4)内弧端到外弧端

注意:路径的fill-rule=evenodd

编辑:添加 ArcSweep

function drawInnerOuterArcs()
{
var centerX=200
var centerY=200
var innerRadius=120
var outerRadius=160
var startAngle=310 //--degrees
var endAngle=30 //--degrees
var ArcSweep = endAngle - startAngle <= 180 ? "0" : "1";

function polarToCartesian(centerX, centerY,radiusX, radiusY, angleInDegrees)
{
var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0;
return {
x: centerX + (radiusX * Math.cos(angleInRadians)),
y: centerY + (radiusY * Math.sin(angleInRadians))
};
}
//---outer points---
var StartPnt1 = polarToCartesian(centerX, centerY, outerRadius, outerRadius, startAngle);
var EndPnt1 = polarToCartesian(centerX, centerY, outerRadius, outerRadius, endAngle);

//---outer arc: begin path---
var d1 = [
"M", StartPnt1.x, StartPnt1.y,
"A", outerRadius, outerRadius, 0,ArcSweep, 1, EndPnt1.x, EndPnt1.y
].join(" ");

//---inner points---
var StartPnt2 = polarToCartesian(centerX, centerY, innerRadius, innerRadius, startAngle);
var EndPnt2 = polarToCartesian(centerX, centerY, innerRadius, innerRadius, endAngle);

//---start bridge--
d1+="M"+ StartPnt1.x+" "+StartPnt1.y+"L"+StartPnt2.x+" "+StartPnt2.y

//---inner arc---
var d2 = [
"A", innerRadius, innerRadius, 0,ArcSweep,1, EndPnt2.x, EndPnt2.y
].join(" ");

//--end bridge--
d2 +="L"+EndPnt1.x+" "+EndPnt1.y

//---arc fill-rule="evenodd"
myArc.setAttribute("d",d1+d2)
}

关于javascript - 圆圈中的 SVG 圆弧区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21940903/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com