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

              
                // Still WIP
canvas
              
            
!

CSS

              
                html {
	background-color: #282228;
	height: 100%;
}

body {
	display: flex;
	height: 100%;
	flex-direction: column;
	justify-content: center;
}

canvas {
	align-self: center;
	border-radius: 0.5rem;
}
              
            
!

JS

              
                // User Vars
var settings = {
	grid: {
		columns: 5,
		rows: 5,
		padding: 4
	},
	cell: {
		size: 40,
		spawnAliveOdds: 0.20,
		isAliveColor: 'white',
		isDeadColor: 'rgba(255, 255, 255, 0.25)'
	}
}

var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');

// Define Canvas Size
canvas.width = ((settings.cell.size + settings.grid.padding) * settings.grid.columns) - settings.grid.padding;
canvas.height = ((settings.cell.size + settings.grid.padding) * settings.grid.rows) - settings.grid.padding;

var cells = [];

function Cell(x, y, id) {
	
	this.id = id;
	this.x = x;
	this.y = y;
	this.isAlive = Math.random() > (1 - settings.cell.spawnAliveOdds) ? true : false;
	this.neighbors;
	
	this.update = () => {
		this.generation();
		this.draw();
	}
	
	this.generation = () => {
		let isOnTop = this.id < settings.grid.columns ? true : false;
		let isOnBottom = this.id >= ((settings.grid.columns - 1) * settings.grid.rows) ? true : false;
		let isOnLeft = this.id % settings.grid.columns === 0 ? true : false;
		let isOnRight = (this.id + 1) % settings.grid.columns === 0 ? true : false;
		let neighbors = 0;
		
		// top left
		// if (!isOnTop && !isOnLeft && cells[this.findNeighborXY('up','left')].isAlive) neighbors++;
		// top middle
		// if (!isOnTop && cells[this.findNeighborXY('up')].isAlive) neighbors++;
		// top right
		// if (!isOnTop && !isOnRight && cells[this.findNeighborXY('up','right')].isAlive) neighbors++;
		// left
		// if (!isOnLeft && cells[this.findNeighborXY('left')].isAlive) neighbors++;
		// right
		// if (!isOnRight && cells[this.findNeighborXY('right')].isAlive) neighbors++;
		// bottom left?
		// if (!isOnBottom && !isOnLeft && cells[this.findNeighborXY('bottom','left')].isAlive) neighbors++;
		// bottom middle?
		if (!isOnBottom && cells[this.findNeighborXY('bottom')].isAlive) neighbors++;
		// bottom right
		// if (!isOnBottom && !isOnRight && cells[this.findNeighborXY('bottom','right')].isAlive) neighbors++;
		
		this.neighbors = neighbors;
	}
	
	this.findNeighborXY = (a, b) => {
		// Pass in the current id and 'up' 'down' 'left' 'right' as strings
		a = a === undefined ? null : a;
		b = b === undefined ? null : b;
		let offset = this.id;

		if (a === 'up' || b === 'up') offset += -settings.grid.columns;
		if (a === 'down' || b === 'down') offset += settings.grid.columns;
		if (a === 'left' || b === 'left') offset--;
		if (a === 'right' || b === 'right') offset++;

		return offset;
	}
	
	this.draw = () => {
		ctx.fillStyle = this.isAlive ? settings.cell.isAliveColor : settings.cell.isDeadColor;
		ctx.fillRect(this.x, this.y, settings.cell.size, settings.cell.size);
		ctx.fillStyle = '#282228';
		ctx.font = '16px sans-serif';
		ctx.fillText(this.id.toString(), this.x + 5, this.y + 16);
	}
}

function init() {
	let cellCounter = 0;
	
	// Build first pass for the cells
	for (let x = 0; x < settings.grid.columns; x++) {
		for (let y = 0; y < settings.grid.rows; y++) {
			let cellX = y * (settings.cell.size + settings.grid.padding);
			let cellY = x * (settings.cell.size + settings.grid.padding);
			let cellID = cellCounter++;
			cells.push(new Cell(cellX, cellY, cellID));
		}
	}
	
	// Get data for first generation
	let consoleGrid = [];
	for(let i = 0; i < cells.length; i++) {
		cells[i].generation();
		cells[i].draw();
		consoleGrid.push(cells[i].neighbors);
	}
	
	// Log Neighbors to console
	let consoleLine = '\n';
	for(let i = 0; i < consoleGrid.length; i++) {
		consoleLine += consoleGrid[i];
		if((i + 1) % settings.grid.columns === 0) consoleLine += '\n';
	}
	console.log(consoleLine);
}

var fps = 1;
var now;
var then = Date.now();
var interval = 1000/fps;
var delta;

function animate() {
	
	window.requestAnimationFrame(animate);
	
	now = Date.now();
	delta = now - then;

	if (delta > interval) {
		then = now - (delta % interval);

		ctx.fillStyle = '#282228';
		ctx.fillRect(0, 0, canvas.width, canvas.height);

		for(let i = 0; i < cells.length; i++) {
			cells[i].update();
		}
	}

}

init();
animate();
// lol
              
            
!
999px

Console