Codebase list libcairo-perl / f1afe7d
Add a subclassing example Also, fix the file permissions of another example. Torsten Schönfeld 14 years ago
2 changed file(s) with 54 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
(No changes)
0 #!/usr/bin/perl
1 use strict;
2 use warnings;
3
4 my $surface = Cairo::ImageSurface->create ('argb32', 1000, 1000);
5 my $context = CustomContext->create ($surface);
6 $context->draw_star;
7 $context->show_page;
8 $surface->write_to_png ($0 . '.png');
9
10 package CustomContext;
11
12 use strict;
13 use warnings;
14 use Cairo;
15 use Math::Trig qw/pi/;
16
17 use base qw/Cairo::Context/;
18
19 sub create {
20 my ($package, $surface) = @_;
21
22 my $self = $package->SUPER::create($surface);
23
24 return bless $self, $package;
25 }
26
27 sub draw_star {
28 my ($self) = @_;
29
30 my $width = $self->get_target()->get_width();
31 my $height = $self->get_target()->get_height();
32
33 $self->rectangle (0, 0, $width, $height);
34 $self->set_source_rgb (1, 1, 1);
35 $self->fill;
36
37 $self->save;
38 {
39 $self->set_source_rgba (0, 0, 0, 0.5);
40 $self->translate ($width / 2, $height / 2);
41
42 my $mx = $width / 3.0;
43 my $count = 100;
44 foreach (0 .. $count-1) {
45 $self->new_path;
46 $self->move_to (0, 0);
47 $self->rel_line_to (-$mx, 0);
48 $self->stroke;
49 $self->rotate ((pi() * 2) / $count);
50 }
51 }
52 $self->restore;
53 }