canvas画布上定位点击位置

两种方法:

1.

cvs.onclick = function (e) {
    if (e.offsetX || e.layerX) {
           var x = e.offsetX == undefined ? e.layerX : e.offsetX;
           var y = e.offsetY == undefined ? e.layerY : e.offsetY;
   }
}

2.

 function windowToCanvas(x,y) {
      var cvsbox = cvs.getBoundingClientRect();
      return { x: Math.round(x - cvsbox.left), y: Math.round(y - cvsbox.top) };
}

cvs.onclick = function (e) {
     var clickXY = windowToCanvas(e.clientX, e.clientY);
}

 

转载于:https://www.cnblogs.com/lunawzh/p/5185462.html

你可能感兴趣的:(python,javascript,游戏)