![](/img/trans.png)
[英]How to move a html5 canvas element(rectangle) by touching it by onTouch method
[英]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.