title: How to get the correct mouse position in relation to a div that has has a scale transform applied
body: <p>I am using CSS transform scale to create a smooth zoom on a div. The problem is that I want to be able to get the correct mouse position in relation to div even when scaled up, but I can seem figure out the correct algorithm to get this data.  I am retrieving the current scale factor from:</p>
  <pre><code>var transform = new WebKitCSSMatrix(window.getComputedStyle($(&quot;#zoom_div&quot;)[0]).webkitTransform);
  scale = transform.a;
  </code></pre>
  <p>When I read the position of the div at various scale settings it seems to report the correct position, i.e. when I scale the div until is is larger the the screen the position left and top values are negative and appear to be correct, as does the returned scale value:</p>
  <pre><code>$(&quot;#zoom_div&quot;).position().left
  $(&quot;#zoom_div&quot;).position().top
  </code></pre>
  <p>To get the current mouse position I am reading the x and y position from the click event and taking away the offset. This works correctly at a scale value of 1 (no scale) but not when the div is scaled up. Here is my basic code:</p>
  <pre><code>$(&quot;#zoom_div&quot;).on(&quot;click&quot;, function(e){
      var org = e.originalEvent;
  
      var pos = $(&quot;#zoom_div&quot;).position();
  
      var offset = {
          x:org.changedTouches[0].pageX - pos.left,
          y:org.changedTouches[0].pageY - pos.top
      }
  
      var rel_x_pos = org.changedTouches[0].pageX - offset.x;
      var rel_y_pos = org.changedTouches[0].pageY - offset.y;
  
      var rel_pos = [rel_x_pos, rel_y_pos];
      return rel_pos;
  });
  </code></pre>
  <p>I have made several attempts at multiplying dividing adding and subtracting the scale factor   to/from from the pageX / Y but without any luck. Can anyone help me figure out how to get the correct value.</p>
  <p>(I have simplified my code from the original to hopefully make my question clearer, any errors you may find in the above code is due to that editing down. My original code with the exception for the mouse position issue).</p>
  <p>To illustrate what I am talking about I have made a quick jsfiddle example that allows the dragging of a div using translate3d.  When the scale is normal (1) the div is dragged at the point where it is clicked. When the div is scales up (2) it no longer drags correctly from the point clicked.</p>
  <p><a href="http://jsfiddle.net/6EsYG/12/" rel="nofollow noreferrer">http://jsfiddle.net/6EsYG/12/</a></p>