Global

Methods

back(distanceopt)

Move the turtle back.
Draws a line, if the pen is down (see pendown and penup).
Parameters:
Name Type Attributes Default Description
distance number <optional>
100 How far to move back, in pixels.
Source:
See:

bearing(x, yopt) → {number}

Get the bearing from the turtle to a given point.
The bearing is the angle from the turtle's heading direction to the given point. In other words, the bearing is the angle the turtle needs to turn right in order to face the given point.
Parameters:
Name Type Attributes Description
x number | Position The x-coordinate of the point to get the bearing to or a Position object. The other parameter (y) is ignored, if a Position object is given.
y number <optional>
The y-coordinate of the point to get the bearing to.
Source:
See:
  • right to turn towards the point.
  • face for a convenience function to face a given point.
Returns:
The bearing to the given point in degrees (-180 to +180).
Type
number

breakout()

Break out of a repeat or foreach loop.
Can only be used within a function given to repeat or foreach. Will immediately terminate the function and cause the loop to stop.
Source:
See:

clone() → {Object}

Get a copy of the turtle object.
Starts out in the same state as the original turtle, but changes to it don't affect the original one.
Source:
Returns:
An exact clone of the turtle object returned by self. Has all turtle functions as properties.
Type
Object

distance(x, yopt) → {number}

Get the distance from the turtle to a given point.
Parameters:
Name Type Attributes Description
x number | Position The x-coordinate of the point to get the distance to or a Position object. The other parameter (y) is ignored, if a Position object is given.
y number <optional>
The y-coordinate of the point to get the distance to.
Source:
Returns:
The distance to the given point in pixels.
Type
number

face(x, yopt)

Turn the turtle to face a given point.
Parameters:
Name Type Attributes Description
x number | Position The x-coordinate of the point to face or a Position object. The other parameter (y) is ignored, if a Position object is given.
y number <optional>
The y-coordinate of the point to face.
Source:

foreach(a, fn) → {Array|undefined}

Call a function for each element of an array.
(Advanced) The loop can be stopped with breakout.
Parameters:
Name Type Description
a Iterable An array or (advanced use) any other Iterable, like the return value of a range call.
fn function The function to be called for each element. It is called with three arguments el, i and a. el is the current element from the array, i is a running index starting at 0, and a is the array itself.
Source:
See:
Returns:
(Advanced) An array of the return values of the individual calls to fn, or undefined if none of the function calls returns anything.
Type
Array | undefined

forward(distanceopt)

Move the turtle forward.
Draws a line, if the pen is down (see pendown and penup).
Parameters:
Name Type Attributes Default Description
distance number <optional>
100 How far to move forward, in pixels.
Source:
See:
  • back to move back.

heading() → {number}

Get the turtle's heading.
Source:
Returns:
The turtle's heading angle in degrees (0–360).
Type
number

inside() → {boolean}

Get whether the turtle is currently inside of the canvas.
Source:
See:
Returns:
true if inbounds, false otherwise.
Type
boolean

isdown() → {boolean}

Get whether the pen is currently down.
Source:
Returns:
true if pen is down, false otherwise.
Type
boolean

isturtle(obj) → {boolean}

Check whether an object is a turtle or not.
Parameters:
Name Type Description
obj any The objcet to check. Can be anything.
Source:
Returns:
true if obj is a Turtle Object, false otherwise.
Type
boolean

isup() → {boolean}

Get whether the pen is currently up.
Source:
Returns:
true if pen is up, false otherwise.
Type
boolean

jumpxy(x, yopt)

Set the turtle's position, without drawing to the new position.
Parameters:
Name Type Attributes Description
x number | Position The x-coordinate or a Position object. The other parameter (y) is ignored if a Position object is given.
y number <optional>
The y-coordinate.
Source:

left(angleopt)

Turn turtle to the left.
Parameters:
Name Type Attributes Default Description
angle number <optional>
90 How far to turn the turtle left, in degrees (0–360).
Source:
See:

mark(sizeopt, rotationopt)

Draw a small + at the turtle's current position independent of heading.
The orientation of the mark is independent of the turtle's current heading and can be specified with the rotation parameter.
Parameters:
Name Type Attributes Default Description
size number <optional>
10 Size of the mark in pixels.
rotation number <optional>
0 Rotation of the mark in degrees (0–90). Set to 45 to draw an ✕.
Source:
See:

newturtle() → {Object}

Create a new turtle object.
Source:
Returns:
A brand new turtle object. Has all turtle functions as properties.
Type
Object

outside() → {boolean}

Get whether the turtle is currently outside of the canvas.
Source:
See:
Returns:
true if out of bounds, false otherwise.
Type
boolean

pendown()

Lower the pen.
Subsequent uses of forward and back will draw lines. A new turtle starts with the pen down.
Source:
See:
  • penup to raise the pen.

penup()

Raise the pen.
Subsequent uses of forward and back will NOT draw lines. A new turtle starts with the pen down.
Source:
See:
  • penup to lower the pen.

plotter()

Get the Plotter object, containing all the functions to control plotting your turtle graphics.
Source:
Returns:
The Plotter object.

pop()

Restore the last pushed turtle state and transformation matrix from the stack.
Source:
See:
  • popstate to only restore the turtle's state.
  • popmatrix to only restore the transformation matrix.

popmatrix()

Restore the last pushed transformation matrix from the stack.
Source:
See:
  • pushmatrix to first save the transformation matrix.

popstate()

Restore the last pushed turtle state from the stack.
Restores position, heading and pen state to what they were when pushstate was last called.
Source:
See:
  • pushstate to first save the turtle's state.

push()

Push the turtle's state and transformation matrix onto the stack.
Source:
See:
  • pushstate to only save the turtle's state.
  • pushmatrix to only save the transformation matrix.

pushmatrix()

Push the current transformation matrix onto the stack.
The transformation matrix contains all transformations, accumulated through calls to translate, rotate and scale.
Source:
See:
  • popmatrix to later restore the pushed transformation matrix.

pushstate()

Push the turtle's state onto the stack.
Saves the current position, heading and pen state.
Source:
See:
  • popstate to later restore the pushed state.

range(start, stopopt, stepopt) → {Iterable}

Get a sequence of numbers for use in loops (like foreach and for...of). This function can be called in two different ways:

range(stop)
Produces a sequence starting at 0, up until but not including stop, with step 1.

range(start, stop, step = 1)
Produces a sequence starting at start, up until but not including stop, with an optional step (default is 1).
Parameters:
Name Type Attributes Default Description
start number Start value, if range is called with two or three arguments, or stop value if called with one arguement only.
stop number <optional>
Stop value, if range is called with two or three arguments, ignored otherwise.
step number <optional>
1 Step value. Can only be used if range is called with three arguments.
Source:
Returns:
An Iterable object that returns the sequence of numbers. Can be used with for...of and (advanced usage) with the spread (...) syntax.
Type
Iterable

repeat(n, fn) → {Array|undefined}

Repeat a function a number of times.
(Advanced) The loop can be stopped with breakout.
Parameters:
Name Type Description
n number Number of times to call the function. Needs to be greater than 0, or no calls will happen.
fn function The function to be called repeatedly. It is called with a single number (0 to n-1) as an argument, containing the count of previous calls.
Source:
See:
Returns:
(Advanced) An array of the return values of the individual calls to fn, or undefined if none of the function calls returns anything.
Type
Array | undefined

reset()

Completetly reset the turtle to its original state.
Resets the turtles position (to the center at x=0, y=0), heading (facing up, at heading 0) and pen (down). Also clears all transformations, that might have been applied. After this, the turtle is like new, like it was just created.
Source:
See:

resetmatrix()

Reset the turtle's transformation matrix.
Source:
See:
  • resetstate to reset state.
  • reset to reset everything (both state and transformations).

resetstate()

Reset the turtle's state.
Resets the turtles position, heading and pen position to its original state, at the center (x=0, y=0), facing up (heading 0) with the pen down. This doesn't cause a line to be drawn to the center.
Source:
See:
  • resetmatrix to reset transformations.
  • reset to reset everything (both state and transformations).
Turn turtle to the right.
Parameters:
Name Type Attributes Default Description
angle number <optional>
90 How far to turn the turtle right, in degrees (0–360).
Source:
See:
  • left to turn left.

rotate(angle)

Rotate the coordinate system around the current position of the turtle.
Parameters:
Name Type Description
angle number The angle in degrees to rotate the coordinate system. A positive number rotates clockwise, a negative number counter-clockwise.
Source:
See:

scale(sx, syopt)

Scale the coordinate system from the current position of the turtle.
Parameters:
Name Type Attributes Description
sx number The scaling factor in x-direction.
sy number <optional>
The scaling factor in y-direction. If ommitted, takes the same value as sx.
Source:
See:

self() → {Object}

Get the turtle object itself.
Source:
Returns:
A turtle object. Has all turtle functions as properties.
Type
Object

setheading(angle)

Set the turtle's heading.
Parameters:
Name Type Description
angle number The heading angle (0–360).
Source:

setmarkfunction(fnopt)

Set a custom function that draws the mark when using mark.
To revert to the default mark omit the parameter: setmarkfunction();
Parameters:
Name Type Attributes Description
fn function <optional>
Function that will be called to draw the mark when using mark. Omit to revert to the default mark. The function will be called with two parameters size and rotation, which contain the size and rotation used in the call to mark. Before the function is called the state of the turtle is saved, the heading is set to rotation and the pen is lowered. After the function is called, the turtle is returned to the saved state.
Source:
See:

setstate(x, yopt, hopt, dopt)

Set the turtle's position, heading angle and/or pen state.
Parameters:
Name Type Attributes Description
x number | State The x-coordinate in pixels or a State object. The other parameters are ignored if a State object is given.
y number <optional>
The y-coordinate in pixels.
h number <optional>
The heading angle in degrees (0–360).
d number <optional>
The pen down state, true for down, false for up.
Source:

setturtlefunction(fnopt)

Set a custom function that draws the turtle when using show.
To revert to the default turtle use omit the parameter: setturtlefunction();
Parameters:
Name Type Attributes Description
fn function <optional>
Function that will be called to draw the turtle when using show. Omit to revert to the default turtle. The function will be called with a single parameter size, which contains the size used in the call to show. Before the function is called, the state of the turtle is saved and the pen is lowered. After the function is called, the turtle is returned to the saved state.
Source:
See:

setxy(x, yopt)

Set the turtle's position.
Draws a line to the new position, if the pen is down (see pendown and penup).
Parameters:
Name Type Attributes Description
x number | Position The x-coordinate or a Position object. The other parameter (y) is ignored if a Position object is given.
y number <optional>
The y-coordinate. Ignored if x is given a Position object.
Source:

show(sizeopt)

Draw the turtle at its current position and heading.
Parameters:
Name Type Attributes Default Description
size number <optional>
15 Size of the drawn turtle in pixels (height from tip to base).
Source:
See:

state() → {State}

Get the turtle's current position, heading angle and pen position as an object.
Source:
Returns:
A State object containing x (the x-coordinate), y (the y-coordinate), a (the heading angle) and d (the pen down state).
Type
State

translate(tx, ty)

Translate the coordinate system.
Parameters:
Name Type Description
tx number Amount in pixels to translate in the x-direction Positive numbers move to the right, negative numbers move to the left.
ty number Amount in pixels to translate in the y-direction. Positive numbers move down, negative numbers move up.
Source:
See:

type(value) → {string}

Determine the type of any value.

Returns one of the following strings:
"number" if the value is any number.
"string" if the value is a string.
"boolean" if the value is true or false.
"function" if the value is a function.
"array" if the value is an array.
"object" if the value is any other object.
"undefined" if the value is undefined.
"null" if the value is null.
Parameters:
Name Type Description
value any The value you want to get the type of, can be anything.
Source:
Returns:
- A string describing the type of value, see above.
Type
string

x() → {number}

Get the turtle's x-coordinate.
Source:
Returns:
The turtle's x-coordinate in pixels.
Type
number

xy() → {Position}

Get the turtle's position.
Source:
Returns:
A Position object containing x and y.
Type
Position

y() → {number}

Get the turtle's y-coordinate.
Source:
Returns:
The turtle's y-coordinate in pixels.
Type
number

Type Definitions

Plotter

An object containing a bunch of functions to control plotting your turtle graphics.
Retrieved with plotter.
Type:
  • Object
Properties:
Name Type Description
plot function Stop recording lines and show to plotter UI.
newlayer function Start a new layer. The plotter pauses between layers so the pen can be changed.
stop function Stop recording lines. Can be used to exclude a part of your drawing from being plotted.
record function Start recording lines. Recording is enabled from the start, so this function is only useful if stop was used before.
clear function Clear all recorded lines. Doesn't change if recording is enabled or not.
isrecording function Returns true if lines are being recorded, false otherwise.
show function Show the plotter UI.
hide function Hide the plotter UI.
isshown function Returns true if the plotter UI is visible, false otherwise.
lines function (Advanced) Returns an array of all recorded lines so far.
layers function (Advanced) Returns an array of layers recorded so far. Each layer is an array of lines.
stats function (Advanced) Returns an object containing statistics for the recorded lines so far.
Source:

Position

An object describing a turtle's position. Used with xy, setxy and jumpxy.
Type:
  • Object
Properties:
Name Type Description
x number The x-coordinate in pixels.
y number The y-coordinate in pixels.
Source:
See:
  • xy to get the turtle's position.
  • setxy to set the turtle's position.
  • jumpxy to set the turtle's position without drawing.

State

An object describing a turtle's state. Used with state and setstate.
Type:
  • Object
Properties:
Name Type Description
x number The x-coordinate in pixels.
y number The y-coordinate in pixels.
a number The heading angle in degreed (0–360).
d boolean true if the pen is down, false otherwise.
Source:
See:
  • state to get the turtle's state.
  • setstate to set the turtles's state.