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.

Get Mastering 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.