in reply to Animation Display appling Delay and Canvas Object
If memory serves, once you pass control off to Tk's MainLoop() call, I don't think the sleep() call will make much difference. Instead, you may have to use the after() or repeat() methods documented in Tk::after. In such a case, I believe you would define a callback function that you would pass to the after() or repeat() calls, along the lines of:
#!/usr/bin/perl use strict; use Tk; my $top = MainWindow->new(); my $canvas = $top->Canvas( width => 300, height => 245 )->pack(); my $origin_x = 110; my $origin_y = 70; my $PI = 3.141592635; my $circle_radius = 5; my $path_radius = 0; my $angle = 0; $canvas->repeat( 1000, sub { &animate( $origin_x, $origin_y, $PI, \$canvas, \$circle_radius, \$path_radius, \$angle ); } ); # Could also be done as follows, although there are # differences in the two methods # $canvas->repeat( # 1000, # [ \&animate, $origin_x, $origin_y, # $PI, \$canvas, \$circle_radius, # \$path_radius, \$angle # ] # ); MainLoop(); sub animate { my ( $origin_x, $origin_y, $PI, $canvas, $circle_radius, $path_radius, $angle ) = @_; $$angle += 10; $$circle_radius += 3; $$path_radius += 7; my $path_x = $origin_x + $$path_radius * cos( $$angle * $PI / 90 ); my $path_y = $origin_y - $$path_radius * sin( $$angle * $PI / 90 ); $$canvas->create( 'oval', $path_x - $circle_radius, $path_y - $circle_radius, $path_x + $circle_radius, $path_y + $circle_radius, -fill => 'yellow' ); $$canvas->create( 'line', $origin_x, $origin_y, $path_x, $path_y, -fill => 'slategray' ); if ( $$angle > 180 ) { $angle = 0; $$circle_radius = 5; $$path_radius = 0; } }
This seems to do most of what you were planning (with the exception that it loops rather than continuing outward, although that can be changed by commenting out my resetting of the circle and path radii).
Hope that helps....
Update 18-May-2006: Changed call method to use anonymous subroutine rather than flat list, because there is the potiential for an apparent difference in the variables' contents between the two cases (in the case of the flat list, the variables have the values they contained at the time the constructor was called; in the case of the anonymous sub, the values they contained when the anonymous sub is actually invoked).
Update 18-May-2006: Added reference for reason for change in previous update.
Reference: Using advanced widgets in Perl/Tk by Philipp Janert, Ph.D.
|
|---|