Controlling The Pan (to Anchor A Point) When Zooming Into An Image
I'm writing a simple image viewer and am implementing a pan and zoom feature (using mouse dragging and mouse wheel scrolling respectively). I've successfully implemented the pan (e
Solution 1:
Ok, I managed to get it working
defwheelEvent(self, event):
oldscale = self.scale
self.scale += event.delta() / 1200.0if (self.scale < 0.1):
self.scale = oldscale
screenpoint = self.mapFromGlobal(QtGui.QCursor.pos())
dx, dy = screenpoint.x(), screenpoint.y()
oldpoint = (screenpoint.x() + self.position[0], screenpoint.y() + self.position[1])
newpoint = (oldpoint[0] * (self.scale/oldscale),
oldpoint[1] * (self.scale/oldscale))
self.position = (newpoint[0] - dx, newpoint[1] - dy)
the logic here:
- we get the mouses position on the screen (screenpoint), and using this, we have the distance between our anchored pixel and the edge of the screen (by definition)
- we use screenpoint and position to find the coordinate of the mouse in terms of the image's plane (ie: the 2D index of the hovered pixel), as oldpoint
- applying our scaling, we calculate the new 2D index of the pixel (new point)
- we want this pixel on our screen, but not in the top left: we want it dx and dy from the top left (position)
The problem was indeed a trivial confusion between image and display coordinates.
Post a Comment for "Controlling The Pan (to Anchor A Point) When Zooming Into An Image"