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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving a canvas

Status
Not open for further replies.

sartor

Programmer
Oct 6, 2005
3
BR
Hi

I'm trying to make an application but I'm having a small problem when I try to move a canvas inside a frame. The problem is that it shakes while I'm moving it and it moves slower than the mouse cursor.

If you can tell me wether my approach isn't right and point me on the right direction, or to say what I'm missing to do it right I would really thank you

The code is below

frame .mainFrame \
-width 400 \
-height 400 \
-bg black

canvas .mainFrame.canvas \
-yscrollcommand ".vertical set" \
-bg white \
-width 400 -height 400 \
-yscrollincrement 400 \
-insertwidth 1

grid .mainFrame.canvas \
-column 0 \
-row 0

scrollbar .vertical \
-orient vertical \
-command scrollCommand

grid .vertical \
-sticky news \
-row 0 -column 1

grid .mainFrame -column 0 -row 0

catch { tk_getOpenFile -initialdir ../imagens -multiple 1 } files

if { [ string compare $files "" ] != 0 } {
set cont 0
set blankSpace 0
foreach photoImage $files {

image create photo photo$cont -file $photoImage

.mainFrame.canvas create image 200 [expr 200 + $blankSpace] -image photo$cont
set blankSpace [expr $blankSpace + 400]
incr cont
}
}

.mainFrame.canvas configure -scrollregion "0 0 400 [expr [expr [llength [image names]] - 3] * 400]"

proc scrollCommand { args } {
eval ".mainFrame.canvas yview $args"
}

bind . <Button-5> {scrollCommand scroll 1 units}
bind . <Button-4> {scrollCommand scroll -1 units}
bind .mainFrame.canvas <B1-Motion> {drag %x %y}
bind .mainFrame.canvas <ButtonPress-1> {pushed %x %y}
bind .mainFrame.canvas <ButtonRelease-1> {released %x %y}
bind . <Key-c> {center}

proc center { } {
place .mainFrame.canvas -x 0 -y 0
}

set coordinates(firstX) 0
set coordinates(firstY) 0

proc pushed { x y } {
global coordinates

set coordinates(pushedX) $x
set coordinates(pushedY) $y
}

proc drag { x y } {
global coordinates

set coordinates(xDeslocation) [expr $x - $coordinates(pushedX)]
set coordinates(yDeslocation) [expr $y - $coordinates(pushedY)]

place .mainFrame.canvas \
-x [expr $coordinates(firstX) + $coordinates(xDeslocation)] \
-y [expr $coordinates(firstY) + $coordinates(yDeslocation)]
}

proc released { x y } {
global coordinates
set coordinates(firstX) [expr $coordinates(firstX) + $coordinates(xDeslocation)]
set coordinates(firstY) [expr $coordinates(firstY) + $coordinates(yDeslocation)]
}
 
Objects with many points

The canvas implementation seems well optimized to handle lots and lots of canvas objects. However, if an object like a line or a polygon has very many points that define it, the implementation ends up scanning through these points linearly. This can adversely affect the time it takes to process mouse events in the area of the canvas containing such an item. Apparently any object in the vicinity of a mouse click is scanned to see if the mouse has hit it so that any bindings can be fired.

Coordinates for canvas items are stored internally as floating point numbers,so the values returned by the coords operation will be floating point numbers.If you have a very large canvas, you may need to adjust the precision with which you see coordinates by setting the tcl_precision variable. This is an issue if you query coordinates, perform a computation on them, and then update the coordinates.

try to create canvas image in the frame:
image create bitmap hourglass \
-file hourglass.bitmap -maskfile hourglass.mask \
-background white -foreground blue]
$frame create image $x 10 -image hourglass2 -anchor nw

Attributes for image canvas items:
-anchor position c n ne e se s sw w nw
-image name
-tags tagList

You can also use tags to move all canvas elements together.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top