奇怪的鼠标事件位置问题
我有一个返回鼠标事件位置的函数。
// returns the xy point where the mouse event was occured.
function getXY(ev){
var xypoint = new Point();
if (ev.layerX || ev.layerY) { // Firefox
xypoint.x = ev.layerX;
xypoint.y = ev.layerY;
} else if (ev.offsetX || ev.offsetX == 0) { // Opera
xypoint.x = ev.offsetX;
xypoint.y = ev.offsetY;
}
return xypoint;
}
我正在捕获鼠标事件以在 html5 画布上执行绘图。有时我会得到 xypoint 的 -ve 值。当我使用 firebug 调试应用程序时,我得到了非常奇怪的行为。例如,如果我将断点放在该函数的第 4 行,条件为 (xypoint.x<0 || xypoint.y<0),它会在断点处停止,我可以看到 layer.x、layer.y是积极且正确的。但 xypoint.x 或 xypoint.y 为负数。如果我使用 firbug 控制台重新分配值,我将在 xypoint 中获得正确的值。谁能解释一下发生了什么事。
如果我以正常速度移动鼠标,上述效果很好。如果我以非常快的速度移动鼠标,我就会出现这种行为。
谢谢
I have a function which returns mouse event positions.
// returns the xy point where the mouse event was occured.
function getXY(ev){
var xypoint = new Point();
if (ev.layerX || ev.layerY) { // Firefox
xypoint.x = ev.layerX;
xypoint.y = ev.layerY;
} else if (ev.offsetX || ev.offsetX == 0) { // Opera
xypoint.x = ev.offsetX;
xypoint.y = ev.offsetY;
}
return xypoint;
}
I am capturing mouse events to perform drawings on html5 canvas. Sometimes I am getting -ve values for xypoint. When I debug the application using firebug I am getting really strange behavior. for example if I put my break point at the 4th line of this function with condition (xypoint.x<0 || xypoint.y<0), it stops at the break point and I can see that layer.x, layer.y was positive and correct. But xypoint.x or xypoint.y is negative. If I reassign the values using firbug console I am getting correct values in xypoint. Can anyone explain me what is happening.
The above works fine if I move mouse with normal speed. If I am moving mouse at very rapid speed I am getting this behavior.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Canvas 处理鼠标位置绝对是一件痛苦的事情。你必须做出大量的调整。我使用这个,它有一些小错误,但即使与我在应用程序中使用的拖放 div 一起工作:
故事的寓意?您需要计算画布的偏移量才能获得正确的结果。您正在从事件中捕获 XY,该事件具有未捕获的相对于窗口 XY 的偏移量。有道理吗?
Handling mouse position was an absolute pain with Canvas. You have to make a ton of adjustments. I use this, which has a few minor errors, but works even with the drag-and-drop divs I use in my app:
Moral of the story? You need to figure in the offset of the canvas to have proper results. You're capturing the XY from the event, which has an offset that's not being captured in relation to the window's XY. Make sense?