#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::RotCanvas; my $mw = tkinit; my $heightmw = 260; my $width = 600; my $height = $heightmw - 60; $mw->geometry($width.'x'.$heightmw .'+100+100'); my $canvas = $mw->RotCanvas(-width => $width, -height => $height, -bg => 'black')->pack(-fill=>'both',-expand => 1); my $angle = 0; my $px0 = 240; my $py0 = 0; my $px_new = 240; my $py_new = $height; my %pend; for(0..4){ $pend{$_}{'line'} = $canvas->createLine( $px0 ,$py0, $px_new ,$py_new, -smooth => 1, -width => 3, -tags => ['line'.$_], -fill => 'white'); $pend{$_}{'ball'} = $canvas->createOval($px_new - 15 ,$py_new , $px_new + 15, $py_new + 30, -tags => ['ball'.$_], -fill => 'orange', ); $canvas->move( $pend{$_}{'line'},$_ * 30,0); $canvas->move( $pend{$_}{'ball'},$_ * 30,0); $pend{$_}{'centerx'} = $px0 + $_ * 30; } my $motion = 0; my $bframe = $mw->Frame()->pack(-fill =>'both'); my $startbut; my $timer; my $toggle = -1; $startbut = $bframe->Button(-text=>'Start', -command =>sub{ if($motion == 0){ $startbut->configure(-text=>'Stop'); $motion = 1; &start; }else{ $startbut->configure(-text=>'Start'); $motion = 0; $timer->cancel; } } )->pack(-side => 'left'); $bframe->Button(-text=>'Quit', -command =>sub{exit})->pack(-side => 'right'); MainLoop; sub start{ #cannot set the timer too low or #the rotate's below don't have enough #time to complete, so lockups occur #so set swing degree higher for more speed $timer = $canvas->repeat(50,sub{ &swing(4); }); } ########################################################## sub swing{ my $degree = shift; $degree = $toggle*$degree; $angle = $angle + $degree; if( $angle > 90 ){$toggle *= -1} if( $angle < -90 ){$toggle *= -1} for(0..4){ # can only specify one tag at a time in RotCanvas, # and it will not do a group $canvas->rotate('line'.$_, $degree, $pend{$_}{'centerx'},$py0); $canvas->rotate('ball'.$_, $degree, $pend{$_}{'centerx'},$py0); } }