简体   繁体   English

Javascript Canvas 触摸事件

[英]Javascript Canvas touch events

Hey I have an issue with my canvas and how it handles touch events, currently it's working fine with mouse events and drawing as intended however when I try to incorporate touch events it does somewhat work but when I touch on the canvas the output is fixed in the top left corner, leading me to believe the offset is way off and honestly I'm really not sure where to go from here so any help would be hugely appriciated.嘿,我的画布及其处理触摸事件的方式有问题,目前它可以正常处理鼠标事件和按预期绘图,但是当我尝试合并触摸事件时,它确实有些工作,但是当我触摸画布时,输出是固定的左上角,让我相信偏移很远,老实说,我真的不知道从哪里开始,所以任何帮助都会非常有用。

$( document ).ready(function() {

  var container = document.getElementById('canvas');
  init(container, 200, 200, '#ddd');


  function init(container, width, height, fillColor) {
    var canvas = createCanvas(container, width, height);
    var ctx = canvas.getContext('2d');


    var mouse = {x: 0, y: 0};
    var touch = {x: 0, y: 0};
    var last_mouse = {x: 0, y: 0};
    var last_touch = {x: 0, y: 0};


    canvas.addEventListener('mousemove', function(e) {
      last_mouse.x = mouse.x;
      last_mouse.y = mouse.y;


      if (e.offsetX) {
        mouse.x = e.offsetX;
        mouse.y = e.offsetY;
      }
    });    

    canvas.addEventListener('touchmove', function(e) {
      var touch = e.touches[0];
      last_touch.x = e.pageX - touch.offsetLeft;
      last_touch.y = e.pageY - touch.offsetTop;


      if (e.offsetX) {
        touch.x = e.offsetX;
        touch.y = e.offsetY;
      }
    });  

    canvas.onmousemove = function(e) {

      var x = e.pageX - this.offsetLeft;
      var y = e.pageY - this.offsetTop;
    };

    canvas.addEventListener('touchstart', function(e) {
      canvas.addEventListener('touchmove', onTouchPaint, false);
    }, false);

    canvas.addEventListener('mousedown', function(e) {
      canvas.addEventListener('mousemove', onPaint, false);
    }, false);
    canvas.addEventListener('mouseup', function() {
      canvas.removeEventListener('mousemove', onPaint, false);
    });

    var onPaint = function() {
      ctx.beginPath();
      ctx.moveTo(last_mouse.x, last_mouse.y);
      ctx.lineTo(mouse.x, mouse.y);
      ctx.closePath();
      ctx.stroke();

      ctx.lineWidth = 15 ;
      ctx.lineJoin = 'round';
      ctx.lineCap = 'round';
      ctx.strokeStyle = 'black';
    };

    var onTouchPaint = function() {
      ctx.beginPath();
      ctx.moveTo(last_touch.x, last_touch.y);
      ctx.lineTo(touch.x, touch.y);
      ctx.closePath();
      ctx.stroke();

      ctx.lineWidth = 15 ;
      ctx.lineJoin = 'round';
      ctx.lineCap = 'round';
      ctx.strokeStyle = 'black';
    };
  } 
});

It's either onTouchPaint or touchmove and it's killing me.它要么是 onTouchPaint 要么是 touchmove,这让我很伤心。 Any help is appriciated.任何帮助是appriciated。

The following should get the proper touch or click location from the event.以下应该从事件中获得正确的触摸或点击位置。 I did fight with this quite a bit myself, and this finally did the trick.我自己确实与这个斗争了很多,这终于成功了。 It is basically a merged snipped from other answers here on stackoverflow.它基本上是从stackoverflow上的其他答案中截取的合并片段。 The trick should be to properly consider the canvas' getBoundingClientRect .诀窍应该是正确考虑画布的getBoundingClientRect

function getInteractionLocation(event) {
    let pos = { x: event.clientX, y: event.clientY };
    if (event.touches) {
        pos = { x: event.touches[0].clientX, y: event.touches[0].clientY };
    }
    const rect = event.target.getBoundingClientRect();
    const x_rel = pos.x - rect.left;
    const y_rel = pos.y - rect.top;
    const x = Math.round((x_rel * event.target.width) / rect.width);
    const y = Math.round((y_rel * event.target.height) / rect.height);
    return [x, y];
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM