Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the correct mouse position in relation to a div that has has a scale transform applied
    primarykey
    data
    text
    <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($("#zoom_div")[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>$("#zoom_div").position().left $("#zoom_div").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>$("#zoom_div").on("click", function(e){ var org = e.originalEvent; var pos = $("#zoom_div").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 fo 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/">http://jsfiddle.net/6EsYG/12/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload