Version 1 of Shaded text on canvas

Updated 2005-11-15 12:14:55

Richard Suchenwirth 2005-11-15 - Here's a simple effect to make canvas text items look nicer: the text is drawn twice, slightly shifted by a pixel in x and y direction. The font used should best be not the regular small one.

http://mini.net/files/shadetext.jpg

 package require Tk

 proc shadedtext {w x y fg bg args} {
   eval [list $w create text $x $y -fill $bg] $args
   eval [list $w create text [incr x -1] [incr y -1] -fill $fg] $args
 }

## Demo:

 pack [canvas .c]
 shadedtext .c 10 30 yellow black -text hello -font {Times 36 bold} -anchor w
 shadedtext .c 10 80 orange red -text world -font {Times 36 bold} -anchor w

UK I found a shift of two pixels nicer looking.

And you can have it even more fancy with a third level colored in the canvas bg-color layered inbetween:

 proc shadedtext3 {w x y fg bg args} {

   set cbg [ $w cget -bg ]

   eval [list $w create text $x $y -fill $bg] $args
   eval [list $w create text [incr x -2] [incr y -2] -fill $cbg] $args
   eval [list $w create text [incr x -1] [incr y -1] -fill  $fg] $args
 }

Arts and crafts of Tcl-Tk programming