public
Created

A jQuery method that attempts to calculate the angle of rotation of the given HTML element.

  • Download Gist
getPureRotation.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
$.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;
};

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

and negative values are return correctly but positive

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.