我正在开发 HTML5 Canvas 上的素描应用程序。我在画布上添加了“Touch Listeners”。
但只有 touchstart 和 touchmove 事件被触发。Touchend 不会被解雇。谁能解释为什么以及解决方法是什么?
<script type="text/javascript" charset="utf-8">
var canvas ;
var context ;
// create a drawer which tracks touch movements
var drawer = {
isDrawing: false,
touchstart: function(coors){
context.beginPath();
context.moveTo(coors.x, coors.y);
this.isDrawing = true;
},
touchmove: function(coors){
if (this.isDrawing) {
context.lineTo(coors.x, coors.y);
current_stroke+= coors.x+','+coors.y+';';
context.stroke();
}
},
touchend: function(coors){
if (this.isDrawing) {
context.lineTo(coors.x, coors.y);
current_stroke+= coors.x+','+coors.y+';';
context.stroke();
this.isDrawing = false;
}
}
}; // end of drawer
// create a function to pass touch events and coordinates to drawer
function draw(event){
// get the touch coordinates
var coors = {
x: event.targetTouches[0].pageX,
y: event.targetTouches[0].pageY
};
// pass the coordinates to the appropriate handler
drawer[event.type](coors);
}
$(document).ready(function() {
// get the canvas element and its context
canvas = document.getElementById('sketchpad');
context = canvas.getContext('2d');
context.lineWidth = 5;
context.strokeStyle = 'blue';
// attach the touchstart, touchmove, touchend event listeners.
canvas.addEventListener('touchstart',draw, false);
canvas.addEventListener('touchmove',draw, false);
canvas.addEventListener('touchend',draw, false);
// prevent elastic scrolling
document.body.addEventListener('touchmove',function(event){
event.preventDefault();
},false); // end body.onTouchMove
});
</script>