Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Canvas coordinate system 1

Status
Not open for further replies.

tcltkmail

Programmer
Feb 14, 2002
3
DE
hi,
I want to draw a black dot on the current cursor position when I click a point on a canvas widget, but the drawing position is not always correct. It seems that the drawing black dot coordinates are propertional to the current cursor coordinates. What's problem? What is the relation between the current cursor postion and the real drawing position?

Thank you very much
Joye
 
It sounds as though you're running into a common canvas programming situation. A canvas widget has a coordinate system for its virtual drawing area. (By the way, coordinates can be floating-point numbers, and can be expressed in any form of screen distance. So, you can even use coordinates like "0.5i".) {0 0} is the upper-lefthand corner of the virtual drawing area (although you can also use negative coordinates).

At first, these coordinates correspond to the actual onscreen coordinates. But if you scroll your canvas around, then the coordinate of the upper-lefthand corner of the displayed area of the canvas isn't {0 0} any more. However, when an event is received by the canvas widget, the x and y coordinates are reported relative to the upper-lefthand corner of the visible widget.

Therefore, we need to translate screen coordinates (received by events) into canvas coordinates (used for canvas operations). Fortunately, because this is such a common situation, the canvas widget includes canvasx and canvasy operations for making this translation. Thus, all of your canvas bindings that receive screen coordinates should immediately translate them using these operations. For example, from my sample rectangle drawing program:

[tt]bind .pic <ButtonPress-1> {rect_create_start %W %x %y}

# ...

proc rect_create_start {canv x y} {
global rect
set x [ignore][$canv canvasx $x][/ignore]
set y [ignore][$canv canvasy $y][/ignore]
set rect(x0) $x
set rect(y0) $y
set rect(id) [ignore][$canv create rect $x $y $x $y][/ignore]
}[/tt] - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top