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" multiple/>
<div id="instruction">Drop 2 or 4 images to upload and interlace or click for prompt.</div>
<canvas id="canvas"></canvas>

            
          
!
            
              body {
  background-color: black;
  color: white;
  display: flex;
  align-items: center;
  flex-direction: column;
  margin: 10px;
}

#drop {
  background-color: rgba(0,0,0,.01);
  bottom: 0;
  left: 0;
  position: absolute;
  right: 0;
  top: -20px;
  width: 100%;
}

#canvas {
  background-image: url("https://i.imgur.com/yE9kWzn.jpg");
  background-size: 600px;
  background-repeat: no-repeat;
  background-position: center;
}
            
          
!
            
              var params = {
  random: false,
  blockSize: 1,
};

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

function min(arr) {
  return arr.reduce(function(acc, x) {return Math.min(acc, x)}, arr[0]);
}


function interlace() {
  var W = min(PX_DATA.map(function(x){ return x.width; }));
  var H = min(PX_DATA.map(function(x){ return x.height; }));
  c.width = W;
  c.height = H;
  var imageData = ctx.getImageData(0, 0, c.width, c.height);
  var data = imageData.data;
  var L = data.length;
  var evenWidth = c.width % 8 === 0;
  for (var i = 0; i < L; i += 4) {
    var row = Math.floor(i / (4 * c.width));
    var col = i % (4 * c.width);
    var imageIndex;
    if ((col/4) % (2*params.blockSize) < params.blockSize) {
      // Left
      imageIndex = row % (2*params.blockSize) < params.blockSize ? 0 : 1;
    } else {
      imageIndex = row % (2*params.blockSize) < params.blockSize ? 3 : 2;
    }
    if (params.random) {
      imageIndex = Math.floor(Math.random() * PX_DATA.length);
    }
    var im = PX_DATA[imageIndex];
    data[i] = im.data[row * 4 * im.width + col];
    data[i + 1] = im.data[row * 4 * im.width + col + 1];
    data[i + 2] = im.data[row * 4 * im.width + col + 2];
    data[i + 3] = im.data[row * 4 * im.width + col + 3];
  }
  ctx.putImageData(imageData, 0, 0);
}

function fileDrop(e) {
  var pixelDatas = [];
  e.preventDefault();
  var reader = new FileReader();
  var index = 0;
  function loadNext() {
    if (e.target.files.length == index) {
      if (pixelDatas.length === 4) {
        PX_DATA = pixelDatas;
        interlace();
      } else if (pixelDatas.length === 2) {
        // We choose opposite corners. Could alternatively do horizontal or vertical striping.
        pixelDatas[2] = pixelDatas[0];
        pixelDatas[3] = pixelDatas[1];
        PX_DATA = pixelDatas;
        interlace();
      }
    }
    reader.readAsDataURL(e.target.files[index]);
    index += 1;
  }
  reader.onloadend = function(event){
    var img = new Image();
    img.src = reader.result;    
    img.onload = function(){
      c.width = img.width;
      c.height = img.height;
      ctx.drawImage(img,0,0);
      var imgData = ctx.getImageData(0, 0, c.width, c.height).data.slice();
      pixelDatas.push({data: imgData, width: c.width, height: c.height});
      
      loadNext();
    }
  }
  loadNext();
}

var gui = new dat.GUI();
var rCont = gui.add(params, 'random');
rCont.onChange(interlace);
var bCont = gui.add(params, 'blockSize', 1, 100).step(1);
bCont.onChange(interlace);
            
          
!
999px
Loading ..................

Console