How to keep the zoom centered ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

How to keep the zoom centered ?

Post by Fig »

Hi,

With a larger image than the screen, and scrolling, do you know how to keep the image centered when I zoom ?
Is my question understandable ?

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0 Or OpenWindow(0, 0, 0,800, 600, "zoom", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)=0 Or OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)=0:MessageRequester("Error", "Error", 0):End:EndIf
Xresolution=800:Yresolution=600
;back sprite
CreateSprite(0,4000,2000):StartDrawing(SpriteOutput(0)):For j=0 To 20:For i=0 To 40:Circle(i*100,j*100,50,Random($FFFFFF)):Next i:Next j:StopDrawing()
;mouse sprite
CreateSprite(1,32,32):StartDrawing(SpriteOutput(1)):Box(0,0,32,32,#Red):StopDrawing()
;coordonnées world
X=0:Y=0:MouseLocate(120,120)
Repeat
  FlipBuffers()
  ClearScreen(#Blue)
  ExamineKeyboard()
  ExamineMouse()
  If WindowEvent()=#PB_Event_CloseWindow Or KeyboardReleased(#PB_Key_Escape):End:EndIf
    Xmouse=MouseX():Ymouse=MouseY():tick.i=MouseWheel()
    ;scroll if mouse on the edge of the screen
    If Xmouse<10:X+10:EndIf
    If Xmouse>Xresolution-10:X-10:EndIf
    If Ymouse<10:Y+10:EndIf
    If Ymouse>Yresolution-10:Y-10:EndIf    
    ;zoom
    If tick
      ZoomWorld+100*tick
      ZoomSprite(0,4000+ZoomWorld,2000+ZoomWorld>>1)
    EndIf
  ;back
  DisplaySprite(0,X,Y)
  ;mouse
  DisplaySprite(1,Xmouse,Ymouse)
ForEver
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: How to keep the zoom centered ?

Post by kenmo »

Code: Select all

    ;zoom
    If tick
      ; Calculate center point
      CenterX = (ScreenWidth()/2 - X)  * 4000.0/(4000+ZoomWorld)
      CenterY = (ScreenHeight()/2 - Y) * 2000.0/(2000+ZoomWorld/2)
      ZoomWorld+100*tick
      ZoomSprite(0,4000+ZoomWorld,2000+ZoomWorld>>1)
      ; Restore center point
      X = ScreenWidth()/2  - CenterX * (4000+ZoomWorld)/4000.0
      Y = ScreenHeight()/2 - CenterY * (2000+ZoomWorld/2)/2000.0
    EndIf
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: How to keep the zoom centered ?

Post by Fig »

Perfect! Thank you very much ! :D
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: How to keep the zoom centered ?

Post by kenmo »

Same thing, more clear:

Code: Select all

    ;zoom
    If tick
      ; Previous zoom factor
      ZoomFactor.f = (4000+ZoomWorld)/4000.0
      ; Calculate center point
      CenterX = (ScreenWidth()/2  - X) / ZoomFactor
      CenterY = (ScreenHeight()/2 - Y) / ZoomFactor
      ZoomWorld+100*tick
      ; New zoom factor
      ZoomFactor.f = (4000+ZoomWorld)/4000.0
      ZoomSprite(0,4000 * ZoomFactor,2000 * ZoomFactor)
      ; Restore center point
      X = ScreenWidth()/2  - (CenterX * ZoomFactor)
      Y = ScreenHeight()/2 - (CenterY * ZoomFactor)
    EndIf
Post Reply