Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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

This comment has been minimized.

Show comment Hide comment
@chrisbod

chrisbod Oct 25, 2012

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

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

This comment has been minimized.

Show comment Hide comment
@chrisbod

chrisbod Oct 25, 2012

and negative values are return correctly but positive

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