Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I actually just built something like this, using nothing but CSS3 &amp; JS. Here's the code I wrote for handling the device orientation. The position needs to be smoothed out, but it should help you. It fires events constantly giving the "heading" &amp; "tilt angle". It's tied in to a much larger framework, so you're going to need to pull it out and trigger the events differently, but that's easy to do.</p> <pre><code>( function( $ ){ // interface to the compass that gets normalized $.Klass.add( 'HTML5.Orientation', $.Klass.Observable, { init : function( config ){ this._super(); $( window ).bind( 'deviceorientation', this.bindMethod( 'orientationChange' ) ); this.degrees = 0; this.config = config; } , orientationChange : function( ev ){ /** * beta = angle that top of device is pointing * gamma = angle that side of device is pointing * * In portrait only: if gamma closer to 0, pointing back. If closer to +- 180, pointing forward **/ var oEv = ev.originalEvent , heading = oEv.webkitCompassHeading - ( this.config.zeroDeg || 0 ) , beta = oEv.beta , gamma = oEv.gamma , orient = window.orientation , aOrient = window.orientation % 180 , angle = ( aOrient ? gamma : beta ); // ignore bad data if( oEv.webkitCompassAccuracy == -1 ){ this.trigger( 'bad-compass' ); return; } // if( oEv.webkitCompassAccuracy &gt;= 30 ){ this.trigger( 'inaccurate-compass' ); return; } // I think this means it's warming up :) if( oEv.webkitCompassAccuracy == 0 ){ return; } // normalize left or bottom being up if( ( 90 === orient ) || ( 180 === orient ) ){ angle = -angle; } // if in portrait and leaning forward, flip the angle around 90deg if( !aOrient ){ if( Math.abs( gamma ) &gt; 90 ){ angle = 180 - angle; } } // normalize compass fliping from 0--&gt;359 or visa-versa if( this.lastHeading &amp;&amp; ( Math.abs( heading - this.lastHeading ) &gt; 180 ) ){ this.lastHeading = 360-this.lastHeading; } if( !this.heading ){ this.heading = heading; } this.trigger( 'heading', this.heading = this.heading + heading - this.lastHeading ); this.trigger( 'angle', this.angle = angle ); this.lastHeading = heading; } } ); }( jQuery ) ); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload