【发布时间】:2012-07-24 01:53:14
【问题描述】:
所以我再次处理环形扇区,这不是我的专长。我可以在画布上使用.arc 方法就好了,问题来自需要我的弧线成为路径的一部分。
例如:
ctx.save();
ctx.arc(centerX, centerY, radius, startAngle, endAngle, true);
ctx.stroke();
ctx.restore();
工作正常。但现在我需要它作为路径的一部分,所以我有这样的东西:
var pointArray = [...]; //this contains all four corner points of the annular sector
ctx.save();
ctx.moveTo(pointArray[0].x, pointArray[0].y);
ctx.lineTo(pointArray[1].x, pointArray[1].y); //so that draws one of the flat ends
ctx.arcTo(?, ?, pointArray[2].x pointArray[2].y, radius);
切线坐标的切线让我发疯。另外,我有一个严重的担忧: http://www.dbp-consulting.com/tutorials/canvas/CanvasArcTo.html 听起来好像用 arcTo 绘制的弧线永远无法覆盖 180 度或更多的圆,而且有时我的环形扇区会大于 180 度。
感谢stackoverflow的高级几何思维的帮助!
更新 好的,所以我必须在这里做 svg canvas inter-polarity,并使用咖啡脚本,实际生产代码如下!
annularSector : (startAngle,endAngle,innerRadius,outerRadius) ->
startAngle = degreesToRadians startAngle+180
endAngle = degreesToRadians endAngle+180
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) ]
]
angleDiff = endAngle - startAngle
largeArc = (if (angleDiff % (Math.PI * 2)) > Math.PI then 1 else 0)
if @isSVG
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(" ")
else
@gaugeCTX.moveTo p[0][0], p[0][1]
@gaugeCTX.lineTo p[1][0], p[1][1]
#@gaugeCTX.arcTo
@gaugeCTX.arc @centerX, @centerY, outerRadius, startAngle, endAngle, false
#@gaugeCTX.moveTo p[2][0], p[2][1]
@gaugeCTX.lineTo p[3][0], p[3][1]
@gaugeCTX.arc @centerX, @centerY, innerRadius, startAngle, endAngle, false
解决方案
@gaugeCTX.moveTo p[0][0], p[0][1]
@gaugeCTX.lineTo p[1][0], p[1][1]
@gaugeCTX.arc @centerX, @centerY, outerRadius, startAngle, endAngle, false
@gaugeCTX.lineTo p[3][0], p[3][1]
@gaugeCTX.arc @centerX, @centerY, innerRadius, endAngle, startAngle, true #note that this arc is set to true and endAngle and startAngle are reversed!
【问题讨论】:
-
你能澄清一下这个问题吗?你想知道在源代码中有
?的地方放什么吗? -
@alex 我已经更新了我的问题。
标签: javascript canvas geometry