Skip to content

Drawing paths

viachpaliy edited this page Apr 8, 2020 · 2 revisions

Now let's look at paths. A path is a list of points, connected by segments of lines that can be of different shapes, curved or not, of different width and of different color. A path, or even a subpath, can be closed. To make shapes using paths, we take some extra steps: 1.First, you create the path. 2.Then you use drawing commands to draw into the path. 3.Once the path has been created, you can stroke or fill the path to render it.

Here are the functions used to perform these steps:

Cairo::Context.new_path()

Creates a new path. Once created, future drawing commands are directed into the path and used to build the path up.

Path methods

Methods to set different paths for objects.

Cairo::Context.close_path()

Adds a straight line to the path, going to the start of the current sub-path.

Cairo::Context.stroke()

Draws the shape by stroking its outline.

Cairo::Context.fill()

Draws a solid shape by filling the path's content area.

The first step to create a path is to call the new_path(). Internally, paths are stored as a list of sub-paths (lines, arcs, etc) which together form a shape. Every time this method is called, the list is reset and we can start drawing new shapes.

Note: When the current path is empty, such as immediately after calling new_path(), or on a newly created canvas, the first path construction command is always treated as a move_to(), regardless of what it actually is. For that reason, you will almost always want to specifically set your starting position after resetting a path.

The second step is calling the methods that actually specify the paths to be drawn. We'll see these shortly.

The third, and an optional step, is to call close_path(). This method tries to close the shape by drawing a straight line from the current point to the start. If the shape has already been closed or there's only one point in the list, this function does nothing.

Note: When you call fill(), any open shapes are closed automatically, so you don't have to call close_path(). This is not the case when you call stroke().

Clone this wiki locally