Subject: Re: Partial display of canvas widget - DN [1]
"D. Richard Hipp" <drh@acm.org> - 18 May 1999 - comp.lang.tcl
David Peebles wrote:
>
> I want to use a canvas widget to display a set of canvas objects (lines,
> bitmaps etc.). However, I require the main part of the canvas to be blank to
> the user, with only a specific rectangular portion of the canvas visible at
> any one time. The location of this rectangular portion is to be determined
> by the user who can change its position by moving the mouse. Only the canvas
> objects under this rectangular portion are to be seen by the user at any one
> time but these must change as the rectangle is moved by the user so that the
> complete set of canvas objects can be seen if the rectangle is moved about
> the canvas. I hope this is clear. To illustrate, A (below) is the canvas
> widget with the canvas objects and B is a portion of the canvas visible to
> the user within a movable rectangle.
>
> A__________ B__________
> | : | | |
> | : <> | | |
> | : | | ___ |
> | |__ <> | | | <>| |
> | | ## | | |__#| |
> |__________| |__________|
>
Sample code is follows:
# The outer frame. (Make it blue. The clipping window will appear
# light gray.)
#
frame .f -width 600 -height 400 -bg skyblue
pack .f
# The clipping window.
#
frame .f.f -width 100 -height 100
# The canvas.
canvas .f.f.c -width 600 -height 400 -bd 0 -highlightthickness 0
# Put something in the canvas so we can see it.
#
for {set y 0} {$y<400} {incr y 25} {
set x [expr int($y*550.0/350.0)]
.f.f.c create text $x $y -text {Hello!} -anchor nw
.f.f.c create text [expr 550-$x] $y -text {Hi There!} -anchor nw
}
# Move the clipping window to $x,$y
#
proc moveit {x y} {
if {$x<0} {set x 0}
if {$x>500} {set x 500}
if {$y<0} {set y 0}
if {$y>300} {set y 300}
place .f.f -x $x -y $y
place .f.f.c -x -$x -y -$y
global clipX clipY
set clipX $x
set clipY $y
}
moveit 0 0
# Bindings to drag the clipping window using button 1.
#
bind .f.f.c <ButtonPress-1> {
set offsetX [expr %x-$clipX]
set offsetY [expr %y-$clipY]
}
bind .f.f.c <B1-Motion> {
moveit [expr %x-$offsetX] [expr %y-$offsetY]
}
--
D. Richard Hipp -- drh@acm.org -- http://www.hwaci.com/drh/
Last modified
1999-09-27
1999-09-27
(195.108.246.50)
Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated
