ZHUOWARE BACKYARD - NOTE INDEX|ZHUOWARE BACKYARD TOP|ZHUOWARE表ページ

Tk canvas item --- outline issue

Feb. 22, 2009 by Zhuo

概要 // Abstract

次の素朴な疑問を解決した.//I gave answers to the following simple questions: //

Sample Code

# tkCanvasItemOutline.tcl
console show
canvas .c -width 480 -height 480 -bg black
pack .c 

bind . q {destroy .}
bind .c  {showDot .c %x %y}
bind .c  {moveDot .c %x %y}
bind .c  {hideDot .c}

set ::prevcx ""
set ::prevcy ""

proc showDot {c x y} {
    set cx [$c canvasx $x]
    set cy [$c canvasy $y]
    $c create line $cx [expr $cy-5] $cx [expr $cy+6] -fill green -width 1 -tags cur -state disabled
    $c create line [expr $cx-5] $cy [expr $cx+6] $cy -fill green -width 1 -tags cur -state disabled
    set ::prevcx $cx
    set ::prevcy $cy
}

proc moveDot {c x y} {
    set cx [$c canvasx $x]
    set cy [$c canvasy $y]
    set dx [expr $cx - $::prevcx]
    set dy [expr $cy - $::prevcy]
    $c move cur $dx $dy
    set ::prevcx $cx
    set ::prevcy $cy
}

proc hideDot {c} {
    $c delete cur
}



#canvas item test:
for { set wi 0 } { $wi < 10 } {incr wi} {
    set testitem [.c  create rectangle [expr 120+$wi*20] 100 [expr 139+$wi*20] 110 -width $wi -outline red -fill blue]
    .c bind $testitem  {putCanvXY .c %x %y enter}
    .c bind $testitem  {putCanvXY .c %x %y motion}
    .c bind $testitem  {putCanvXY .c %x %y leave}
    puts "horiz: wi=$wi: coords: [.c coords $testitem]; bbox: [.c bbox $testitem]"
}

for { set wi 0 } { $wi < 10 } {incr wi} {
    set testitem [.c  create rectangle 70 [expr 150+$wi*20] 80 [expr 169+$wi*20] -width $wi -outline red -fill blue]
    .c bind $testitem  {putCanvXY .c %x %y enter}
    .c bind $testitem  {putCanvXY .c %x %y motion}
    .c bind $testitem  {putCanvXY .c %x %y leave}
    puts "vert:  wi=$wi: coords: [.c coords $testitem]; bbox: [.c bbox $testitem]"
}


proc putCanvXY {c x y str} {
    puts "cx: [$c canvasx $x] cy: [$c canvasy $y] ($str)"
}

Experiment

Result1: screen shot

Result2: width option, coords/bbox, cx for Enter

For X coordinates: Actually, pixel cx = 70 thru 80 are painted blue for width=0.
width optiond1d2coords
x0 y0 x1 y1
bbox
x0 y0 x1 y1
actual outline coveragecx for Enter event
00070 150 80 16969 149 81 170(no outline. body:70-80)69-81
11170 170 80 18969 169 81 19070-70, 81-8169-81
21170 190 80 20969 189 81 21069-70, 80-81 68-82
32370 210 80 22968 208 82 23169-71, 80-8268-82
42370 230 80 24968 228 82 25168-71, 79-8267-83
53570 250 80 26967 247 83 27268-72, 79-8367-83
63570 270 80 28967 267 83 29267-72, 78-8366-84
74770 290 80 30966 286 84 31367-73, 78-8466-84
84770 310 80 32966 306 84 33366-73, 77-8465-85
95970 330 80 34965 325 85 35466-74, 77-8565-85