Perl Cairo tutorial - Draw a circle

This is one page of a Perl Cairo tutorial.

This shows drawing a circle on the background with the arc command. It also demonstrates the use of RGB colours.
use warnings;
use strict;
use Cairo;
use Math::Trig;
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);
# Set the colours to red=0, green=0.5, blue=1.
$cr->set_source_rgb (0, 0.5, 1);
# Fill the rectangle.
$cr->fill ();
# Draw an arc at the centre (first two arguments), of radius $size/3,
# starting at angle 0 and finishing at angle 2Π.
$cr->arc ($size/2, $size/2, $size/3, 0, 2 * pi);
# Set the line width.
$cr->set_line_width (30);
# Set the colour.
$cr->set_source_rgb (1, 0.5, 0);
# Draw the line.
$cr->stroke ();
# Write the surface to the png file specified by $png.
$surface->write_to_png ('circle.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