getting touchy - an introduction to touch and pointer events / frontend ne / 2 march 2017

273
getting touchy AN INTRODUCTION TO TOUCH AND POINTER EVENTS Patrick H. Lauke / Frontend NE / 2 March 2017

Upload: patrick-lauke

Post on 19-Mar-2017

97 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

getting touchyAN INTRODUCTION TO TOUCH AND POINTER EVENTS

Patrick H. Lauke / Frontend NE / 2 March 2017

Page 2: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

about me...•   senior accessibility consultant at The Paciello Group

•   previously developer relations at Opera

•   co-editor Touch Events Level 2

•   WG chair and co-editor Pointer Events Level 2

Page 3: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

github.com/patrickhlauke/getting-touchy-presentation"evergreen" expanded version of this presentation

(and branches for specific conferences)

Page 4: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch

Page 5: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017
Page 6: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Touch/pointer events test results

Page 7: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

my JavaScript sucks... (but will hopefully convey the right concepts)

Page 8: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

how can I make my website work on touch devices?

Page 9: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

you don't need touch events browsers emulate regular

mouse events

Page 10: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 11: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 12: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

(focus) > mouseup > click* only a single sacrificial mousemove event fired

Page 13: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

on first tap(mouseenter) > mouseover > mousemove >

mousedown > (focus) > mouseup > click

subsequent tapsmousemove > mousedown > mouseup > click

tapping away(mouseout) > (blur)

focus / blur only on focusable elements; subtle differences between browsers Mobile/tablet touchscreen activation/tap event order

Page 14: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

emulation works, but is limiting/problematic

Page 15: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

1.  delayed event dispatch2.  mousemove doesn't track

Page 16: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

1.  delayed event dispatch2.  mousemove doesn't track

Page 17: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/tests/event-listener_show-delay.htmlless of a problem in modern browsers, but for iOS < 9.3 or older Androids still relevant

Page 18: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 19: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

1.  delayed event dispatch2.  mousemove doesn't track

Page 20: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/particle/2

Page 21: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/particle/2

Page 22: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

we need to go deeper...

Page 23: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

touch events

introduced by Apple, adoptedin Chrome/Firefox/Opera

(and belatedly IE/Edge – more on that later)

www.w3.org/TR/touch-events

Page 24: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

caniuse.com: Can I use touch events?

Page 25: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

touchstart touchmove touchend

touchcancel

Page 26: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 27: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

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

(mouse events only fired for single-finger tap)

Page 28: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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 29: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

•   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 touchmove•   differences in focus / blur and some mouse compatibility events

(e.g. mouseenter / mouseleave )

•   some events may only fire when tapping away to another focusable

element (e.g. blur )

some browsers outright weird...

Page 30: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Browser/Android 4.3mouseover > mousemove > touchstart > touchend >

mousedown > mouseup > click

Browser/Blackberry PlayBook 2.0touchstart > mouseover > mousemove > mousedown >

touchend > mouseup > click

UC Browser 10.8/Android 6mouseover > mousemove > touchstart > (touchmove)+ > touchend >

mousedown > focus > mouseup > click

Page 31: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Touch/pointer events test results

Page 32: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

shouldn't affect your code,unless you're expecting a very

specific sequence...

Page 33: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

<interlude >simple feature detection for

touch events

Page 34: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/� 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 35: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/tests/touch-feature-detect.html

Page 36: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* conditional "touch OR mouse/keyboard" event binding */ if ('ontouchstart' in window) { // set up event listeners for touch ... } else { // set up event listeners for mouse/keyboard ... }

Page 37: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

don't make it touch-exclusive

Page 38: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

hybrid devices touch + mouse + keyboard

Page 39: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017
Page 40: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017
Page 41: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

@patrick_h_lauke showing a naive "touch or mouse" issue on Flickr (which has since been fixed)

Page 42: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Bug 888304 - touch-events on Firefox-desktop should be disabled until we can support themproperly

Page 43: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Issue 392584: TouchEvent API should be enabled consistently

Page 44: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Chrome now returns window.TouchEvent on non-touch devicespatrickhlauke.github.io/touch/tests/touch-feature-detect.html

Page 45: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

even on "mobile" we can havemultiple inputs...

Page 46: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017
Page 47: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017
Page 48: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Android + mouse – like touch (mouse emulating touch)touchstart > touchend > mouseover > mousemove > mousedown >

(focus) > mouseup > click

Page 49: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Blackberry PlayBook 2.0 + mouse – like desktopmouseover > mousedown > (mousemove)+ > mouseup > click

Page 50: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Blackberry Leap (BBOS 10.1) + mouse – like desktopmouseover > mousedown > (mousemove)+ > mouseup > click

Page 51: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Windows 10 Mobile/Microsoft Edge + mouse – like desktopmouse events (+ pointer events) + click

Page 52: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Windows 10 Mobile/Microsoft Edge + keyboard – like desktopfocus > click

Page 53: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Android + keyboard – like desktopfocus > click

Page 54: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

iOS keyboard only works insame situations as on-screen

keyboard (e.g. text inputs, URL entry)

Page 55: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

VoiceOver enables full keyboard access on iOS

Page 56: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

iOS + keyboard – similar to touchfocus � touchstart > touchend > (mouseenter) > mouseover >

mousemove > mousedown > blur > mouseup > click

Page 57: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

further scenarios?•   desktop with external touchscreen

•   touchscreen laptop with non-touch second screen

•   touchscreen laptop with trackpad/mouse

•   ...and other permutations?

Page 58: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

no way to detect these cases...

Page 59: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Modernizr.touch detects touch events not touch devices

Page 60: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Stu Cox: “ou can't detect a touchscreen

Page 61: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 62: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* 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 63: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

touch or mouse or keyboard

Page 64: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

touch and mouse and keyboard

Page 65: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

what about CSS4 Media Queries?

Page 66: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

W3C Media Queries Level 4 (Editor's Draft)

Page 67: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* MQ4 Interaction Media Features*/ pointer: none | coarse | fine hover: none | hover any-pointer: none | coarse | fine any-hover: none | hover

hover: on-demand / any-hover: on-demand removed in recent drafts

Page 68: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

dev.opera - Interaction Media Features and their potential (for incorrect assumptions)

Page 69: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/pointer-hover-any-pointer-any-hoverprimary input is unchanged (and Chrome/Android required a full restart)

Page 70: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* Naive uses of Interaction Media Features */ @media (pointer: fine) { /* primary input has fine pointer precision... so make all buttons/controls small? */ } @media (hover: hover) { /* primary input has hover...so we can rely on it? */ } /* pointer and hover only check "primary" input, but what if there's a secondary input that lacks capabilities? */

Page 71: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* Better uses of Interaction Media Features */ @media (any-pointer: coarse) { /* at least one input has coarse pointer precision... provide larger buttons/controls for touch */ } /* test for *any* "least capable" inputs (primary or not) */

Limitation: [mediaqueries-4] any-hover can't be used to detect mixed hover and non-hover

capable pointers. Also, pointer / hover / any-pointer / any-hover don't cover non-pointerinputs (e.g. keyboards) – always assume non-hover-capable inputs

@media (any-hover: none) {/* at least one input lacks hover capability...

don't rely on it or avoid altogether */}

Page 72: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

detect which input the users isusing right now...

Page 73: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

GitHub: ten1seven / what-input

Page 74: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Demo: What Input?

Page 75: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

if in doubt, offer a way to switch interfaces... or just always use a touch-optimised interface

Page 76: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

<� interlude >

Page 77: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

touch events vs

limitations/problems

Page 78: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

1.  delayed event dispatch2.  mousemove doesn't track

Page 79: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

1.  delayed event dispatch2.  mousemove doesn't track

Page 80: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/tests/event-listener_show-delay.htmlless of a problem in modern browsers, but for iOS < 9.3 or older Androids still relevant

Page 81: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

why the delay? double-tap to zoom

(mostly anyway)

Page 82: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

when does the delay happen?

Page 83: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 84: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

[300ms delay] (mouseenter) > mouseover > mousemove > mousedown >

(focus) > mouseup > click

Page 85: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

how can we make it feelresponsive like a native app?

Page 86: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

react to events fired before the300ms delay...

Page 87: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

touchstart for animmediate control (e.g. fire/jump button on a game)

Page 88: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

touchend for a control thatfires after finger lifted

(but this can result in events firing after zoom/scroll)

Page 89: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

don't make it touch-exclusive

Page 90: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* DON'T DO THIS: conditional "touch OR mouse/keyboard" event binding */ if ('ontouchstart' in window) { // set up event listeners for touch foo.addEventListener('touchend', ...); ... } else { // set up event listeners for mouse/keyboard foo.addEventListener('click', ...); ... }

Page 91: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/tests/event-listener_naive-touch-or-mouse.html

Page 92: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* DO THIS: doubled-up "touch AND mouse/keyboard" event binding */ // set up event listeners for touch foo.addEventListener('touchend', ...); // set up event listeners for mouse/keyboard foo.addEventListener('click', ...); /* but this would fire our function twice for touch? */

patrickhlauke.github.io/touch/tests/event-listener_naive-event-doubling.html

Page 93: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* DO THIS: doubled-up "touch AND mouse/keyboard" event binding */ // set up event listeners for touch foo.addEventListener('touchend', function(e) { // prevent compatibility mouse events and click e.preventDefault(); ... }); // set up event listeners for mouse/keyboard foo.addEventListener('click', ...);

patrickhlauke.github.io/touch/tests/event-listener_naive-event-doubling-fixed.html

Page 94: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

preventDefault() kills scrolling, pinch/zoom, etc

Page 95: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

apply preventDefault()carefully

(just on buttons/links, not entire page)

Page 96: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

github.com/ftlabs/fastclick

Page 97: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/fastclick“ouTube: iOS/Safari 300ms click delay: vanilla Bootstrap and using fastclick.js

Page 98: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/fastclick/fastclick.html“ouTube: iOS/Safari 300ms click delay: vanilla Bootstrap and using fastclick.js

Page 99: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

browsers working to removedouble-tap to zoom delay

Page 100: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

non-scalable/zoomableviewport and

"double-tap to zoom"

Page 101: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 102: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Changeset 191072 - Web pages with unscalable viewports shouldn't have a single tap delay(from iOS 9.3 onwards)

Page 103: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

what about accessibility?

Page 104: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Chrome: Settings > Accessibility > Force enable zoom

Page 105: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Opera: Settings > Force enable zoom

Page 106: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Firefox: Settings > Accessibility > Always enable zoom

Page 107: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Samsung Internet: Internet settings > Manual zoom

Page 108: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

"Force enable zoom" reintroduces delay – a fair compromise?patrickhlauke.github.io/touch/tests/event-listener_user-scalable-no.html

Page 109: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

@thomasfuchs - Safari/iOS10beta3 ignores unscalable viewports(no official Apple documentation about the change...but the 300ms delay is back)

Page 110: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Windows 10 build 15007 on Mobile ignores unscalable viewports

Page 111: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

"mobile optimised" viewportand

"double-tap to zoom"

Page 112: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Chrome 32+ / Android: content="width=device-width" suppresses double-tap-to-zoom, still allows pinch zoom

Google Developers: 300ms tap delay, gone away

Page 113: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 114: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

WebKit blog: More Responsive Tapping on iOS

Page 115: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/tests/results/#suppressing-300ms-delay

Page 116: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

suppressing 300ms delayif your code does rely on handling click / mouse events:

•   "mobile optimised" <meta name="viewport"content="width=device-width">

•   add touch-action:manipulation in CSS (part of Pointer Events)

•   use helper library like fastclick.js for older browsers

•   make your viewport non-scalable...

Page 117: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

1.  delayed event dispatch2.  mousemove doesn't track

Page 118: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/particle/2

Page 119: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/particle/2

Page 120: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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 121: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

doubling up handling ofmousemove and touchmove

Page 122: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 123: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

�ar posX, posY; function positionHandler(e) { posX = e.clientX ; posY = e.clientY ; } canvas.addEventListener(' mousemove ', positionHandler, false); canvas.addEventListener(' touchmove ', positionHandler, false); /* but this won't work for touch... */

Page 124: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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; // [DOM4] UI Events readonly attribute unsigned short buttons; };

www.w3.org/TR/DOM-Level-2-Events/events.html#Events-MouseEventwww.w3.org/TR/uievents/#events-mouseevents

Page 125: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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 126: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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 127: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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 128: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

TouchList differencestouches

all touch points on screen

targetTouchesall touch points that started on the element

changedTouchestouch points that caused the event to fire

Page 129: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/touchlist-objects

Page 130: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

�ar 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 131: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/particle/3

Page 132: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/paranoid_0.5www.splintered.co.uk/experiments/archives/paranoid_0.5

Page 133: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/picture-slider

Page 134: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

tracking finger movement overtime ... swipe gestures

Page 135: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/swipe

Page 136: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* Swipe detection from basic principles */ Δt = end.time - start.time; Δx = end.x - start.x; Δy = end.y - start.y; if (( Δt > timeThreshold ) || ( (Δx + Δy) < distanceThreshold )) { /* too slow or movement too small */ } else { /* it's a swipe! - use �x and �y to determine direction - pythagoras √( �x² + �y² ) for distance - distance/�t to determine speed */ }

Page 137: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

don't forget mouse/keyboard!

Page 138: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

touchmove fires...a lot!

Page 139: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

do absolute minimum on eachtouchmove

(usually: store coordinates)

Page 140: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

do heavy lifting (drawing etc.)separately, avoid queueing

(e.g. using setTimeout and/or requestAnimationFrame )

Page 141: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

debounce / throttle events

Page 142: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

GitHub: m-gagne / limit.js

Page 143: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/touch-limit

Page 144: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

why stop at a single point? multitouch support

Page 145: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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 146: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* iterate over relevant TouchList */ for (i=0; i< e.targetTouches .length; i++) { ... posX = e.targetTouches[i].clientX ; posY = e.targetTouches[i].clientY ; ... }

Page 147: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 148: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 149: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

iOS7+ Safari/WebViewpreventDefault() can't

override edge swipes

Page 150: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

multitouch gestures

Page 151: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* iOS/Safari/WebView has gesture events for size/rotation, not part of the W3C Touch Events spec. */ /* gesturestart / gesturechange / gestureend */ foo.addEventListener('gesturechange', function(e) { /* e.scale e.rotation */ /* values can be plugged directly into CSS transforms etc */ }); /* not supported in Chrome/Firefox/Opera */

iOS Developer Library - Safari Web Content Guide - Handling gesture events

Page 152: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* Pinch/rotation detection from basic principles */ Δx = x2 - x1; Δy = y2 - y1;

/* pythagoras √( �x² + �y² ) to calculate distance */ Δd = Math.sqrt( (Δx * Δx) + (Δy * Δy) );

/* trigonometry to calculate angle */ α = Math.atan2( Δy, Δx );

Mozilla Developer Network: Math.atan2()

Page 153: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/picture-organiser

Page 154: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Touch Events specification grey areas...

Page 155: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

do touch events fire duringscroll/zoom?

Page 156: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

not defined in spec (yet), but de facto yes in most modern browsers

patrickhlauke.github.io/touch/gesture-touch

Page 157: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Touch Events extensions...

Page 158: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

W3C Touch Events Extensions WG Note

Page 159: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* Extension to touch objects */ partial interface Touch { readonly attribute float radiusX; readonly attribute float radiusY; readonly attribute float rotationAngle; readonly attribute float force; };

Page 160: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* Touch Events – contact geometry */ partial interface Touch { readonly attribute float radiusX ; readonly attribute float radiusY ; readonly attribute float rotationAngle ; readonly attribute float force; };

Page 161: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/tracker/tracker-radius-rotationangle.html“ouTube: Touch Events: radiusX, radius“ and rotationAngle

Page 162: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* Touch Events – force */ partial interface Touch { readonly attribute float radiusX; readonly attribute float radiusY; readonly attribute float rotationAngle; readonly attribute float force ; };

force : value in range 0 – 1 . if no hardware support 0 (some devices, e.g. Nexus 10, "fake" force based on radiusX / radiusY )

Page 163: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/tracker/tracker-force-pressure.htmliPhone 6s with 3D Touch supports force ...

(Safari and WKWebView, e.g. Chrome/iOS, but not UIWebView, e.g. Firefox/iOS)

Page 164: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

W3C Touch Events Level 2 (Editor's Draft)(merges errata, touch events extensions, fractional touch coordinates)

Page 165: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Pointer Events

Page 166: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 167: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

in IE10 Microsoft introducedPointer Events

Page 168: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

unifies mouse, touch and pen input into a single event model

Page 169: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

...and some new inputs (though currently mapped to mouse)Building Xbox One Apps using HTML and JavaScript

“ouTube: Xbox One Edge Browser sends Pointer Events

Page 170: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

not just some not invented here

technology

Page 171: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

submitted by Microsoft as W3C Candidate REC 09 May 2013

Page 172: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Pointer Events - W3C REC 24 February 2015

Page 173: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

work now continues to enhance/expand Pointer Events...W3C Pointer Events Level 2 (Editor's Draft)

Page 174: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

caniuse.com: Can I use pointer events?

Page 175: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Bug 822898 - Implement pointer events

Page 176: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

about:config � dom.w3c_pointer_events.enabled

Page 177: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

...what about Apple?

Page 178: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Microsoft submitted a proof of concept WebKit patchhtml5labs.interoperabilitybridges.com/prototypes/...

Page 179: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Bug 105463 - Implement pointer events RESOLVED WONTFIX

Page 180: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Maciej Stachowiak - [webkit-dev] pointer events specification - first editors draft

Page 181: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

@patrick_h_lauke paraphrasing Apple's stance on Pointer Events...

Page 182: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017
Page 183: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Apple Pencil currentlyindistinguishable from touch

in browser / JavaScript (no tilt information, fires touch events)

Page 184: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

the anatomy of Pointer Events (sequence, event object, ...)

Page 185: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 186: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

events fired on tap (Edge)mousemove* >

pointerover > mouseover > pointerenter > mouseenter > pointerdown > mousedown >

focus gotpointercapture >

pointermove > mousemove > pointerup > mouseup > lostpointercapture >

click > pointerout > mouseout > pointerleave > mouseleave

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

Page 187: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

IE10/IE11 quirks•   vendor-prefixed in IE10 ( MSPointerDown ...,

navigator.msMaxTouchPoints , -ms-touch-action )

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

•   event order (when click is fired) incorrect in IE10/IE11

see W3C Pointer Events WG mailing list - Jacob Rossi (Microsoft)

Page 188: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Chrome, Edge (on mobile), IE11 (Windows Phone 8.1update1) support both Touch Events and Pointer Events

w3c.github.io/pointerevents/#mapping-for-devices-that-do-not-support-hover

Page 189: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 190: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

about:flags in Microsoft Edge to turn on touch events on desktop(e.g. touch-enabled laptops) – off by default for site compatibility

Page 191: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

... when touch events alsosupported (Edge)

pointerover > pointerenter > pointerdown > touchstart > pointerup > touchend >

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

pointerout > pointerleaveSpecific order of events is not defined in the spec in this case – will vary between browsers...

only problem if handling both pointer events and mouse events (which is nonsensical)

Page 192: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* Pointer Events extend Mouse Events vs Touch Events and their new objects / TouchLists / Touches */ interface PointerEvent : MouseEvent { readonly attribute long pointerId; readonly attribute long width; readonly attribute long height; readonly attribute float pressure; readonly attribute float tangentialPressure; /* Level 2 */ readonly attribute long tiltX; readonly attribute long tiltY; readonly attribute long twist; /* Level 2 */ readonly attribute DOMString pointerType; readonly attribute boolean isPrimary; }

/* plus all MouseEvent attributes: clientX , clientY , etc */

Page 193: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

handling mouse input isexactly the same as traditional

mouse events (only change pointer* instead of mouse* event names)

Page 194: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

handling touch or stylus isalso exactly the same astraditional mouse events

(mouse code can be adapted to touch/stylus quickly)

Page 195: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

simple feature detection forpointer events

Page 196: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* 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 197: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

"don't forget about keyboard users" note in Pointer Events spec

Page 198: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* detect maximum number of touch points */ if ( navigator.maxTouchPoints > 0 ) { /* device with a touchscreen */ } if ( navigator.maxTouchPoints > 1 ) { /* multitouch-capable device */ }

"you can detect a touchscreen" ...but user may still use other input mechanisms

Page 199: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 200: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

do pointer events fire duringscroll/zoom?

Page 201: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

w3c.github.io/pointerevents/#the-pointercancel-event

Page 202: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

once a browser handles scrolling, it sends pointercancelpatrickhlauke.github.io/touch/gesture-touch/pointerevents.html

Page 203: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

pointer events vs

limitations/problems of mouseevent emulation

Page 204: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

1.  delayed event dispatch2.  mousemove doesn't track

Page 205: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

1.  delayed event dispatch2.  mousemove doesn't track

Page 206: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

(IE11/WinPhone 8.1 Update no optimization for width=device-width )

Page 207: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 208: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/tests/event-listener.html(Microsoft Edge/Win10 has double-tap to zoom, so problem on desktop too)

Page 209: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

pointer / mouse events and delay

[300ms delay] click ...

300ms delay just before click event

Page 210: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

how can we make it feelresponsive like a native app?

Page 211: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

we could try a similarapproach to touch events...

Page 212: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

•   double-up pointerup and click listeners?

•   prevent code firing twice with preventDefault ?

won't work: preventDefault() stops mouse compatibility events, butclick is not considered mouse compatibility event

Page 213: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

a more declarative approach with touch-action

Page 214: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

CSS propertywhat action should the browser handle?

touch-action: auto | none | [ pan-x || pan-y ] | manipulationwww.w3.org/TR/pointerevents/#the-touch-action-css-property

only determines default touch action, does not stop compatibilitymouse events nor click

Page 215: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Pointer Events Level 2expanded set of values (useful for pull-to-refresh, carousels, etc)

touch-action: auto | none | [ [ pan-x | pan-left | pan-right ] || [ pan-y | pan-up | pan-down ] ] | manipulation

w3c.github.io/pointerevents/#the-touch-action-css-property

Page 216: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

compat.spec.whatwg.org add extra value pinch-zoom

Page 217: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/touch-action-scrolling

Page 218: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

touch-action:none(suppress all default browser behaviour)

Page 219: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 220: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

touch-action:none killsscrolling, long-press,

pinch/zoom

Page 221: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

touch-action:manipulation(suppress double-tap-to-zoom)

Page 222: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 223: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

caniuse.com: Can I use touch-action?

Page 224: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

browsers working to removedouble-tap to zoom delay

(when page not zoomable)

Page 225: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 226: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017
Page 227: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

"Allow zooming on all web content" reintroduces delaypatrickhlauke.github.io/touch/tests/event-listener_user-scalable-no.html

Page 228: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

"Allow zooming..." option currently missing in Win 10 Mobile

Page 229: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Windows 10 build 15007 on Mobile ignores unscalable viewportsno delay until user zooms for first time...

Page 230: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

width=device-width still has delay in IE11/WinPhone 8.1 Updatepatrickhlauke.github.io/touch/tests/event-listener_width-device-width.html

Page 231: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Microsoft Edge removes delay for width=device-widthpatrickhlauke.github.io/touch/tests/event-listener_width-device-width.html

Page 232: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/tests/results/#suppressing-300ms-delay

Page 233: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

1.  delayed event dispatch2.  mousemove doesn't track

Page 234: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

just listen to pointermove ...

Page 235: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

no need for separate mouse ortouch event listeners

Page 236: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* 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 237: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 238: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* Reminder: Touch Events need separate code for x / y coords */ 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; ... } } canvas.addEventListener('mousemove', positionHandler, false ); canvas.addEventListener('touchmove', positionHandler, false );

Page 239: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* 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 240: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

3D Rotator by Creative Punchcoded to only use mouse events

Page 241: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

3D Rotator modified to use Pointer Eventsminimal code changes, as Pointer Events extend mouse events

Page 242: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

but you can distinguish mouse or touch or stylus

Page 243: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 244: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

what about multitouch?

Page 245: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* PointerEvents don't have the handy TouchList objects, so we have to replicate something similar... */ ar 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 ': case ' pointercancel ': /* remove the relevant array entry */ break; }

Page 246: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/tracker/multi-touch-tracker-pointer-hud.html(note multiple isPrimary pointers, per pointer type)

Page 247: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* 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... */

MSDN IE10 Developer Guide: Gesture object and events

Page 248: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

extended capabilities (if supported by hardware)

Page 249: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* Pointer Events - pressure */ interface PointerEvent : MouseEvent { readonly attribute long pointerId; readonly attribute long width; readonly attribute long height; readonly attribute float pressure ; readonly attribute float tangentialPressure; readonly attribute long tiltX; readonly attribute long tiltY; readonly attribute long twist; readonly attribute DOMString pointerType; readonly attribute boolean isPrimary; }

pressure : value in range 0 – 1 . if no hardware support, 0.5 in active button state, 0 otherwise

Page 250: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/tracker/...“ouTube: Touch tracker with Surface 3 pen

Page 251: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

hovering stylus•   hardware-dependant

•    pointermove fires

•    pressure == 0 (non-active button state)

•   track pointerdown / pointerup to be safe

Page 252: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* Pointer Events - contact geometry */ interface PointerEvent : MouseEvent { readonly attribute long pointerId; readonly attribute long width ; readonly attribute long height ; readonly attribute float pressure; readonly attribute float tangentialPressure; readonly attribute long tiltX; readonly attribute long tiltY; readonly attribute long twist; readonly attribute DOMString pointerType; readonly attribute boolean isPrimary; }

if hardware can't detect contact geometry, either 0 or "best guess" (e.g. for mouse/stylus, return width / height of 1 )

Page 253: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/tracker/tracker-width-height.html“ouTube: Pointer Events width and height ...

Page 254: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* Pointer Events - tilt */ interface PointerEvent : MouseEvent { readonly attribute long pointerId; readonly attribute long width; readonly attribute long height; readonly attribute float pressure; readonly attribute float tangentialPressure; readonly attribute long tiltX ; readonly attribute long tiltY ; readonly attribute long twist; readonly attribute DOMString pointerType; readonly attribute boolean isPrimary; }

tiltX / tiltY : value in degrees -90 – 90 . returns 0 if hardware does not support tilt

Page 255: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/pen-tracker“ouTube: ...pen with tilt, pressure and hover support

Page 256: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/pen-tracker“ouTube: Chrome on Samsung Galaxy Note 4 - Pointer Events and stylus

Page 257: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

pointermove fires if anyproperty changes,

not just x/y position ( width , height , tiltX , tiltY , pressure )

Page 258: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

pointer events as the future?

Page 259: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

transitional event handling (until all browsers support pointer events)

Page 260: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

/* cover all cases (hat-tip Stu Cox) */ if (window.PointerEvent) { /* 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 261: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

polyfills for pointer events (code for tomorrow, today)

Page 262: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

GitHub - jQuery Pointer Events Polyfill (PEP)

Page 263: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

�� adding jQuery PEP *� <script src="https:��code.jquery.com�pep�0.4.1�pep.js"><�script> �* need to use custom touch-action attribute, not CSS (yet) *� <button touch-action="none" >...<�div>

Page 264: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

patrickhlauke.github.io/touch/pep-draw

Page 265: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

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

Page 266: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

debugging/testing

Page 267: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

nothing beats having real devices...

Page 268: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Chrome DevTools / Device Mode & Mobile Emulationcorrectly emulates touch and pointer events

Page 269: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

Firefox Developer Tools > Responsive Design Modecorrectly emulates touch events

Page 270: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

no touch emulation, nor touch events + pointer events (like on realWindows 10 Mobile) emulation, in Edge/F12 Tools

Page 271: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

no touch emulation in Safari "Responsive Design Mode"

Page 272: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

youtube.com/watch?v=A”KpByV5764

Page 273: Getting touchy - an introduction to touch and pointer events / Frontend NE / 2 March 2017

get in touch@patrick_h_lauke

github.com/patrickhlauke/touch patrickhlauke.github.io/getting-touchy-presentation

slideshare.net/redux paciellogroup.com

splintered.co.uk