简体   繁体   English

如何使用html5 canvas处理触摸事件?

[英]How to handle touch events with html5 canvas?

I have created the painting based feature with html5 canvas. 我已经使用html5 canvas创建了基于绘画的功能。 When it comes to the desktop it's working as I expected, but the problem only in touch devices. 当涉及到台式机时,它可以按我预期的那样工作,但问题仅在触摸设备上。 I'm clueless where is the issues are coming from. 我不知道问题是从哪里来的。 Can someone help me to fix this one? 有人可以帮我解决这个问题吗? I'm struggling with past two days. 我过去两天都在挣扎。

My code 我的密码

if(windowWidth <= 1024) {
  canvas2.addEventListener('touchmove', function(e) {
      mouse.x = e.pageX;
      mouse.y = e.pageY;
      handlerPos.x = e.pageX;
      handlerPos.y = e.pageY;
  }, false);
} else {
  canvas2.addEventListener('mousemove', function(e) {
    mouse.x = e.pageX;
    mouse.y = e.pageY;
    handlerPos.x = e.pageX;
    handlerPos.y = e.pageY;
  }, false);
}

Maybe you are looking for this 也许您正在寻找这个

Unfortunately, all was not well. 不幸的是,一切都不顺利。 An issue arose from a conflict with built-in browser gestures. 与内置浏览器手势冲突引起了问题。 When I moved my finger on the canvas, I wanted the page to stay still so I could draw. 当我在画布上移动手指时,我希望页面保持静止以便可以绘画。 However, if my page had horizontal or vertical scrolling (and it did) the page would move along with my finger and making proper drawing impossible. 但是,如果我的页面具有水平或垂直滚动​​(确实如此),则该页面会随着手指移动,从而无法进行适当的绘制。 After some frustration, I stumbled upon the solution: preventing scrolling on document.body if the target of a touch event is the canvas. 经过一番挫折后,我偶然发现了解决方案:如果触摸事件的目标是画布,则防止在document.body上滚动。

http://bencentra.com/code/2014/12/05/html5-canvas-touch-events.html http://bencentra.com/code/2014/12/05/html5-canvas-touch-events.html

document.body.addEventListener('touchmove', function(e) {
   mouse.x = e.pageX;
   mouse.y = e.pageY;
   handlerPos.x = e.pageX;
   handlerPos.y = e.pageY;
}, false);

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

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