title: Calculate the bounding box's X, Y, Height and Width of a rotated element via JavaScript
body: <p>Basically I'm asking this question for JavaScript: <a href="https://stackoverflow.com/questions/622140/calculate-bounding-box-coordinates-from-a-rotated-rectangle-picture-inside">Calculate Bounding box coordinates from a rotated rectangle</a></p>
  
  <p><img src="https://i.stack.imgur.com/41w45.png" alt="Diagram"></p>
  
  <p>In this case:</p>
  
  <ul>
  <li>iX = Width of rotated (blue) HTML element</li>
  <li>iY = Height of rotated (blue) HTML element</li>
  <li>bx = Width of Bounding Box (red)</li>
  <li>by = Height of Bounding Box (red)</li>
  <li>x = X coord of Bounding Box (red)</li>
  <li>y = Y coord of Bounding Box (red)</li>
  <li>iAngle/t = Angle of rotation of HTML element (blue; not shown but
  used in code below), FYI: It's 37 degrees in this example (not that it matters for the example)</li>
  </ul>
  
  <p>How does one calculate the X, Y, Height and Width of a bounding box (all the red numbers) surrounding a rotated HTML element (given its width, height, and Angle of rotation) via JavaScript? A sticky bit to this will be getting the rotated HTML element (blue box)'s original X/Y coords to use as an offset somehow (this is not represented in the code below). This may well have to look at <a href="http://www.w3schools.com/cssref/css3_pr_transform-origin.asp" rel="noreferrer">CSS3's transform-origin</a> to determine the center point.</p>
  
  <p>I've got a partial solution, but the calculation of the X/Y coords is not functioning properly...</p>
  
  <pre><code>var boundingBox = function (iX, iY, iAngle) {
      var x, y, bx, by, t;
  
      //# Allow for negetive iAngle's that rotate counter clockwise while always ensuring iAngle's &lt; 360
      t = ((iAngle &lt; 0 ? 360 - iAngle : iAngle) % 360);
  
      //# Calculate the width (bx) and height (by) of the .boundingBox
      //#     NOTE: See https://stackoverflow.com/questions/3231176/how-to-get-size-of-a-rotated-rectangle
      bx = (iX * Math.sin(iAngle) + iY * Math.cos(iAngle));
      by = (iX * Math.cos(iAngle) + iY * Math.sin(iAngle));
  
      //# This part is wrong, as it's re-calculating the iX/iY of the rotated element (blue)
      //# we want the x/y of the bounding box (red)
      //#     NOTE: See https://stackoverflow.com/questions/9971230/calculate-rotated-rectangle-size-from-known-bounding-box-coordinates
      x = (1 / (Math.pow(Math.cos(t), 2) - Math.pow(Math.sin(t), 2))) * (bx * Math.cos(t) - by * Math.sin(t));
      y = (1 / (Math.pow(Math.cos(t), 2) - Math.pow(Math.sin(t), 2))) * (-bx * Math.sin(t) + by * Math.cos(t));
  
      //# Return an object to the caller representing the x/y and width/height of the calculated .boundingBox
      return {
          x: parseInt(x), width: parseInt(bx),
          y: parseInt(y), height: parseInt(by)
      }
  };
  </code></pre>
  
  <p>I feel like I am so close, and yet so far...</p>
  
  <p>Many thanks for any help you can provide!</p>
  
  <p><strong><em>TO HELP THE NON-JAVASCRIPTERS...</em></strong></p>
  
  <p>Once the HTML element is rotated, the browser returns a "matrix transform" or "rotation matrix" which seems to be this: <code>rotate(Xdeg) = matrix(cos(X), sin(X), -sin(X), cos(X), 0, 0);</code> <a href="http://css-tricks.com/get-value-of-css-rotation-through-javascript/" rel="noreferrer">See this page for more info</a>.</p>
  
  <p>I have a feeling this will enlighten us on how to get the X,Y of the bounding box (red) based solely on the Width, Height and Angle of the rotated element (blue).</p>
  
  <p><strong>New Info</strong></p>
  
  <p>Humm... interesting...</p>
  
  <p><img src="https://i.stack.imgur.com/h419y.png" alt="enter image description here"></p>
  
  <p>Each browser seems to handle the rotation differently from an X/Y perspective! FF ignores it completely, IE &amp; Opera draw the bounding box (but its properties are not exposed, ie: bx &amp; by) and Chrome &amp; Safari rotate the rectangle! All are properly reporting the X/Y except FF. So... the X/Y issue seems to exist for FF only! How very odd!</p>
  
  <p>Also of note, it seems that <code>$(document).ready(function () {...});</code> fires too early for the rotated X/Y to be recognized (which was part of my original problem!). I am rotating the elements directly before the X/Y interrogation calls in <code>$(document).ready(function () {...});</code> but they don't seem to update until some time after(!?).</p>
  
  <p>When I get a little more time, I will toss up a jFiddle with the example, but I'm using a modified form of "jquery-css-transform.js" so I have a tiny bit of tinkering before the jFiddle...</p>
  
  <p>So... what's up, FireFox? That ain't cool, man!</p>
  
  <p><strong><em>The Plot Thickens...</em></strong></p>
  
  <p>Well, FF12 seems to fix the issue with FF11, and now acts like IE and Opera. But now I am back to square one with the X/Y, but at least I think I know why now...</p>
  
  <p>It seems that even though the X/Y is being reported correctly by the browsers for the rotated object, a "ghost" X/Y still exists on the un-rotated version. It seems as though this is the order of operations:</p>
  
  <ul>
  <li>Starting with an un-rotated element at an X,Y of 20,20</li>
  <li>Rotate said element, resulting in the reporting of X,Y as 15,35</li>
  <li>Move said element via JavaScript/CSS to X,Y 10,10</li>
  <li>Browser logically un-rotates element back to 20,20, moves to 10,10 then re-rotates, resulting in an X,Y of 5,25</li>
  </ul>
  
  <p>So... I want the element to end up at 10,10 post rotation, but thanks to the fact that the element is (seemingly) re-rotated post move, the resulting X,Y differs from the set X,Y.</p>
  
  <p>This is my problem! So what I really need is a function to take the desired destination coords (10,10), and work backwards from there to get the starting X,Y coords that will result in the element being rotated into 10,10. At least I know what my problem is now, as thanks to the inner workings of the browsers, it seems with a rotated element 10=5!</p>