Methods
back(distanceopt)
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
distance |
number |
<optional> |
100 | How far to move back, in pixels. |
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
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:
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.
Can only be used within a function given to repeat or foreach. Will immediately terminate the function and cause the loop to stop.
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.
Starts out in the same state as the original turtle, but changes to it don't affect the original one.
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. |
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. |
foreach(a, fn) → {Array|undefined}
Call a function for each element of an array.
(Advanced) The loop can be stopped with breakout.
(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. |
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)
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
distance |
number |
<optional> |
100 | How far to move forward, in pixels. |
heading() → {number}
Get the turtle's heading.
Returns:
The turtle's heading angle in degrees (0–360).
- Type
- number
inside() → {boolean}
Get whether the turtle is currently inside of the canvas.
Returns:
true
if inbounds, false
otherwise.
- Type
- boolean
isdown() → {boolean}
Get whether the pen is currently down.
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. |
Returns:
true
if obj
is a Turtle Object, false
otherwise.
- Type
- boolean
isup() → {boolean}
Get whether the pen is currently up.
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. |
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). |
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
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:
-
- setmarkfunction to customize drawing of the mark.
newturtle() → {Object}
Create a new turtle object.
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.
Returns:
true
if out of bounds, false
otherwise.
- Type
- boolean
pendown()
Lower the pen.
Subsequent uses of
Subsequent uses of
forward
and back
will draw lines.
A new turtle starts with the pen down.
penup()
Raise the pen.
Subsequent uses of
Subsequent uses of
forward
and back
will NOT draw lines.
A new turtle starts with the pen down.
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:
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.
Restores position, heading and pen state to what they were when pushstate was last called.
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
The transformation matrix contains all transformations, accumulated through calls to
translate
, rotate
and scale
.
pushstate()
Push the turtle's state onto the stack.
Saves the current position, heading and pen state.
Saves the current position, heading and pen state.
range(start, stopopt, stepopt) → {Iterable}
Get a sequence of numbers for use in loops (like foreach and
Produces a sequence starting at 0, up until but not including
Produces a sequence starting at
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. |
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.
(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. |
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.
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:
-
resetstate
to reset state only.resetmatrix
to reset transformation only.
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.
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).
right(angleopt)
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). |
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:
-
translate
andscale
, the other transformations.resetmatrix
to reset transformations.pushmatrix
to save transformations.popmatrix
to restore previously saved transformations.
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:
-
translate
androtate
, the other transformations.resetmatrix
to reset transformations.pushmatrix
to save transformations.popmatrix
to restore previously saved transformations.
self() → {Object}
Get the turtle object itself.
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). |
setmarkfunction(fnopt)
Set a custom function that draws the mark when using mark.
To revert to the default mark omit the parameter:
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. |
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. |
setturtlefunction(fnopt)
Set a custom function that draws the turtle when using show.
To revert to the default turtle use omit the parameter:
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. |
setxy(x, yopt)
Set the turtle's position.
Draws a line to the new position, if the pen is down (see
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. |
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:
-
- setturtlefunction to customize drawing of the turtle.
state() → {State}
Get the turtle's current position, heading angle and pen position as an object.
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:
-
rotate
andscale
, the other transformations.resetmatrix
to reset transformations.pushmatrix
to save transformations.popmatrix
to restore previously saved transformations.
type(value) → {string}
Determine the type of any value.
Returns one of the following strings:
–
–
–
–
–
–
–
–
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. |
Returns:
- A string describing the type of
value
, see above.
- Type
- string
x() → {number}
Get the turtle's x-coordinate.
Returns:
The turtle's x-coordinate in pixels.
- Type
- number
xy() → {Position}
Get the turtle's position.
Returns:
- Type
- Position
y() → {number}
Get the turtle's y-coordinate.
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.
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
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
x |
number | The x-coordinate in pixels. |
y |
number | The y-coordinate in pixels. |
- Source:
- See:
State
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. |