Press F1 or click here to show/hide this help

Welcome to JavaScript Turtle Graphics!

Type your commands at the prompt and see them faithfully executed by our eponymous turtle. Press F1 to toggle between the prompt and this help screen.

What's turtle graphics? From Wikipedia: "Turtle graphics is a term in computer graphics for a method of programming vector graphics using a relative cursor (the "turtle") upon a Cartesian plane. Turtle graphics is a key feature of the Logo programming language."

Here, you'll be programming in JavaScript instead of the Logo language, but you still get to watch a triangle race around the screen! The interactive prompt below exposes the valiable t , which is an instance of class Turtle . Here's a quick rundown of its methods.

method description t.fd(n) Forward. Move ahead by n pixels. t.bk(n) Back. Move in reverse by n pixels. t.rt(deg) Right turn. Rotate deg clockwise. t.lt(deg) Left turn. Rotate deg counter-clockwise. t.pu() Pen up. Turtle no longer traces line along its path. t.pd() Pen down. Turtle resumes tracing line along its path. t.circle(radius) Make an empty circle with midpoint at turtle position, with given radius . t.disc(radius) Make a solid disc with midpoint at turtle position, with given radius . t.clean(bgColor) Erases all lines. If bgColor is provided, updates background color. t.home() Revert turtle to center screen (0,0) and facing "north". t.clear() Equivalent to clean() and home() together. t.reset() Same as clear() , but also restores defaults. t.color(color) Sets pen color. Accepts any CSS color. t.thickness(n) Sets the width in px of the pen stroke. t.face(x, y) Turtle faces toward the given absolute coordinate. t.butt(x, y) Turtle faces away from the given absolute coordinate. t.xy(x, y) Moves turtle to given absolute coords, where origin (0,0) is center of screen. t.x(x) Moves turtle to given absolute X coord, leaving Y coord unchanged. t.y(y) Moves turtle to given Y coord, leaving X coord unchanged. t.heading(deg) Sets absolute turtle direction in degrees, with 0 being "north".

The above methods all return this , so they can be chained, e.g. t.fd(100).rt(90).fd(100) . You may also wish to query turtle state. For that, we have the get methods.

method description t.get.x() Turtle's x position where origin (0,0) is center of screen. t.get.y() Turtle's y position where origin (0,0) is center of screen. t.get.heading() Turtle's orientation in degrees, where 0 is "north". t.get.pu() true if pen is up, false otherwise. t.get.pd() true if pen is down, false otherwise. t.get.color() Pen color. t.get.background() Background color. t.get.thickness() Width in pixels of pen stroke. t.get.top() Top boundary of screen. t.get.bottom() Bottom boundary of screen. t.get.left() Left boundary of screen. t.get.right() Right boundary of screen. t.get.oob() Returns true if the turtle has moved out of bounds of the screen.

...and also some randomness functions for convenience.

method description t.rand.uni([lower, ]upper) A uniform distribution between lower (or zero) and upper . t.rand.norm(mean, stdDev) A normal distribution with the given mean and stdDev (standard deviation). t.rand.chance(odds) Returns true with a probability of odds , where odds is between 0 and 1.

Depth notes I

JavaScript provides a number of ways to do loops, and you're certainly free to use them here. Alternatively, the turtle gives you a couple of its own ways to do things over and over again. This allows you to do loops the "turtle" way, which isn't necessarily better, just fitting with the rest of the API.

method description t.repeat(n, func) Runs func until it returns false or until it runs n times, passing i as arg. t.forever(func) Runs func until it returns false, passing i as arg.

Depth notes II

Colors can take any form accepted by CSS or canvas. This includes "#fff" , "#ffffff" , "rgb(...)" , "hsl(...)" , and even "rgba(...)" and "hsla(...)" .

Depth notes III

Some drawings finish quickly, while others take a long time. This is because the drawing is rendered sequentially, piece by piece, and sometimes there are lots of pieces. To speed things up, when the number of pieces gets quite long, more and more of them are batched together between renders. When the drawing approaches the end, this batching decreases and then stops. You'll see the turtle initially tear through the drawing at a high rate of speed, and then slow to a more leisurely pace at the end. Your machine is actually working harder at the beginning of the drawing than at the end.

Depth notes IV

Who am I? My name is Greg Reimer. I build web applications for fun and profit. Find me on github and LinkedIn. This turtle graphics project is also on github under the MIT license. If you write an awesome sample program that you think should be included here, send me a pull request!