Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom image to cursor breaks when mouse is moved

Tags:

This is a followup question to How to zoom to mouse pointer while using my own mousewheel smoothscroll?

I am using css transforms to zoom an image to the mouse pointer. I am also using my own smooth scroll algorithm to interpolate and provide momentum to the mousewheel.

With Bali Balo's help in my previous question I have managed to get 90% of the way there.

You can now zoom the image all the way in to the mouse pointer while still having smooth scrolling as the following JSFiddle illustrates:

http://jsfiddle.net/qGGwx/7/

However, the functionality is broken when the mouse pointer is moved.

To further clarify, If I zoom in one notch on the mousewheel the image is zoomed around the correct position. This behavior continues for every notch I zoom in on the mousewheel, completely as intended. If however, after zooming part way in, I move the mouse to a different position, the functionality breaks and I have to zoom out completely in order to change the zoom position.

The intended behavior is for any changes in mouse position during the zooming process to be correctly reflected in the zoomed image.

The two main functions that control the current behavior are as follows:

self.container.on('mousewheel', function (e, delta) {         var offset = self.image.offset();          self.mouseLocation.x = (e.pageX - offset.left) / self.currentscale;         self.mouseLocation.y = (e.pageY - offset.top) / self.currentscale;          if (!self.running) {             self.running = true;             self.animateLoop();         }         self.delta = delta         self.smoothWheel(delta);         return false;     }); 

This function collects the current position of the mouse at the current scale of the zoomed image.

It then starts my smooth scroll algorithm which results in the next function being called for every interpolation:

zoom: function (scale) {      var self = this;     self.currentLocation.x += ((self.mouseLocation.x - self.currentLocation.x) / self.currentscale);     self.currentLocation.y += ((self.mouseLocation.y - self.currentLocation.y) / self.currentscale);      var compat = ['-moz-', '-webkit-', '-o-', '-ms-', ''];     var newCss = {};     for (var i = compat.length - 1; i; i--) {         newCss[compat[i] + 'transform'] = 'scale(' + scale + ')';         newCss[compat[i] + 'transform-origin'] = self.currentLocation.x + 'px ' + self.currentLocation.y + 'px';     }     self.image.css(newCss);       self.currentscale = scale; }, 

This function takes the scale amount (1-10) and applies the css transforms, repositioning the image using transform-origin.

Although this works perfectly for a stationary mouse position chosen when the image is completely zoomed out; as stated above it breaks when the mouse cursor is moved after a partial zoom.

Huge thanks in advance to anyone who can help.

like image 612
gordyr Avatar asked Jan 21 '13 10:01

gordyr


1 Answers

Actually, not too complicated. You just need to separate the mouse location updating logic from the zoom updating logic. Check out my fiddle: http://jsfiddle.net/qGGwx/41/

All I have done here is add a 'mousemove' listener on the container, and put the self.mouseLocation updating logic in there. Since it is no longer required, I also took out the mouseLocation updating logic from the 'mousewheel' handler. The animation code stays the same, as does the decision of when to start/stop the animation loop.

here's the code:

    self.container.on('mousewheel', function (e, delta) {         if (!self.running) {             self.running = true;             self.animateLoop();         }         self.delta = delta         self.smoothWheel(delta);         return false;     });      self.container.on('mousemove', function (e) {         var offset = self.image.offset();          self.mouseLocation.x = (e.pageX - offset.left) / self.currentscale;         self.mouseLocation.y = (e.pageY - offset.top) / self.currentscale;     }); 
like image 117
jordancpaul Avatar answered Sep 28 '22 13:09

jordancpaul