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.

            
              // Click to regenerate a new function

// no. of instructions in genome:
var genome_size = 12;
// image resolution:
var dim = 100;

// image, and copy for double buffering:
var vid = new field2D(dim);
var past = vid.clone();

// set of available operations:
var ops = [//"constant", 
           "rotor", 
           "average", //"noise", 
           "translate", "rotate", "scale",  
           "sample", "sample", "sample"];

// generate a random genome:
function generate(len) {
  var geno = [];
  for (var i = 0; i < len; i++) {
    // generate a random instruction-gene:
    geno.push([
      ops[random(ops.length)],
      random(5),
      random(5)
    ]);
  }
  return geno;
}

// the "big switch statement" interpeter
// takes a genome (array of instructions)
// converts it to a string of JavaScript code
// (to be used as the body of a new function)
function interpret(list) {
  // the growing list of statements in the function:
  // initialized with one statement to define the current image coordinate:
  var stats = ["var norm = new vec2(x/dim, y/dim);"];
  // the growing list of available register names:
  var vars = ["norm"];

  // now the big per instruction switch:
  for (var i = 0; i < list.length; i++) {
    var item = list[i];
    // pull out the operator & arguments
    var op = item[0];
    var i1 = item[1];
    var i2 = item[2];
    // get the nth most recent register, counting backward
    var v1 = vars[vars.length-1 - wrap(i1, vars.length)];
    var v2 = vars[vars.length-1 - wrap(i2, vars.length)];
    // get the expression:
    var val;
    switch (op) {
      case "constant":
        // generate a constant vector:
        val = "new vec2(" + ((i1-3) * 0.1) + "," + ((i2-3) * 0.1) + ")";
        break;
      case "rotor":
        // create a vector as a function of time:
        val = "vec2.fromPolar(now * " + (i1-3) + ")";
        break;
      case "noise":
        // add some noise to a register:
        val = "vec2.random(random()).mul(" + v1 + ")";
        break;
      case "average":
        // get average of two registers:
        val = v1 + ".clone().add(" + v2 + ").mul(0.5)";
        break;
      case "translate":
        // add one register to another:
        val = v1 + ".clone().translate(" + v2 + ")";
        break;
      case "scale":
        // scale one register:
        val = v1 + ".clone().scale(" + v2 + ".clone().add(1))";
        break;
      case "rotate":
        // rotate a register by the angle of another:
        val = v1 + ".clone().rotate(" + v2 + ".angle()*0.1)";
        break;
      case "sample":
        // sample the image at a coordinate
        // determined by a register:
        val = "new vec2(past.sample(" + v1 + ".clone().add(norm), 0), past.sample(" + v2 + ".clone().add(norm), 1) )";
        break;
    }
    // create the output name:
    var name = "r" + vars.length;
    // remember this as a possible register:
    vars.push(name);
    // now build & add the full statement:
    stats.push("var " + name + " = " + val + ";");
  }
  // build the return value (the last created register)
  var name = vars[vars.length-1];
  stats.push("return [" + name + "[0], " + name + "[1] , " + name + "[1] ];");
  // join up all the statements into a single string:
  return stats.join("\n");
}

function reset() {
  vid.set(function() {
    return [random(), random()];
  });

  var test = generate(genome_size);
  var code = interpret(test);
  write(code);
  fun = new Function("x", "y", code);
}

reset();

function mouse(e, p) {
  if (e == "down") reset();
}

function update() {
  var tmp = past;
  past = vid;
  vid = tmp;
  
  vid.set(fun).normalize();
  
  if (Math.abs(vid.min() - vid.max()) < 0.01) reset();
  
  write(vid.min(), vid.max());
}

function draw() {
  vid.draw();
}
            
          
!
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