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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <html>
  <body>
<canvas id="golCanvas">Go and look for a new browser.</canvas>
  </body>
</html>
              
            
!

CSS

              
                * {margin:0;}

body { 
  background: rgb(37,38,30);
  height: 100%;
}

              
            
!

JS

              
                	var canvas = document.getElementById("golCanvas");
	canvas.width = $(document).width();
	canvas.height = $(document).height();
	var WIDTH = canvas.width;
	var HEIGHT = canvas.height;
	var ctx = canvas.getContext("2d");
	var LEN = 10;
	var x = Math.floor(WIDTH/LEN);
	var y = HEIGHT/LEN;
	var myGol;
	var golTmp;
			
	function initTmp(){
		for(var xVal = 0; xVal<=x+2;xVal++){
	    golTmp[xVal] = new Array();
		    for(var yVal = 0; yVal<=y+2; yVal++){
			    golTmp[xVal][yVal] = 0;
		    }
	    }	
	}	
	
	function initMatrix(){
		// reset matrix
		myGol = new Array();
		golTmp = new Array();
	
	    for(var xVal = 0; xVal<=x+2;xVal++){
	    	myGol[xVal] = new Array();
	    	golTmp[xVal] = new Array();
		    for(var yVal = 0; yVal<=y+2; yVal++){
			    golTmp[xVal][yVal] = 0;
			    var randVal = Math.floor((Math.random()*2));
			    myGol[xVal][yVal] = randVal;
				if (randVal == 1){
					draw(xVal+1,yVal+1)
				}
		    }
	    }
	
	    
	}
	
	function draw(x,y){
		ctx.fillRect(LEN*(x-1),LEN*(y-1),LEN,LEN);
	}
	
	function nextStep(){
		// reset tempArray
		initTmp();
		// reset canvas
		ctx.fillStyle = "rgb(37,38,30)";
		ctx.fillRect(0,0,WIDTH,HEIGHT);

	    for(var xVal = 1; xVal<=x+1;xVal++){
		    for(var yVal = 1; yVal<=y+1; yVal++){
			    var neighbourSum = myGol[xVal-1][yVal] + myGol[xVal-1][yVal-1] + myGol[xVal-1][yVal+1] + myGol[xVal][yVal-1] + myGol[xVal][yVal+1] + myGol[xVal+1][yVal] + myGol[xVal+1][yVal+1] + myGol[xVal+1][yVal-1];
			    if(myGol[xVal][yVal] == 1){
				    if(neighbourSum == 2 || neighbourSum == 3){
					    golTmp[xVal][yVal] = 1;
					    ctx.fillStyle = "rgb(166,109,3)";
					    draw(xVal,yVal);
				    }
				} else {
					if(neighbourSum == 3) {
						golTmp[xVal][yVal] = 1;
						ctx.fillStyle = "rgb(191,144,4)";
						draw(xVal,yVal);
					}
				}
		    }
	    }
	    
	    myGol = golTmp.slice();
	    
	}

	initMatrix();
	setInterval(nextStep, 50);
              
            
!
999px

Console