需要在javascript中捕获鼠标位置
如何使用 javascript 和画布捕获鼠标的位置?
当我转到此页面时:http://billmill.org/static/canvastutorial/mouse.html
他们显示:
function init_mouse() {
canvasMinX = $("#canvas").offset().left;
canvasMaxX = canvasMinX + WIDTH;
}
function onMouseMove(evt) {
if (evt.pageX > canvasMinX && evt.pageX < canvasMaxX) { //how can you access the canvasMinX when its out of scope?
//also, what is pageX? is it the coordinate of the mouse? if not, how do i get it?
paddlex = evt.pageX - canvasMinX;
}
}
$(document).mousemove(onMouseMove);
最后,我需要在单击鼠标时发生这种情况。所以我这样做:
$(document).mouseclick(onMouseClick)
是这样吗?
how do i capture the position of the mouse using javascript and the canvas?
when i go to this page:http://billmill.org/static/canvastutorial/mouse.html
they show this:
function init_mouse() {
canvasMinX = $("#canvas").offset().left;
canvasMaxX = canvasMinX + WIDTH;
}
function onMouseMove(evt) {
if (evt.pageX > canvasMinX && evt.pageX < canvasMaxX) { //how can you access the canvasMinX when its out of scope?
//also, what is pageX? is it the coordinate of the mouse? if not, how do i get it?
paddlex = evt.pageX - canvasMinX;
}
}
$(document).mousemove(onMouseMove);
and finally, i need this to happen on mouse click. so i do:
$(document).mouseclick(onMouseClick)
is that it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是关于如何使用 JS 实现它的最佳解释: JavaScript Capture Mouse XY Position脚本 - 快速入门迷你教程
This is the best for a perfect explanation on how to implement it using JS: JavaScript Capture Mouse X-Y Position Script - Quick-Take Mini-Tutorial