Created
December 18, 2011 05:22
-
-
Save wilhelm-murdoch/1492454 to your computer and use it in GitHub Desktop.
A jQuery method that attempts to calculate the angle of rotation of the given HTML element.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.getPureRotation = function(element) { | |
var degrees = null; | |
$.each(['-webkit-transform', '-moz-transform', '-o-transform', '-sand-transform', '-ms-transform', 'transform'], function(index, value) { | |
var matrix = $(element).css(value); | |
if(degrees == null || Boolean(matrix)) { | |
var arrMatrix = matrix.match(/[\-0-9.]+/g); | |
if( | |
(parseFloat(arrMatrix[1]) == (-1 * parseFloat(arrMatrix[2]))) || | |
(parseFloat(arrMatrix[3]) == parseFloat(arrMatrix[0])) || | |
((parseFloat(arrMatrix[0]) * parseFloat(arrMatrix[3]) - parseFloat(arrMatrix[2]) * parseFloat(arrMatrix[1])) == 1) | |
) { | |
degrees = Math.round(Math.acos(parseFloat(arrMatrix[0])) * 180 / Math.PI); | |
} else { | |
degrees = 0; | |
} | |
} | |
}); | |
return degrees; | |
}; |
and negative values are return correctly but positive
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there just to let you know this doesnt work for values over 180 (it gives 360-rotation) e.g. rotating 185 gives 175