2017-08-07 1 views
0

Ich versuche Kompass innerhalb der A-Frame-Szene basierend auf Mausklick und ziehen ... aber das Problem, das ich hier bin, basiert auf Mauszeigerbewegung; Das Bild wird gedreht.Erstellen eines Kompass in

function diff(x, y) { 
 
    var centerItem = $('#pointer'), 
 
     centerLoc = centerItem.offset(); 
 
    var dx = x - (centerLoc.left + (centerItem.width()/2)); 
 
    dy = y - (centerLoc.top + (centerItem.height()/2)); 
 
    return Math.atan2(dy, dx) * (180/Math.PI); 
 
} 
 

 
$('body').mousemove(function(e) { 
 
    var x = e.pageX; 
 
    var y = e.pageY; 
 
    var myAngle = diff(x, y); 
 
    var rotationValue = 'rotate(' + myAngle + 'deg)'; 
 

 
    $("#pointer").css({ 
 
    '-moz-transform': rotationValue, // FireFox 
 
    '-webkit-transform': rotationValue, // Chrome 
 
    '-o-transform': rotationValue, 
 
    '-ms-transform': rotationValue, 
 
    'transform': rotationValue 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
<img class="compass" src="http://i.imgur.com/YahETIk.png" id="pointer" />

Wie ich dieses Problem lösen kann? Danke im Voraus.

Antwort

0

Verwenden Sie anstelle der Maus die Kameradrehung.

<script> 
    AFRAME.registerComponent('compass', { 
     init: function() { 
     this.pointer = document.getElementById('pointer'); 
     }, 

     tick: function() { 
     var yRotation = this.el.getAttribute('rotation').y; 
     this.pointer.style.transform = 'rotate(' + yRotation + 'deg)'; 
     } 
    }); 
    </script> 

    <img class="compass" src="http://i.imgur.com/YahETIk.png" id="pointer"/> 

    <a-scene> 
     <a-camera compass></a-camera> 
    </a-scene>