Perl Cairo tutorial - Draw dashed lines

This is one page of a Perl Cairo tutorial.

This draws some dashed lines. Notice that the offset comes first, and the dash arguments come in the following array, unlike set_dash.
#!/home/ben/software/install/bin/perl
use warnings;
use strict;
use Cairo;
my $size = 500;
my $surface = Cairo::ImageSurface->create ('argb32', $size, $size);
my $cr = Cairo::Context->create ($surface);
my @dashes = (1, 2, 3, 5, 10, 20);
my $o = $size / scalar (@dashes);
my $i = 0;
my $offset = 0;
$cr->set_line_width ($o/4);
for my $dash (@dashes) {
    # Use a factor of ten to make the dashes visible
    $cr->set_dash ($offset, $dash*10, $dash*10);
    $cr->set_source_rgb (rand (), rand (), rand ());
    my $x = $o*($i + 0.5);
    $cr->move_to ($x, 0);
    $cr->line_to ($x, $size);
    $cr->stroke ();
    $i++;
}
$surface->write_to_png ('dashed.png');

(download)


Copyright © Ben Bullock 2009-2024. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com). / Privacy / Disclaimer