getting touchy - an introduction to touch and pointer events / web rebels / oslo / 22 may 2014

206
getting touchy AN INTRODUCTION TO TOUCH AND POINTER EVENTS Patrick H. Lauke / Web Rebels / Oslo / 22 May 2014

Upload: patrick-lauke

Post on 27-Jan-2015

111 views

Category:

Technology


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

getting touchyAN INTRODUCTION TO TOUCH AND POINTER EVENTS

Patrick H. Lauke / Web Rebels / Oslo / 22 May 2014

Page 2: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch

Page 3: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Touch/pointer events test results

Page 4: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

“how can I make my websitework on touch devices?”

Page 5: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

you don't need touch eventsbrowsers emulate regular

mouse events

Page 6: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 7: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 8: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

compatibility mouse events(mouseenter) > mouseover > mousemove* > mousedown >

(focus) > mouseup > click

* only a single “sacrificial” mousemove event fired

Page 9: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 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 Firefoxmouseout not on iOS Safari/WebView (e.g. iOS Chrome)

Opera Mobile and

Page 10: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

emulation works,but is limiting/problematic

Page 11: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

1.  delayed event dispatch2.  mousemove doesn't track

Page 12: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

1.  delayed event dispatch2.  mousemove doesn't track

Page 13: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 14: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 15: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

1.  delayed event dispatch2.  mousemove doesn't track

Page 16: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/particle/2

Page 17: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/particle/2

(iOS7 bug: moving finger fires mousemove on scroll)

Page 18: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

“we need to go deeper...”

Page 19: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touch events

introduced by Apple, adoptedin Chrome/Firefox/Opera

www.w3.org/TR/touch-events

Page 20: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touchstarttouchmovetouchend

touchcancel

touchentertouchleave

Page 21: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 22: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Bug 128534 - 'mouseenter' mouse compat event not fired...

Page 23: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

events fired on taptouchstart > [touchmove]+ > touchend >

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

(mouse events only fired for single-finger tap)

Page 24: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 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)

Page 25: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

•  too many touchmove events prevent mouse compatibility events

after touchend (not considered a "clean" tap)

•  too many touchmove events on activatable elements can lead to

touchcancel (in old Chrome/Browser versions)

•  not all browsers consistently send the touchmove

some browsers outright weird...

Page 26: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 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 27: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Touch/pointer events test results

Page 28: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touch eventsvs

limitations/problems

Page 29: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

1.  delayed event dispatch2.  mousemove doesn't track

Page 30: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

1.  delayed event dispatch2.  mousemove doesn't track

Page 31: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 32: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

why the delay?double-tap to zoom

(mostly anyway)

Page 33: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

what if browsers didn't wait?

Page 34: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Puffin/Android double-tap zooms and fires mouse events + click(also, doesn't support touch events)

Page 35: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Try it out in Chrome? chrome://flags/

Page 36: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

when does the delay happen?

Page 37: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 38: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touch / mouse events delaytouchstart > [touchmove]+ > touchend >

[300ms delay]

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

Page 39: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 40: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

react to events fired before the300ms delay...

Page 41: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touchstart for an “immediate”control

(fire/jump button on a game)

Page 42: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touchend for a control thatfires after finger lifted

Page 43: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

interlude: simple featuredetection for touch events

Page 44: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* feature detection for touch events */

if ( 'ontouchstart' in window ) { /* some clever stuff here */}

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

Page 45: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* common performance “trick” */

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

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

Page 46: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

don't make it touch-exclusive

Page 47: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* common performance “trick” */

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

...

/* if touch events are supported, only listen to touchend, not click */

Page 48: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

hybrid devicestouch + mouse + keyboard

Page 49: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014
Page 50: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014
Page 51: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014
Page 52: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014
Page 53: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014
Page 54: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

(focus) > mouseup > click

Page 55: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Blackberry PlayBook 2.0 + mouse - like desktopmouse mouseover > mousedown > mousemove > mouseup > click

Page 56: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Android + keyboard – like desktop keyboardfocus > click

Page 57: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

iOS + VoiceOver (with/without keyboard) – similar to touchfocus > touchstart > touchend > (mouseenter) > mouseover >

mousemove > mousedown > blur > mouseup > click

Page 58: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 59: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

hacks.mozilla.org - Detecting touch [...]

Page 60: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* feature detection for touch events */

if ('ontouchstart' in window) { /* browser supports touch events but user is not necessarily using touch (exclusively) */

/* it could be a mobile, tablet, desktop, fridge ... */}

Page 61: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touch or mouse or keyboard

Page 62: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touch and mouse and keyboard

Page 63: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* doubled-up event listeners */

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

Page 64: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* prevent delay + mouse events */

foo.addEventListener(' touchstart ', function(e) { e.preventDefault();}, false);

/* doubled-up event listeners */

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

Page 65: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

preventDefault() killsscrolling, pinch/zoom, etc

Page 66: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

apply preventDefault()carefully

(just on buttons/links, not entire page)

Page 67: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

github.com/ftlabs/fastclick

Page 68: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

browsers working to removedouble-tap to zoom delay

(when page not zoomable)

Page 69: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

<meta name="viewport" content="user-scalable=no">patrickhlauke.github.io/touch/tests/event-listener_user-scalable-no.html

Page 70: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

<meta name="viewport" content="user-scalable=no">patrickhlauke.github.io/touch/tests/event-listener_user-scalable-no.html

Page 71: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

... content="minimum-scale=1, maximum-scale=1"patrickhlauke.github.io/touch/tests/event-listener_minimum-maximum-scale.html

Page 72: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

... content="minimum-scale=1, maximum-scale=1"patrickhlauke.github.io/touch/tests/event-listener_minimum-maximum-scale.html

Page 73: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Bug 922896 - Optimizations to remove 300ms [...] delay[RESOLVED - FIXED]

Page 74: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

what about accessibility?

Page 75: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014
Page 76: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 77: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Chrome 32+ / Android: ... content="width=device-width"updates.html5rocks.com/2013/12/300ms-tap-delay-gone-away

Page 78: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Bug 941995 - Remove 300ms [...] on "responsive" pages

Page 79: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 80: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

iOS/Safari designed themselves into a corner: “double-tap to scroll”bugs.webkit.org/show_bug.cgi?id=122212

Page 81: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

1.  delayed event dispatch2.  mousemove doesn't track

Page 82: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/particle/2

Page 83: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/particle/2(iOS7 bug: moving finger fires mousemove on scroll)

Page 84: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

events fired on taptouchstart > [touchmove]+ > touchend >

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

mouseup > click

* mouse event emulation fires only a single mousemove

too many touchmove events prevent mouse compatibility events after touchend

Page 85: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

doubling up handling ofmousemove and touchmove

Page 86: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

var posX, posY;...function positionHandler(e) { posX = e.clientX ; posY = e.clientY ;}...canvas.addEventListener(' mousemove ', positionHandler, false);

Page 87: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

var posX, posY;...function positionHandler(e) { /* handle both mouse and touch */}...canvas.addEventListener(' mousemove ', positionHandler, false);canvas.addEventListener(' touchmove ', positionHandler, false);

Page 88: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 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 89: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

partial interface MouseEvent { readonly attribute double screenX; readonly attribute double screenY; readonly attribute double pageX ; readonly attribute double pageY ; readonly attribute double clientX; readonly attribute double clientY; readonly attribute double x ; readonly attribute double y ; readonly attribute double offsetX ; readonly attribute double offsetY ;};

www.w3.org/TR/cssom-view/#extensions-to-the-mouseevent-interface

Page 90: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 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 91: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 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 92: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touches

all touch points on screen

targetTouches

all touch points that started on the element

changedTouches

touch points that caused the event to fire

Page 93: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/touchlist-objects

Page 94: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 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 95: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/particle/3

Page 96: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

tracking finger movement overtime ... swipe gestures

Page 97: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/swipe

Page 98: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/picture-slider

Page 99: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

don't forget mouse/keyboard!

Page 100: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

bradfrostweb.com/demo/mobile-first

Page 101: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touchmove fires...a lot!

Page 102: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

do absolute minimum on eachtouchmove

(usually: store coordinates)

Page 103: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

heavy JavaScript onsetInterval or

requestAnimationFrame

Page 104: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/touch-limit

Page 105: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

why stop at a single point?multitouch support

Page 106: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 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 107: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* iterate over touch array */

for (i=0; i< e.targetTouches .length; i++) { ... posX = e.targetTouches[i].clientX ; posY = e.targetTouches[i].clientY ; ...}

Page 108: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 109: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

gestures

Page 110: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

swipe gestures

Page 111: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

multitouch gestures

Page 112: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* iOS/Safari/WebView has gesture events for size/rotation,not part of the W3C Touch Events spec. */

gesturestart / gesturechange / gestureend

function(e) { /* e.scale e.rotation */}

/* not supported in Chrome/Firefox/Opera */

Page 113: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

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

Page 114: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/picture-organiser

Page 115: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

not all old/cheap devices/OSssupport multitouch!

Page 116: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

HTC Hero – Android 2.1

Page 117: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

LG Optimus 2X – Android 2.3.7

Page 118: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

ZTE Open – Firefox OS 1.1

Page 119: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

what about Internet Explorer?

Page 120: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

up to IE9 (Win7 / WinPhone7.5)only mouse events

Page 121: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

in IE10 Microsoft introducedPointer Events

Page 123: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

not just some“not invented here”

technology

Page 124: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Pointer Events - W3C Candidate Recommendation 09 May 2013

Page 125: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Pointer Events - W3C Editor's Draft 14 May 2014

Page 127: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Issue 162757: Implement pointer events in Chrome behind experimental flag

Page 128: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Bug 822898 - Implement pointer events

Page 129: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

...what about Apple?

Page 130: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Bug 105463 - Implement pointer events RESOLVED WONTFIX

Page 131: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 132: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 133: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

events fired on tapmousemove* >

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 134: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

vendor-prefixed in IE10MSPointerDown etc

navigator.msMaxTouchPoints-ms-touch-action

unprefixed in IE11 (but prefixed versions still mapped for compatibility)

Page 135: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 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;}

/* plus all the classic MouseEvent attributes like clientX , clientY , etc */

Page 136: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

simple feature detection forpointer events

Page 137: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* detecting pointer events support */

if ( window.PointerEvent ) { /* some clever stuff here but this covers touch, stylus, mouse, etc */}

/* still listen to click for keyboard! */

Page 138: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014
Page 139: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* detect maximum number of touch points */

if ( navigator.maxTouchPoints > 0 ) { /* device with a touchscreen */}

if ( navigator.maxTouchPoints > 1 ) { /* multitouch-capable device */}

Page 140: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

are pointer events better?

Page 141: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

no need for separate mouse ortouch event listeners

Page 142: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 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 143: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

no need for separate mouse ortouch code to get x / y coords

Page 144: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* Pointer Events extend Mouse Events */

foo.addEventListener(' pointermove ', function(e) { ... posX = e.clientX ; posY = e.clientY ; ...}, false);

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

Page 145: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

but you can distinguish mouseor touch or stylus if needed

Page 146: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 147: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

pointer eventsvs

limitations/problems of mouseevent emulation

Page 148: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

1.  delayed event dispatch2.  mousemove doesn't track

Page 149: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

1.  delayed event dispatch2.  mousemove doesn't track

Page 150: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/tests/event-listener_show-delay.html(IE/Win8 has double-tap to zoom, so problem on desktop too)

Page 151: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 152: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 153: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

pointer / mouse events and delaymousemove >

pointerover > mouseover >pointerenter > mouseenter >pointerdown > mousedown >pointermove > mousemove >

gotpointercapture >focus >

pointerup > mouseup >lostpointercapture >

pointerout > mouseout >pointerleave > mouseleave >

[300ms delay]click

Page 154: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 155: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

we could try a similarapproach to touch events...

Page 156: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

•  double-up listeners - pointerup and click

•  prevent code firing twice - preventDefault

preventDefault() on pointerdown stops mouse compatibilityevents, but click is not considered mouse compatibility event

Page 157: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 158: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touch-action

Page 159: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

CSS propertytouch-action: auto | none | [ pan-x || pan-y ] | manipulation

www.w3.org/TR/pointerevents/#the-touch-action-css-propertyonly prevents default touch action (e.g. double-tap to zoom) does not

stop synthetic mouse events nor click

Page 160: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touch-action:none killsscrolling, long-press,

pinch/zoom

Page 161: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touch-action:manipulation

Page 162: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/tests/event-listener_touch[...]

Page 163: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Bug 979345 - Implement touch-action:manipulation [...]

Page 164: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Issue 349016: Add support for touch-action:manipulation

Page 165: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Bug 133114 - Implement " touch-action:manipulation " [...]

Page 166: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

1.  delayed event dispatch2.  mousemove doesn't track

Page 167: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/particle/2

Page 168: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touch-action:none

Page 169: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

patrickhlauke.github.io/touch/particle/2a

Page 170: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

what about multitouch?

Page 171: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* PointerEvents don't have the handy TouchList objects, 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 172: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

(note multiple isPrimary pointers)

Page 173: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Replicate these from basic principles again... */

Page 174: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* advanced topic: pointer capture */

gotpointercapture / lostpointercapture

element.setPointerCapture(pointerId)

patrickhlauke.github.io/touch/tests/pointercapture.html

Page 175: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

what about backwards-compatibility?

Page 176: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

touchstart > [touchmove]+ > touchend >[300ms delay] >

mouseover > mousemove > mousedown > mouseup > click

vs

pointerover > mouseover > pointerdown > mousedown >pointermove > mousemove > pointerup > mouseup >pointerout > mouseout > [300ms delay] > click

Page 177: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

W3C Touch Events Community Group

Page 178: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* cover all cases (hat-tip Stu Cox) */

if ('onpointerdown' in window) {

/* bind to Pointer Events: pointerdown, pointerup, etc */

} else {

/* bind to mouse events: mousedown, mouseup, etc */

if ('ontouchstart' in window) { /* bind to Touch Events: touchstart, touchend, etc */ }}

/* bind to keyboard / click */

Page 179: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

polyfills for pointer events(code for tomorrow, today)

Page 181: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

www.catuhe.com/msdn/handjs/

Page 183: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

GitHub - Polymer/PointerEvents

Page 184: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* Polymer's PointerEvents are not fired unless an element has a (custom) touch-action attribute */

<div id="foo" touch-action="none" ></div>

Page 185: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 187: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

/* Hammer's high-level events example */

var element = document.getElementById('test_el');

var hammertime = Hammer(element).on("swipeleft swiperight", function(event) { /* handle horizontal swipes */});

Page 188: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

jQuery Mobile? Sencha Touch?check for support of IE10+

and “this is a touch device”

assumptions

Page 189: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

debugging/testing

Page 190: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Chrome DevTools / Using the Console / Monitoring events

Page 191: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

chrome://flags/#touch-events

Page 192: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

beware inaccurate emulation

Page 193: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Issue 181204: [...] event order different from real devicesFixed in Chrome (Canary) 37

Page 194: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

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

Page 195: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

beware inaccurateimplementation

Page 196: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014
Page 197: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Bug 861876 - [...] multiple mousemoves being fired

Page 198: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

Bug 861876 - [...] preventDefault on touchstart doesn't stop synthetic mouse events

Page 199: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014
Page 200: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

further reading...

Page 201: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

•  Matt Gaunt – Touch Feedback for Mobile Sites

•  Jonathan Stark – FastActive

•  Stephen Woods – HTML5 Touch Interfaces

•  Chris Wilson + Paul Kinlan – Touch And Mouse: Together Again For

The First Time

•  Ryan Fioravanti – Creating Fast Buttons for Mobile Web Applications

•  Boris Smus – Multi-touch Web Development

•  Boris Smus – Generalized input on the cross-device web

•  Boris Smus – Interactive touch laptop experiments

Page 202: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

•  Rick Byers + Boris Smus (Google I/O) – Point, Click, Tap, Touch -

Building Multi-Device Web Interfaces

•  Grant Goodale – Touch Events

•  W3C – Touch Events Extensions

•  Mozilla Developer Network – Touch Events

•  WebPlatform.org – Pointer Events

•  Rick Byers – The best way to avoid the dreaded 300ms click delay is

to disable double-tap zoom

•  Chromium Issue 152149: All touch-event related APIs should exist if

touch support is enabled at runtime

Page 203: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

•  Tim Kadlec – Avoiding the 300ms Click Delay, Accessibly

•  Microsoft – Pointer events updates (differences between IE10-IE11)

•  Patrick H. Lauke – Webseiten zum Anfassen

•  Patrick H. Lauke – Drei unter einem Dach: Pointer-Events für Maus,

Stylus und Touchscreen

•  Patrick H. Lauke – Make your site work on touch devices

•  Stu Cox – You can't detect a touchscreen

•  Tilo Mitra – The State of Gestures

•  Microsoft – Hover touch support (IE10/IE11)

•  W3C Media Queries Level 4 – pointer

Page 204: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

•  Stu Cox – The Good & Bad of Level 4 Media Queries

•  Peter-Paul Koch – Touch table

•  Peter-Paul Koch – Preventing the touch default

•  Peter-Paul Koch – Mouse event bubbling in iOS

•  Edge Conference (Feb 2013 London) – Panel: Input

•  Edge Conference (Mar 2014 London) – Panel: Pointers and Interactions

•  Trent Walton – Device-Agnostic

•  Stu Cox – The Golden Pattern for Handling Touch Input

•  Matt Gaunt – ‘Focusing’ on the Web Today

•  Mobiscroll – Working with touch events

Page 205: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

youtube.com/watch?v=AZKpByV5764

Page 206: Getting touchy - an introduction to touch and pointer events / Web Rebels / Oslo / 22 May 2014

get in touch@patrick_h_lauke

github.com/patrickhlauke/touchslideshare.net/redux

paciellogroup.comsplintered.co.uk