css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              // 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", 
           "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:
  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) + ")";
      case "rotor":
        // create a vector as a function of time:
        val = "vec2.fromPolar(now * " + (i1-3) + ")";
      case "noise":
        // add some noise to a register:
        val = "vec2.random(random()).mul(" + v1 + ")";
      case "average":
        // get average of two registers:
        val = v1 + ".clone().add(" + v2 + ").mul(0.5)";
      case "translate":
        // add one register to another:
        val = v1 + ".clone().translate(" + v2 + ")";
      case "scale":
        // scale one register:
        val = v1 + ".clone().scale(" + v2 + ".clone().add(1))";
      case "rotate":
        // rotate a register by the angle of another:
        val = v1 + ".clone().rotate(" + v2 + ".angle()*0.1)";
      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) )";
    // create the output name:
    var name = "r" + vars.length;
    // remember this as a possible register:
    // 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);
  fun = new Function("x", "y", code);


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

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

function draw() {
🕑 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 ..................