HTML5:从画布弧获取点击位置
请参阅此 jsFiddle 帖子以获取工作弧线图;感谢 Simon Sarris 修复了我之前的问题。
我使用 KineticJS 插件来创建形状并使用事件处理程序。假设您单击了弧上的某处,并且弧知道您单击的位置(x
、y
),那么如何使用这 2 个坐标来确定百分比?
当您单击任意位置时,总百分比始终为 100%。
Addin
为了使这个更简单,我可以做什么 (x
, y
) 来虚拟弯曲对象,以便 x 从 0 到最大
x
?
See this jsFiddle post for a working arc drawing; thanks to Simon Sarris for the fix in my previous questions.
I'm using the KineticJS plugin to create shapes and make use of event handlers. Assuming you clicked somewhere on the arc and the arc knew where you clicked (x
, y
), how could those 2 coordinates be used to determine a percentage?
When you click anywhere, the total percentage is always 100%.
Addin
To make this simpler, what could I do to (x
, y
) to virtually bend the object so that x
goes from 0 to maximum x
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简单的三角函数。
sin(角度) = 对边/相邻
。opposite
是y
值,adjacent
是x
值。所以Math.asin((xx - x) / (yy - y))
其中 xx 和 yy 是圆弧中心的坐标。这给出了角度,然后您可以将其除以2 * Math.PI
。我一时记不起负数会发生什么。您可能必须获取参数的
Math.abs
值,然后计算出点击位于哪个象限(使用<
和> 可以轻松完成;
)并为每一项添加Math.PI / 2
。Simple trigonometry.
sin(angle) = opposite / adjacent
.opposite
is they
value andadjacent
is thex
value. SoMath.asin((xx - x) / (yy - y))
where xx and yy are the coords of the centre of the arc. That gives you the angle, which you can then divide2 * Math.PI
by.Off the top of my head I can't remember what happens with negative numbers. You may have to take the
Math.abs
value of the arguments, then work out which quadrant the click is in (easy to do by using<
and>
) and addMath.PI / 2
for each one.这包括检查鼠标是否在弧内:
This includes a check if the mouse is inside the arc:
没有真正测试它,但从技术上讲,它应该可以工作:
编辑
对于负数,您可以尝试添加 1,因为它在 [-1, 1] 范围内
Did not really test it, but technically, it should work:
Edit
For negative numbers you can try adding 1 since it's in [-1, 1] range