Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zooming an image aimed at the mouse cursor

Use case:

User clicks a link, a modal window is displayed containing one big image, scaled down to fit this window. When the user scrolls their mousewheel up or down, the image zooms in or out. The image is zoomed at whatever location the mouse cursor is pointing.

Problem:

I can't wrap my brain around the how-to part of doing this.

Current workings:

When a link is clicked, a jQuery plugin creates a "viewer" collection of html elements that are styled with CSS. It's an absolute positioned DIV that contains an IMG-element. The image is scaled to 100% width. When scrolling, the image is increased by 5% per scroll action.

Because the image is scaled up whilst zooming in, the containing DIV (overflow: hidden) will eventually clip the image. The image is draggable so that you can always see all parts of it.

Variables I keep and know:

The image's scaled down ratio. The image's dimensions pre our most recent zoom action. The image's new dimensions after our most recent zoom action. The difference in these two dimensions. The X and Y position of the image. The X and Y position of the mouse cursor, relative to the positioned container DIV.

What I probably need:

I know the variables I need, I just can't grasp in what order to put them and what operators to use when. Math never was my strong suit, neither was grammar. So any help is appreciated.

Research done:

I Googled a lot for existing solutions. Most were implementations of OpenGL or Flash/ActionScript, none of which make any sense to me whatsoever. The few javascript-based examples I found were for commercial purposes, and even then their source code was obfuscated or minified, and I couldn't quite extract the inner workings for this little feature properly.

Also, I probably don't have the correct search words to figure it out, either.

like image 358
MHD Avatar asked Aug 31 '11 13:08

MHD


1 Answers

You could try to use an existing jQuery plugin, such as MapBox or GZoom:

  • http://wayfarerweb.com/jquery/plugins/mapbox
  • http://lab.gianiaz.com/jquery/gzoom

Or you could try to roll your own. If you do this, there are a three pieces you'll need to put together:

  1. Use javascript to scale the images in and out http://agilepartners.com/blog/2005/12/07/iphoto-image-resizing-using-javascript/
  2. Use javascript to allow you to pan the zoomed-in image
  3. Capture the scroll wheel movements in javascript http://www.switchonthecode.com/tutorials/javascript-tutorial-the-scroll-wheel
like image 131
CodeThug Avatar answered Oct 23 '22 22:10

CodeThug