
{"id":1210,"date":"2013-01-31T20:17:22","date_gmt":"2013-02-01T01:17:22","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=1210"},"modified":"2013-01-31T20:17:22","modified_gmt":"2013-02-01T01:17:22","slug":"touches","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=1210","title":{"rendered":"Touches"},"content":{"rendered":"<p>I wrote <a href=\"http:\/\/dev.ikriv.com\/m\/touches.html\">a little web page<\/a> that &#8220;debugs&#8221; HTML 5 touch interface (requires touch screen device). Unlike regular mouse clicks, there is no &#8220;onTouch&#8221; attribute, you have to add and remove listeners via <code>addEventListener()<\/code> method of a DOM element. Event name is passed as string, which is not very safe, but hey, it&#8217;s JavaScript, folks! Nothing is save there.<\/p>\n<p>Another quirk is that unlike <code>MouseUp<\/code> notification in various graphic systems, <code>touchend<\/code> event does not give you coordinates. In some way this makes sense, because the mouse has only one on-screen cursor, and touch interface must support multiple touches. By the time of <code>touchend<\/code> the number of current touches is zero, which is reflected in the event.<\/p>\n<p>Unfortunately, this means that even if you are only interested in the start and end point of a touch, you still must monitor the <code>touchmove<\/code> event.<\/p>\n<p>I tested the app on three different phones and here are the results:<\/p>\n<p><b>Motorola Droid Razr Maxx<\/b> &#8211; can handle virtually any number of touches. The maximum I was able to reach was 12; I am sure the software can handle even more, but we ran out of screen space. Just in case: I ran out of my own fingers at 10 touches and had to use other people&#8217;s help to reach 12 \ud83d\ude42<\/p>\n<p><b>IPhone 4S<\/b> &#8211; bails out after 5 touches (go backs to zero if I remember correctly). Not very cool and kinda sloppy, but enough for the vast majority of practical applications.<\/p>\n<p><b>HTC Droid Incredible<\/b> &#8211; I could not get it to work with more than one touch, because it ignores &#8220;<code>user-scalable=no<\/code>&#8221; meta tag. When you touch screen we second finger, the touch event is not passed to the web page, and the page is zoomed instead.<\/p>\n<p>Oh, yeah, and the relevant piece of JavaScript code is below. I did not use JQuery or any similar framework on purpose, to get to the bare bones API.<\/p>\n<pre><code>function reporter(name) {\n    return function (e) {\n        e.preventDefault();\n\n        var message =\n            name + \"&lt;br\/&gt;\" +\n            \"touches: \" + e.touches.length + \"&lt;br\/&gt;\";\n\n        for (var i = 0; i &lt; e.touches.length; ++i) {\n            var touch = e.touches[i];\n            message += \"screenX: \" + touch.screenX + \"&lt;br\/&gt;\";\n            message += \"screenY: \" + touch.screenY + \"&lt;br\/&gt;\";\n        }\n\n        var info = document.getElementById(\"info\");\n        info.innerHTML = message;\n    };\n}\n\nfunction report(name) {\n    window.addEventListener(name, reporter(name), true);\n}\n\nreport(\"touchstart\");\nreport(\"touchmove\");\nreport(\"touchend\");<\/code><\/pre>\n<p><a style=\"display:none\" href=\"http:\/\/www.codeproject.com\/script\/Articles\/BlogFeedList.aspx?amid=1181663\" rel=\"tag\">CodeProject<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wrote a little web page that &#8220;debugs&#8221; HTML 5 touch interface (requires touch screen device). Unlike regular mouse clicks, there is no &#8220;onTouch&#8221; attribute, you have to add and <a href=\"https:\/\/ikriv.com\/blog\/?p=1210\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4,20,15],"tags":[],"class_list":["entry","author-ikriv","post-1210","post","type-post","status-publish","format-standard","category-hack","category-mobile","category-webdev"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1210"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1210\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}