Hints

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 34.  The Canvas Widget


graphics/tip_icon.gif

Screen Coordinates vs. Canvas Coordinates

The canvasx and canvasy operations map from a screen coordinate to a canvas coordinate. If the scroll region is larger than the display area, then you need to use these operations to map from the X and Y in an event (i.e., %x and %y) and the canvas coordinates. The typical use is:

 set id [$c find closest [$c canvasx %x] [$c canvasy %y]] 

Large Coordinate Spaces

Coordinates for canvas items are stored internally as floating point numbers, so the values returned by the coords operation will be floating point numbers. If you have a very large canvas, you may need to adjust the precision with which you see coordinates by setting the tcl_precision variable. This is an issue if you query coordinates, perform a computation on them, and then update the coordinates. (Tcl 8.0 changed the default tcl_precision from 6 to 12.)

Scaling and Rotation

The scale operation scales the coordinates of one or more canvas items. It is not possible to scale the whole coordinate space. The main problem with this is that you can lose precision when scaling and unscaling objects because their internal coordinates are actually changed by the scale operation. For simple cases this is not a problem, but in extreme cases it can show up.

The canvas does not support rotation.

Resources

There is no resource database support built into the canvas and its items. You can, however, define resources and query them yourself. For example, you could define:

 *Canvas.foreground:   blue 

This would have no effect by default. However, your code could look for this resource with option get, and specify this color directly for the -fill attribute of your objects:

 set fg [option get $c foreground {}] $c create rect 0 0 10 10 -fill $fg 

The main reason to take this approach is to let your users customize the appearance of canvas objects without changing your code.

Objects with Many Points

The canvas implementation seems well optimized to handle lots of canvas objects. However, if an object like a line or a polygon has many points that define it, the implementation ends up scanning through these points linearly. This can adversely affect the time it takes to process mouse events in the area of the canvas containing such an item. Apparently any object in the vicinity of a mouse click is scanned to see if the mouse has hit it so that any bindings can be fired.

Selecting Canvas Items

Example 35-5 on page 512 implements cut and paste of canvas objects. The example exchanges the logical description of canvas objects with the selection mechanism.


       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net