Reputation: 41
I have a little question of the touch handler...it sometimes work on touch sometimes not, and it cant read my data after i draw it, and it draw with straight line, so i wonder what problem and what i did wrong? please help me..i already put my code into jsfiddle..please help me (http://jsfiddle.net/Frebu/1/)
function touchHandler(event) {
var touches = event.changedTouches,
first = touches[0],
type = "";
switch (event.type) {
case "touchstart": type = "mousedown"; break;
case "touchmove": type = "mousemove"; break;
case "touchend": type = "mouseup"; break;
default: return;
}
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type, true, true, window, 1,
first.screenX, first.screenY,
first.clientX, first.clientY, false,
false, false, false, 0/*left*/, null);
first.target.dispatchEvent(simulatedEvent);
event.preventDefault();
}
function init(id) {
document.getElementById(id).addEventListener("touchstart", touchHandler, true);
document.getElementById(id).addEventListener("touchmove", touchHandler, true);
document.getElementById(id).addEventListener("touchend", touchHandler, true);
}
$(document).ready(function() {
init('myCanvas');
});
Upvotes: 1
Views: 1774
Reputation: 1368
My recomendation is to use hammer.js, it's a great touch library (that fallbacks to mouse on browsers).
http://eightmedia.github.com/hammer.js/
Aside from that, i didn't have any issues testing the fiddle on Chrome simulating touch events. And altought weird, the code seems to be fine.
Upvotes: 1