After I stumbled upon this post about how to draw a Koch curve using turtle graphics, I decided to implement some turtle commands in Javascript and test them inside a <canvas/>.

The application

Set the width and the number of recursions of the Koch algorithm, and watch a classic Koch fractal!

Koch curve parameters:

width:recursions:

The implementation

The hearth of the application is the fracturtle object. It implements some turtle graphics commands and the algorithm of the Koch curve (may the SRP god forgive me!). The turtle commands implemented here are:

right(): turns the turtle (i.e. the drawing brush) clockwise

left(): turns the turtle counter-clockwise

forward(): moves the turtle ahead

The whole process starts with the init() function, but the real drawing happens after the fracturtle has received all the commands: