--- url: "https://web.dev/articles/mobile-touch" titulek: "Multi-touch web development | Articles | web.dev" h1: "Multi-touch web development Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/mobile-touch" drobecky: ["Articles", "Multi-touch web development"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:54:54" znaku: 12156 slov: 1719 kodovani: "iso-8859-2" sha256: "1c75671603a3ba72284ad3c3f2eec4132eb518e76f729f2e4149935dc2414aed" --- Multi-touch web development Stay organized with collections Save and categorize content based on your preferences. Boris Smus Introduction Mobile devices such as smartphones and tablets usually have a capacitive touch-sensitive screen to capture interactions made with the user's fingers. As the mobile web evolves to enable increasingly sophisticated applications, web developers need a way to handle these events. For example, nearly any fast-paced game requires the player to press multiple buttons at once, which, in the context of a touchscreen, implies multi-touch. Apple introduced their [touch events API](http://developer.apple.com/library/safari/#documentation/UserExperience/Reference/TouchEventClassReference/TouchEvent/TouchEvent.html#//apple_ref/doc/uid/TP40009358) in iOS 2.0. Android has been catching up to this de-facto standard and closing the gap. Recently a W3C working group has come together to work on this [touch events specification](http://dvcs.w3.org/hg/webevents/raw-file/tip/touchevents.html). In this article Iâll dive into the touch events API provided by iOS and Android devices, as well as desktop Chrome on hardware that supports touch, and explore what sorts of applications you can build, present some best practices, and cover useful techniques that make it easier to develop touch-enabled applications. Touch events Three basic touch events are outlined in the spec and implemented widely across mobile devices: **touchstart**: a finger is placed on a DOM element. **touchmove**: a finger is dragged along a DOM element. **touchend**: a finger is removed from a DOM element. Each touch event includes three lists of touches: **touches**: a list of all fingers currently on the screen. **targetTouches**: a list of fingers on the current DOM element. **changedTouches**: a list of fingers involved in the current event. For example, in a touchend event, this will be the finger that was removed. These lists consist of objects that contain touch information: **identifier**: a number that uniquely identifies the current finger in the touch session. **target**: the DOM element that was the target of the action. **client/page/screen coordinates**: where on the screen the action happened. **radius coordinates and rotationAngle**: describe the ellipse that approximates finger shape. Touch-enabled apps The **touchstart**, **touchmove**, and **touchend** events provide a rich enough feature set to support virtually any kind of touch-based interaction â including all of the usual multi-touch gestures like pinch-zoom, rotation, and so on. This snippet lets you drag a DOM element around using single-finger touch: `var obj = document.getElementById('id'); obj.addEventListener('touchmove', function(event) { // If there's exactly one finger inside this element if (event.targetTouches.length == 1) { var touch = event.targetTouches[0]; // Place element where the finger is obj.style.left = touch.pageX + 'px'; obj.style.top = touch.pageY + 'px'; } }, false);` Below is a [sample](https://github.com/borismus/MagicTouch/blob/master/samples/tracker.html) that displays all current touches on the screen. Itâs useful just to get a feeling for the responsiveness of the device. `// Setup canvas and expose context via ctx variable canvas.addEventListener('touchmove', function(event) { for (var i = 0; i < event.touches.length; i++) { var touch = event.touches[i]; ctx.beginPath(); ctx.arc(touch.pageX, touch.pageY, 20, 0, 2*Math.PI, true); ctx.fill(); ctx.stroke(); } }, false);` Demos A number of interesting multi-touch demos are already in the wild, such as this [canvas-based drawing](http://paulirish.com/demo/multi) demo by Paul Irish and others. And [Browser Ninja](http://borismus.github.com/mobile-web-samples/browser-ninja/), a tech demo that is a Fruit Ninja clone using CSS3 transforms and transitions, as well as canvas: Best practices Prevent zooming Default settings don't work very well for multi-touch, since your swipes and gestures are often associated with browser behavior, such as scrolling and zooming. To disable zooming, setup your viewport so that it is not user scalable using the following meta tag: ` ...
... ` You may need to enable the plugin. A live demo with magictouch.js is available at [paulirish.com/demo/multi](http://www.paulirish.com/demo/multi): I tested this approach only with Chrome 10, but it should work on other modern browsers with only minor tweaks. If your computer does not have multi-touch input, you can simulate touch events using other TUIO trackers, such as the [reacTIVision](http://reactivision.sourceforge.net/). For more information, see the [TUIO project page](http://www.tuio.org/). Note that your gestures might be identical to OS-level multi-touch gestures. On OS X, you can configure system-wide events by going to the Trackpad preference pane in System Preferences. As multi-touch features become more widely supported across mobile browsers, I'm very excited to see new web applications take full advantage of this rich API. Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2011-08-21 UTC.