Final Product (must have iOS 4.0 or greater to view): http://joelongstreet.com/blog_files/ios_accelerometer_1/index.html
I’ll be covering the basics of accessing the accelerometer and gyroscope with JavaScript in the post below. We’ll discuss difference between the two technologies and write a simple example to log out information when the device has detected a change in orientation. To simplify DOM manipulation, the code will depend on either Zepto or jQuery (I highly recommend Zepto when working in mobile).
In iOS 4.0 and above, Apple has allowed the web browser (via JavaScript) to access data returned from both the accelerometer and gyroscope. Supported devices include (only if the operating system is 4.0 or greater):
- Accelerometer – iPhone 4, iPad, iPad2, iPod Touch
- Gyroscope – iPhone 4, iPad2, iPod Touch
Unfortunately, Android devices do not currently support the use of the accelerometer or gyroscope in the web browser. You’ll have to stick to the rudimentary ‘onorientationchange’ event. If you’d like to read more about this event, the Safari Developer Library has several good explanations.
What’s the Difference between the Accelerometer and the Gyroscope?
The accelerometer measures orientation in the x, y, and z dimensions. If we rotate the device left or right (think of steering a car), the x and y values are changing. If we let the top of the device fall towards or away from us, the z dimension is changing.
The gyroscope measures forces relative to the original position of the device. Unlike the accelerometer, all of the channels of the gyroscope will start at 0 upon initialization and will return values based on the changed orientation. Moving the device vertically through space (think of doing squats) changes alpha. Twisting the device (ringint out a rag) changes the beta. Bringing the device towards or away from you (think of face punching), effects gamma.
By combining the 6 forces of the gyroscope and accelerometer we can summon Captain Planet and find the exact orientation of the device, but only relative to it’s starting position.
The Code
I’m starting with an entirely blank html file. The only things we’ll need to include are a reference to Zepto and a series of DOM elements to hold the textual information we’ll be logging out. You can download the final zip package here – http://joelongstreet.com/blog_files/ios_accelerometer_1/package.zip.
All I’m doing here is associating DOM elements with JavaScript variables. Nothing special.
1 2 3 4 5 6 | var x_dom = $('.x'); var y_dom = $('.y'); var z_dom = $('.z'); var a_dom = $('.a'); var b_dom = $('.b'); var g_dom = $('.g'); |
Accelerometer – The “ondevicemotion” event is native to browsers that support the accelerometer. We can use this event to constantly track the changes in motion related to the x, y, and z dimensions. After collecting the data from the event, we write the information to the DOM.
1 2 3 4 5 6 7 8 9 | window.ondevicemotion = function(event) { var x = event.accelerationIncludingGravity.x; var y = event.accelerationIncludingGravity.y; var z = event.accelerationIncludingGravity.z; x_dom.text(x); y_dom.text(y); z_dom.text(z); } |
In the example above, the use of the word acceleration is misleading. Acceleration by definition is the rate of change of speed. This may lead you to believe that the returned values are dependent on how fast you’re moving the device, this is not the case. The values are constant and are entirely dependent on the devices position, not the acceleration required to achieve that position.
Gyroscope – We’re doing the same thing here with the gyroscope. Like the accelerometer, this method is native to devices that support the respective instrument.
1 2 3 4 5 6 7 8 9 | window.ondeviceorientation = function(event) { var a = event.alpha; var b = event.beta; var g = event.gamma; a_dom.text(a); b_dom.text(b); g_dom.text(g); } |
If you log the above code, you’ll notice the instruments are very, very sensitive and the strings returned are somewhat long. Since it’s kind of difficult to look at that kind of information in the DOM, I’ve added a rounding function to display a single whole number. Also adding a + sign to the string makes switches between positive and negative numbers less visually striking.
The Final Product:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | var x_dom = $('.x'); var y_dom = $('.y'); var z_dom = $('.z'); var a_dom = $('.a'); var b_dom = $('.b'); var g_dom = $('.g'); //This is only for devices that support the use of a gyroscope (iPhone 4, iPad2, iPod Touch) window.ondeviceorientation = function(event) { var a = Math.round(event.alpha*1/1); var b = Math.round(event.beta*1/1); var g = Math.round(event.gamma*1/1); if(a >= 0) { a = '+' + a} if(b >= 0) { b = '+' + b} if(g >= 0) { g = '+' + g} a_dom.text(a); b_dom.text(b); g_dom.text(g); } window.ondevicemotion = function(event) { var x = Math.round(event.accelerationIncludingGravity.x*1/1); var y = Math.round(event.accelerationIncludingGravity.y*1/1); var z = Math.round(event.accelerationIncludingGravity.z*1/1); if(x >= 0) { x = '+' + x} if(y >= 0) { y = '+' + y} if(z >= 0) { z = '+' + z} x_dom.text(x); y_dom.text(y); z_dom.text(z); } |
Finished, not a whole lot to it and pretty easy overall. I’m really looking forward to seeing how people use these new technologies to build web applications. Note: In real life scenarios it’s probably overkill to collect information on device motion and orientation change.
Downloads
- Final Product: http://joelongstreet.com/blog_files/ios_accelerometer_1/index.html.
- Download the zip package : http://joelongstreet.com/blog_files/ios_accelerometer_1/package.zip.
Further Reading:
