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.

Quick-add: + add another resource

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.

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.

            
              <input type="file" onChange="fileDrop(event)"  ondragover="" ondrop="" id="drop"/>
<canvas id="canvas"></canvas>
            
          
!
            
              body {
  background-color: black;
  color: white;
  display: flex;
  align-items: center;
  flex-direction: column;
  margin: 0;
}

#drop {
  background-color: rgba(0,0,0,.01);
  bottom: 0;
  left: 0;
  position: absolute;
  right: 0;
  top: 0;
  width: 100%;
}
            
          
!
            
              var params = {
  fps: 60,
  diagonal: false,
  v1: false,
  v2: 0,
  whiteBlack: false,
  staySelf: 0,
  mutate: 0.0001,
}

// RGB Colors take values in the range 0 to 255 
function randomUint8() {
  return Math.floor(Math.random()*256);
}
function seed(canvas, is_rgb, seedFunc) {
  var ctx = canvas.getContext('2d');
  var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
  var data = imageData.data;
  for (var i = 0; i < data.length; i += 4) {
    var rgb;
    if (seedFunc) {
      rgb = seedFunc(i);
    } else {
      var r = randomUint8();
      rgb = is_rgb ? [r, randomUint8(), randomUint8()] : [r, r, r];
      if (params.whiteBlack) {
        r = r > 128 ? 255 : 0;
        rgb = [r,r,r];
      }
    }
    data[i]     = rgb[0];
    data[i + 1] = rgb[1];
    data[i + 2] = rgb[2];
    data[i + 3] = 255;
  }
  ctx.putImageData(imageData, 0, 0);
}

// Border pixels don't have four neighbors
function isOnBorder(i, width, height) {
  return (i % width === 0 || 
          i % width === width - 1 ||
          i < width ||
          i > width * (height-1));
}

// Given a canvas, swap each pixel. Note that we do this in-place and left to right, top to bottom. This gives some interesting behavior when setting the north and west probabilities to be large (while doing a pure update wouldn't) and is faster.
function neighborSwap(canvas, is_rgb, distribution) {
  var distribution = distribution ? distribution : [.25, .5, .75];
  var ctx = canvas.getContext('2d');
  var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
  var data = imageData.data;
  var L = data.length;
  for (var i = 0; i < L; i += 4) {
    if (!isOnBorder(i/4, canvas.width, canvas.height)) {
      var direction = Math.random();
      var offset;
      if (direction < distribution[0]) {
        offset = -canvas.width * 4; // North
      } else if (direction < distribution[1]) {
        offset = 1 * 4; // East
      } else if (direction < distribution[2]) {
        offset = canvas.width * 4; // South
      } else {
        offset = - 1 * 4; // West
      }
      if (params.diagonal) {
        var ox = Math.sign(Math.random() - .5) * 4;
        var oy = Math.sign(Math.random() - .5) * canvas.width * 4;
        offset = ox + oy;
      }
      if (Math.random() < params.staySelf) {
        offset = 0;
      }
      if (params.v2) {
        offset = -canvas.width * 4 + 4 * Math.round((Math.random()-.5) * 4 * params.v2);
      }
      data[i] = data[i + offset + 0];
      data[i+1] = data[i + offset + 0];
      data[i+2] = data[i + offset + 0];
      
      if (params.v1) {
        data[i+1] = data[i + offset + 1];
      }
      if (Math.random() < params.mutate) {
        var v = randomUint8();
        v = params.whiteBlack ? (v>128 ? 255 : 0) : v;
        data[i] = v;
        data[i+1] = v;
        data[i+2] = v;
      }
      
    }
  }
  ctx.putImageData(imageData, 0, 0);
}

var c = document.getElementById("canvas");
c.width = window.innerWidth;
c.height = window.innerHeight;
var ctx = c.getContext("2d");
seed(c, true);

var previous_time = 0;
function update() {
  var time = (new Date()).getTime()/1000;
  if (time - previous_time > 1/params.fps) {
    neighborSwap(c, params.color, [.25, .5, .75]);
    previous_time = time;
  }
  window.requestAnimationFrame(update);
}

function fileDrop(e) {
  e.preventDefault();
  var reader = new FileReader();
  reader.readAsDataURL(e.target.files[0]);
  reader.onloadend = function(event){
    var img = new Image();
    img.src = reader.result;    
    img.onload = function(){
      debugger;
      c.width = img.width;
      c.height = img.height;
      ctx.drawImage(img,0,0);
    }
  }
}

var gui = new dat.GUI();
gui.add(params, 'fps', 1, 60);
params.init = function(){seed(c, true);};
gui.add(params, 'init');
gui.add(params, 'diagonal');
gui.add(params, 'v1');
gui.add(params, 'v2', 0, 10).step(1);
gui.add(params, 'whiteBlack');
gui.add(params, 'staySelf', 0, 1);
gui.add(params, 'mutate', 0, 1);


update();

            
          
!
999px
Loading ..................

Console