簡體   English   中英

如何使此功能更新當前鼠標位置?

[英]How do I make this function update the current mouse position?

我正在制作一個繪圖板,並且我希望在拖動鼠標時在陣列中更新鼠標位置。 這是我的代碼:

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]);
 }
};           

控制台日志顯示,當您移動鼠標時,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);

}

可能是因為您的函數penDown僅被調用了一次,所以分配給localPen的x和y的值不會改變。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM