繁体   English   中英

如何在画布HTML5中使用ontouch事件?

[英]How to use ontouch event in canvas HTML5?

我是这个网站的新手,这是我在这里提出的第一个问题。如果它简单,请原谅我。.我绘制了一个背景图像,在此我使用drawImage()方法绘制了5个小图像。 一旦我触摸了小图像,它们应该消失或弹出一个警报框,但到目前为止尚未发生ontouch事件,我已经搜索过ontouch事件但没有用。 我不想通过HTML中的img标签绘制图像。 这是我的代码段

var car = new Image();
car.src = "img/car.jpg";
    car.onload = function() {
    imgLoaded = true;
}
if (imgLoaded = true) {
    drawBackgroundImg();
    drawCarImg();

}


 function drawCarImg() {
if (i == 0) {
    x1 = Math.floor((Math.random() * 501));
    y1 = Math.floor((Math.random() * 301));
    cxt.save();
    cxt.drawImage(car, x1, y1, car.width, car.height);
    cxt.restore();

   }
  }
 car.on('touchstart', function() {
 alert("T");
 });

Here is my HTML code

<!DOCTYPE html>
  <body>   
<div id="container" onclick="void(0)">
    <canvas id="can"> </canvas>

</div>
<div id="btn">
    <input type="image" id="zoomIn" src="img/up.png" onclick="zoomIn()" />
    <input type="image" id="zoomOut" src="img/down.png"
        onclick="zoomOut()" />
</div>
<div id="score">
    <p id="scoreCount">
        <b></b>
    </p>
</div>
 </body>
</html>

任何人都可以帮助我获得解决方案。 仅供参考:我在Phonegap中而不​​是在浏览器中运行此代码。

$( '#id' ).on( 'touchstart',function(){
                     //your code
                       });

id是要在其上监听触摸事件的html元素的ID。

$('#id').on('touchmove',function(){
      alert("T");

  }); 
$('#id').on('click',function(){
       alert("T");

});

我找到了答案,我将div设置为不可见,并将图像放置在绝对位置,并使用图像的ID并触发touchstart事件。 上面说的例子在这里

  $('#id').on('touchmove',function(){
  alert("T");

   }); 
 $('#id').on('click',function(){
   alert("T");

  });

暂无
暂无

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

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