需要在javascript中捕获鼠标位置

发布于 2024-12-19 04:11:38 字数 684 浏览 0 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北座城市 2024-12-26 04:11:38

这是关于如何使用 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

神魇的王 2024-12-26 04:11:38

试试这个...希望有帮助。

    $("#divClick").click(function (e) {
            var posX = $(this).position().left;
            var posY = $(this).position().top;
            var cursorX = (e.pageX - posX);
            var cursorY = (e.pageY - posY);
            //cursorX, cursorY is the absolute position of the mouse pointer
});

try this... hope it helps.

    $("#divClick").click(function (e) {
            var posX = $(this).position().left;
            var posY = $(this).position().top;
            var cursorX = (e.pageX - posX);
            var cursorY = (e.pageY - posY);
            //cursorX, cursorY is the absolute position of the mouse pointer
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文