I'm having trouble with zooming (using scroll wheel) in and out of a 2D image based on mouse position, just like you would see in any paint program. I've got the panning alone working - holding middle mouse button allows you to pan around, but zoom is wonky.
Edit: New code pasted below to show you how the zoom doesn't work well. Imagine if you were trying to zoom in to a particular point on the square. It seems to work but goes adrift soon enough.
Code: Select all
#Include "fbgfx.bi"
#Define RES_W 1280
#Define RES_H 720
Dim As Integer mx,my,ms,mb, old_ms
Dim As Integer zoom, pan_x, pan_y, old_pan_x, old_pan_y, grabbed, mGrabx, mGraby
ScreenRes(res_w,res_h,32,,0)
pan_x = 400
pan_y = 200
Zoom = 1
Do
GetMouse mx,my,ms,mb
If ms <> old_ms Then
Zoom = ms + 1
If Zoom < 1 Then Zoom = 1
If Zoom > 60 Then Zoom = 60
If ms > old_ms Then
'' Still a wonky zoom but closest I can get.
'' Divide the zoom amount by the same ratio as mouse is to screen edge and adjust pan by that amount.
pan_x -= ((res_w * .1) / (res_w/(-pan_x+mx)) )
pan_y -= ((res_h * .1) / (res_h/(-pan_y+my)) )
ElseIf ms < old_ms Then
pan_x += ((res_w * .1) / (res_w/(-pan_x+mx)) )
pan_y += ((res_h * .1) / (res_h/(-pan_y+my)) )
EndIf
old_ms = ms
EndIf
If mb = 0 Then
grabbed = FALSE
ElseIf mb = 4 Then
If grabbed = FALSE Then
old_pan_x = pan_x
old_pan_y = pan_y
mGrabx = mx
mGraby = my
grabbed = TRUE
Else
pan_x = old_pan_x + -(mGrabx - mx)
pan_y = old_pan_y + -(mGraby - my)
End If
End If
If pan_x < 0 Then pan_x = 0
If pan_y < 0 Then pan_y = 0
If pan_x > res_w-Zoom*10 Then pan_x = res_w-Zoom*10
If pan_y > res_h-Zoom*10 Then pan_y = res_h-Zoom*10
ScreenLock
Cls
Locate 1,1,0
Print "Center mouse button to pan, scroll to zoom, q to quit"
Print "pan_x: "; pan_x
Print "pan_y: "; pan_y
Print "zoom: "; zoom
Print "mx,my: "; mx,my
Print "mb: "; mb
Print "ms: "; ms
Line (pan_x,pan_y)-(pan_x+(10*Zoom),pan_y+(10*Zoom)),RGB(255,100,100),bf
ScreenUnLock
Sleep(1,1)
Loop Until InKey = "q"
So the simple goal here is for the increased/decreased zoom to be divided into all four directions appropriately in order to maintain focus on point at which the mouse x/y was hovering just before scroll wheel rotation. For example, if mouse cursor is all the way at (or beyond) the upper-left corner of image, all zoom/scale is extended at the right and bottom of image. In contrast, if cursor is somewhere near bottom-right corner of image, most of the zoom/scale is extended at upper-left sides, but a fraction of it is also extended at the other sides. This way focal point under the cursor is never moved during scaling.
I've tried awhile tinkering around. The solution is probably pretty easy, I'm just not thinking clear.
If you have ideas, I'd be interested and thankful. :)