JavaScript EditorFree JavaScript Editor     Perl Manuals 

Main Page

17.9. Tk::Animation

Recall our circle bitmap from Figure 17-5 and how, with the addition of an inverted mask, we made the circle transparent. Using a timer event, we can make the bitmap cycle between solid and transparent so it flashes like a pulsar.

$mw->repeat(100 => sub {
    $b->configure(-maskfile => $count++ % 2 ? undef : 'circle.msk');

Some call this animation, crude as it may be.

Perl/Tk supplies an Animation widget in the standard distribution, designed to render a series of Photos, like frames in a movie film. It's also smart enough to handle GIF89 images that contain multiple frames per image. Here's a program that accepts a list of image filenames from the command line and animates them.

my $animate;
if (@ARGV) {
    $animate = $mw->Animation;
    foreach (@ARGV) {
        $animate->add_frame($mw->Photo(-file => $_));
} else {
    my $gif89 = Tk->findINC('anim.gif');
    $animate = $mw->Animation(-format => 'gif', -file => $gif89);

my $lab = $mw->Label(-image => $animate);

my $start = $mw->Button(
    -text => 'Start',
    -command => [$animate => 'start_animation', 500]);
my $stop  = $mw->Button(
    -text => 'Stop',
    -command => [$animate => 'stop_animation']);

If no images are specified, the program defaults to the multiframe "Powered by Perl" GIF89 image, as Figure 17-16 shows.

Figure 17-16

Figure 17-16. GIF89 images can be multiframe

JavaScript EditorJavaScript Formatter     Perl Manuals