Methods
Table of contents
Methods specific to Turtle.js
- setlinecap()
- setlinejoin()
- seteasing()
- setduration()
- on()
- enable()
- disable()
- toggle()
- register()
- unregister()
Turtle methods
- Turtle motion
- Move and draw
- forward() | fd()
- backward() | bk() | back()
- right() | rt()
- left() | lt()
- goto() | setpos() | setposition()
- teleport()
- setx()
- sety()
- setheading() | seth()
- home()
- circle()
- dot()
- Tell Turtle’s state
- Move and draw
- Pen control
- Drawing state
- Color control
- Filling
- More drawing control
- Turtle state
- Visibility
- Appearance
Methods of RawTurtle/Turtle and corresponding functions
Most of the examples in this section refer to a Turtle instance called turtle
.
Methods specific to Turtle.js
turtle.setlinecap(type)
Parameters:
type –round
( Default ),butt
orsquare
- Sets how line segment ends are drawn
turtle.setlinejoin(type)
Parameters:
type –round
( Default ),bevel
ormiter
- Set how connected parts of line segments are connected together
turtle.seteasing(easing)
Parameters:
easing –ease
,ease-in
,ease-out
,ease-in-out
,tep-start
,tep-end
,linear
,easeInQuad
,easeOutQuad
,easeInOutQuad
,easeInCubic
,easeOutCubic
,easeInOutCubic
,easeInQuart
,easeOutQuart
,easeInOutQuart
,easeInQuint
,easeOutQuint
,easeInOutQuint
,easeInSine
,easeOutSine
,easeInOutSine
,easeInExpo
,easeOutExpo
,easeInOutExpo
,easeInCirc
,easeOutCirc
,easeInOutCirc
( Default ),easeInBack
,easeOutBack
,easeInOutBack
,easeInElastic
,easeOutElastic
,easeInOutElastic
,easeInBounce
,easeOutBounce
,easeInOutBounce
- Set the easing function of turtle motion transition
turtle.setduration(duration)
Parameters:
duration – a positive number- Set the duration of turtle motion transition
turtle.on(event, func)
Parameters:
- event – One of
start
,complete
,click
,dblclick
,mousedown
,mouseenter
,mouseleave
,mouseover
,mouseout
,mouseup
,contextmenu
,mousemove
or a string. - func – A function
- event – One of
- Listen an event
turtle.enable(pluginName)
Parameters:
pluginName – a string- Enable a plugin
turtle.disable(pluginName)
Parameters:
pluginName – a string- Disable a plugin
turtle.toggle(pluginName)
Parameters:
pluginName – a string- Toggle enable a plugin
turtle.register(plugin)
Parameters:
plugin – an object- Register a plugin
turtle.unregister(pluginName)
Parameters:
pluginName – a string- Unregister a plugin
Learn more on the Plugin page.
Turtle motion
turtle.forward(distance)
Parameters:
distance – a number (integer or float)- Short functions : turtle.fd(distance)
- Move the turtle forward by the specified
distance
, in the direction the turtle is headed.
turtle.backward(distance)
Parameters:
distance – a number- Short functions : turtle.bk(distance), turtle.back(distance)
- Move the turtle backward by
distance
, opposite to the direction the turtle is headed. Do not change the turtle’s heading.
turtle.right(angle)
Parameters:
angle – a number (integer or float)- Short functions : turtle.rt(angle)
- Turn turtle right by
angle
units. (Units are by default degrees, but can be set via the degrees() and radians() functions.) Angle orientation depends on the turtle mode, see mode().
turtle.left(angle)
Parameters:
angle – a number (integer or float)- Short functions : turtle.lt(angle)
- Turn turtle left by
angle
units. (Units are by default degrees, but can be set via the degrees() and radians() functions.) Angle orientation depends on the turtle mode, see mode().
turtle.goto(x, y)
Parameters:
- x – a number
- y – a number
- Short functions : turtle.setpos(x, y), turtle.setposition(x, y)
- Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle’s orientation.
turtle.teleport(x, y)
Parameters:
- x – a number
- y – a number
- Move turtle to an absolute position. Unlike goto(x, y), a line will not be drawn. The turtle’s orientation does not change.
turtle.setx(x)
Parameters:
x – a number (integer or float)- Set the turtle’s first coordinate to
x
, leave second coordinate unchanged.
turtle.sety(y)
Parameters:
y – a number (integer or float)- Set the turtle’s second coordinate to
y
, leave first coordinate unchanged.
turtle.setheading(angle)
Parameters:
angle – a number (integer or float)- Short functions : turtle.seth(angle)
- Set the orientation of the turtle to
angle
. Here are some common directions in degrees:Directions East South West North Degrees 0 90 180 270
turtle.home()
- Move turtle to the origin – coordinates (0,0) – and set its heading to its start-orientation (which depends on the mode, see mode()).
turtle.circle(radius, startAngle=0, endAngle=360)
Parameters:
- radius – a number
- startAngle – a number
- endAngle – a number
- Draws a circle with
radius
specified. The center of the circle is at the position of the turtle; the difference betweenstartAngle
andendAngle
is an angle, which is used to determine the part of the circle to be drawn. IfstartAngle
andendAngle
are not specified, the entire circle is drawn.
turtle.dot(size, color)
Parameters:
- size – a positive number
- color – a colorstring or a color code
- Draw a circular dot with diameter
size
, usingcolor
. Ifsize
is not given, the maximum of penSize+4 and penSize*2 is used.
Tell Turtle’s state
turtle.position()
- Short functions : pos()
- Return the turtle’s current location (x,y) (as an object).
turtle.xcor()
- Return the turtle’s x coordinate.
turtle.ycor()
- Return the turtle’s y coordinate.
turtle.heading()
- Return the turtle’s current heading (value depends on the turtle mode, see
mode()).
Pen control
Drawing state
turtle.pendown()
- Short functions : pd(), down()
- Pull the pen down – drawing when moving.
turtle.penup()
- Short functions : pu(), up()
- Pull the pen up – no drawing when moving.
turtle.pensize(width=None)
Parameters:
width – a positive number- Short functions : width()
- Set the line thickness to
width
or return it. If no argument is given, the current penSize is returned.
turtle.isdown()
- Return True if pen is down, False if it’s up.
Color control
turtle.pencolor(color)
Parameters:
color – a colorstring or a numeric color code- Return or set the pencolor.
turtle.fillcolor(color)
Parameters:
color – a colorstring or a numeric color code- Return or set the fillcolor.
turtle.color(color)
Parameters:
- color – a colorstring or a numeric color code
- Set pencolor and fillcolor to
color
or return them. If no argument is given, the current pencolor and fillcolor are returned.
Filling
The fill methods ( turtle.filling()、turtle.begin_fill()、turtle.end_fill() ) are not yet unavailable
turtle.filling()
- Return fillstate (True if filling, False else).
turtle.begin_fill()
- To be called just before drawing a shape to be filled.
turtle.end_fill()
- Fill the shape drawn after the last call to begin_fill().
More drawing control
turtle.reset()
- Delete the turtle’s drawings from the screen, re-center the turtle and set variables to the default values.
turtle.write(arg, move=false, align='left', font)
Parameters:
- arg – object to be written to the TurtleScreen
- move – True/False ( unavailable )
- align – one of the strings
left
,center
orright
- font – a string using the same syntax as the CSS font property
Turtle state
Visibility
turtle.showturtle()
- Short functions : st()
- Make the turtle visible.
turtle.hideturtle()
- Short functions : ht()
- Make the turtle invisible. It’s a good idea to do this while you’re in the middle of doing some complex drawing, because hiding the turtle speeds up the drawing observably.
turtle.isvisible()
- Return
True
if the Turtle is shown,False
if it’s hidden.
Appearance
turtle.shape(name=None)
Parameters:
name – a string which is a valid shapename- Set turtle shape to shape with given
name
or, if name is not given, return name of current shape. Shape withname
must exist in the TurtleScreen’s shape dictionary. Initially there are the following polygon shapes:arrow
,turtle
,circle
,square
,triangle
,classic
.