Code archives/Algorithms/Zooming In/Out
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
| Cool for space games, zoom in out of the action. All you do is apply +newscale to any setscale command. For example. setscale(1.0 + newscale, 1.0 + newscale) drawimage (image, x,y) setting 1.0 always ensures the image loads at its normal scale, and only applies newscale based on how much you zoom in or out with the mouse wheel. 1.0 could even by a variable. like DefaultScale. Default_Scale = 1.0 setscale (default_scale + newscale, default_scale + newscale) so you could have different variables for different images if you want them loaded bigger or smaller by default. The zoom has a little catch up, if you scroll the wheel like 5 clicks fast, it zoom as you do that, and continue on a little after you stopped. Makes it smooth and cool 1500@... if any questions. | |||||
Strict
Graphics 800,600,0
Global newscale:Float
While Not KeyHit(KEY_ESCAPE)
Cls
SetColor(244,122,134)
SetScale(1.0 + newscale,1.0 + newscale)
'handle cube by the centre
SetHandle(50,50)
DrawRect(400,300,100,100)
'call function
zoomcamera()
Flip
Wend
Function ZoomCamera()
Local MaxZoom:Float = 3.0
Local minzoom:Float = -0.8
Local speed:Float = 0.03
Local set_Zoom:Float
Local zoom
'mouse wheel value = to variable zoom
zoom = MouseZ()
'so that it doesn't zoom in whole numbers( which wouldn't be smooth)
set_Zoom = 0.2 * zoom
'limit max zoom in
If set_Zoom > maxZoom
Set_Zoom = maxZoom
End If
'zooms to the set zoom value(determined by how much you scroll)
If KeyDown(KEY_Z) = 0 And set_Zoom > newScale
newscale :+ speed
End If
'same as above
If KeyDown(KEY_X) = 0 And set_Zoom < newScale
newscale :- speed
End If
'limit zoom out
If newscale < minZoom
newscale = minZoom
End If
SetColor(100,100,100)
SetScale(1.0,1.0)
DrawText("Newscale: "+newscale,100,100)
End Function |
Comments
| ||
| this is the correct one .BMX sorry can an admin delete, the .BB one? |
| ||
| I have changed your Code a little bit so it zooms immediatly if you scroll the MouseWheel. I hope you have nothing against it. Otherwise it is a nice piece of Code (very useful for me) |
| ||
| aren't you famous around here, I cant believe code I did is useful to you. I should have kept this up. |
| ||
| just curious, what is it useful to you for. And since ive started back coding, can I ask you for some help in my project, mainly consultative? |
| ||
for HK if he looks here, run this code, i have put two rects in now. you will see the problem i mean. going closer together.Strict
Graphics 800,600,0
Global newscale:Float
While Not KeyHit(KEY_ESCAPE)
Cls
SetColor(244,122,134)
SetScale(1.0 + newscale,1.0 + newscale)
'handle cube by the centre
SetHandle(50,50)
DrawRect(500,300,100,100)
SetHandle(50,50)
DrawRect(100,300,100,100)
'call function
zoomcamera()
Flip
Wend
Function ZoomCamera()
Local MaxZoom:Float = 3.0
Local minzoom:Float = -0.8
Local speed:Float = 0.03
Local set_Zoom:Float
Local zoom
'mouse wheel value = to variable zoom
zoom = MouseZ()
'so that it doesn't zoom in whole numbers( which wouldn't be smooth)
set_Zoom = 0.2 * zoom
'limit max zoom in
If set_Zoom > maxZoom
Set_Zoom = maxZoom
End If
'zooms to the set zoom value(determined by how much you scroll)
If KeyDown(KEY_Z) = 0 And set_Zoom > newScale
newscale :+ speed
End If
'same as above
If KeyDown(KEY_X) = 0 And set_Zoom < newScale
newscale :- speed
End If
'limit zoom out
If newscale < minZoom
newscale = minZoom
End If
SetColor(100,100,100)
SetScale(1.0,1.0)
DrawText("Newscale: "+newscale,100,100)
End Function
|
| ||
| |
| ||
| Here is my take on it. Pans the draw area using setorigin and stops the rects getting closer together by scaling the draw position. Implements a slightly different zoom function which acccepts Min Zoom, Max Zoom, Resolution and number of steps to achieve a target zoom at this resolution by using the mousewheel. This calls a MouseZspeed function similar to the old Blitz one, but which accepts a speed modifier. |
Code Archives Forum