I'm trying to get up to speed on HTML5 by prototyping an extremely simple drawing app using <canvas>
and touch events like ontouchstart
and ontouchmove
.
This is displaying and working fine in an Android emulator where I use mouse clicks to simulate touch events. However, it doesn't work at all when running in my desktop browser (Safari 5.1.1, Firefox 7 on Windows).
I thought the mouse click events would be interpreted as touch events like they are when running within the emulator.
I wonder now if perhaps desktop browsers simply don't support touch events.
It's actually the other way round. Mobile browsers translate touch to mouse events for legacy support. If it needs to work on both mobile and desktop, and you don't really need touch events, you could just use mouse events instead. Failing that, bind both touch and mouse events and have the touch event cancel the mouse event. This way on mobile devices the touch fires and mouse doesn't. On desktop the touch doesn't fire and the mouse will.
For pilau, here's a simple example of cancelling the click event on mobile devices. Please note the question is more about drawing which would involve click dragging, which would require a bit more code for detection, but the basic idea is the same, bind the events you need to handle. Then e.preventdefault()
will stop the mobile browsers from emulating click type events.
$('#element').on('touchend click', function(e){
// Do your onclick action now
// cancel the onclick firing
e.preventDefault();
});
Firefox 6 and above supports the touch events. See the compatibility table here.
MDN article on Touch Events
EDIT : Are you testing with a touchscreen or the mouse? The mouse events do not automatically translate to touch events. See this post for how to simulate touch events with a mouse.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With