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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <h3>Image creation over time</h3> 
<p>This page is painting a picture pixel by pixel. The idea is that every day <span id="pixelPerDay"></span> pixel will be added and at some point the image reveals. You can read the <a target="_blank" href="">blog post</a> for more details.</p>
<canvas id="canvas"></canvas>
<canvas id="canvasHidden"></canvas>
<p>The image was started to paint on <span id="startDate"></span></p>
              #canvasHidden {
  display: none;  

#canvas {
  border: 2px solid lightgrey;

body {
  display: flex;
  flex-direction: column;
  align-items: center;
              // Settings ===================
var pixelPerDay = 1;
var imgUrl = "";
var startDate = new Date('2000-01-01');

// Code =======================
document.getElementById('pixelPerDay').innerHTML = pixelPerDay;
document.getElementById('startDate').innerHTML = startDate.toLocaleDateString();

var renderSpeed = 86400000; // 1 day
var today =;
var dayDifference = Math.floor((today - startDate) / (1000*60*60*24))
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.src = imgUrl;

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

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]);

function randomEmptyCoordinate(context, hiddenContext) {
    var maxWidth = hiddenContext.canvas.width;
    var maxHeight = hiddenContext.canvas.height;  
    var x = Math.floor(Math.random() * maxWidth + 1); 
    var y = Math.floor(Math.random() * maxHeight + 1);   
    if(!isNewImageData(x, y)){
      return randomEmptyCoordinate(context, hiddenContext);
    return [x, y];

var imageDataArray = [];
function setUsedCoordinate(item) {

function isNewImageData(x,y) {
  return !isItemInArray(imageDataArray, [x,y]);  

function isItemInArray(array, item) {
    for (var i = 0; i < array.length; i++) {
        if (array[i][0] == item[0] && array[i][1] == item[1]) {
            return true;
    return false;

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;  
🕑 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.