Skip to content

Instantly share code, notes, and snippets.

@soutar
Created July 11, 2014 10:57
Show Gist options
  • Save soutar/fc8e3f34015a6147b391 to your computer and use it in GitHub Desktop.
Save soutar/fc8e3f34015a6147b391 to your computer and use it in GitHub Desktop.
How to massively over-engineer a method which is possible natively with getBoundingClientRect() - A guide by someone who didn't know about getBoundingClientRect()
calculateWidthOld: function (elem) {
var tr, values, transform,
a, b,
angle, width, height,
adjustedWidth;
transform = [
elem.css('-webkit-transform'),
elem.css('-moz-transform'),
elem.css('-ms-transform'),
elem.css('-o-transform'),
elem.css('transform')
];
// Take the first value that isn't undefined from our array
tr = transform[0] || transform[1] || transform[2] || transform[3];
values = tr.split('(')[1];
values = values.split(')')[0];
values = values.split(',');
a = values[0];
b = values[1];
angle = Math.atan2(b, a); // Angle in radians
if (angle < 0) { angle = angle * -1; } // Make sure our angle is positive
width = elem.width();
height = elem.height();
// http://stackoverflow.com/questions/9791403/how-to-get-actual-not-original-height-of-a-css-rotated-element
adjustedWidth = (height * Math.sin(angle)) + (width * Math.cos(angle));
return adjustedWidth;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment