Canvas slowdowns

Ro, 2007 Feb

The canvas is slow when many items are on-screen vs. when there are many items on the canvas (but not on-screen).

If you have 15k items on the canvas but only 100 onscreen and you move about using panning, it'll be responsive.

But if you have 5k items on the canvas and all of them are onscreen and you move about using panning, it'll be closer to unresponsive.

  # generate random integer number in the range [min,max]
  proc RandomInteger4 {min max} {
    return [expr {int(rand()*($max-$min+1)+$min)}]

  namespace eval ::largec:: {}

  proc ::largec::init {} {

    variable c

    set t .largec
    toplevel $t -bg red
    set c [canvas $t.c -bg black]
    pack $c -fill both -expand 1

    # panning
    bind $c <ButtonPress-1> {%W scan mark   %x %y}
    bind $c <B1-Motion>     {%W scan dragto %x %y 1}


  proc ::largec::about_6000_onscreen {} {

    variable c

    set line_counts {}

    for {set i 0} {$i < 100} {incr i} {
      lappend line_counts [RandomInteger4 40 80]

    set size 4

    set count 0
    foreach el $line_counts {

      for {set i 0} {$i < $el} {incr i} {

        set x1 [expr $size * $i]
        set y1 [expr $count * $size]
        set x2 [expr $x1 + $size]
        set y2 [expr $y1 + $size]

        $c create rect $x1 $y1 $x2 $y2 -fill yellow   ;# -outline yellow


      incr count




  puts "number of canvas elements: [llength [.largec.c find withtag all]]"

PWQ 28 Feb 07, Ro, What machine are you doing the test on. On my 600Mhz P4 running Linux, I do not have any lag or slow down dragging 5000 items on the canvas. With 30,000 I can see a slight but acceptable lag. In my tests I made the window bigger so all the elements are on the screen. Note my X11 is accellerated NVidia driver.

Ro March 2nd 2007 I find this highly unlikely. Firstly, is there even a 600Mhz P4? Lets say its a P3. If so, I find it extremely unlikely. I think you're probably dragging around the box, and not doing the panning. The panning is done by single clicking in the canvas and then dragging around. Resize the canvas to contain all elements first. This has been tested on WinXP SP2 on a P4 2.4Ghz, on a Sempron 3000+, and on a AMD64 3500+ with Tcl/Tk 8.3.2, 8.3.5, and 8.5a4.

lv What is panning?

AM Panning is the process whereby the whole picture is shifted - you get a different viewing rectangle. With zooming you enlarge the drawn objects, with panning you move them around. One way to do it (from the user's point of view) is to keep the mouse button down and move it around. The "map" underneath then shifts with the mouse pointer.

Screenshots Section

Canvas slowdowns screen.png

gold added pix