Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Goo'ey Waves of Goo_ness

by zentara (Archbishop)
on Aug 05, 2008 at 17:52 UTC ( #702432=sourcecode: print w/replies, xml ) Need Help??
Category: Gui Programming
Author/Contact Info zentara of perlmonks
Description: Just an optical illusion, based on the Goo::Canvas, to help hypnotize your girlfriends. :-) Shows rotations and scaling.
#!/usr/bin/perl -w
use strict;
use warnings;
use Goo::Canvas;
use Gtk2 '-init';
use Glib qw(TRUE FALSE);

my @results;
my $r = 0;
my $ang = 0;

for my $count (1..25){

      for my $z(1..100){
         $r += .4;
         $ang += .062832;
         my $x = $r * cos $ang;
         my $y = $r * sin $ang;
      
      push @results,$x,$y;
      }

}
#print "@results\n";

my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
$window->set_default_size(680, 600);


my $vbox = Gtk2::VBox->new;
$vbox->set_border_width(4);
$vbox->show;

my $hbox = Gtk2::HBox->new(FALSE, 4);
$vbox->pack_start($hbox, FALSE, FALSE, 0);
$hbox->show;

$window->add($vbox);

my $swin = Gtk2::ScrolledWindow->new;
$swin->set_shadow_type('in');

$vbox->pack_start($swin, 1, 1, 0); 

my $cwidth = 1000;
my $cheight = 1000;
my ($canvas,$root,$group) = get_canvas();

# Zoom
my $z = Gtk2::Label->new("Zoom:");
$hbox->pack_start($z, FALSE, FALSE, 0);
$z->show;

my $adj = Gtk2::Adjustment->new(1, 0.05, 100, 0.05, 0.5, 0.5);
my $sb = Gtk2::SpinButton->new($adj, 0, 2);
$adj->signal_connect("value-changed", \&zoom_changed, $canvas);
$sb->set_size_request(60, -1);
$hbox->pack_start($sb, FALSE, FALSE, 10);
$sb->show;

# Create PDF                                                          
+                
my $bpdf = Gtk2::Button->new_with_label('Write PDF');                 
+                      
$hbox->pack_start($bpdf, FALSE, FALSE, 0);                            
+                   
$bpdf->show;                                                          
+                   
$bpdf->signal_connect("clicked", \&write_pdf_clicked, $canvas);       
+  

$window->show_all();

&draw();

Gtk2->main;


sub get_canvas{

 my $canvas = Goo::Canvas->new();
 $canvas->set_size_request(600, 450); # minimum size on screen
 $canvas->set_bounds(0, 0, $cwidth, $cheight); # scrollregion
 $swin->add($canvas);
 my $root = $canvas->get_root_item();
 $canvas->signal_connect('button-press-event',
                      \&on_can_button_press);

 my $rect = Goo::Canvas::Rect->new(
      $root, 0, 0, $cwidth, $cheight,
      'line-width' => 1,
      'fill-color' => 'black'
 );

$rect->lower;

 my $g = Goo::Canvas::Group->new($root); 
 $g->translate(310,300);
 
 return ($canvas,$root,$g);
}

sub draw{

    my $points = Goo::Canvas::Points->new(\@results);

     my $poly = Goo::Canvas::Polyline->new(
        $group, FALSE,
         undef, # points need to be set after creation
        'stroke-color' => 'pink',
        # 'fill_color_rgba' => 0xff00ffcc,
        'line-width' => 15,
       );
   #setting after line creation, sets the 'points' property by name
   $poly->set(points => $points);

# add some action
  Glib::Timeout->add (50, sub {
              $group->scale(1.001,1.001);
              $group->rotate(-20,0,0); #angle, rot_center_x, rot_cente
+r_y
              #$group->translate(15,15);  #make drunk friends puke :-)
              return 1;
            });
return 0;
}

sub write_pdf_clicked {
    my ($but, $canvas) = @_;
    print "Write PDF...\n";

    my $scale = $adj->get_value;
    print "scale->$scale\n"; 
    
    my $surface = Cairo::PdfSurface->create("$0-$scale.pdf", 
                    $scale*$cwidth, $scale*$cheight);
    
    my $cr = Cairo::Context->create($surface);

    # needed to save scaled version
    $cr->scale($scale, $scale);

    $canvas->render($cr, undef, 1);
    $cr->show_page;
    print "done\n";
    return TRUE;
}

sub zoom_changed {
    my ($adj, $canvas) = @_;
    my $scale = $adj->get_value;
    $canvas->set_scale($scale);
    # zoom from 0,0 ...like Gnome2::Canvas's set_center_scroll_region 
+(FALSE);
    $canvas->scroll_to (0,0);   
}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://702432]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2020-09-25 20:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (140 votes). Check out past polls.

    Notices?