Discussion:
canvas: change the background of a text item
(too old to reply)
Ralf Fassel
2024-10-30 12:00:43 UTC
Permalink
Is there a way to change the background of a text item on a canvas?
I can change the color of the text via -fill, but I found no way to
change the background color of the text:

pack [canvas .c] -fill both -expand yes
# red text on yellow background
.c create text 100 100 -text "Example text" -font {Helvetica 20} -fill red

=> how to change the background of the text field to yellow?
i.e. the same as ".c configure -background yellow", but restricted
to the area of the text.

TNX
R'
Ralf Fassel
2024-10-30 13:42:02 UTC
Permalink
* Ralf Fassel <***@gmx.de>
| Is there a way to change the background of a text item on a canvas?
| I can change the color of the text via -fill, but I found no way to
| change the background color of the text:
| pack [canvas .c] -fill both -expand yes
| # red text on yellow background
| .c create text 100 100 -text "Example text" -font {Helvetica 20} -fill red
|
| => how to change the background of the text field to yellow?
| i.e. the same as ".c configure -background yellow", but restricted
| to the area of the text.

Ok, found a workaround on the WIKI:

https://wiki.tcl-lang.org/page/canvas+text

- create a rectangle with the bbox of the text and lower it beneath
the text.

Somehow the 'rectangle' item was not there when I scanned the canvas
manpage earlier :-/

R'
undroidwish
2024-10-30 19:29:38 UTC
Permalink
Post by Ralf Fassel
https://wiki.tcl-lang.org/page/canvas+text
...
thanks for digging, that's intriguing. The XLFD allowed in ancient times
for transformations, didn't know that. Not that it is relevant for now,
since we all render our texts in Tk using libxft. But until 2.3.5, that
library, too, had still problems scaling/rotating/translating
bitmap fonts (especially the color emojis).
Post by Ralf Fassel
- create a rectangle with the bbox of the text and lower it beneath
the text.
And yes, the challenge now is to make the rectangle (ahem, rotated, a
polygon) for a somewhat rotated text out of its bbox (if you'd like to
have a text background).

And please, enhance the wiki in this regard.

BR,
Christian
saito
2024-10-30 21:42:46 UTC
Permalink
Post by undroidwish
And yes, the challenge now is to make the rectangle (ahem, rotated, a
polygon) for a somewhat rotated text out of its bbox (if you'd like to
have a text background).
Not sure when it became available but you can rotate text on a canvas
using the -angle option. As for the background, the rectangle option
works. Since, in this case, you will have the coordinates for the
rectangle, you can translate the four coordinates to match the angle of
the text. Not sure at all whether bbox can help you with the rotated stuff.

Tkpath is worth checking out. As I recall, it could rotations before Tk.
Loading...