aniwave.tcl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # aniwave.tcl --
  2. #
  3. # This demonstration script illustrates how to adjust canvas item
  4. # coordinates in a way that does something fairly similar to waveform
  5. # display.
  6. if {![info exists widgetDemo]} {
  7. error "This script should be run from the \"widget\" demo."
  8. }
  9. package require Tk
  10. set w .aniwave
  11. catch {destroy $w}
  12. toplevel $w
  13. wm title $w "Animated Wave Demonstration"
  14. wm iconname $w "aniwave"
  15. positionWindow $w
  16. label $w.msg -font $font -wraplength 4i -justify left -text "This demonstration contains a canvas widget with a line item inside it. The animation routines work by adjusting the coordinates list of the line; a trace on a variable is used so updates to the variable result in a change of position of the line."
  17. pack $w.msg -side top
  18. ## See Code / Dismiss buttons
  19. set btns [addSeeDismiss $w.buttons $w]
  20. pack $btns -side bottom -fill x
  21. # Create a canvas large enough to hold the wave. In fact, the wave
  22. # sticks off both sides of the canvas to prevent visual glitches.
  23. pack [canvas $w.c -width 300 -height 200 -background black] -padx 10 -pady 10 -expand yes
  24. # Ensure that this this is an array
  25. array set animationCallbacks {}
  26. # Creates a coordinates list of a wave. This code does a very sketchy
  27. # job and relies on Tk's line smoothing to make things look better.
  28. set waveCoords {}
  29. for {set x -10} {$x<=300} {incr x 5} {
  30. lappend waveCoords $x 100
  31. }
  32. lappend waveCoords $x 0 [incr x 5] 200
  33. # Create a smoothed line and arrange for its coordinates to be the
  34. # contents of the variable waveCoords.
  35. $w.c create line $waveCoords -tags wave -width 1 -fill green -smooth 1
  36. proc waveCoordsTracer {w args} {
  37. global waveCoords
  38. # Actual visual update will wait until we have finished
  39. # processing; Tk does that for us automatically.
  40. $w.c coords wave $waveCoords
  41. }
  42. trace add variable waveCoords write [list waveCoordsTracer $w]
  43. # Basic motion handler. Given what direction the wave is travelling
  44. # in, it advances the y coordinates in the coordinate-list one step in
  45. # that direction.
  46. proc basicMotion {} {
  47. global waveCoords direction
  48. set oc $waveCoords
  49. for {set i 1} {$i<[llength $oc]} {incr i 2} {
  50. if {$direction eq "left"} {
  51. lset waveCoords $i [lindex $oc \
  52. [expr {$i+2>[llength $oc] ? 1 : $i+2}]]
  53. } else {
  54. lset waveCoords $i \
  55. [lindex $oc [expr {$i-2<0 ? "end" : $i-2}]]
  56. }
  57. }
  58. }
  59. # Oscillation handler. This detects whether to reverse the direction
  60. # of the wave by checking to see if the peak of the wave has moved off
  61. # the screen (whose size we know already.)
  62. proc reverser {} {
  63. global waveCoords direction
  64. if {[lindex $waveCoords 1] < 10} {
  65. set direction "right"
  66. } elseif {[lindex $waveCoords end] < 10} {
  67. set direction "left"
  68. }
  69. }
  70. # Main animation "loop". This calls the two procedures that handle the
  71. # movement repeatedly by scheduling asynchronous calls back to itself
  72. # using the [after] command. This procedure is the fundamental basis
  73. # for all animated effect handling in Tk.
  74. proc move {} {
  75. basicMotion
  76. reverser
  77. # Theoretically 100 frames-per-second (==10ms between frames)
  78. global animationCallbacks
  79. set animationCallbacks(simpleWave) [after 10 move]
  80. }
  81. # Initialise our remaining animation variables
  82. set direction "left"
  83. set animateAfterCallback {}
  84. # Arrange for the animation loop to stop when the canvas is deleted
  85. bind $w.c <Destroy> {
  86. after cancel $animationCallbacks(simpleWave)
  87. unset animationCallbacks(simpleWave)
  88. }
  89. # Start the animation processing
  90. move