Pencil Code Reference > moveto
The turtle starts in the middle of the window, at (x, y) coordinates (0, 0).
moveto x, y
moves to an absolute location in
traditional Cartesian coordinates. In this coordinate system,
each unit is one pixel, and each graph paper grid square is 25 units.
The mathematical coordinate axes are used, placing (0, 0)
at the middle with x increasing to the right and y increasing upward.
A vector represented as an array of two numbers can be used.
moveto [x, y]
is treated the same as
moveto x, y
.
moveto
will trace lines if a pen is being used. It allows
lines to be drawn between any points without worrying about the angle
and distance of the turtle. moveto
does not affect the
turtle rotation, only its position.
pen peru moveto 50, 50 moveto -75, -25 moveto 0, -50 moveto -125, 100
The native coordinate system supported by HTML follows a different
convention: it places the (pageX, pageY) origin at the upper-left corner
of the page. pageX
increases to the right, but
pageY
is inverted: pageY
increases going down.
moveto
supports native HTML coordinates if they are passed
with the pageX
and pageY
properties. Note
that HTML mouse events all have pageX and pageY properties, so
moveto lastclick
will move to the location of the last
mouse click.
moveto pageX: 50, pageY: 100 pen slateblue moveto pageX: 150, pageY: 200 moveto pageX: 250, pageY: 100 moveto lastclick
moveto
will also move to any element or jQuery object or
any other object that supports a pagexy() method that returns page
coordinates. For example moveto otherturtle
will move
to the location of another turtle.
Here is a summary of some types of locations that
moveto
understands.
example | motion |
---|---|
moveto x, y |
move to (x, y) in traditional mathematical y-up coordinates. |
moveto [x, y] |
same as moveto x, y |
moveto pageX: px, pageY: py |
move using HTML page coordinates. |
moveto lastmouse |
move to the location of the last mouse event. |
moveto otherturtle |
move to another turtle. |
moveto $('#spot') |
move to the element with id spot. |
moveto document |
move to the center of the document. |
moveto window |
move to the center of the visible window. |
When passing a single location object to moveto
, it supports
an optional second argument limiting the distance of the motion. When the
second argument is given, the turtle will move towards the location, but
no farther than the limiting distance in pixels.
The following program will move the turtle towards the last mouse event twice per second, but will move no more than 10 pixels each time.
pen green
tick 2, ->
moveto lastmouse, 10
In all these uses, moveto
moves the turtle without changing
its direction.