Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<div class="buttons">
	<h1>The Game Of Life</h1>
	<button  class="btn" id="stop">Stop</button>
	<button  class="btn" id="step">Step</button>
	<button  class="btn" id="resume">Resume</button>


	<p class="info">Generations: <span class="value" id="generations"></span></p>
	<p class="info">Births:<span class="value" id="births"></span></p>
	<p class="info">Living:<span class="value" id="living"></span></p>
	<p class="info">Deaths:<span class="value" id="deaths"></span></p>
</div>
<br />
<canvas id="c">

</canvas>


              
            
!

CSS

              
                body {
	background:#222;
	color:#fff;
	margin: 0;
	float:none;
	padding: 0;
	line-height:12px;
	letter-spacing:0.1em;
	height:100%;
	width:100%;
	font-family: 'Electrolize', sans-serif;

}

h1 {
	text-align:center;
	color:#fff;
	font-size:20px;
}

.buttons {
	text-align:left;
	top:0px;
	z-index:1000;
	position:absolute;	
	padding:20px 40px;
	background: rgba(80, 80, 80, 0.9);
	border-radius: 0px 0px 10px 0px;
}

.btn {
	background: #555;
	color:#fff;
	padding: 4px 16px;
	border: 1px solid #aaa;
}

.btn:hover {

	background: rgba(52, 73, 94, 0.89);
	border: 1px solid #ccc;
	color:#fff;

}

.info {

	width:100%;

}
.value {
	text-align:right;
	float: right;
}

.green {
	color: #0fa;
}

.almost  {
	color: #FFAF00;
}
canvas {
	margin:0;
	top:0px;
	position:absolute;	
	z-index:1;
	float:none;
	border:none;
	border-radius:0px;
  background:#fff;

}


              
            
!

JS

              
                $( document ).ready( function() {

	// Force hack canvas object
	//
	var innerHeight = window.innerHeight;

	$("#c").attr("width", window.innerWidth);
	$("#c").attr("height", innerHeight);

	var generations = 0;

	// global (a)live count to determine of evolution has ended
	var alive = 0;
	var no_change = 0;
	// evo_gap : after how many generations has the evolution 
	// obviously stopped
	const evo_gap = 40;

	const cell_size = 6;
	var BOARD_SIZE = Math.floor( $("#c").width() / cell_size );
	var BOARD_HEIGHT = Math.floor( ($("#c").height()) / cell_size );

	var drawTimer;
	startTimer();

	function startTimer() {
		drawTimer =	setInterval( function() { cycle(); }, 1);
	}

 	//
	// controls
	//
	$("#stop").click( function(){ 
		
		clearInterval( drawTimer );

	});
	$("#step").click( function(){ cycle(); });
	$("#resume").click( function(){ startTimer(); });


	//
	// seed
	//
	var creation = new Array(BOARD_SIZE * BOARD_HEIGHT);
	var nextStep = new Array(BOARD_SIZE * BOARD_HEIGHT);


		for (var i = 0; i < creation.length; i++) {

			creation[i] = Math.round(Math.random());
		};


	function cycle() {

		var lifeCount = 0;
		
		// statistics
		var deaths = 0;
		var living = 0;
		var births = 0;

		for (var i = 0; i < creation.length; i++) {

			lifeCount = 0;
			//
			// count live and make world without
			// borders
			//
			if ( i < BOARD_SIZE) {

				var p = BOARD_SIZE * BOARD_HEIGHT - i;

				lifeCount = creation[p-1] ;
				lifeCount += creation[p-BOARD_SIZE] ;
				lifeCount += creation[  p- ( BOARD_SIZE  - 1 )] ;
				lifeCount += creation[  p- ( BOARD_SIZE  + 1 )] ;
				lifeCount += creation[i + 1] ;
				lifeCount += creation[  i + BOARD_SIZE ] ;
				lifeCount += creation[  i + ( BOARD_SIZE  - 1 )] ;
				lifeCount += creation[  i + ( BOARD_SIZE  + 1) ];
			
			} else if ( i > BOARD_SIZE * BOARD_HEIGHT - BOARD_SIZE ) {

				var p = i - BOARD_SIZE * BOARD_HEIGHT + BOARD_SIZE;

				lifeCount = creation[i-1] ; 
				lifeCount += creation[i-BOARD_SIZE] ;
				lifeCount += creation[  i- ( BOARD_SIZE  - 1 )] ;
				lifeCount += creation[  i- ( BOARD_SIZE  + 1 )] ;
				lifeCount += creation[i + 1] ;
				lifeCount += creation[  p + BOARD_SIZE ] ;
				lifeCount += creation[  p + ( BOARD_SIZE  - 1 )] ;
				lifeCount += creation[  p + ( BOARD_SIZE  + 1) ];
      } else {
				lifeCount = creation[i-1] ;
				lifeCount += creation[i-BOARD_SIZE] ;
				lifeCount += creation[  i- ( BOARD_SIZE  - 1 )] ;
				lifeCount += creation[  i- ( BOARD_SIZE  + 1 )] ;
				lifeCount += creation[i + 1] ;
				lifeCount += creation[  i + BOARD_SIZE ] ;
				lifeCount += creation[  i + ( BOARD_SIZE  - 1 )] ;
				lifeCount += creation[  i + ( BOARD_SIZE  + 1) ];

			}
				// 1 == true 
			if ( creation[i] ) {
				if ( lifeCount < 2 || lifeCount > 3) {
						nextStep[i] = 0;
						deaths += 1;
					}
				else if ( lifeCount == 2 || lifeCount == 3 ) {
						nextStep[i] = 1;
						living += 1;
					}
			} else {
				if ( lifeCount == 3 ) {
					nextStep[i] = 1;
					births += 1;
				}  else {
					// by default dead cells stay dead	
					// basically copy this to the next 
					// genration
					nextStep[i] = 0;
					}
				}
		}

		drawBoard(nextStep);

		// copy current state of array
		generations += 1;
		creation = nextStep.slice();

		$("#generations").text(generations);
		$("#births").text(births);
		$("#living").text(living);
		$("#deaths").text(deaths);

		if ( alive == living ) {
			
			no_change += 1;

			$("#living").addClass("almost");
			if ( no_change >= evo_gap ) {
			
				clearInterval( drawTimer );

				$("#generations").text(generations - evo_gap);
				$("#living").removeClass("almost");
				$("#living, #generations").addClass("green");

			}
			
		} else {
			// set global live count to current
			alive = living;
			// reset
			no_change = 0;		
			$("#living").removeClass("almost");
		}
	}


	function drawBoard(cells) {
		var canvas = document.getElementById("c");
		var context = canvas.getContext("2d");
		var x = 0;
		var y = 0;
		var p = 0; // piece count
		var c = 0;
	   	for	(var index = 0; index < cells.length; index++) {

			if (cells[index] == 1) {
				context.fillStyle = "#ffffff";
				context.fillRect(x, y, cell_size,cell_size);

				
			} else { 
				if ( y > BOARD_HEIGHT * cell_size - 400 ) {
					context.fillStyle = "rgb(0, 0, " + c + ")";
				} else {
					context.fillStyle = "rgb(0, 0, 0)";

				}

				context.fillRect(x, y, cell_size,cell_size); 
				}
			x += cell_size;
			p += 1; // next piece
			if (p == BOARD_SIZE) {
				// start next line
				y += cell_size;
				x = 0;
				p = 0;
				if ( c <= 255 && y > BOARD_HEIGHT * cell_size - 400  ) {
					c += 1;
				}
			} 
		}
	}

});

              
            
!
999px

Console