Orientation Sensors

In this example we experiment with the device orientation API. We create an app that shows the current orienrtation of the device and rotates an image with CSS3 when the device changes orientation.

The HTML code contains the text where to show the orientation parameters and the image.

  %strong Device Orientation
    Absolute Coord
  Rotate (Alpha): 
  frontToBack (Beta):
  leftToRight (Gamma):
  %strong Device Motion
  .container(style="-moz-perspective: 300; perspective: 300;")
    %img(src="http://www.w3.org/html/logo/downloads/HTML5_Badge_64.png" id="imgLogo" class="logo")

Below there is the Javascript code that listen to the device orientation events. For each event the handler updates the data about the orientation on the html page and applies a new 3d transformation to the logo.

window.addEventListener("deviceorientation", function( event ) {  
  if(event.absolute) {
  } else
  //alpha: rotation around z-axis  

  //gamma: left to right  

  //beta: front back motion  
  var rotation = "rotateX("+(event.beta * -1) + "deg) rotateY(" + (event.gamma) + "deg) rotateZ(" + (event.alpha * -1) + "deg)";


}, false);