cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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.

            
              <canvas id="canvas1"></canvas>
<canvas id="canvas2"></canvas>
            
          
!
            
              body, html {
  background-color: lightgrey;
  margin: 0;
  padding: 0;
  text-align: center;
}

canvas {
  -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.24), 0 1px 5px rgba(0, 0, 0, 0.05);
  -moz-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.24), 0 1px 5px rgba(0, 0, 0, 0.05);
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.24), 0 1px 5px rgba(0, 0, 0, 0.05);
  display: inline-block;
  margin: 30px;
}
            
          
!
            
              var minimalmonkey = minimalmonkey || {};

minimalmonkey.MatrixChallenge = (function() {

	var MATRIX_SIZE = 15;
	var BLOCK_SIZE = 15;
  var DELAY_INTERVAL = 100;
  
  var matrix;
  var context;
  var delay;

	var init = function(matrixSize, blockSize) {
		if(matrixSize) MATRIX_SIZE = matrixSize;
		if(blockSize) BLOCK_SIZE = blockSize;

		matrix = generateRandomMatrix(MATRIX_SIZE, setCanvas('canvas1', MATRIX_SIZE * BLOCK_SIZE));
    
    context = setCanvas('canvas2', MATRIX_SIZE * BLOCK_SIZE);
    delay = 0;

		for(var rows = 0; rows < MATRIX_SIZE; ++rows) {
			for(var cols = 0; cols < MATRIX_SIZE; ++cols) {
				if(matrix[rows][cols] === 1) {
					var color = matrix[rows][cols] = randomColor();
          setTimeout(drawBlock, delay * DELAY_INTERVAL, context, cols, rows, color);
          ++delay;
					checkNeighbours(rows, cols, color);
				}
			}
		}
	};
  
  var checkNeighbours = function(rows, cols, color) {
    if(rows-1 >= 0 && matrix[rows-1][cols] === 1) {
      matrix[rows-1][cols] = color;
      setTimeout(drawBlock, delay * DELAY_INTERVAL, context, cols, rows-1, color);
      ++delay;
      checkNeighbours(rows-1, cols, color);
    }
    if(rows+1 < MATRIX_SIZE && matrix[rows+1][cols] === 1) {
      matrix[rows+1][cols] = color;
      setTimeout(drawBlock, delay * DELAY_INTERVAL, context, cols, rows+1, color);
      ++delay;
      checkNeighbours(rows+1, cols, color);
    }
    if(cols-1 >= 0 && matrix[rows][cols-1] === 1) {
      matrix[rows][cols-1] = color;
      setTimeout(drawBlock, delay * DELAY_INTERVAL, context, cols-1, rows, color);
      ++delay;
      checkNeighbours(rows, cols-1, color);
    }
    if(cols+1 < MATRIX_SIZE && matrix[rows][cols+1] === 1) {
      matrix[rows][cols+1] = color;
      setTimeout(drawBlock, delay * DELAY_INTERVAL, context, cols+1, rows, color);
      ++delay;
      checkNeighbours(rows, cols+1, color);
    }
  };
  
  var drawBlock = function(context, x, y, color) {
    context.fillStyle = color;
    context.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
  };

	var setCanvas = function(id, size) {
		var canvas = document.getElementById(id);
		canvas.width = size;
		canvas.height = size;
    var context = canvas.getContext('2d');
    context.fillStyle = 'white';
    context.fillRect(0, 0, size, size);
		return context;
	};

	var generateRandomMatrix = function(size, context) {
		var rows = size;
		var cols;
		var m = [];
		while(--rows > -1) {
			m[rows] = [];
			cols = size;
			while(--cols > -1) {
				m[rows][cols] = Math.round(Math.random());
				context.fillStyle = m[rows][cols] === 0 ? 'white' : 'black';
				context.fillRect(cols * BLOCK_SIZE, rows * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
			}
		}
		return m;
	};

	var randomColor = function() {
		var r = Math.round(Math.random() * 200);
		var g = Math.round(Math.random() * 200);
		var b = Math.round(Math.random() * 200);
		return 'rgb(' + r + ',' + g + ',' + b + ')';
	};

	return {
		'init': init
	};
});

new minimalmonkey.MatrixChallenge().init();
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console