开发者

HTML5 iPhone preventDefault() doesn't stop from "copy" to appear upon a touch

开发者 https://www.devze.com 2023-01-13 08:54 出处:网络
I have this code, which shows the coordinates of the touch as the user drags his finger across the screen. The issue is that if I touch the screen for too long, it selects the entire canvas and displa

I have this code, which shows the coordinates of the touch as the user drags his finger across the screen. The issue is that if I touch the screen for too long, it selects the entire canvas and displays the "copy" bubble, which is the default behaviour. How do I get rid of that?

<html>
  <head>
    <script type="application/javascript">
    function drawCross(color,x,y){
      var canvas = document.getElementById('canvas');
      var ctx = canvas.getContext('2d');
      ctx.beginPath();
      ctx.strokeStyle=color;
      ctx.lineWidth=4;
      ctx.moveTo(x,0);
      ctx.lineTo(x,480);
      ctx.moveTo(0,y);
 开发者_StackOverflow中文版     ctx.lineTo(320,y);
      ctx.stroke();
    }
    document.addEventListener('touchmove', function(event) {
      event.preventDefault();
      var touch = event.touches[0];
      drawCross('#ffffff', document.getElementById('oldX').value,
        document.getElementById('oldY').value);
      drawCross('#cc0000',touch.pageX,touch.pageY);
      document.getElementById('oldX').value=touch.pageX;
      document.getElementById('oldY').value=touch.pageY;
    }, false);
    document.addEventListener('touchend', function(event) {
      event.preventDefault();
      drawCross('#ffffff', document.getElementById('oldX').value,
        document.getElementById('oldY').value);
      document.getElementById('oldX').value=0;
      document.getElementById('oldY').value=0;
    }, false);
  </script>
</head>
<body>
  <input id="oldX" type="hidden" value="0">
  <input id="oldY" type="hidden" value="0">
  <div>
    <canvas id="canvas" width="320" height="480"></canvas>
  </div>
</body>
</html>


Just added a listener for touchstart, and it worked :)

// listener for the touch
document.addEventListener('touchstart', function(event) {
    event.preventDefault();
}, false);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号