getting touchy - an introduction to touch events / webinale / berlin 04.06.2013

109
getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke / Webinale / Berlin / 4 Juni 2013

Upload: patrick-lauke

Post on 15-May-2015

2.534 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

getting touchy AN INTRODUCTION TO TOUCH EVENTS

Patrick H. Lauke / Webinale / Berlin / 4 Juni 2013

Page 2: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

webkrauts.de/artikel/2012/einfuehrung-touch-events

Page 3: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

how can I make my website work on touch devices?

Page 4: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

you don't need touch eventsbrowsers emulate regular mouse events

Page 5: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 6: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

*only a single “sacrificial” events is fired

Page 7: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

on first tapmouseover > mousemove > mousedown >

(focus) > mouseup > click

subsequent tapsmousemove > mousedown > (focus) >

mouseup > click

tapping awaymouseout > (blur)

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

Page 8: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

emulation works but is limiting/problematic

(more on that in a minute)

Page 9: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 10: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 11: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 12: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 13: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

vimeo.com/ondemand

Page 14: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 15: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

patrickhlauke.github.io/touch/particle/2

Page 16: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

“we need to go deeper...”

Page 17: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

www.w3.org/TR/touch-events

Page 18: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

touchstart

touchmovetouchend

touchcancel

Page 19: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 20: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

touchstart > [touchmove]+ > touchend > mouseover > mousemove > mousedown >

(focus) > mouseup > click

Page 21: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

on first taptouchstart > [touchmove]+ > touchend > mouseover > mousemove > mousedown > (focus) > mouseup > click

subsequent tapstouchstart > [touchmove]+ > touchend > mousemove >

mousedown > (focus) > mouseup > click

tapping awaymouseout > (blur)

too many touchmove events abort the tap (after touchend)not all browsers consistently send the touchmove

some browsers outright weird...

Page 22: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

Browser/Android 4.2.2(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 23: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

touch eventsvs

limitations/problems of mouse event emulation

Page 24: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 25: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 26: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 27: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 28: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 29: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

simple feature detection for touch events

Page 30: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

if ('ontouchstart' in window) {

/* some clever stuff here */

}

Page 31: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

/* common performance “trick” */

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

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

Page 32: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

don't make it touch-exclusive

Page 33: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 34: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

if ('ontouchstart' in window) {

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

}

Page 35: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

hybrid devicestouch + mouse + keyboard

Page 36: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013
Page 37: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 38: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

Blackberry PlayBook 2.0 + mouse – like desktop mousemouseover > mousedown > mousemove > mouseup > click

Page 39: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

Android + keyboard – like desktop keyboardfocus > click

Page 40: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

VoiceOver enables keyboard access on iOS

Page 41: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

iOS + keyboard – similar to touchfocus > touchstart > touchend > mouseover > mousemove > mousedown

blur > mouseup > click

Page 42: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

iOS + VoiceOver – similar to touchfocus > touchstart > touchend > mouseover > mousemove > mousedown

blur > mouseup > click

Page 43: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 44: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

mouse or touchvs

mouse and touch and keyboard

Page 45: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

/* doubled-up event listeners */

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

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

Page 46: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

/* doubled-up event listeners */

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

}, false);

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

Page 47: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

preventDefaultkills scrolling, long-press,

pinch/zoom

Page 48: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013
Page 49: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

browsers working to remove delay when possible

e.g. Chrome on desktop, Chrome/Firefox on Android

Page 50: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 51: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013
Page 52: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 53: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 54: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

vimeo.com/ondemand

Page 55: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013
Page 56: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013
Page 57: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

no isolated hover (or focus) on touch devices*

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

Page 59: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

avoid hover-based interfaces?

complement for keyboard / touch!

Page 60: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

Modernizr issue 869: Detecting a mouse user

Page 61: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 62: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 63: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

patrickhlauke.github.io/touch/particle/2

Page 64: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

touchstart > [touchmove]+ > touchend > mouseover > mousemove* > mousedown >

(focus) > mouseup > click

*mouse event emulation fires only a single mousemove

Page 65: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

doubling up handling of mousemove and touchmove

Page 66: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

var posX, posY;

...

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

}

...

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

Page 67: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

var posX, posY;

...

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

}

...

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

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

Page 68: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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 69: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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 70: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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 71: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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 72: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

patrickhlauke.github.io/touch/particle/3

Page 73: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

patrickhlauke.github.io/touch/particle/4

Page 74: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

tracking finger movement=> swipe gestures

Page 75: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

patrickhlauke.github.io/touch/picture-slider

Page 76: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

don't forget mouse/keyboard !

Page 77: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

bradfrostweb.com/demo/mobile-first

Page 78: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

touchmove fires...a lot!

Page 79: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

patrickhlauke.github.io/touch/touch-limit

Page 80: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

heavy javascript on requestAnimationFrame

setInterval

Page 81: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

why stop at a single point?multitouch support

Page 82: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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 83: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

...

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

...

}

Page 84: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 85: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 86: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

multitouch gestures

Page 87: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

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

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

Page 88: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

patrickhlauke.github.io/touch/pinch-zoom-rotate

Page 89: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

shinydemos.com/picture-organizer

Page 90: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

www.html5rocks.com/en/mobile/touchandmouse

Page 91: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

touch events and IE10

Page 93: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

www.w3.org/TR/pointerevents

Page 94: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

(focus) > pointerup > mouseup >

pointerout > mouseout > click

mouse events fired “inline” with pointer events!

Page 95: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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 96: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

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

};

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

Page 97: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

preventDefault() won't work

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

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

Page 100: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

handjs.codeplex.com

Page 101: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Replicate these from basic principles again? */

Page 102: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

www.exploretouch.ie/behind-the-scenes

Page 103: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

debugging/testing

Page 104: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 105: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 106: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

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

Page 107: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013
Page 108: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

youtube.com/watch?v=AZKpByV5764

Page 109: Getting touchy - an introduction to touch events / Webinale / Berlin 04.06.2013

danke@patrick_h_lauke

github.com/patrickhlauke/touchslideshare.net/redux

paciellogroup.comsplintered.co.uk