getting touchy - introduction to touch (and pointer) events / jquery europe 2014 / vienna 28.02.2014

175
getting touchy AN INTRODUCTION TO TOUCH (AND POINTER) EVENTS Patrick H. Lauke / jQuery Europe / Vienna / 28 February 2014

Upload: patrick-lauke

Post on 15-May-2015

7.165 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

getting touchy AN INTRODUCTION TO TOUCH (AND POINTER) EVENTS

Patrick H. Lauke / jQuery Europe / Vienna / 28 February 2014

Page 2: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch

Page 3: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

“how can I make my website work on touch devices?”

Page 4: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

you don't need touch eventsbrowsers emulate regular mouse events

Page 5: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_mouse-only.html

Page 6: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

(mouseenter) > mouseover > mousemove* > mousedown > (focus) > mouseup > click

*only a single “sacrificial” event is fired

Page 7: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

on first tap(mouseenter) > mouseover > mousemove > mousedown > (focus) > mouseup > click

subsequent tapsmousemove > mousedown > mouseup > click

tapping awaymouseout > (blur)

focus/blur only on focusable elements in Opera Mobile and Firefoxmouseout not on iOS Safari and embedded WebView (e.g. iOS Chrome)

Page 8: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

emulation works,but is limiting/problematic

Page 9: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

1. delayed event dispatch2. mouse-specific interfaces3. mousemove doesn't track

Page 10: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

1. delayed event dispatch2. mouse-specific interfaces3. mousemove doesn't track

Page 11: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_show-delay.html

Page 12: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_show-delay.html

Page 13: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

1. delayed event dispatch2. mouse-specific interfaces3. mousemove doesn't track

Page 14: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

vimeo.com/ondemand

Page 15: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

1. delayed event dispatch2. mouse-specific interfaces3. mousemove doesn't track

Page 16: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/particle/2

Page 17: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/particle/2(bug in iOS7: moving finger does seem to scroll and fire multiple mousemove events?!)

Page 18: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

“we need to go deeper...”

Page 19: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touch eventsintroduced in iOS 2.0, adopted in Chrome/Firefox/Opera

www.w3.org/TR/touch-events

Page 20: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touchstarttouchmove

touchend

touchcanceltouchentertouchleave

Page 21: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_all-no-timings.html

Page 22: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_all-no-timings.htmlBug 128534 - 'mouseenter' mouse compat event not fired when listeners for touch events

Page 23: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touchstart > [touchmove]+ > touchend > (mouseenter) > mouseover >

mousemove > mousedown > (focus) > mouseup > click

(mouse events only fired for single-finger tap)

Page 24: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

on first taptouchstart > [touchmove]+ > touchend >

(mouseenter) > mouseover > mousemove > mousedown > (focus) > mouseup > click

subsequent tapstouchstart > [touchmove]+ > touchend > mousemove > mousedown > mouseup > click

tapping awaymouseout > (mouseleave) > (blur)

too many touchmove events abort the tap (after touchend)too many touchmove events on activatable elements lead to touchcancel

not all browsers consistently send the touchmove

some browsers outright weird...

Page 25: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

Browser/Android 4.3(AppleWebKit/534.30)

mouseover > mousemove > touchstart > touchend > mousedown > mouseup > click

Browser/Blackberry PlayBook 2.0(AppleWebKit/536.2)

touchstart > mouseover > mousemove > mousedown > touchend > mouseup > click

Page 26: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touch eventsvs

limitations/problems

Page 27: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

1. delayed event dispatch2. mouse-specific interfaces3. mousemove doesn't track

Page 28: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

1. delayed event dispatch2. mouse-specific interfaces3. mousemove doesn't track

Page 29: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_show-delay.html

Page 30: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_show-delay.html

Page 31: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

why the delay?double-tap to zoom

(mostly anyway)

Page 32: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

what if browsers didn't wait?

Page 33: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

Puffin/Android (popular in Korea?) puffinbrowser.comdouble-tap zooms and fires mouse events + click

(also, doesn't support touch events at all)

Page 34: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

when does the delay happen?

Page 35: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener.html

Page 36: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener.html

Page 37: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

“how can we make it feel responsive like a native app?”

Page 38: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

react to events fired before the 300ms delay...

Page 39: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touchstartfor an “immediate” control

touchendfor a control that fires after finger lifted

Page 40: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

interlude:simple feature detection for

touch events

Page 41: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

if ('ontouchstart' in window) {

/* some clever stuff here */

}

/* older browsers have flaky support so more hacky tests needed... use Modernizr.touch or similar */

Page 42: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

/* common performance “trick” */

var clickEvent = ('ontouchstart' in window ? 'touchend' : 'click');

blah.addEventListener(clickEvent,function() { ... }, false);

Page 43: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

don't make it touch-exclusive

Page 44: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

hacks.mozilla.org/2013/04/detecting-touch...

Page 45: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

if ('ontouchstart' in window) {

/* browser supports touch events but user is not necessarily using touch (exclusively) */

}

Page 46: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

hybrid devicestouch + mouse + keyboard

Page 47: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014
Page 48: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014
Page 49: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

Android + mouse – behaves like touchtouchstart > touchend > mouseover > mousemove > mousedown >

(focus) > mouseup > click

Page 50: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

Android + keyboard – like desktop keyboardfocus > click

Page 51: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

VoiceOver enables keyboard access on iOS

Page 52: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

iOS + keyboard – similar to touchfocus > touchstart > touchend >

(mouseenter) > mouseover > mousemove > mousedownblur > mouseup > click

Page 53: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

iOS + VoiceOver – similar to touchfocus > touchstart > touchend >

(mouseenter) > mouseover > mousemove > mousedownblur > mouseup > click

Page 54: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

Android + TalkBack – keyboard/mouse hybridfocus > blur > mousedown > mouseup > click > focus(?)

(though seems a bit flakey...)

Page 55: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touch or mouse or keyboard

Page 56: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touch and mouse and keyboard

Page 57: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

/* doubled-up event listeners */

foo.addEventListener('touchend', someFunction, false);

foo.addEventListener('click', someFunction, false);

Page 58: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

foo.addEventListener('touchstart',function(e) {/* prevent delay + mouse events */e.preventDefault();

}, false);

/* doubled-up event listeners */

foo.addEventListener('touchend', someFunction, false);

foo.addEventListener('click', someFunction, false);

Page 59: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

preventDefaultcan kill scrolling, pinch/zoom, etc on

touchstart/touchmove

Page 60: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

github.com/ftlabs/fastclick

Page 61: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

browsers working to remove delay when possible

Page 62: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_user-scalable-no.html

Page 63: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_minimum-maximum-scale.html

Page 64: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014
Page 65: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_user-scalable-no.html

Page 66: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_user-scalable-no.html

Page 67: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_minimum-maximum-scale.html

Page 68: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

Bug 922896 - Optimizations to remove 300ms touch > mouse events delay[RESOLVED - FIXED]

Page 69: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

Chrome 32 for Android removes delay also when

<meta name="viewport" content="width=device-width">

updates.html5rocks.com/2013/12/300ms-tap-delay-gone-away

Page 70: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_user-scalable-no.html

Page 71: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

iOS/Safari designed themselves into a cornerwith “double-tap to scroll”

bugs.webkit.org/show_bug.cgi?id=122212

Page 72: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

1. delayed event dispatch2. mouse-specific interfaces3. mousemove doesn't track

Page 73: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

vimeo.com/ondemand

Page 74: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

no isolated hover (or focus) on touch devices*

* iOS fakes it, Samsung Galaxy S4 + stylus, ...

Page 75: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/css-dropdown/mouse-only.html

Page 77: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

Modernizr issue 869: Detecting a mouse user

Page 78: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

www.stucox.com/blog/you-cant-detect-a-touchscreen

Page 79: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

avoid hover-based interfaces?

complement for keyboard / touch!

Page 80: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/css-dropdown/mouse-keyboard-touch.html

Page 81: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

1. delayed event dispatch2. mouse-specific interfaces3. mousemove doesn't track

Page 82: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/particle/2

Page 83: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touchstart > [touchmove]+ > touchend >(mouseenter) > mouseover >

mousemove* > mousedown > (focus) >mouseup > click

*mouse event emulation fires only a single mousemove

Page 84: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

doubling up handling of mousemove and touchmove

Page 85: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

var posX, posY;

...

function positionHandler(e) {posX = e.clientX;posY = e.clientY;

}

...

canvas.addEventListener('mousemove', positionHandler, false);

Page 86: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

var posX, posY;

...

function positionHandler(e) {/* handle both mouse and touch? */

}

...

canvas.addEventListener('mousemove',positionHandler, false);

canvas.addEventListener('touchmove',positionHandler, false);

Page 87: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

interface MouseEvent : UIEvent {readonly attribute long screenX;readonly attribute long screenY;readonly attribute long clientX;readonly attribute long clientY;readonly attribute boolean ctrlKey;readonly attribute boolean shiftKey;readonly attribute boolean altKey;readonly attribute boolean metaKey;readonly attribute unsigned short button;readonly attribute EventTarget relatedTarget;void initMouseEvent(...);

};

www.w3.org/TR/DOM-Level-2-Events/events.html#Events-MouseEvent

Page 88: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

interface TouchEvent : UIEvent {readonly attribute TouchList touches;readonly attribute TouchList targetTouches;readonly attribute TouchList changedTouches;readonly attribute boolean altKey;readonly attribute boolean metaKey;readonly attribute boolean ctrlKey;readonly attribute boolean shiftKey;

};

www.w3.org/TR/touch-events/#touchevent-interface

Page 89: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

interface Touch {readonly attribute long identifier;readonly attribute EventTarget target;readonly attribute long screenX;readonly attribute long screenY;readonly attribute long clientX;readonly attribute long clientY;readonly attribute long pageX;readonly attribute long pageY;

};

www.w3.org/TR/touch-events/#touch-interface

Page 90: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/touchlist-objects

Page 91: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

var posX, posY;

...

function positionHandler(e) {if ((e.clientX)&&(e.clientY)) {

posX = e.clientX;posY = e.clientY;

} else if (e.targetTouches) {posX = e.targetTouches[0].clientX;posY = e.targetTouches[0].clientY;e.preventDefault();

}}

...

canvas.addEventListener('mousemove',positionHandler, false );

canvas.addEventListener('touchmove',positionHandler, false );

Page 92: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/particle/3

Page 93: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

tracking finger movementover time ... swipe gestures

Page 94: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/swipe

Page 95: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/swipe

Page 96: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/picture-slider

Page 97: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

don't forget mouse/keyboard !

Page 98: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touchmove fires...a lot!

Page 99: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

do absolute minimum on each touchmove

(usually: store new coordinates)

Page 100: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

heavy JavaScript on requestAnimationFrame

setInterval

Page 101: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/touch-limit

Page 102: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

why stop at a single point?multitouch support

Page 103: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

interface TouchEvent : UIEvent {readonly attribute TouchList touches;readonly attribute TouchList targetTouches;readonly attribute TouchList changedTouches;readonly attribute boolean altKey;readonly attribute boolean metaKey;readonly attribute boolean ctrlKey;readonly attribute boolean shiftKey;

};

www.w3.org/TR/touch-events/#touchevent-interface

Page 104: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

for (i=0; i<e.targetTouches.length; i++) {

...

posX = e.targetTouches[i].clientX;posY = e.targetTouches[i].clientY;

...

}

Page 105: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tracker/multi-touch-tracker.html

Page 106: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

iOS/iPad even preventDefault()can't override 4-finger gestures

Page 107: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

iOS7/Safari even preventDefault()can't override back/forward swipe gestures

Page 108: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

multitouch gestures

Page 109: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

/* iOS/Safari has gesture events for size/rotation, not supported in Chrome/Firefox/Opera, not part of the W3C Touch Events spec. */

gesturestart, gesturechange, gestureend

e.scale, e.rotation

Page 110: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

/* with some trigonometry we can replicate these from basic principles. */

var distance = Math.sqrt(Math.pow(...)+Math.pow(...));var angle = Math.atan2(...);

Page 111: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/picture-organiser

Page 112: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

older/cheaper devices/OS versionsand multitouch?

Page 113: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

LG Optimus 2X – Android 2.3.7

Page 114: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

ZTE Open – FirefoxOS 1.1

Page 115: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touch events andInternet Explorer...

Page 117: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

www.w3.org/TR/pointerevents

Page 118: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

not just some “not invented here” new approach for IE10+

Page 120: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

code.google.com/p/chromium/issues/detail?id=162757

Page 121: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

bugzilla.mozilla.org/show_bug.cgi?id=822898

Page 122: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014
Page 123: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

...what about Apple?

Page 124: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

bugs.webkit.org/show_bug.cgi?id=105463RESOLVED WONTFIX?!

Page 125: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_all-no-timings.html

Page 126: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_all-no-timings.html

Page 127: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

mousemove* >pointerover > mouseover >

pointerenter > mouseenter > pointerdown > mousedown > pointermove > mousemove >

gotpointercapture > focus >

pointerup > mouseup >lostpointercapture >

pointerout > mouseout >pointerleave > mouseleave >

click

mouse events fired “inline” with pointer events(for a primary pointer, e.g. first finger on screen)

Page 128: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

vendor-prefixed in IE10MSPointerDown etc

navigator.msMaxTouchPoints-ms-touch-action

unprefixed in IE11

Page 129: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

simple feature detection for pointer events

Page 130: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

if (window.PointerEvent) {

/* some clever stuff here but this covers touch, stylus, mouse, etc */

}

/* still listen to click for keyboard! */

Page 131: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

if (navigator.maxTouchPoints > 1) {

/* multitouch-capable device */

}

Page 132: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/pointer-multitouch-detect.html

Page 133: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

no need for separatemouse or touchevent listeners

Page 134: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

/* touch events: separate handling */

foo.addEventListener('touchmove', ... , false);foo.addEventListener('mousemove', ... , false);

/* pointer events: single listener for mouse, stylus, touch */

foo.addEventListener('pointermove', ... , false);

Page 135: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

no need for separatemouse or touch

code to get x / y coords

Page 136: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

interface MouseEvent : UIEvent {readonly attribute long screenX;readonly attribute long screenY;readonly attribute long clientX;readonly attribute long clientY;readonly attribute boolean ctrlKey;readonly attribute boolean shiftKey;readonly attribute boolean altKey;readonly attribute boolean metaKey;readonly attribute unsigned short button;readonly attribute EventTarget relatedTarget;void initMouseEvent(...);

};

www.w3.org/TR/DOM-Level-2-Events/events.html#Events-MouseEvent

Page 137: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

/* Pointer Events extend Mouse Events vs Touch Events and their completely new objects/arrays */

interface PointerEvent : MouseEvent {readonly attribute long pointerId;readonly attribute long width;readonly attribute long height;readonly attribute float pressure;readonly attribute long tiltX;readonly attribute long tiltY;readonly attribute DOMString pointerType;readonly attribute boolean isPrimary;

};

www.w3.org/TR/pointerevents/#pointerevent-interface

Page 138: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

but you can distinguishmouse or touch or stylus

if needed

Page 139: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

foo.addEventListener('pointermove', function(e) { ... switch(e.pointerType) { case 'mouse': ... break; case 'pen': ... break; case 'touch': ... break; default: /* future-proof */ ... } , false);

Page 140: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

“how can we make it feel responsive like a native app?”

Page 141: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

delayed event dispatch

Page 142: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener.html

Page 143: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

you can preventDefault()most mouse compatibility events on pointerdown

...but click is not considered mouse compatibility event, and the 300ms delay is not affected by this.

Page 144: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener.html

Page 145: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener.html

Page 146: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touch-action: auto|none|[pan-x][pan-y]

www.w3.org/TR/pointerevents/#the-touch-action-css-property

only prevents default touch action (e.g. double-tap to zoom)does not stop synthetic mouse events nor click

Page 147: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touch-action:none

Page 148: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tests/event-listener_touch-action-none.html

Page 149: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touch-action:nonekills scrolling, long-press,

pinch/zoom

Page 150: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

what about

multitouch?

Page 151: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

/* PointerEvents don't have the handy touch arrays, so we have to replicate something similar... */

Page 152: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

/* PointerEvents don't have the handy touch arrays, so we have to replicate something similar... */

var points = [];

switch (e.type) {

case 'pointerdown': /* add to the array */ break;

case 'pointermove': /* update the relevant array entry's x and y */ break;

case 'pointerup': /* remove the relevant array entry */ break;

}

Page 153: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

patrickhlauke.github.io/touch/tracker/multi-touch-tracker-pointer.html

Page 154: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

/* like iOS/Safari, IE10/Win has higher-level gestures, but these are not part of the W3C Pointer Events spec.

Replicate these from basic principles again? */

Page 155: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

pointer events as the future?

Page 156: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

what about backwards-compatibility?

Page 157: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

touchstart > [touchmove]+ > touchend >[300ms delay] > mouseenter > mouseover > mousemove > mousedown > mouseup > click

+mousemove > pointerover > mouseover > pointerdown > mousedown > pointermove >

mousemove > pointerup > mouseup > pointerout > mouseout > [300ms delay] > click

Page 158: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

www.w3.org/community/touchevents

Page 159: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

polyfills for pointer events(code for tomorrow, today)

Page 160: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

handjs.codeplex.com

Page 161: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

github.com/Polymer/PointerEvents

Page 162: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

utility libraries(because life is too short to hand-code gesture support etc)

Page 163: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

eightmedia.github.io/hammer.js

Page 164: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

jQuery Mobile? Sencha Touch? …check for support of IE10+ and “this is a touch device” assumptions

Page 165: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

debugging/testing

Page 166: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

Issue 181204: Emulate touch events - event order different from real devices

Page 167: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

Bug 920956 - DevTools touch emulation: suppress regular mouse events ...

Page 168: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

developers.google.com/chrome-developer-tools/docs/console#monitoring_events

Page 169: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

developers.google.com/chrome-developer-tools/docs/remote-debugging

Page 170: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

further reading...

Page 171: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

Matt Gaunt – Touch Feedback for Mobile Siteswww.gauntface.co.uk/blog/2013/06/25/touch-feedback-for-mobile-sites

Jonathan Stark – FastActivegithub.com/jonathanstark/FastActive

Stephen Woods – HTML5 Touch Interfaceswww.slideshare.net/ysaw/html5-touch-interfaces-sxsw-extended-version

David Rousset – Unifying touch and mouse: how Pointer Events will make cross-browsers touch support easyblogs.msdn.com/b/davrous/archive/2013/02/20/handling-touch[...]

Chris Wilson + Paul Kinlan – Touch And Mouse: Together Again For The First Timewww.html5rocks.com/en/mobile/touchandmouse

Patrick H. Lauke – Webseiten zum Anfassenwebkrauts.de/artikel/2012/einfuehrung-touch-events

Ryan Fioravanti – Creating Fast Buttons for Mobile Web Applicationsdevelopers.google.com/mobile/articles/fast_buttons

Boris Smus – Multi-touch Web Developmentwww.html5rocks.com/en/mobile/touch

Boris Smus – Generalized input on the cross-device websmus.com/mouse-touch-pointer

Page 172: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

Rick Byers + Boris Smus (Google I/O) – Point, Click, Tap, Touch - Building Multi-Device Web Interfacesdevelopers.google.com/events/io/sessions/361772634

Grant Goodale – Touch Eventswww.w3.org/conf/2011/#Touch_Events

W3C – Touch Events Extensionswww.w3.org/TR/2013/NOTE-touch-events-extensions-20131031

Mozilla Developer Network – Touch Eventsdeveloper.mozilla.org/en-US/docs/Web/Guide/API/DOM/Events/Touch_events

WebPlatform.org – Pointer Eventsdocs.webplatform.org/wiki/concepts/Pointer_Events

Rick Byers – The best way to avoid the dreaded 300ms click delay is to disable double-tap zoomplus.google.com/+RickByers/posts/ej7nsuoaaDa

Tim Kadlec – Avoiding the 300ms Click Delay, Accessiblytimkadlec.com/2013/11/Avoiding-the-300ms-click-delay-accessibly

Microsoft – Pointer events updates (differences between IE10 and IE11)msdn.microsoft.com/en-us/library/ie/dn304886(v=vs.85).aspx

Page 173: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

Patrick H. Lauke – Webseiten zum Anfassenwebkrauts.de/artikel/2012/einfuehrung-touch-events

Patrick H. Lauke – Drei unter einem Dach: Pointer-Events für Maus, Stylus und Touchscreenwebkrauts.de/artikel/2013/drei-unter-einem-dach

Tilo Mitra – The State of Gesturesspeakerdeck.com/tilomitra/the-state-of-gestures

Microsoft – Hover touch support (IE10/IE11)msdn.microsoft.com/en-us/library/ie/dn265029(v=vs.85).aspx

Stu Cox – The Golden Pattern for Handling Touch Inputwww.stucox.com/blog/the-golden-pattern-for-handling-touch-input/

Peter-Paul Koch – Touch tablewww.quirksmode.org/mobile/tableTouch.html

Peter-Paul Koch – Preventing the touch defaultwww.quirksmode.org/mobile/default.html

Peter-Paul Koch – Mouse event bubbling in iOSwww.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html

Page 174: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

youtube.com/watch?v=AZKpByV5764

Page 175: Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2014 / Vienna 28.02.2014

get in touch@patrick_h_lauke

github.com/patrickhlauke/touchslideshare.net/redux

paciellogroup.comsplintered.co.uk