#!/proj/tcl/install/5.x-sparc/bin//wish4.2b1 canvas .c pack .c # we need to do this so that the canvas is mapped on the display, # and we can get an accurate size for it, so that later on, when # we calculate the origin we get the right numbers. update idletasks set p(delay) 150 ;# The delay for repeating set p(xorigin) [expr [winfo width .c]/2] ;# The center of the canvas is the 'origin' set p(yorigin) [expr [winfo height .c]/2] set p(scale) 5 ;# We want to scale the difference a bit set p(afterid) {} ;# The ID of the next after event. set p(lmx) 0 ;# last reported mouse x set p(lmy) 0 ;# last reported mouse y set p(id) [.c create oval 100 100 150 150] # Given an x and a y, this will calculate the # offset from the origin, and divide that by the # scale, and then move the item that much each tick. # finally, it reschedules itself for the next tick. proc moveitem {} { global p set x $p(lmx) set y $p(lmy) set movex [expr ($x-$p(xorigin))/$p(scale)] set movey [expr ($y-$p(yorigin))/$p(scale)] .c move $p(id) $movex $movey set p(afterid) [after $p(delay) moveitem] } # When we get the button down, record this as our # latest mouse position, and start moving. bind .c <1> { set p(lmx) %x set p(lmy) %y moveitem } # As the mouse position changes, record it so we # can figure out what to do in the moveitem procedure. bind .c { set p(lmx) %x set p(lmy) %y } # When we get the button up, stop. bind .c { after cancel $p(afterid) }