Version 3 of Canvas slowdowns

Updated 2007-02-27 13:14:49

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
    }

  }





  ::largec::init

  ::largec::about_6000_onscreen

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

Category GUI Category Performance