简体   繁体   English

用touchmove事件替换mousemove

[英]Replace mousemove with touchmove event

My program recognizes the mousemove event . 我的程序识别mousemove event What I would like to do is make my program work for mobile. 我想做的是让我的程序适用于移动设备。

That is why I would like to transform the mousemove event to a touchmove event . 这就是为什么我想将mousemove事件转换为touchmove event

My code is like this so far: 到目前为止我的代码是这样的:

var d=ctx.getImageData(0,0,canvas.width,canvas.height).data;
$hit=$("#hit");
$("#canvas").mousemove(function(e){handleMouseMove(e);});

tolerance = 20;
function handleMouseMove(e){

  e.preventDefault();
  e.stopPropagation();
  mouseX=parseInt(e.clientX-offsetX);
  mouseY=parseInt(e.clientY-offsetY);
  var isHit=d[(mouseY*cw+mouseX)*4+3]>tolerance;

  if(isHit){
    $hit.text("Yeahhhh");
  }else{
    //document.onmousemove = crash;
    $hit.text("Noooooooo");
  }
}

Is there a way that I can do that easily? 我有办法轻松地做到这一点吗?

I have read that is equivalent When to use touchmove vs mousemove? 我读过相当于何时使用touchmove vs mousemove? but I am not sure how to handle the mouseX and the mouseY . 但我不知道如何处理mouseXmouseY

toe.js - a lightweight touch events library, works pretty well. toe.js - 一个轻量级的触摸事件库,效果很好。 Hammerjs definitely has more customization, but toejs looked simpler. Hammerjs肯定有更多的定制,但toejs看起来更简单。

and have a suggestion in your code to avoid unnecessary event.stopPropagation, see here . 并在您的代码中有一个建议,以避免不必要的event.stopPropagation,请参阅此处

You can use jQuery UI Touch Punch for hack jquery events 您可以使用jQuery UI Touch Punch进行hack jquery事件

read this: http://touchpunch.furf.com/ 阅读本文: http//touchpunch.furf.com/

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

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