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.

            
              <h3>Image creation Timelaps</h3> 
<p>This page is a timelaps of the following Codepen: <a target="_blank" href="https://codepen.io/jwillmer/pen/eWdowe">Every day a pixel</a>.</p>
<canvas id="canvas"></canvas>
<canvas id="canvasHidden"></canvas>
<p>The image is now <span id="years">0</span> years old.</p>
            
          
!
            
              #canvasHidden {
  display: none;  
}

#canvas {
  border: 2px solid lightgrey;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
}
            
          
!
            
              // Settings ===================
var renderSpeed = 100;
var imgUrl = "https://jwillmer.de/media/img/2017-04-25-every-day-a-pixel-abstract-painting.jpg";

// Code =======================
var years = document.getElementById('years');
var img = new Image;
img.onload = function() {
  var canvas = getEmptyCanvas(img.width, img.height);
  var context = canvas.getContext('2d');    
  var hiddenCanvas = setHiddenCanvas(img);  
  var hiddenContext = hiddenCanvas.getContext('2d');
  render(context, hiddenContext);  
}
img.crossOrigin="anonymous";
img.src = imgUrl;

function render(context, hiddenContext) {
    var maxWidth = hiddenContext.canvas.width;
    var maxHeight = hiddenContext.canvas.height;
    var maxPixels = maxWidth * maxHeight;    
    var renderedPixels = 0;
    createPixelMap(hiddenContext);
  
    var timer = setInterval(function() {        
      if(renderedPixels < maxPixels){   
        var pixelsLeft = maxPixels - renderedPixels;
        renderSpeed = pixelsLeft > renderSpeed ? renderSpeed : pixelsLeft;
        renderPixels(context, hiddenContext, renderSpeed);  
        renderedPixels += renderSpeed;
        updateAge(renderedPixels);
      } else {
        clearInterval(timer);
      }   
      // Break for development
      // clearInterval(timer);
    }, 10);
}

function updateAge(pixelCount){
    years.innerHTML = Math.floor(pixelCount/365);
}

function renderPixels(context, hiddenContext, amount) {
  for(var n = 0; n < amount; n++){
    renderPixel(context, hiddenContext);
  }
}

function renderPixel(context, hiddenContext) {
  var coordinate = randomEmptyCoordinate(context, hiddenContext);
  var imageData = hiddenContext.getImageData(coordinate[0], coordinate[1], 1, 1);
  context.putImageData(imageData, coordinate[0], coordinate[1]);
}

var pixelMap;
function createPixelMap(context){
    var width = context.canvas.width;
    var height = context.canvas.height;
    var pixels = width * height;  
    pixelMap = Array.from(Array(pixels).keys());
}

function pixelMapNumberToCoordinate(width, number) {  
  var y = Math.floor(number/width);
  var x = number % width;
  return [x, y];
}

function randomEmptyCoordinate(context, hiddenContext) {
  var width = context.canvas.width;
  var length = pixelMap.length; 
  var position = Math.floor(Math.random() * length + 1); 
  var coordinate = pixelMapNumberToCoordinate(width, pixelMap[position]);
  pixelMap.splice(position, 1);
  return coordinate;
}

function getEmptyCanvas(width, height) {
   var canvas = document.getElementById('canvas');
  canvas.width = width;
  canvas.height = height;
  return canvas;
}

function setHiddenCanvas(img) {
  var canvas = document.getElementById('canvasHidden');
  canvas.width = img.width;
  canvas.height = img.height;
  var ctx = canvas.getContext('2d');
  ctx.drawImage(img, 0, 0, img.width, img.height);
  return canvas;  
}
            
          
!
999px
Loading ..................

Console