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

 

posted @ 2016-02-09 11:00  lunawzh  阅读(3923)  评论(0编辑  收藏  举报