O'Reilly logo

Mastering Perl/Tk by Nancy Walsh, Stephen Lidie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Moving Items Around

Once an item has been created on the Canvas, use move or coords to move it around. The move method takes a tag or ID to indicate which items to move and the amounts to add to the x and y coordinates:

$canvas->move(tag/id, xdistance, ydistance);

For instance, the following code moves items with the "blue" tag 100 pixels in the x direction and 100 pixels in the y direction:

$canvas->move("blue", 100, 100);

To move an item in the negative direction, simply specify a negative value for the xdistance and/or ydistance. The other method, coords, allows you to specify a new x and y location for the first item found that is identified by the tag or ID:

$canvas->coords(tag/id, newx, newy);

If the item requires more than one set of (x, y) coordinates, you simply continue to specify them:

$canvas->coords(tag/id, newx1, newy1, newx2, newy2...);

To find where an item currently is in the Canvas, use coords without specifying the x or y coordinates:

@coords_list = $canvas->coords(tag/id);

Remember, the coords method only applies to the first item it finds that matches the given tag or ID.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required