Skip to content

Instantly share code, notes, and snippets.

@wilhelm-murdoch
Created December 18, 2011 05:22
Show Gist options
  • Save wilhelm-murdoch/1492454 to your computer and use it in GitHub Desktop.
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.
$.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;
};
@chrisbod
Copy link

Hi there just to let you know this doesnt work for values over 180 (it gives 360-rotation) e.g. rotating 185 gives 175

@chrisbod
Copy link

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