尝试获取JS画布中点与鼠标之间的距离

时间:2019-01-14 14:38:28

标签: javascript canvas

因此,我用centerX和centerY获得了该点的中心,我想获得该点与鼠标之间的距离。

我收到一条错误消息,说clientX不起作用。

function dist(event){
  var rect = canvas.getBoundingClientRect();
  var mouseX = event.clientX - rect.left;
  var mouseY = event.clientY - rect.top;

  var a = centerX - mouseX;
  var b = centerY - mouseY;

  var dist = Math.hypot(a, b);
  if(dist <= 50){
    alert("test");
  }
}

0 个答案:

没有答案