Canvas Structured Graphics

JBR - 2010-11-20

Here is a small namespace of commands to draw classical 2d structured graphics on the Tk canvas. Its currently about 200 lines of code so I placed it here [L1 ].

2d transforms are handled with this code: https://wiki.tcl-lang.org/1234 .

I need a better name its just called "canvas::" for now.

  • canvas::init $c - prepare the Tk canvas for canvas:: commands. Scales the coordinates so that 0,0 is at the center of the display and Y is up.

Drawing commands:

  • rect $c $x $y $width $height - draw a rectangle
  • oval $c $x $y $width $height - draw a canvas oval (circle)
  • poly $c $x $y $points - draw a polyline
  • txti $c $x $y $text - draw a text string
  • ngon $c $x $y $width $height -sides $sides - draw a polygon of $sides sides
  • csys $c $x $y tag - create a container to hold other graphical items.

Each of the above graphic display items have their own coordinate system and may contain other items that will be positioned relative to them. In addition the "csys" command creates a coordinate system that can be used as a container for other graphical items and csys coordinate systems but contains no drawn item of its own. CSys coordinate system tags are in same namespace as canvas item tags and as such should not be integers. The "txti" item draws a canvas text item but is named so that it can be imported without conflicting with the Tk text widget command.

Drawing command take these options in addition to the options allowed by the canvas create item commands.

  • -rot $rotation
  • -scale $scale - set the items scale, may be a single real or a list of two reals ($xscale $yscale).
  • -in $TagOrId - place this item in the coordinate system of the indicated item.
  • -rmode no|yes|parent|total - control the rotation mode of text items.
  • -sides $sides - control the number of sides for the non command.

Move commands:

  • canvas::amov $c $TagOrId $x $y $rot - move the item to the absolute position $x, $y in its coordinate system. Optionally adjust its rotation to $r. If any of the position parameters are specified as "-" the current position is used.
  • canvas::rmov $c $TagOrId $dx $dy $drot - change the item's position of rotation by $dx, $dy and optionally $drot.

Other:

  • draw $c $tagOrId - redraw canvas after moving items. When called with no $id it redraws the entire canvas. Only items which have changed are redrawn.
  • erase $c $TagOrId - remove an item
  • size $c $xsize $ysize - get/set the size of an item
  • coords $c $points - get/set the canvas "coords" value. The coordinates are transformed by the current graphics context before being applied to the canvas item.

There are two methods for creating a hierarchy of graphics object coordinate systems. Items may be explicitly placed in the system of other objects with the "-in" option, or a code block can be included as the final parameter to an item command, any items created in that block are placed in the parent system by default:

 set box [canvas::rect $c 0 0 10 10]
 canvas::rect 10 10 10 10 -in $box

Or:

 canvas::rect $c 0 0 10 10 {
    canvas::rect 10 10 10 10
 }

Related

See Drawing diagrams as well.