cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              // the alphabet of possible symbols:
var alphabet = "FFFfff<>+-|=".split("");
var mutation_rate = 0.2;
var genome_size = 20;

// create a graphics turtle:
var turtle = {};

function turtle_reset(t) {
  t.pos = new vec2();
  t.dir = new vec2(0, 1);
  t.spin = 1;
}

turtle_reset(turtle);

function turtle_clone(t) {
  return {
    pos: t.pos.clone(),
    dir: t.dir.clone(),
    spin: t.spin,
  };
}

var angle1 = Math.PI / 2;
var angle2 = Math.PI / 3;

var uid = (function() {
  var id = 0;
  return function() {
    return "t" + (id++);
  }
})();

// generate drawing code from the turtle code
// "out" is an array to be filled with lines of JavaScript code
// "code" is the turtle code 
// "t" is the variable name of the current turtle
function turtle_code_generate(out, code, t) {
  for (var i = 0; i < code.length; i++) {
    var c = code.substr(i, 1);
    //out.push("// " + c);
    if (c == "F") {
      // draw a line
      out.push("lines.push("+t+".pos);");
      out.push(t + ".pos = " + t + ".dir.clone().add(" + t + ".pos)");
      out.push("lines.push("+t+".pos);");
      
    } else if (c == "f") {
      // draw a short line
      out.push("lines.push("+t+".pos);");
      out.push(t + ".pos = " + t + ".dir.clone().mul(0.5).add(" + t + ".pos)");
      out.push("lines.push("+t+".pos);");
    } else if (c == "+") {
      // turn turtle:
      out.push(t + ".dir.rotate(" + t + ".spin * angle1);");
    } else if (c == "-") {
      // turn turtle:
      out.push(t + ".dir.rotate(-" + t + ".spin * angle1);");
    } else if (c == ">") {
      // turn turtle:
      out.push(t + ".dir.rotate(" + t + ".spin * angle2);");
    } else if (c == "<") {
      // turn turtle:
      out.push(t + ".dir.rotate(-" + t + ".spin * angle2);");
    } else if (c == "|") {
      // mirror turtle:
      var t1 = uid();
      out.push(t1 + " = { pos: " + t + ".pos.clone(), dir: " + t + ".dir.clone(), spin: -" + t + ".spin };");
      var code1 = code.substring(i + 1);
      turtle_code_generate(out, code1, t1);
    } else if (c == "=") {
      // flip it around:
      var t1 = uid();
      out.push(t1 + " = { pos: " + t + ".pos.clone(), dir: " + t + ".dir.clone().negate(), spin: " + t + ".spin };");
      var code1 = code.substring(i + 1);
      turtle_code_generate(out, code1, t1);
    }
  }
}

function turtle_code(code) {
  var out = ["// turtle:"]; // container of lines of code
  turtle_code_generate(out, code, "t0");
  out = out.join("\n");
  //console.log(out);
  try {
    var f = new Function("t0", "lines", out);
    return f;
  } catch(e) {
    console.log(e);
  } 
}

// now the population:
var pop = [];
for (var i = 0; i < 8; i++) {
  var g = "";
  for (var j = 0; j < genome_size; j++) {
    g += alphabet[random(alphabet.length)];
  }
  pop.push({
    code: g,
    draw: turtle_code(g)
  });
}

function geno_child(parent) {
  var g = parent.code;

  if (random() < 0.1) {
    // reshuffle:
    var cut0 = random(g.length);
    var cut1 = random(g.length);
    var a = g.slice(0, cut0);
    var b = g.slice(cut1);
    g = b + a;
  } else {
    // mutate one gene:
    var c = alphabet[random(alphabet.length)];
    var cut = random(g.length);
    g = g.substring(0, cut) + c + g.substring(cut + 1);
  }
  g.draw = turtle_code(g);
  return {
    code: g,
    draw: turtle_code(g)
  }
}

function update() {
  angle1 = Math.PI / 3 * Math.sin(now);
  angle2 = Math.PI / 8 * Math.cos(4 * now);
}

function draw() {
  for (var i = 0; i < pop.length; i++) {
    var g = pop[i];

    draw2D.push()
      .translate((0.5 + i) / pop.length, 0.5)
      .scale(0.01);

    turtle_reset(turtle);

    write(i, g.code);
    var lines = [];
    g.draw(turtle, lines);
    
    draw2D.lines(lines);
    
    draw2D.pop();
  }
}

function mouse(e, p) {
  if (e == "down") {
    var choice = Math.floor(p[0] * pop.length);
    var parent = pop[choice];
    for (var i = 0; i < pop.length; i++) {
      pop[i] = geno_child(parent);
    }
  }
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console