查找落在填充画布弧内的 x/y 坐标
这是 fiddle
我在圆的外侧绘制的弧 - 我想知道如何找到它们覆盖的所有 x/y 坐标,这样我就不必每次使用 isPointInPath() 重新绘制它们来确定鼠标光标是否位于它们上方。我正在考虑将所有 x/y 坐标写入一个数组,我可以检查鼠标位置 x/y 坐标,如果找到匹配项,则更改光标。问题是,我不知道导出所有 x/y 值的代码。
Here is the fiddle
The arcs I've drawn around the outside of the circle - I'd like to know how to find all of the x/y coordinates that they cover so that I don't have to re-draw them every time using isPointInPath() to determine whether the mouse cursor is over them or not. I was thinking about writing all of the x/y coordinates to an array that I could check against the mouse position x/y coordinates and if I find a match then I change the cursor. The problem is, I don't know the code to derive all of the x/y values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上,您不必重新绘制弧线即可使用
.isPointInPath()
- 只需省略对.fill()
或.Stroke()< 的任何调用即可/code> 你将有一个路径,你可以用它来测试它是否包含一个点。
我建议使用一个函数来概述弧路径(
.beginPath()
、路径命令、.closePath()
),然后使用两个函数来调用它——其中一个函数是调用圆弧路径函数,然后设置填充样式并填充路径以绘制它,另一个调用圆弧路径函数,然后仅测试点是否在路径中。You don't actually have to redraw your arcs to use
.isPointInPath()
-- just omit any calls to.fill()
or.stroke()
and you'll have a path which you can use to test whether it contains a point.I would suggest having one function which outlines the arc path (
.beginPath()
, path commands,.closePath()
) and then two functions which call it-- one which calls the arc path function, then sets the fill style and fills the path to draw it, and another which calls the arc path function and then just tests whether a point is in the path.这是您应该使用的方法:
http://en.wikipedia.org/wiki/Point_in_polygon
它的工作方式实际上非常简单:如果在任意点结束的光线穿过多边形周界的次数为甚至,相应的点必须位于多边形之外。如果是奇数,则它在多边形内。
这是 Pimvdb 找到的一个函数:
This is the method you should use:
http://en.wikipedia.org/wiki/Point_in_polygon
The way it works is actually extremely simple: if the amount of times a ray that ends at any point passes through the polygon perimeter is even, the respective point HAS to be outside of the polygon. If it's odd, it's within the polygon.
Here's a function found by Pimvdb:
我不会将你所拥有的称为“弧”(它们更像是弓形矩形),但这里有一个如何编写函数来确定一个点是否在这样的东西内的大致草图:
Math.atan2
)Math.atan2
包装值的端点。您无法计算该区域中的“所有”点,因为它们的数量是无限的。创建图像中所有整数像素坐标的查找表是可能的,但很浪费。
我会做的,而不是你所建议的,是使用保留模式图形系统(提示:SVG)并让它使用其远距离跟踪鼠标事件。 -更高效的代码。
I wouldn't call what you have 'arcs' (they're more like bowed rectangles), but here's a broad sketch of how to write a function to determine if a point is within such a thing:
Math.atan2
)Math.atan2
.You can't calculate "all" points in this region, as there an an infinite number of them. Creating a lookup table of all integer pixel coordinates in your image is possible, but wasteful.
What I would do, instead of what you are proposing, is use a retained-mode graphics system (hint: SVG) and let it track mouse events for me using its far-more-efficient code.
这是一个更简单的方法:
我更改了drawtabs函数来检查鼠标是否在选项卡内:
http://jsfiddle.net/kkDqz/4/
警告
此方法很简单,但需要您在 mousemove 上重绘
所有内容
。Here's an easier method:
I've altered the drawtabs function to also check if mouse is within a tab:
http://jsfiddle.net/kkDqz/4/
WARNING
This method is easy, but requires you to redraw
EVERYTHING
on mousemove.