Perl Cairo tutorial - Draw a line

This is one page of a Perl Cairo tutorial.

This shows drawing a line on the background with the move_to and line_to commands, and completing it with the stroke command.
use warnings;
use strict;
use Cairo;
my $size = 200;
# Create a drawing surface.
my $surface = Cairo::ImageSurface->create ('argb32', $size, $size);
# Create a drawing context.
my $cr = Cairo::Context->create ($surface);
# Create a rectangle
$cr->rectangle (0, 0, $size, $size);
# Black
$cr->set_source_rgb (0, 0, 0);
# Fill the rectangle.
$cr->fill ();
# Move to the top left corner.
$cr->move_to ($size/5, $size/5);
# Draw a line to the bottom right.
$cr->line_to ((4*$size)/5, (4*$size)/5);
# Set the line width.
$cr->set_line_width (20);
# White
$cr->set_source_rgb (1, 1, 1);
# Draw the line.
$cr->stroke ();
# Write the surface to the png file specified by $png.
$surface->write_to_png ('line.png');


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer