Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do it more easily with css3 transitions.<br> Exemple: <a href="http://jsfiddle.net/BaliBalo/xn75a/" rel="nofollow">http://jsfiddle.net/BaliBalo/xn75a/</a><br> The main center-mouse algorithm is here:</p> <pre><code>//Center the image if it's smaller than the container if(scale &lt;= 1) { currentLocation.x = im.width() / 2; currentLocation.y = im.height() / 2; } else { currentLocation.x += moveSmooth * (mouseLocation.x - currentLocation.x) / currentScale; currentLocation.y += moveSmooth * (mouseLocation.y - currentLocation.y) / currentScale; } </code></pre> <p>If you want to keep your already-existing code, I think you can do it approximatively the same: the trick to get the mouse position on the zoomed image when the image itself is zoomed by a css scale transform and using transform-origin is to substract the transform origin to your point then multiply by the factor and finally re-add the transform-origin.</p> <p>You can also use translate as in this updated example: <a href="http://jsfiddle.net/BaliBalo/xn75a/15/" rel="nofollow">http://jsfiddle.net/BaliBalo/xn75a/15/</a> As you can see it even simplifies the formulas but don't forget to sub the center of the element to mouse point as a translation of {0, 0} will zoom on the middle of the image.</p> <hr> <h2>EDIT:</h2> <p>I stumbled on this answer of mine today and figured it wasn't really the behaviour you wanted. I didn't understand correctly the first time that you didn't want to center the point under the mouse but to keep it in the same spot. As I did it recently for a personnal project I tried to correct myself. So here is a more accurate answer :<br> <a href="http://jsfiddle.net/BaliBalo/7ozrc1cq/" rel="nofollow">http://jsfiddle.net/BaliBalo/7ozrc1cq/</a></p> <p>The main part is now (if currentLocation is the top-left point of the image in the container) :</p> <pre><code>var factor = 1 - newScale / currentScale; currentLocation.x += (mouseLocation.x - currentLocation.x) * factor; currentLocation.y += (mouseLocation.y - currentLocation.y) * factor; </code></pre> <p>I also removed the CSS transition. I think the best way to achieve a smooth scrolling would be to use <code>requestAnimationFrame</code> to create an animation loop and instead of changing values directly in the <code>zoom</code> function, store values and animate towards them in the loop.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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