views:

99

answers:

2

this is my simple function ('mycompass' is a div in my body)

function watchCompass() {

    var suc = function(a){       
        var r =a.magneticHeading;  
        document.getElementById('mycompass').style.webkitTransform  = "rotateZ("+-r+"deg)";  
    };
    var fail = function(){};
    var opt = {};
    opt.frequency = 50;
    timer = navigator.compass.watchHeading(suc,fail,opt);
}

for some reason when the compass 'wheel' does a complete rotation from 0 to 360 it goes back to 0 and not to the "virtual" 361... etc.. is not able to understand that from 0 and 360 there is no difference ...

and I don't know how to calculate a way to have the wheel spining smoothy clockwise and anticlockwise

another way to put this question is:

how can i rotate my objext from, for instance, from 15deg to 270deg, in anti-clockwise direction instead of clockwise? how can i tell my script that vale?

A: 

Again, here is an example of how to do this: http://gutfullofbeer.net/compass.html

Pointy
I still dont think thi sis the right example because this compass is dumb ad doesnt recognize the shortest lenght between two point, it simply goes from A to B without asking "is shorter to go from A to B clokwise or go from B backward?"like a real compass
camelCase
+1  A: 

this is my workaround but i stil have problem when the wheel degree == -360 or 360

var angle = 0;
var lastAngle = 0;




   function watchCompass() {

          var suc = function(a){
          var angle = roundNyc(a.magneticHeading);

       l1 = angle -  lastAngle;
       l2 = 360 - l1;

      if( Math.abs(l1) >=  Math.abs(l2)){ 

       r =   (lastAngle - l2) ; 

      }else{

       r =  angle ; 

      }    

       lastAngle = r;     
       l1 = 0;
       l2 = 0;

   document.getElementById('mycompass').style.webkitTransform = 'rotateZ(' + -r + 'deg)';

          };
          var fail = function(){};
          var opt = {};
          opt.frequency = 50;
          timer = navigator.compass.watchHeading(suc,fail,opt);
        } 
camelCase