如何使此功能更新当前鼠标位置?

时间:2014-02-25 21:26:14

标签: javascript location

我正在制作绘图板,我希望鼠标位置在拖动鼠标时在数组中更新。这是我的代码:

function penDown (x, y) {
  isPenDown = true;
  localPen.x = x;
  localPen.y = y;
}

var X = [],
Y = [],
i = -1;

function penMove (x, y) {
  if (isPenDown) {
     ++i; 
     X[i] = localPen.x;
     Y[i] = localPen.y;
     console.log("i is " + i + ", x is " + X[i] + ", y is " + Y[i]);
 }
};           

控制台日志显示我在移动鼠标时会不断更新,但鼠标的X和Y坐标不会改变 - 当您第一次按下鼠标时,它们只会保留在初始鼠标位置。

以下是我如何调用penDown:

function pointerDownListener (e) {
  // Retrieve a reference to the Event object for this mousedown event.
  var event = e || window.event; 
  // Determine where the user clicked the mouse.
  var mouseX = event.clientX - canvas.offsetLeft;
  var mouseY = event.clientY - canvas.offsetTop;

  // Move the drawing pen to the position that was clicked
  penDown(mouseX, mouseY);
}

function pointerMoveListener (e) {

  var event = e || window.event; // IE uses window.event, not e
  var mouseX = event.clientX - canvas.offsetLeft;
  var mouseY = event.clientY - canvas.offsetTop;

  // Draw a line if the pen is down
  penMove(mouseX, mouseY);

}

1 个答案:

答案 0 :(得分:0)

可能是,因为你的函数penDown只被调用一次,所以分配给localPen的x和y的值不会改变。