Get different angles

Get the different angles using the JavaScript HTML5 orientation API

Typical use

The use of this API is very straightforward:

    1. Test if your browser supports the orientation API (window.DeviceOrientationEvent is not null),
    2. Define a listener for the ‘deviceorientation‘ event as follows: window.addEventListener(‘deviceorientation’, callback, false); with the callback function accepting the event object as its single input parameter,
    3. Extract the angles from the event (use its properties: alpha, beta, gamma).

Here’s an example on JsBin. Try it with  a smartphone, a tablet, or a device with an accelerometer: 

(If using a mobile device,  open the page in standalone mode (without the JsBin editor) )

orientation api 2

The above screenshot came from an iPad laying immobile on a desk. Theoretically, all the angle values will be zero when the device is laid flat, providing it has not been moved since the page loaded. However, depending on the hardware, these values may change even if the device is stationary: a very sensitive sensor might report constantly changing values. This is why, in the example, we round the returned values withMath.round() at display time (see code).
If we change the orientation of the device here are the results:
orientation api 3orientation api 1orientation api 2
Typical use / code from the above example:
  1. <h2>Device Orientation with HTML5</h2>
  2. You need to be on a mobile device or use a laptop with accelerometer/orientation
  3. device.
  4. <p>
  5.  id=“LR”>
  6.  id=“FB”>
  7.  id=“DIR”>
  8.  type=“text/javascript”>
  9.    if (window.DeviceOrientationEvent) {
  10.       console.log(“DeviceOrientation is supported”);
  11.       window.addEventListener(‘deviceorientation’, function(eventData) {
  12.          // gamme is for left/right inclination
  13.          var LR = eventData.gamma;
  14.          // beta is for front/back inclination
  15.          var FB = eventData.beta;
  16.          // alpha is for orientation
  17.          var DIR = eventData.alpha;
  18.          // display values on screen
  19.          deviceOrientationHandler(LR, FB, DIR);
  20.       }, false);
  21.    } else {
  22.       alert(“Device orientation not supported on your device or browser. Sorry.”);
  23.    }
  24. function deviceOrientationHandler(LR, FB, DIR) {
  25.    document.querySelector(“#LR”).innerHTML = “gamma : “ + Math.round(LR);
  26.    document.querySelector(“#FB”).innerHTML = “beta : “ + Math.round(FB);
  27.    document.querySelector(“#DIR”).innerHTML = “alpha : “ + Math.round(DIR);
  28. }
  29.  …

Another example that shows how to orient the HTML5 logo using the orientation API + CSS3 3D rotations

This is just a variation of the previous example, try it at JsBin 

orientation api example 3

Results on the iPad: the logo rotates when we change the iPad’s orientation. This is a good “visual feedback” for an orientation controlled game…

logo 1logo 2logo 3

This example as a Youtube video: http://www.youtube.com/watch?v=OrNLhOAGSdE

Code from the example:

  1. <h2>Device Orientation with HTML5</h2>
  2. You need to be on a mobile device or use a laptop with accelerometer/orientation
  3. device.
  4. <p>
  5.  id=“LR”>
  6.  id=“FB”>
  7.  id=“DIR”>
  8. <img src=http://www.html5
  9. rocks.com/en/tutorials/device/orientation/html5_logo.png” id=“imgLogo”
  10. class=“logo”>
  11.  type=“text/javascript”>
  12.    if (window.DeviceOrientationEvent) {
  13.       console.log(“DeviceOrientation is supported”);
  14.       window.addEventListener(‘deviceorientation’, function(eventData) {
  15.           var LR = eventData.gamma;
  16.           var FB = eventData.beta;
  17.           var DIR = eventData.alpha;
  18.           deviceOrientationHandler(LR, FB, DIR);
  19.       }, false);
  20.    } else {
  21.       alert(“Not supported on your device or browser. Sorry.”);
  22.    }
  23.    function deviceOrientationHandler(LR, FB, DIR) {
  24.       // USE CSS3 rotations for rotating the HTML5 logo
  25.       //for webkit browser
  26.       document.getElementById(“imgLogo”).style.webkitTransform =
  27.       “rotate(“ + LR + “deg) rotate3d(1,0,0, “ + (FB * 1) + “deg)”;
  28.       //for HTML5 standard-compliance
  29.       document.getElementById(“imgLogo”).style.transform =
  30.       “rotate(“ + LR + “deg) rotate3d(1,0,0, “ + (FB * 1) + “deg)”;
  31.       document.querySelector(“#LR”).innerHTML = “gamma : “ + Math.round(LR);
  32.       document.querySelector(“#FB”).innerHTML = “beta : “ + Math.round(FB);
  33.       document.querySelector(“#DIR”).innerHTML = “alpha : “ + Math.round(DIR);
  34. }
  35.  …

A simple level tool using device orientation

This example works in Firefox, Chrome, and IOS Safari. Created by Derek Anderson @ Media Upstream. Original source code available GitHub.

We adapted the source code so that you can tweak it in JsBin, or test it in standalone mode (using a mobile device).

level tool using device orientation

Other interesting uses: mix orientation API and WebSockets

You can imagine the above example that sends the current orientation of the device to a server using WebSockets. The server in turn updates the logo and position on a PC screen. If multiple devices connect, they can chat together and take control of the 3D Logo.

This video shows one of the above examples slightly modified: the JavaScript code running in the Web page on the iPad sends in real time the device orientation using the Web Sockets API to a server that in turns sends the orientation to a client running on a desktop browser. In this way the tablet “controls” the HTML5 logo that is shown on the desktop browser:

Click on the image to see the YouTube video:

orientation API + websockets

Leave a comment