如何使用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)
是吗?
答案 0 :(得分:1)
答案 1 :(得分:0)
试试这个...希望它有所帮助。
$("#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
});