News:

If you need instructions on how to get through the hotels, check out the enclosed instruction book.

Main Menu

Zoom and Pan

Farted by F U Clock, August 18, 2010, 02:47:25 PM

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

F U Clock

Sweet Jesus I have no idea why this is so hard to find, it really doesn't seem that complicated.

So anyways one of our clients sort of screwed us at work so now I'm trying to program this myself with limited knowledge. We need a map of a festival that will have control buttons on the top-right of the screen. Users will be able to press the zoom in button and the map image will grow, zoom out, shrink, etc.

Basically, I need to do two things. 1.) Make the image of the map draggable, which I can do, but also make it so the map can't move offscreen. This seems particularly difficult considering the zoom, because the map would theoretically be fully-zoomed out at 100%, so while I could set the boundaries so you can't pan it at all at this zoom-level, you wouldn't be able to move it once you zoomed in at all. Same problem exists if I were to make it draggable to some degree, because while that might work at a zoomed-in state, it would be possible to drag it offscreen at the 100% state too.

2.) I need to make it zoom in and out, preferably tweening larger and smaller. Seems simple enough and I've tried a couple of approaches, but they're not working too well... (btw this has to be in AS2) This is my favorite, but I'll explain why it sucks.

zoomIn_btn.onPress = function(){
zoomIn(map_mc);
}

zoomOut_btn.onPress = function(){
if(map_mc._xscale > 100){zoomOut(map_mc);
}
else{stop();
}
}

function 
zoomIn(map_mc){var myTween:Tween = new Tween(map_mc, "_xscale", None.easeNone, _xscale, _xscale + 100, .5, true);
var myTween:Tween = new Tween(map_mc, "_yscale", None.easeNone, _yscale, _yscale + 100, .5, true);
}

function 
zoomOut(map_mc){var myTween:Tween = new Tween(map_mc, "_xscale", None.easeNone, _xscale, _xscale - 100, .5, true);
var myTween:Tween = new Tween(map_mc, "_yscale", None.easeNone, _yscale, _yscale - 100, .5, true);
}


Now this makes it tween and successfully grows and shrinks, however as you can see since scale refers to percentage not actual size putting at -100 scale makes it dis a fuckin' ppear. I tried it with _width and _height too, and that seemed to blow up in my face, but maybe I'm just doing it wrong.

So yeah! Any suggestions would be greatly appreciated. I've managed to find oddly little about this exact thang, considering that it seems like it would be easy and a pretty common.

RomanClock

[FLASH]http://files.myfrogbag.com/hp3miy/PanAndZoom.swf height=400 width=400[/FLASH]

Pan with mouse or arrow keys.
Zoom with buttons or mouse wheel.
Double click zoom in/out to point. (not 100% working but you can just disable it)

Source: http://files.myfrogbag.com/hp3miy/PanAndZoom.fla [CS3]
// change to desired values //
theScale = 100;//desired zoom level
curScale = theScale;//current zoom level
maxZoom = 500;//maximum zoom
minZoom = 100;//minumum zoom
multi = 2;//scroll speed multiplier
scaleRate = 10;//smoothness of zoom
camWidth = 400;//camera width
camHeight = 400;//camera height
atx = 200;//center of screen x position
aty = 200;//center of screen y position
panRate = 10;//smoothness of panning
panSpeed = 10;//used for arrow keys
doubleClick = 20;//how long to wait for double click
clickZoom = true;//double click zooms in/out if true, else just goes there
buttonZoom = .6;//zoom button, a percent multiplier
lowx = 0;//lower x bound
hix = 400;//higher x bound
lowy = 0;//lower y bound
hiy = 400;//higher y bound
/////////////

// no edit //
panning = false;//is panning mode
panx = atx;//panning position x
pany = aty;//panning position y
ancx = 0;//anchor position x
ancy = 0;//anchor position y
difx = 0;//difference from anchor x
dify = 0;//difference from anchor y
curx = 0;//store camera position x
cury = 0;//stort camera position y
dbl = 0;//time to detect double click
/////////////

//mouse methods//
var mousey:Object = new Object ();
mousey.onMouseWheel = function (dif, theEvent)
{
theScale += dif * multi * (theScale / 100);
};
mousey.onMouseDown = function (theEvent)
{
ancx = difx = _xmouse;
ancy = dify = _ymouse;
curx = atx;
cury = aty;
if (dbl < 1)
{
dbl = doubleClick;
panning = true;
}
else
{
theScale = (clickZoom && theScale < maxZoom * .9) ? maxZoom : minZoom;
panx = _xmouse;
pany = _ymouse;
}
};
mousey.onMouseUp = function (theEvent)
{
panning = false;
};
mousey.onMouseMove = function (theEvent)
{
if (panning)
{
difx = _xmouse;
dify = _ymouse;
curx = atx;
cury = aty;
}
};
Mouse.addListener (mousey);
//////////

//button methods//
box.zin.onRelease = function ()
{
theScale *= 1 / buttonZoom;
dbl = 0;
};
box.zout.onRelease = function ()
{
theScale *= buttonZoom;
dbl = 0;
};
//////////

setInterval(cameraMan,10);
function cameraMan ()
{
//calculate scale
theScale = Math.max (minZoom, Math.min (theScale, maxZoom));
curScale += (theScale - curScale) / scaleRate;
scl = curScale / 100;

if (panning)
{
//calcluate position for mouse panning
panx = curx + (ancx - difx);
pany = cury + (ancy - dify);
}
else
{
//arrow keys panning
panx += (Key.isDown (Key.RIGHT) - Key.isDown (Key.LEFT)) * panSpeed / scl;
pany += (Key.isDown (Key.DOWN) - Key.isDown (Key.UP)) * panSpeed / scl;
}
//smooth panning
atx += (panx - atx) / (panRate);
aty += (pany - aty) / (panRate);

//restrain bounds
panx = Math.max ((lowx + (camWidth / 2) / scl), Math.min ((hix - (camWidth / 2) / scl), panx));
pany = Math.max ((lowy + (camHeight / 2) / scl), Math.min ((hiy - (camHeight / 2) / scl), pany));
atx = Math.max ((lowx + (camWidth / 2) / scl), Math.min ((hix - (camWidth / 2) / scl), atx));
aty = Math.max ((lowy + (camHeight / 2) / scl), Math.min ((hiy - (camHeight / 2) / scl), aty));

//set this properties
_xscale = _yscale = curScale;
_x = -atx * scl + camWidth / 2;
_y = -aty * scl + camHeight / 2;

//set box properties
box._xscale = box._yscale = 10000 / curScale;
box._x = (-_x + camWidth / 2) / scl;
box._y = (-_y + camHeight / 2) / scl;

//other
dbl--;
}

cameraMan ();

PS: lol Tween class is silly :3
lemayo lol :soups:

VirusClock

.

AstronautClock


F U Clock

Quote from: RomanClock;1778976[FLASH]http://files.myfrogbag.com/hp3miy/PanAndZoom.swf height=400 width=400[/FLASH]

Pan with mouse or arrow keys.
Zoom with buttons or mouse wheel.
Double click zoom in/out to point.

Source: http://files.myfrogbag.com/hp3miy/PanAndZoom.fla [CS3]

// change to desired values //
theScale = 100;//desired zoom level
curScale = theScale;//current zoom level
maxZoom = 500;//maximum zoom
minZoom = 100;//minumum zoom
multi = 2;//scroll speed multiplier
scaleRate = 10;//smoothness of zoom
camWidth = 400;//camera width
camHeight = 400;//camera height
atx = 200;//center of screen x position
aty = 200;//center of screen y position
panRate = 10;//smoothness of panning
panSpeed = 10;//used for arrow keys
doubleClick = 20;//how long to wait for double click
clickZoom = true;//double click zooms in/out if true, else just goes there
buttonZoom = .6;//zoom button, a percent multiplier
lowx = 0;//lower x bound
hix = 400;//higher x bound
lowy = 0;//lower y bound
hiy = 400;//higher y bound
/////////////

// no edit //
panning = false;//is panning mode
panx = atx;//panning position x
pany = aty;//panning position y
ancx = 0;//anchor position x
ancy = 0;//anchor position y
difx = 0;//difference from anchor x
dify = 0;//difference from anchor y
curx = 0;//store camera position x
cury = 0;//stort camera position y
dbl = 0;//time to detect double click
/////////////

//mouse methods//
var mousey:Object = new Object ();
mousey.onMouseWheel = function (dif, theEvent)
{
theScale += dif * multi * (theScale / 100);
};
mousey.onMouseDown = function (theEvent)
{
ancx = difx = _xmouse;
ancy = dify = _ymouse;
curx = atx;
cury = aty;
if (dbl < 1)
{
dbl = doubleClick;
panning = true;
}
else
{
theScale = (clickZoom && theScale < maxZoom * .9) ? maxZoom : minZoom;
panx = _xmouse;
pany = _ymouse;
}
};
mousey.onMouseUp = function (theEvent)
{
panning = false;
};
mousey.onMouseMove = function (theEvent)
{
if (panning)
{
difx = _xmouse;
dify = _ymouse;
curx = atx;
cury = aty;
}
};
Mouse.addListener (mousey);
//////////

//button methods//
box.zin.onRelease = function ()
{
theScale *= 1 / buttonZoom;
dbl = 0;
};
box.zout.onRelease = function ()
{
theScale *= buttonZoom;
dbl = 0;
};
//////////

setInterval(cameraMan,10);
function cameraMan ()
{
//calculate scale
theScale = Math.max (minZoom, Math.min (theScale, maxZoom));
curScale += (theScale - curScale) / scaleRate;
scl = curScale / 100;

if (panning)
{
//calcluate position for mouse panning
panx = curx + (ancx - difx);
pany = cury + (ancy - dify);
}
else
{
//arrow keys panning
panx += (Key.isDown (Key.RIGHT) - Key.isDown (Key.LEFT)) * panSpeed / scl;
pany += (Key.isDown (Key.DOWN) - Key.isDown (Key.UP)) * panSpeed / scl;
}
//smooth panning
atx += (panx - atx) / (panRate);
aty += (pany - aty) / (panRate);

//restrain bounds
panx = Math.max ((lowx + (camWidth / 2) / scl), Math.min ((hix - (camWidth / 2) / scl), panx));
pany = Math.max ((lowy + (camHeight / 2) / scl), Math.min ((hiy - (camHeight / 2) / scl), pany));
atx = Math.max ((lowx + (camWidth / 2) / scl), Math.min ((hix - (camWidth / 2) / scl), atx));
aty = Math.max ((lowy + (camHeight / 2) / scl), Math.min ((hiy - (camHeight / 2) / scl), aty));

//set this properties
_xscale = _yscale = curScale;
_x = -atx * scl + camWidth / 2;
_y = -aty * scl + camHeight / 2;

//set box properties
box._xscale = box._yscale = 10000 / curScale;
box._x = (-_x + camWidth / 2) / scl;
box._y = (-_y + camHeight / 2) / scl;

//other
dbl--;
}

cameraMan ();


PS: lol Tween class is silly :3

Man you are a bro! You want my job? I'm sure they'll pay you well. Seriously how can I get you back for this.

RomanClock

Quote from: F U Clock;1778991Man you are a bro! You want my job? I'm sure they'll pay you well. Seriously how can I get you back for this.

If you have an iPhone buy some stuff when TTT comes out :3
lemayo lol :soups:

F U Clock

Quote from: RomanClock;1778997If you have an iPhone buy some stuff when TTT comes out :3

I don't, but... I'll tell the guy I'm working with on this to.