9.19. Scaling the Canvas

When you put a large number of items on the canvas, it's sometimes hard to see them all without scrolling all over the place. It's possible to scale the canvas, for example, so it will shrink everything in half or explode it to twice the original size. The usage for scale is as follows:

$canvas->scale(tag/id, xorigin, yorigin, xscale, yscale);

The scaling is centered around the xorigin and yorigin. I suggest using the real origin (0, 0) unless you can come up with a good reason not to. Both xscale and yscale are the scaling factors used on each coordinate in each item. Here are some examples:

$canvas->scale("all", 0, 0, 1, 1);   # no change!
$canvas->scale("all", 0, 0, .5, .5); # make all 1/2 size
$canvas->scale("all", 0, 0, 2, 2);   # double everything
$canvas->scale("all", 0, 0, 3, 3);   # triple everything!

It's a great idea to add a Zoom In and Zoom Out button that takes care of the scaling for you. Keep track of the scaling factor in a variable ($scale, for instance); set it to 1 to start with. Multiply it by .5 to zoom out and by 2 to zoom in. The last thing you'll need to do is make sure that, if you insert any new items into the canvas, you multiply those coordinates by the scale factor as well (otherwise they will look either too large or too small compared to the rest of the canvas items).

Get Learning Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.