我正在尝试在画布上获取鼠标位置的坐标。但我得到的只是NaN,NaN
var canvasBg = document.getElementById('canvasBg');
var ctxBg = canvasBg.getContext('2d');
var mouseX;
var mouseY;
canvasBg.addEventListener('mousemove', mouseMoved, false);
canvasBg.addEventListener('click', mouseClicked, false);
function mouseMoved(e) {
mouseX = e.pageX - canvasBg.pageOffsetLeft;
mouseY = e.pageY - canvasBg.pageOffsetTop;
document.getElementById('mouseCoors').innerHTML = 'X: ' + mouseX + ' Y: ' + mouseY;
}
function mouseClicked(e) {
alert( mouseX + "," + mouseY);
}