getting touchy - an introduction to touch events / sainté mobile days / saint-etienne 22.11.2013

92
getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke / Sainté Mobile Days / Saint-Etienne / 22 November 2013

Upload: patrick-lauke

Post on 27-Jan-2015

106 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

getting touchy AN INTRODUCTION TO TOUCH EVENTS

Patrick H. Lauke / Sainté Mobile Days / Saint-Etienne / 22 November 2013

Page 2: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 3: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

you don't need touch eventsbrowsers emulate regular mouse events

Page 4: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 5: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 6: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

*only a single “sacrificial” event is fired

Page 7: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

emulation works,but is limiting/problematic

Page 8: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

1. delayed event dispatch2. mousemove doesn't trackthere are more (e.g. no concept of hover on touchscreens)

Page 9: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

1. delayed event dispatch2. mousemove doesn't track

Page 10: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 11: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 12: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

1. delayed event dispatch2. mousemove doesn't track

Page 13: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

patrickhlauke.github.io/touch/particle/2

Page 14: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 15: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

“we need to go deeper...”

Page 16: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

www.w3.org/TR/touch-events

Page 17: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

touchstarttouchmove

touchend

touchcanceltouchentertouchleave

Page 18: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 19: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 20: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

(focus) > mouseup > click

Page 21: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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 22: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

touch eventsvs

limitations/problems

Page 23: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

1. delayed event dispatch2. mousemove doesn't track

Page 24: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

1. delayed event dispatch2. mousemove doesn't track

Page 25: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 26: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 27: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 28: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 29: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

interlude:simple feature detection for

touch events

Page 30: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

if ('ontouchstart' in window) {

/* some clever stuff here */

}

Page 31: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.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 / Sainté Mobile Days / Saint-Etienne 22.11.2013

don't make it touch-exclusive

Page 33: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

if ('ontouchstart' in window) {

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

}

Page 34: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

hybrid devicestouch + mouse + keyboard

Page 35: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013
Page 36: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013
Page 37: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 38: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

Android + keyboard – like desktop keyboardfocus > click

Page 39: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

VoiceOver enables keyboard access on iOS

Page 40: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

blur > mouseup > click

Page 41: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

blur > mouseup > click

Page 42: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 43: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

touch or mouse or keyboard

Page 44: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

touch and mouse and keyboard

Page 45: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

/* doubled-up event listeners */

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

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

Page 46: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

/* doubled-up event listeners */

foo.addEventListener('touchend',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 / Sainté Mobile Days / Saint-Etienne 22.11.2013

github.com/ftlabs/fastclick

Page 48: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

preventDefaultkills scrolling, long-press,

pinch/zoom

Page 49: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

browsers working to remove delay when possible

Page 50: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 51: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 52: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013
Page 53: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 54: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

[...] no more 300ms click delay, or double-tap zoom, on mobile websites

Page 55: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 56: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

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

Page 57: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

1. delayed event dispatch2. mousemove doesn't track

Page 58: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

patrickhlauke.github.io/touch/particle/2

Page 59: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 60: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

(focus) > mouseup > click

*mouse event emulation fires only a single mousemove

Page 61: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

doubling up handling of mousemove and touchmove

Page 62: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

var posX, posY;

...

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

}

...

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

Page 63: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

var posX, posY;

...

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

}

...

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

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

Page 64: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.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 65: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.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 66: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.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 67: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.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 68: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

patrickhlauke.github.io/touch/particle/3

Page 69: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

tracking finger movementover time ... swipe gestures

Page 70: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

patrickhlauke.github.io/touch/swipe

Page 71: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

patrickhlauke.github.io/touch/swipe

Page 72: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

patrickhlauke.github.io/touch/picture-slider

Page 73: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

don't forget mouse/keyboard !

Page 74: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

touchmove fires...a lot!

Page 75: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

do absolute minimum on each touchmove

(usually: store new coordinates)

Page 76: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

heavy JavaScript on requestAnimationFrame

setInterval

Page 77: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

patrickhlauke.github.io/touch/touch-limit

Page 78: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

why stop at a single point?multitouch support

Page 79: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.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 80: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

...

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

...

}

Page 81: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 82: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 83: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 84: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

/* iOS/Safari has gesture events for size/rotation not supported in Chrome/Firefox, not part of the W3C Touch Events specification. With some trigonometry we can replicate these from basic principles. */

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

Page 85: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

shinydemos.com/picture-organizer

Page 86: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

touch events andInternet Explorer...

Page 88: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

www.w3.org/TR/pointerevents

Page 89: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

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

Page 90: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

active development by Mozilla and Chrome teams

...Apple?

Page 91: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

but that's probably enough for now...

Page 92: Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint-Etienne 22.11.2013

pour en savoir plus:

rendez-vous à l'atelier demain matin

@patrick_h_lauke

github.com/patrickhlauke/touchslideshare.net/redux

paciellogroup.comsplintered.co.uk