问题描述
这里是我的伪代码:
var percentage = 0.781743; // no specific length
var degrees = percentage * 360.0;
var radians = degrees * Math.PI / 180.0;
var x = 50;
var y = 50;
var r = 30;
var s = 1.5 * Math.PI;
var context = canvas.getContext('2d');
context.beginPath();
context.lineWidth = 5;
context.arc(x, y, r, s, radians, false);
context.closePath();
context.stroke();
我使用库来控制我所做的形状,并根据需要重绘它们。我的问题是上面的代码不工作。我假设我的数学不正确,因为如果我把 radians
改成类似 4.0 * Math.PI
整个圆圈。
I'm using the KineticJS library to control the shapes I make and redraw them as necessary. My problem is that the above code does not work at all. I assume I have the math incorrect, because if I change radians
to something like 4.0 * Math.PI
is draws the entire circle.
我一直在使用以供参考。
I've been using HTML5 Canvas Arc Tutorial for reference.
推荐答案
您的代码运行正常,角度应该是零,以得到你期望的。这里是工作代码:
Your code works just fine, but you have a starting angle that ought to be zero to get what you expect. Here's working code:
我认为你的困惑是从什么开始角度。这并不意味着它从那个点开始,然后添加endAngle弧度。
I think your confusion is from what starting angle does. It does not mean that it starts at that point and then adds endAngle radians to it. It means that the angle starts at that point and ends at the endAngle radians point absolutely.
所以如果你的startAngle是1.0,endAngle是1.3,你只会看到一个
So if you startAngle was 1.0 and endAngle was 1.3, you'd only see an arc of 0.3 radians.
如果你想让它按照你想的方式工作,你需要将startAngle添加到你的endAngle:
If you want it to work the way you're thinking, you're going to have add the startAngle to your endAngle:
context.arc(x, y, r, s, radians+s, false);
像这个例子:
这篇关于HTML5:按百分比填充圆/弧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!