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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              var lexicon = "FFFFF+-<>=^".split("");
var angle1 = Math.PI / 12;
var angle2 = 0;
var t = 0;

function pick(arr) {
  return arr[random(arr.length)];
}

function geno_make() {
  var g = "";
  for (var i = 0; i < 25; i++) {
    g += pick(lexicon);
  }
  return {
    genome: g
  };
}

var pop = [];
for (var i = 0; i < 9; i++) {
  pop[i] = geno_make();
}

code = geno_make();

turtle = {
  pos: new vec2(),
  dir: new vec2(0, 1),
  spin: 1,
};

function turtle_draw(turtle, code, lines) {
  for (var i = 0; i < code.length; i++) {
    var c = code.substr(i, 1);
    if (c == "F") {
      var p2 = turtle.dir.clone().add(turtle.pos);
      lines.push(turtle.pos);
      lines.push(p2);
      turtle.pos = p2;
    } else if (c == "+") {
      turtle.dir.rotate(angle1 * turtle.spin);
    } else if (c == "-") {
      turtle.dir.rotate(-angle1 * turtle.spin);
    } else if (c == "<") {
      turtle.dir.rotate(angle2 * turtle.spin);
    } else if (c == ">") {
      turtle.dir.rotate(-angle2 * turtle.spin);
    } else if (c == "=") {
      var t2 = {
        pos: turtle.pos.clone(),
        dir: turtle.dir.clone().negate(),
        spin: turtle.spin
      };
      turtle_draw(t2, code.substr(i + 1), lines);
    } else if (c == "^") {
      var t2 = {
        pos: turtle.pos.clone(),
        dir: turtle.dir.clone(),
        spin: -turtle.spin
      };
      turtle_draw(t2, code.substr(i + 1), lines);
    }
  }
}

function regenerate(choice) {
    t = 0;
    // convert mouse position p to choice of biomorph:
    var parent = pop[choice];

    // make a new population:
    var newpop = [];
    for (var i=1; i<9; i++) {
      var child = parent.genome;
      
      // TODO: mutations;
      var cut = random(child.length);
      var a = child.slice(0, cut);
      var cut = random(child.length);
      var b = child.slice(cut);
      var c = pick(lexicon);
      child = b+c+a;
      
      if (child.length > 20) {
        child = child.slice(0, 20);
      } else  {
        while (child.length < 10) {
          child = child + pick(lexicon);
        }
      }
      
      newpop.push({
        genome: child
      });
    }
    // add a random one too:
    newpop.push(geno_make());
    pop = newpop;
}

function mouse(e, p) {
  if (e == "down") {
    
    var col = Math.floor(p[0]*3);
    var row = Math.floor(p[1]*3);
    var choice = row*3 + col;
    regenerate(choice);
  }
}

function draw() {
  var limit = Math.floor(t * 31);
  for (var i = 0; i < pop.length; i++) {
    var code = pop[i].genome;
    
    var col = i % 3;
    var row = Math.floor(i/3);
    
    write(col, row, code);
    draw2D.push()
      .translate((col + 0.5)/3, (row + 0.5)/3)
      .scale(0.01);

    var lines = [];
    turtle.pos.set(0, 0);
    turtle.dir.set(0, 1);
    turtle_draw(turtle, code.substring(0, limit), lines);
    
    draw2D.lines(lines);

    draw2D.pop();
  }
}

var t0 = now;

function update() {
  
  var auto = false;
  if (auto && now - t0 > 5) {
    regenerate(random(9));
    t0 = now;
  }
  
  t += dt;
  angle1 = Math.sin(now);
  angle2 = 0.25 * Math.sin(now * Math.PI * 4);
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console