Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                body {padding: 0; margin: 0;} 
canvas {vertical-align: top;}


              
            
!

JS

              
                
var step = 0;
var iteration = 4;
var commands;
var turtle;


function setup() {

  
  angleMode(DEGREES);
  
  
  createCanvas(windowWidth, windowHeight);
  
  // create the turtle that does the drawing
  turtle = createTurtle();

  // create the command string for drawing a koch curve ...
  commands = kochCommands(iteration);
  
  // ... or call your own function here
  // commands = yourCommands(iteration);

}

function draw() {
  if (step < commands.length) {
    // execute current step
    turtle.obey(commands[step]);
    // increase the step by 1
    step = step + 1;
  } else {
    // reset turtle and step
    turte.reset();
    step = 0;
  }
}


function windowResized() {
  // resize the canvas and clear it
  resizeCanvas(windowWidth, windowHeight);
  clear();
  // reset turtle and step
  turtle.reset();
  step = 0;
}


////////////////////////////////////////////////////////////////////////////////
//    turtle                                                                  //
////////////////////////////////////////////////////////////////////////////////

function createTurtle() {
  
  // create turtle object
  var turtle = {

    // screen coordinates of the turtle
    x: 0,
    y: 0,

    // inital direction
    heading: 0,

    // turn angle in degrees
    turnangle: 60,

    // pixels the turtle moves forward in one step
    stepsize: 3,

    goto: function(x, y) {
      this.x = x;
      this.y = y;
    },

    forward: function(distance) {
      // current coordinates
      var px = this.x;
      var py = this.y;
      // calculate new coordinates
      this.x = this.x + distance * cos(this.heading);
      this.y = this.y + distance * sin(this.heading);
      // draw a line between pervious and new coordinates
     line(px, py, this.x, this.y);
    },

    // turn to the right
    turn: function(angle) {
      // update heading of the turtle
      this.heading = this.heading + angle;
    },

    // pass command(s) to the turtle
    obey: function(commands) {

      // iterate over all the commands
      for (var i = 0; i < commands.length; i++) {

        // get the current command
        var cmd = commands[i];

        // execute the command
        switch (cmd) {

          case 'f': // go one step forward
            this.forward(this.stepsize);
            break;

          case '<': // turn to the left
            this.turn(-this.turnangle);
            break;

          case '>': // turn to the right
            this.turn(+this.turnangle);
            break;

        }
      }
    },

    // reset turtle position and direction
    reset: function() {
      //
      //this.heading = -PI / 4;
      this.goto( width/2, 2* height /3);
      this.heading = 0;
    }
  };

  // initialization
  turtle.reset();

  // return the object
  return turtle;

}



// CREATE YOUR OWN FUNCTION HERE:
function yourCommands(iter) {
  var seq = 'f';
  return seq;
}

// l-system for the koch curve
function kochCommands(iter) {

  // the axiom (initial sequence)
  var axiom = 'f';
  // the replacement sequence
  var rule = 'fff>ffff<f<fff';
  // start out with the axiom
  var seq = axiom;

  for (i = 0; i < iter; i++) {
    // start out with an empty sequence
    var newseq = '';
    // iterate over all characters of the string
    for (j = 0; j < seq.length; j++) {
      // get a command from the sequence
      var cmd = seq[j];
      // rewriting rule
      if (cmd === 'f') {
        // add rule expansion to the new sequence
        newseq = newseq + rule;
      } else {
        // copy the original character to the new sequence
        newseq = newseq + cmd;
      }
    }
    seq = newseq;
  }

  return seq;
}
              
            
!
999px

Console