Code archives/Algorithms/Zooming In/Out

This code has been declared by its author to be Public Domain code.

Download source code

Zooming In/Out by Cruis.In2005
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

Cruis.In2005
this is the correct one .BMX
sorry can an admin delete, the .BB one?


klepto22005
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)




Cruis.In2006
aren't you famous around here, I cant believe code I did is useful to you. I should have kept this up.


Cruis.In2006
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?


Cruis.In2006
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



bradford62006



DJWoodgate2007
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