C00lGuy C00lGuy - 5 months ago 8
jQuery Question

Get direction a div is facing

I have code that rotates a div and I would like to figure out which direction it is facing.

How would I find the direction a div is facing?

function getDirectionX(elem){
lcelement = document.getElementById(elem);
// find direction lcelement is facing here
// return x;
}
function getDirectionY(elem){
lcelement = document.getElementById(elem);
// find direction lcelement is facing here
// return y;
}

Answer

Does this help?

https://jsfiddle.net/m3130qo8/1/

Code:

function spinit(){
    var woah = document.getElementById('woah');
    var deg = Math.floor(Math.random() * 360) + 0;
    woah.style.webkitTransform = 'rotate('+deg+'deg)'; 
    woah.style.mozTransform    = 'rotate('+deg+'deg)'; 
    woah.style.msTransform     = 'rotate('+deg+'deg)'; 
    woah.style.oTransform      = 'rotate('+deg+'deg)'; 
    woah.style.transform       = 'rotate('+deg+'deg)'; 

    var current_rotation = /[0-9]+/.exec(woah.style.transform);
    document.getElementById('woah').innerHTML=current_rotation;
}