Results 1 to 6 of 6

Thread: zooming to the mouse

  1. #1

    zooming to the mouse

    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:

    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

  2. #2

    Re: zooming to the mouse

    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.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  3. #3

    Re: zooming to the mouse

    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.

  4. #4

    Re: zooming to the mouse

    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.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  5. #5

    Re: zooming to the mouse

    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:
    Code:
      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:
    Code:
      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.

  6. #6

    Re: zooming to the mouse

    I'm glad you figured it out...
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •