penWidth

Category:Turtle

penWidth(width)

Category: Turtle

Sets the width of the line in pixels that the turtle draws behind it as it moves.

When artists draw pictures they use many different pens that can draw different line thicknesses. You can also change the thickness of the lines that the turtle draws. The default pen width is one pixel and the pen is by default in the down (drawing) position.

Examples


// Draw a line 10 pixels wide and 100 pixels long
penWidth(10);
moveForward(100);

Example: Skyscraper Draws a skyscraper.

// Draw a skyscraper.
penWidth(20);
moveForward();
penWidth(15);
moveForward();
penWidth(10);
moveForward();
penWidth(5);
moveForward();
penWidth(1);
moveForward();

Example: Sample Line Thicknesses Draws horizontal lines with thickness 10 pixels to 90 pixels, counting by 10.

// Draws horizontal lines with thickness 10 pixels to 90 pixels, counting by 10.
penUp();
moveTo(0, 0);
turnRight(90);
for (var i = 1; i < 10; i++) {
  penDown();
  penWidth(i * 10);
  moveForward(250);
  moveBackward(250);
  penUp();
  turnRight(90);
  moveForward(i * 10 + 10);
  turnLeft(90);
}

Syntax

penWidth(width);

Parameters

Name Type Required? Description
width number Yes The width of the line in pixels that the turtle draws behind it as it moves.

Returns

No return value. Modifies turtle drawing only.

Tips

  • penUp() causes no line to be drawn.
  • Turtle drawing commands are not effected by the show() and hide() commands, which control if the turtle icon is displayed or not.
  • Even though it may look like a line, circles are actually being drawn behind the turtle as it moves. The width passed to penWidth sets the diameter of these circles.

Found a bug in the documentation? Let us know at documentation@code.org

Found a bug in the documentation? Let us know at documentation@code.org