Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Zooming/Scaling a Canvas - want to center the screen or autoscroll

Status
Not open for further replies.

xhonzi

Programmer
Jul 29, 2003
196
US
I have a canvas that is 1080 x 540. It has a 1080x540 .jpg as the back ground (it's an Earth map) and I'm placing icons on it based on Lat/Long coords.

I use scale(0,0,2,2) to zoom in on the icons, and that works. I use zoom on the jpg to keep the map in scale with the icons. That all works great.

So here's my issue- When you zoom in, the origin is 0,0, so the upper left corner stays the upper left corner and everything is pushed off the viewable area of the canvas down and to the right. What I would like to do is zoom in on the center of the viewable area, and push off the visible area equally up/down and left/right. If I have to fake it by zooming about the upper left corner, and then auto scrolling the scroll bars the appropriate number of pixels, I'm pretty cool with that.

How do I do that?

I'm sure it's easy, but I can't seem to get it to work.

Thanks,
 
Yes, Perl. With basic TK. I would post the code, but it's on an unconnected system.
 
Okay, I printed the important parts and will retype them here. Obvious typos are obvious typos since I'm reading from a print out.

The creation of the canvas and scroll/zoom bars:

Code:
$mapScaleW=$cvtl->Scale(-from=>1,-to=>3,-width=>10,-variable=\$mapScale,-command=>\&scaleMap,-length=>500)->pack;
$cvScrolled=$cvtl->Scrolled("Canvas",-scrollbars=>'se',-height=>540,-width=>1079)->pack;
$cvScrolled->Subwidget("xscrollbar")->configure(-width=>'10');
$cvScrolled->Subwidget("yscrollbar")->configure(-width=>'10');
$cv=$cvScrolled->Subwidget("canvas");
$map_pic=$mw->Photo(-file=>'map_med.jpg');
$map=$cv->createImage(540,270, -image=>$map_pic);

And the scaling/zooming routine:
Code:
sub scaleMap
{
  my $zoomed=$mw->Photo;
  $zoomed->blank;

  if ($lastZoom < $mapScale) #Zooming in
  {
    $cv->scale("all", 0, 0, 2, 2); #Scale the map text by a factor of 2, using 0,0 as origin
  }
  elsif ($lastZoom > $mapScale) #Zooming out
  {
    $cv->scale("all", 0, 0, .5, .5); #Scale the map text by .5 (divide by 2)
  }
  $zoomed->copy($map_pic) if $mapScale eq 1;
  $zoomed->copy($map_pic, -zoom=>2) if $mapScale eq 2;
  $zoomed->copy($map_pic, -zoom=>4) if $mapScale eq 3;
  $cv->itemconfigure($map, image=>$zoomed);
  $cv->configure(-scrollregion=>[$cv->bbox("all")]);
  $lastZoom=$mapScale;
}

I used to scale the the map items around 540 and 270, which would nicely zoom them by the center of the map. Of course, it did odd stuff with trying to keep my map and text items in the same relative relationship... so I went back to using 0,0 as origin. Like I said in the OP, this zooms in keeping the upper left corner in the upper left corner... What I would like, is for the zooming to occur about the center of the viewable area. Probably easiest by manipulating the scrollbars after each zoom, but I can't find how to do that either.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top