{"id":125352,"date":"2010-11-24T10:52:30","date_gmt":"2010-11-24T17:52:30","guid":{"rendered":"http:\/\/chinadigitaltimes.net\/chinese\/2010\/11\/safari-on-iphone-ipad-4-2-accelerometer-websockets-better-html5-support\/"},"modified":"2010-11-24T10:52:30","modified_gmt":"2010-11-24T17:52:30","slug":"safari-on-iphone-ipad-4-2-accelerometer-websockets-better-html5-support","status":"publish","type":"post","link":"https:\/\/chinadigitaltimes.net\/chinese\/125352.html","title":{"rendered":"Safari on iPhone &amp; iPad 4.2: Accelerometer, WebSockets &amp; better HTML5 support"},"content":{"rendered":"<p><p>\u6765\u81ea: <a href=\"http:\/\/www.feedzshare.com\/b\/6442203\/2\" target=\"_blank\" rel=\"noopener\">Mobile Web Programming &#8211; FeedzShare<\/a>\u00a0\u00a0<a href=\"http:\/\/www.feedzshare.com\/b\/341\/2\" target=\"_blank\" rel=\"noopener\">Daring Fireball &#8211; FeedzShare<\/a>\u00a0\u00a0<br \/>\n<br \/>\u53d1\u5e03\u65f6\u95f4:2010\u5e7411\u670823\u65e5,\u00a0\u00a0\u5df2\u6709 5 \u4eba\u63a8\u8350 <\/p>\n<hr>\n<div>\n<div>\n\t\t<a href=\"http:\/\/twitter.com\/share\" target=\"_blank\" rel=\"noopener\">Tweet<\/a>\n\t<\/div>\n<p>iOS 4.2 is a free update for every iPhone, iPod or iPad device available now. This new release provides some major changes on HTML5 and W3C future standards support, like <strong>WebSockets<\/strong> and\u00a0<strong>Accelerometer support<\/strong>, print support, new JavaScript data-types and better SVG support.<span><\/span><\/p>\n<hr>\n<p>\nApple didn\u2019t update yet Safari documentation to reflect these new APIs. This information is based on JavaScript research and testing over Safari itself I\u2019ve been doing. The list of new features I\u2019ve detected are:<\/p>\n<ul>\n<li><strong>Accelerometer<\/strong> support through the DeviceOrientation API<\/li>\n<li><strong>WebSockets<\/strong> API from HTML5<\/li>\n<li>Updated <strong>HTML5 Form<\/strong> Support<\/li>\n<li>Partial <strong>XHR-2<\/strong> Support<\/li>\n<li>Print Support<\/li>\n<li>New JavaScript data types<\/li>\n<li>New DOM events<\/li>\n<li>Enhanced <strong>SVG<\/strong> and <strong>Canvas<\/strong> support<\/li>\n<\/ul>\n<h3>Accelerometer support<\/h3>\n<p>As you may know, iOS devices all have accelerometer sensor (plus others, like magnetometer and gyroscope on some devices) but a web developer didn\u2019t have access to such sensors, until now. Safari now supports the <strong><a href=\"http:\/\/dev.w3.org\/geo\/api\/spec-source-orientation.html\" target=\"_blank\" rel=\"noopener\">DeviceOrientation API<\/a><\/strong> (W3C draft); looking at the available objects, it seems that all the API is fully supported (including ondeviceorientation and ondevicemotion events), however on my tests up to now, I could only get <strong>accelerometer<\/strong> data with success.<\/p>\n<p>If you have an iOS 4.2 device, go to <strong><a href=\"http:\/\/ad.ag\/wjmtgt\" target=\"_blank\" rel=\"noopener\">ad.ag\/wjmtgt<\/a> <\/strong>from Safari browser. I\u2019ve coded a sample in 15 minutes using JavaScript and some CSS3: it\u2019s the typical ball moving on the screen regarding the iOS device\u2019s position. The next video shows this sample in action:<\/p>\n<\/p>\n<p>The API detects and delivers accelerometer data 50 times per second. To get them you need to capture <strong>ondevicemotion<\/strong> event on the window global object (or using <strong>addEventListener<\/strong> with <strong>devicemotion<\/strong> as the event name) and then use the <strong>accelerationIncludingGravity<\/strong> property on the <strong>DeviceOrientationEvent<\/strong> parameter. It has three values, <strong>x<\/strong>, <strong>y<\/strong> &#038; <strong>z<\/strong>, representing the acceleration in g (gravities) for each axis. You should use typical accelerometer math for games, effects or CSS transformations.<\/p>\n<p><code>window.ondevicemotion = function(event)  {<br \/>\n\/\/\u00a0event.accelerationIncludingGravity.x<br \/>\n\/\/ event.accelerationIncludingGravity.y<br \/>\n\/\/ event.accelerationIncludingGravity.z<br \/>\n}<br \/>\n<\/code><\/p>\n<h3>WebSockets<\/h3>\n<p>The other big new update is WebSockets support. <a href=\"http:\/\/dev.w3.org\/html5\/websockets\/\" target=\"_blank\" rel=\"noopener\">WebSockets<\/a> is a W3C HTML5 API currently in draft that allows JavaScript to use an open, bi-directional full-duplex connection to a server using TCP sockets. This is a great news for chat and real-time applications that will reduce AJAX periodic calls.<\/p>\n<p>You will need a web server understanding the new WS protocol through an HTTP handshake. You should always rely on a fallback mechanism if WS is not supported on the server, or because of a proxy\/gateway.<\/p>\n<h3>HTML5 Form Support<\/h3>\n<p>Besides the HTML5 Form support I\u2019ve already discussed on <a href=\"http:\/\/www.mobilexweb.com\/book\" target=\"_blank\" rel=\"noopener\">the book<\/a>, now we have support for the\u00a0<strong>required<\/strong> boolean attribute and the new <strong>:invalid<\/strong> CSS pseudoclass. Therefore, the following code will show a green input text when completed and a yellow one when incomplete:<\/p>\n<p><code> <\/p>\n<style>\ninput {<br \/>\nbackground-color: green;<br \/>\ncolor: white;<br \/>\n}<br \/>\ninput:invalid {<br \/>\nbackground-color: yellow;<br \/>\n}<br \/>\n<\/style>\n<p>\n<input type=\"text\" required><br \/>\n<\/code><\/p>\n<h3>AJAX 2<\/h3>\n<p>The W3C draft called <a href=\"http:\/\/dev.w3.org\/2006\/webapi\/XMLHttpRequest-2\/Overview.html\" target=\"_blank\" rel=\"noopener\">XMLHttpRequest Level 2<\/a> (aka AJAX 2) adds new features to the XHR object and functionality. From that specification, now Safari supports the <a href=\"https:\/\/developer.mozilla.org\/en\/XMLHttpRequest\/FormData\" target=\"_blank\" rel=\"noopener\">FormData<\/a> object that allow us to send form data via AJAX easily.<\/p>\n<h3>Print Support<\/h3>\n<p>iOS 4.2 includes AirPrint, a wireless printing solution. Therefore, we can use now <strong>window.print()<\/strong> to invoke the printing dialog on Safari.<\/p>\n<h3>New JavaScript Data-types<\/h3>\n<p>Safari now supports the Blob class and many integer-type collections, like Float32Array, Int8Array, Uint8Array, Int16Array Uint16Aray, Int32Array and UInt32Array defined on <a href=\"https:\/\/cvs.khronos.org\/svn\/repos\/registry\/trunk\/public\/webgl\/doc\/spec\/TypedArray-spec.html\" target=\"_blank\" rel=\"noopener\">Typed Arrays<\/a> specification. More information on <a href=\"https:\/\/developer.mozilla.org\/en\/JavaScript_typed_arrays\" target=\"_blank\" rel=\"noopener\">Firefox website<\/a>.<\/p>\n<h3>New DOM events<\/h3>\n<p>Besides the new motion events, now we can use the HTML5 new <strong>onhashchange<\/strong> event that detects changes on the URL after the hash (#) for AJAX-like webapps; the <strong>invalid, onbeforeload and onpopstate<\/strong> events from HTML5 draft specification.<\/p>\n<p>Now, we can also use <strong>window.captureEvents<\/strong> and <strong>window.releaseEvents<\/strong> to capture events in a global way.<\/p>\n<h3>Enhanced SVG and Canvas support<\/h3>\n<p>iOS supports SVG as a separate document and also inline SVG (using the <strong>svg<\/strong> tag). And now we can also create SVG documents on the fly using a list of more than 20 classes SVG____, like <strong>SVGDocument<\/strong>, <strong>SVGImage<\/strong> directly from our code.<br \/>\nFrom HTML5 Canvas, there is now support for <a href=\"http:\/\/nooshu.com\/using-image-data-inside-the-html5-canvas-element\/\" target=\"_blank\" rel=\"noopener\">ImageData<\/a> data-type, a way to manipulate images pixel by pixel from JavaScript.<\/p>\n<h3>Other stuff<\/h3>\n<ul>\n<li>A <strong>styleMedia<\/strong> attribute for window global object.<\/li>\n<li>A <strong>WebGLRenderingContext<\/strong> class available, part of the 3D Drawing API (aka WebGL). However, I\u2019m not seeing any real WebGL support.<\/li>\n<li>A global<strong> RGBColor<\/strong> constructor<\/li>\n<\/ul>\n<p>I will continue testing new HTML5 features and APIs available in this new release. Do you know any other new feature? Feel free to contact me by twitter (@firt) or commenting this post.<\/p>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/img.tongji.linezing.com\/1017243\/tongji.gif\" \/><img decoding=\"async\" src=\"http:\/\/img.tongji.linezing.com\/997968\/tongji.gif\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p> \u6765\u81ea: Mobile Web Programming &#8211; FeedzShare \u00a0\u00a0 Daring Fireball &#8211; FeedzShare \u00a0\u00a0 \u53d1\u5e03\u65f6\u95f4:2010\u5e7411\u670823\u65e5,\u00a0\u00a0\u5df2\u6709 5 \u4eba\u63a8\u8350 Tweet iOS 4.2 is a free update for every iPhone, iPod or iPad device available now. This new release provides some major changes on HTML5 and W3C future standards support, like WebSockets and\u00a0 Accelerometer support , print support, new JavaScript data-types and better SVG support. Apple didn\u2019t update yet Safari documentation to reflect these new APIs. <\/p>\n","protected":false},"author":176,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","ampforwp-amp-on-off":"","home_featured":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[1],"tags":[],"class_list":["post-125352","post","type-post","status-publish","format-standard","hentry","category-uncategorized","et-doesnt-have-format-content","et_post_format-et-post-format-standard"],"_links":{"self":[{"href":"https:\/\/chinadigitaltimes.net\/chinese\/wp-json\/wp\/v2\/posts\/125352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chinadigitaltimes.net\/chinese\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chinadigitaltimes.net\/chinese\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chinadigitaltimes.net\/chinese\/wp-json\/wp\/v2\/users\/176"}],"replies":[{"embeddable":true,"href":"https:\/\/chinadigitaltimes.net\/chinese\/wp-json\/wp\/v2\/comments?post=125352"}],"version-history":[{"count":0,"href":"https:\/\/chinadigitaltimes.net\/chinese\/wp-json\/wp\/v2\/posts\/125352\/revisions"}],"wp:attachment":[{"href":"https:\/\/chinadigitaltimes.net\/chinese\/wp-json\/wp\/v2\/media?parent=125352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chinadigitaltimes.net\/chinese\/wp-json\/wp\/v2\/categories?post=125352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chinadigitaltimes.net\/chinese\/wp-json\/wp\/v2\/tags?post=125352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}