PDA

View Full Version : zooming to the mouse



FreeMind
04-04-2009, 10:27 AM
So. Ive been trying to achieve this for two days, but i just can't get it right.
Ok, so i have a script right here that draws a triangle:

Code:


TheImage.canvas.MoveTo(round((100*zoom-moved.x)), round((100*zoom-moved.y)));
TheImage.canvas.LineTo(round((200*zoom-moved.x)), round((200*zoom-moved.y)));
TheImage.canvas.LineTo(round(100*zoom-moved.x)), round((200*zoom-moved.y)));
TheImage.canvas.LineTo(round((100*zoom-moved.x)), round((100*zoom-moved.y)));


Zoom is the distance. When i roll my mouse wheel, it gets multyplied by 1.1 or 0.9 to increase or decrease the size.

moved.x and moved.y is the distance the view is shifted. Like, if i take a hand tool, i can shift the view.

Now, how should i do the math so the view could zoom to the mouse instead of the 0 0 position? Embarrassed

chronozphere
04-04-2009, 12:35 PM
You could do something like this (not tested though):

Compute the vector between your mouse and the center of the view, like this:

Vec.X := Center.X - Mouse.X;
Vec.Y := Center.Y - Mouse.Y;

And now you have to move along that vector when you zoom in or out. For example, you can do this when you zoom in:

Moved.X := Moved.X + Vec.X * 0.1;
Moved.Y := Moved.Y + Vec.Y * 0.1;

And when you zoom out:

Moved.X := Moved.X - Vec.X * 0.1;
Moved.Y := Moved.Y - Vec.Y * 0.1;

I don't know if it works. Just try it. :)



Ok, so i have a script right here that draws a triangle


FYI, Pascal is code, not script (no pun ofcourse ;) )

Hope this helps.

FreeMind
04-04-2009, 03:01 PM
not working.

As far as i understand, in this case the distance it moves is allways the same if i keep my mouse steady, as i understand, the more i'm zoomed in, the more it has to move.

chronozphere
04-04-2009, 05:41 PM
The more you are zoomed in the higher "Zoom" is. You said:



the more i'm zoomed in, the more it has to move.


So you could try this:

Moved.X := Moved.X - Vec.X * Zoom;
Moved.Y := Moved.Y - Vec.Y * Zoom;

The ammount of movement now depends on how much you zoomed.

It would be good if you wrote short comprehensible sentences instead of this:



As far as i understand, in this case the distance it moves is allways the same if i keep my mouse steady, as i understand, the more i'm zoomed in, the more it has to move.

I had to read it 4 times to figure out what you ment. ???

FreeMind
04-04-2009, 07:36 PM
I figured it out. Actually your first code was close to what should have been done, though i only notice that after i achieved it.

mousewheel up:


if zoom<6 then begin
zoom:=zoom*1.1;
moved.x:=round(moved.x+(moved.x+pt.x)*0.1) ;
moved.y:=round(moved.y+(moved.y+pt.y)*0.1);
end;

mousewheel down:


if zoom>0.02 then begin
zoom:=zoom*10/11;
moved.x:=round(moved.x+(moved.x+pt.x)*(10/11-1)) ;
moved.y:=round(moved.y+(moved.y+pt.y)*(10/11-1));
end;

Thanks a lot for your time! Appreciate. ;)

edit: pt.x and pt.y is the mouse position.

chronozphere
04-04-2009, 11:20 PM
I'm glad you figured it out... :)