getting touchy -...

59
getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke / Web Standards Days / Москва/ 24 November 2012

Upload: others

Post on 18-Nov-2019

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

getting touchy AN INTRODUCTION TO TOUCH EVENTS

Patrick H. Lauke / Web Standards Days / Москва/ 24 November 2012

Page 2: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

you don't need touch events

Page 3: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

browsers emulate regular mouse events

Page 5: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

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

Page 6: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

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 7: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

emulation works but is limiting/problematic

(more on that in a minute)

Page 8: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

touch eventswww.w3.org/TR/touch-events

Page 9: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

touchstart

touchmovetouchend

touchcancel

Page 11: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

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

(focus) > mouseup > click

Page 12: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

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

Page 13: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

limitations/problems of mouse event emulation

Page 14: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

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

Page 15: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

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

Page 17: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

simple feature detection for touch events

Page 18: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

if ('ontouchstart' in window) {

/* some clever stuff here */

}

Page 19: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

/* common performance “trick” */

var event = 'click';

if ('ontouchstart' in window) {

event = 'touchstart';

}

foo.addEventListener(event,function(e)

{

/* execute on click or touch */

}, false);

Page 20: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

don't make it touch-exclusive

Page 21: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

hybrid devicestouch + mouse + keyboard

Page 22: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

/* doubled-up event listeners */

foo.addEventListener('click', function(e) {…}, false);

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

Page 23: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

/* doubled-up event listeners */

foo.addEventListener('click', function(e) {…},false);

foo.addEventListener('touchstart', function(e) {

/* stop mouse event emulation */

e.preventDefault();

},false);

Page 24: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

preventDefaultkills scrolling, long-press,

pinch/zoom

Page 25: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

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

Page 26: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke
Page 27: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke
Page 28: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

no isolated hover (or focus) on touch devices

Page 31: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

/* pure CSS dropdown */

ul.menu li:hover ul { display: block; }

<ul class="menu"><li><a href="">Menu 1</a></li><li class="submenu"><a href="">Menu 2</a>

<ul>...

</ul></li>

...

</ul>

Page 32: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

/* CSS and JS dropdown */

ul.menu li:hover ul,ul.menu li.open ul { display: block; }

/* make it keyboard accessible */

document.querySelectorAll('ul.menu li.submenu > a').addEventListener('focus', function(e) {

...

this.parentNode.classList.add('open');

},false);

Page 33: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

/* CSS and JS dropdown */

ul.menu li:hover ul,ul.menu li.open ul { display: block; }

/* touch opens menu */

document.querySelectorAll('ul.menu li.submenu > a').addEventListener('touchstart', function(e) {

...if (!this.parentNode.classList.contains('open')) {

...this.parentNode.classList.add('open');e.preventDefault();

}

},false);

Page 34: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

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

Page 35: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

people.opera.com/patrickl/experiments/canvas/particle/2

Page 36: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

var posX, posY;

...

function positionHandler(e) {

posX = e.clientX;

posY = e.clientY;

}

...

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

Page 37: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

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

Page 38: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

var posX, posY;

...

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

}

...

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

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

Page 39: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

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

Page 40: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

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

Page 41: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

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 42: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

people.opera.com/patrickl/experiments/canvas/particle/3

Page 43: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

people.opera.com/patrickl/experiments/canvas/particle/4

Page 45: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

touchmove fires...a lot!

Page 47: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

why stop at a single point?multitouch support

Page 48: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

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

...

posX = e.targetTouches[i].clientX;

posY = e.targetTouches[i].clientY;

/* do something clever */

}

Page 50: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

multitouch gestures

Page 51: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

/* iOS Safari has gesture events for size/rotation

with some trigonometry we can make this x-browser */

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

var angle = Math.atan2(...);

Page 53: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

shinydemos.com/picture-organizer

Page 54: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

touch events and IE10

Page 56: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

www.w3.org/2012/pointerevents

Page 57: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

smus.com/mouse-touch-pointer

Page 58: Getting touchy - cache-ash02.cdn.yandex.netcache-ash02.cdn.yandex.net/download.yandex.ru/company/experience/WSD/... · getting touchy AN INTRODUCTION TO TOUCH EVENTS Patrick H. Lauke

youtube.com/watch?v=AZKpByV5764