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.

            
              * {
  margin: 0; padding: 0;
}
            
          
!
            
              var features = {
  num: 0.5,
  str: 0.5,
  arr: 0.5,
  mutate: 0.6,

  chance: function(v) {
    return Math.random() <= v;
  },

  choose: function(a, b, type) {
    if (features.chance(type)) {
      return a;
    } else {
      return b;
    }
  },

  lerp: function(a, b, f) {
    f += Math.random() * (features.mutate * 2) - features.mutate;
    return a + f * (b - a);
  },

  mix: function(a, b) {
    // unmixable types
    if (typeof a !== typeof b) {
      return choose(a, b, features.mutate);

      // numbers
    } else if (
      typeof a === "number" ||
      (typeof parseFloat(a) === "number" && !isNaN(parseFloat(a)))
    ) {
      return parseInt(features.lerp(a, b, features.num));

      // strings
    } else if (typeof a === "string") {
      return features.choose(a, b, features.str);

      // arrays
    } else if (Array.isArray(a)) {
      return a.foreEach(function(v, i) {
        return features.mix(v, b[i]);
      });

      // objects
    } else if (typeof a === "object") {
      return features.breed(a, b);
    }
  },

  breed: function(a, b) {
    var aProps = Object.keys(a),
      bProps = Object.keys(b),
      c = {},
      t;

    if (bProps.length > aProps.length) {
      t = a;
      a = b;
      b = t;
    }

    for (var prop in a) {
      c[prop] = features.mix(a[prop], b[prop]);
    }
    return c;
  }
};

function col() {
  return box({
    r: parseInt(Math.random() * 255),
    g: parseInt(Math.random() * 255),
    b: parseInt(Math.random() * 255)
  });
}

function box(rgb) {
  var r = rgb.r,
    g = rgb.g,
    b = rgb.b,
    // @TODO convert to jQuery and use expando on node
    el = document.createElement("div");
  el.setAttribute("class", "swatch");
  // document.body.prepend(el);
  document.body.insertBefore(el, document.body.firstChild)
  el.style.height = "70px";
  el.style.width = "20%";
  el.style.float = "left";
  el.style.margin = "0px";
  el.style.cursor = "pointer";
  el.style.background = "rgb(" + r + ", " + g + ", " + b + ")";
  el.rgb = rgb;
  return el;
}

// interaction

var parents = [],
  isTouch = "ontouchstart" in document.documentElement,
  start;

function touch(e) {
  var oldParents;

  if (isTouch && start != document.body.scrollTop) return;

  if (!e.target.rgb) return;
  parents.push(e.target);
  e.target.style.WebkitBoxShadow =
    "inset 0px 0px 0px 15px rgba(255, 255, 255, 0.8)";
  if (parents.length == 2) {
    for (var i = 0; i < 5; i++) {
      box(features.breed(parents[0].rgb, parents[1].rgb));
    }

    oldParents = parents.concat();
    setTimeout(function() {
      oldParents.forEach(function(p) {
        p.style.WebkitBoxShadow = "";
      });
    }, 100);
    parents = [];
  }
  start = false;
}

if (isTouch) {
  document.addEventListener("touchstart", function() {
    start = document.body.scrollTop;
  });
  document.addEventListener("touchend", touch);
} else {
  document.addEventListener("click", touch);
}

// create
for (var i = 0; i < 50; i++) {
  col();
}
            
          
!
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