Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom to cursor without canvas in javascript

I have an <img> that is zoomed upon mousewheel scrolling, by adjusting transform: scale(). I want the zooming to be like in Google Maps, where you zoom to where the mouse cursor is, not to the center of the image. I'd like to not use canvas though, just for the learning experience (that's also why the other questions I found did not really help).

I set up a JSFiddle to demonstrate the problem. My thought process was as follows: when zooming in by 10%, the image expands in all directions, from the center of the image, by 10%. That means that e.g., the left and right edge will travel 5% of the original width in each direction. I therefore tried to solve the problem like so:

  1. Calculate mouse offset from image center
  2. Calculate new image offset (top and left) by multiplying mouse offset with zoom factor and divide by two
  3. Apply offset and watch it all blow up it my face with the power of a million burning suns

It seems that I just can't find a formula or algorithm that fits.

like image 753
Vey Avatar asked Feb 04 '23 09:02

Vey


1 Answers

Eventually I figured it out myself, although only by looking at existing solutions. Here is the JSFiddle that contains only the essentials.

The idea is to first set transform-origin: 0 0. This makes sure that, upon zooming, the image expands down and right, instead of distributing the increase in width over all four sides. Note that it does not reposition the image, it just changes the origin for all transformations.

Additionally, this JSFiddle assumes that the top and left margins of the image are aligned with the top and left margins of the container element. If the image should be repositioned before zooming occurs, this should be done through transform: translate() and the translateX and translateY values need to be updated accordingly.

The heart of the logic is this:

  // Track the percentage change between the old
  // and the new scale of the image
  const ratio = 1 - nextScale / currentScale

  // get the current mouse offset
  const {
    clientX,
    clientY
  } = event

  // The += here is extremely important!
  // The new 2D translation values are derived from the difference
  // between mouse cursor position and current (!) 2D translation.
  // So where is the mouse cursor relative to the translated image
  // This difference is then adjusted by the % change of the scaling
  translateX += (clientX - translateX) * ratio
  translateY += (clientY - translateY) * ratio

  /*
  This would work for the first wheel scroll. But afterwards, the 
  image will not be translated enough to offset the zooming because 
  we're not taking into account the existing translation
  translateX += (clientX - translateX) * ratio
  translateY += (clientY - translateY) * ratio
  */

So to summarize the required steps:

  1. Calculate the next scale
  2. Calculate the current mouse offset relative to the translated image
  3. Adjust the mouse offset for the change in scaling, e.g., const percentChange = 1 - nextScale / currentScale
  4. Add the adjusted mouse offset to the existing values for translate()
  5. Apply the transformation (scaling and the translation)

The linked JSFiddle also includes Lodash and transition: transform 330ms ease-in-out; to make the scrolling a little smoother and not affect browser performance too much.

like image 74
Vey Avatar answered Feb 16 '23 03:02

Vey