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.

            
              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
🕑 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