cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - 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.

            
              <canvas id="canvas"></canvas>
            
          
!
            
              body {
  background-color: black;
  color: white;
  display: flex;
  align-items: center;
  flex-direction: column;
  margin: 0;
}
            
          
!
            
              var params = {
  fps: 60,
  checkerboard: 30,
  north: .25,
  east: .25,
  south: .25,
  west: .25,
}

// 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];
    }
    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
      }
      data[i] = data[i + offset];
      data[i+1] = data[i + offset + (is_rgb ? 1 : 0)];
      data[i+2] = data[i + offset + (is_rgb ? 2 : 0)];
    }
  }
  ctx.putImageData(imageData, 0, 0);
}

var c = document.getElementById("canvas");
c.width = window.innerWidth;
c.height = window.innerHeight;
seed(c, false);

var previous_time = 0;
function update() {
  var time = (new Date()).getTime()/1000;
  if (time - previous_time > 1/params.fps) {
    var z = params.north + params.east + params.south + params.west;
    var distribution = [params.north/z, (params.north + params.east)/z, (params.north + params.east + params.south)/z];
    neighborSwap(c, params.color, distribution);
    previous_time = time;
  }
  window.requestAnimationFrame(update);
}

var gui = new dat.GUI();
gui.add(params, 'fps', 1, 60);

var seedFolder = gui.addFolder('Re-seed');
params.grayscale = function(){seed(canvas, false);};
seedFolder.add(params, 'grayscale');
params.color = function(){seed(canvas, true);};
seedFolder.add(params, 'color');

function checkerSeed(i) {
  var x = Math.floor(((i%(4*canvas.width))/4)/params.checkerboard) % 2;
  var y = Math.floor(i/(4 * canvas.width * params.checkerboard)) % 2;
  var v = x == y ? 255 : 0;
  return [v, v, v];
}
var checkerboard = seedFolder.add(params, 'checkerboard', 1, 500);
checkerboard.onChange(function(){
  seed(canvas, true, checkerSeed);
});

var slantFolder = gui.addFolder('Direction Probabilities');
slantFolder.add(params, 'north', 0.01, 1);
slantFolder.add(params, 'east', 0, 1);
slantFolder.add(params, 'south', 0, 1);
slantFolder.add(params, 'west', 0, 1);

update();

            
          
!
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