canvas画布获取定位的坐标位置

方法一:

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

方法二:

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

你可能感兴趣的:(h5)