Pencil Code Reference > pen

 

pen changes the pen color

pen will pick a drawing color When the turtle moves, it will trace out a line in the pen color.

pen blueviolet

Colors

Here are some basic colors that can be used with pen:

white yellow lime aqua blue fuchsia red silver
gray olive green teal navy purple maroon black

For all 140 standard named colors, see the color table.

Thick Lines

pen accepts an optional second argument, which is the width of the pen in pixels. Remember that arguments need to be separated by a comma.

pen pink, 20

Line Cap Styles

pen accepts an optional third argument, which is the name of a line-cap style to use.

line-cap style pen shape example
'round' (default) circle pen coral, 25, 'round'
'square' square pen skyblue, 25, 'square'
'butt' flat brush pen violet, 25, 'butt'

Remember to enclose the line cap style in quotes. These line cap values are strings.

Round line caps are the default. They are forgiving and useful for general gometric shapes. Butt line cas are useful when you need a line that is exactly the length that you asked for. Square line caps are useful for lining up rectangular shapes with line ends.

Line Join Styles

To control the appearance of corners, use the optional fourth argument of pen, which controls a line-join style to use.

line-join style corners example
'round' (default) rounded pen coral, 25, 'butt', 'round'
'miter' sharp pen skyblue, 25, 'butt', 'miter'
'bevel' flat pen violet, 25, 'butt', 'bevel'

Names of both line-cap styles and line-join styles are taken from the HTML5 Canvas and SVG standards.

Pretty Paths

When tracing out a colored path with a, the path is continuously drawn with one end at the turtle.

This can sometimes result in overdrawing. For example, if you have a line-cap style like square that extends beyond the line-join shape for a sharp angle, extra pixels can stick out if you draw the path continuously.

This can be solved by using a pen to trace out an invisible path, then using the fill command to draw the whole path at once. With fill, you specify the line drawing options by name:

pen path
fd 100
rt 120
fd 100
fill
  strokeStyle: green
  lineWidth: 25
  lineCap: square

fill can also be used to fill shapes with solid colors using the default fillStyle property.

Erasing

There is also special "color" called erase that will remove any existing color it passes over.

pen red
fd 50
home()

pen erase
fd 50