キャンバスWidget

キャンバスWidgetは、図形を描画するためのWidgetです。
キャンバス内に描いた図形は、PostScript形式のファイルに出力できます。
描画できるアイテムは以下の通りです。

arc

弧を描きます。
-startで開始角を指定します。0は時計の3時の方向を示す。
-extentで時計と反対方向の終了角を指定します。
-styleで弧のスタイルをpiesliceは, chord, arcから選べます。

arc
pack [canvas .c -width 100 -height 100 -bg blue]
.c create arc 20 20 80 80 -start 0 -extent 270 -fill yellow
.c create arc 20 20 80 80 -start 270 -extent 90 -fill red

bitmap

ビットマップを描きます。
-bitmapでビットマップを指定します。

bitmap
pack [canvas .c -width 100 -height 100 -bg blue]
.c create bitmap 30 30 -bitmap questhead -foreground red
.c create bitmap 70 70 -bitmap hourglass -foreground yellow

image

イメージを描きます。
-imageでイメージを指定します。

image
image create photo foo -file tori.gif
pack [canvas .c -width 120 -height 100 -bg blue]
.c create image 5 50 -image foo -anchor w

line

線を描きます。
-arrowで矢印を描くことができます。

line
pack [canvas .c -width 100 -height 100 -bg blue]
.c create line 20 15 60 55 -fill red -arrow first
.c create line 50 15 90 55 -fill green -arrow last
.c create line 35 50 75 90 -fill yellow -arrow both

oval

円または楕円を描きます。

oval
pack [canvas .c -width 100 -height 100 -bg blue]
.c create oval 20 15 60 55 -fill red
.c create oval 50 15 90 55 -fill green
.c create oval 35 50 75 90 -fill yellow

polygon

多角形を描きます。
-smoothでスムージングを行うかどうか指定します。
-splinestepsでスムージングする度合を指定できます。

polygon
pack [canvas .c -width 100 -height 100 -bg pink]
.c create polygon 50 27 37 16 17 22 10 50 50 85 50 85 \
90 50 82 22 62 16 50 27 50 27 -smooth true -splinesteps 5 -fill red

rectangle

正方形または長方形を描きます。

rectangle
pack [canvas .c -width 100 -height 100 -bg blue]
.c create rectangle 20 20 80 80 -fill yellow
.c create rectangle 40 40 60 60 -fill red

text

テキストを描きます。
-textで文字列を指定します。

text
pack [canvas .c -width 100 -height 100 -bg blue]
.c create text 30 30 -text Hello -anchor w -fill white
.c create text 30 60 -text World! -anchor w -fill yellow

window

Widgetを描きます。
-windowでWidgetのパスを指定します。

widget
pack [canvas .c -width 100 -height 100 -bg blue]
button .c.b -text Push -width 5
entry .c.e -width 6
.c create window 20 30 -window .c.b -anchor w
.c create window 20 60 -window .c.e -anchor w

アイテム共通オプション

-dash pattern点線のパタンを指定します。
-activedash pattern点線のパタンを指定します。
-disableddash pattern点線のパタンを指定します。
-dashoffset offsetdashの開始オフセットを指定します。
-fill color図形の色を指定します。
-activefill color図形の色を指定します。
-disabledfill color図形の色を指定します。
-outline color輪郭線の色を指定します。
-activeoutline color輪郭線の色を指定します。
-disabledoutline color輪郭線の色を指定します。
-offset offsetstippleのオフセットを指定します。
-outlinestipple bitmapoutlineにビットマップを指定します。
-activeoutlinestipple bitmapoutlineにビットマップを指定します。
-disabledoutlinestipple bitmapoutlineにビットマップを指定します。
-stipple bitmapfillにビットマップを指定します。
-activestipple bitmapfillにビットマップを指定します。
-disabledstipple bitmapfillにビットマップを指定します。
-state state状態を指定します。(normal, disabled, hiddenのいずれか)
-tags tagListアイテムにタグ名を付けます。
-width outlineWidth輪郭線の太さを指定します。
-activewidth outlineWidth輪郭線の太さを指定します。
-disabledwidth outlineWidth輪郭線の太さを指定します。

タグ

キャンバスに置いたアイテムには順番に番号が付きますが、
タグ名を付けると、タグ名でアイテムを操作することができます。
1つのアイテムに複数のタグ名を付けることができます。
また、複数のアイテムに同じタグ名を付けることができます。

次の例は、キャンバスWidget内の図形にタグ名を付けて、
その図形の輪郭線の色を変更するバインドをしています。

pack [canvas .c -height 70 -width 70 -bg white]
.c create oval 10 10 40 40 -fill red -width 4 -tag foo
.c create oval 30 10 60 40 -fill green -width 4 -tag foo
.c create oval 20 30 50 60 -fill blue -width 4 -tag foo

.c bind foo <Enter> {
    %W itemconfigure foo -outline gold
}

.c bind foo <Leave> {
    %W itemconfigure foo -outline black
}

tags

タグ名を付けるときの注意として、キャンバスWidgetでは、
allという名前のタグ名はすべてのタグを指すために予約されているという点です。

PostScript

キャンバスは、PostScript形式のファイルに出力できます。
ただし、日本語テキストやwindowはPostScriptに変換できません。

.c postscript -file foo.ps