Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                let width, height, cols, rows, cellWidth, current, grid = [],stack = [],
above = 0, right = 1, bottom = 2, left = 3;

function setup(){
	width = 600;
	cellWidth = 30;
	height = width;
	cols = floor(width/cellWidth);
	rows = floor(height/cellWidth);
	frameRate(15)
	
	for( let r = 0; r < rows; r++){
		for( let c = 0; c < cols; c++){
			let cell = new Cell(c,r);
			grid.push(cell);
		}
	}
	createCanvas(width,height)
	// Make the initial cell and mark it as visited
	current = grid[0];
}

// While there are unvisited cells:
function draw(){
	background(51);
	
	// Draws the grid
	grid.forEach((cell, pos) => {
		cell.show();
	})
	current.visited = true;
	current.highlight();
	
	// Choose randomly one of the unvisited neighbours
	let next = current.checkNeighbours();
	if(next){
		//mark it as visited
		next.visited = true;
		
		// Push the current cell to the stack
		stack.push(current);
		
		// Remove the wall between the current cell and the chosen cell
		removeWalls(current, next);

		// Make the chosen cell the current cell
		current = next;
	}else if(stack.length){
		//else if stack is not empty
		current = stack.pop();
	}
}
// Return a valid index given and x and a y
function index(col, row){
	if(col < 0 || row < 0 || col > cols - 1 || row > rows - 1){
		return -1;
	}
	let index = col + ( row * cols );
	return index;
}
function Cell(col,row){
	this.col = col;
	this.row = row;
	this.w = cellWidth
	this.x = col * this.w;
	this.y = row * this.w;
	// top, right, bottom, left
	this.walls = [true,true,true,true];
	this.visited = false;
	this.show = () => {
		let x = this.x,
				y = this.y,
				w = this.w;
				
		this.plot(x,y,w);
	}	
	// draws the lines for the grid
	this.plot = (x,y,w) => {
		let	corners = [[x,y],[x+w,y],[x+w,y+w], [x,y+w]];
		stroke(255);
		corners.forEach((corner, pos) => {
			let next = pos + 1;
			if(pos == corners.length - 1){
				next = 0;
			}
			if(this.walls[pos]){
				line(corner[0], corner[1], corners[next][0],corners[next][1])
			}
		})
		
		if(this.visited){
			noStroke()
			fill(255,0,255, 100)
			rect(x,y,w,w)
		}
	}
	this.checkNeighbours = () => {
		let notVisited = [],
				neighbours = [
					grid[index(this.col, this.row - 1)],
					grid[index(this.col + 1, this.row)],
					grid[index(this.col, this.row + 1)],
					grid[index(this.col - 1, this.row)]	
				]
		neighbours.forEach( neighbour => {
			if(neighbour && !neighbour.visited){
				notVisited.push(neighbour)
			}
		})
		if(notVisited.length > 0){
			let n = floor(random(0, notVisited.length))
			return notVisited[n];
		}else{
			return null;
		}
	}
	// highlights the current cell
	this.highlight = () => {
		let x = this.x,
				y = this.y,
				w = this.w;
		noStroke();
		fill(0,0,255,100);
		rect(x,y,w,w);
	}
}

function removeWalls(a,b){
		let x = a.col - b.col;
		if( x === -1 ){
			a.walls[right] = false;
			b.walls[left] = false;
		}else if( x === 1 ){
			a.walls[left] = false;
			b.walls[right] = false;
		}
		let y = a.row - b.row;
		if( y === -1 ){
			a.walls[bottom] = false;
			b.walls[above] = false;
		}else if( y === 1 ){
			a.walls[above] = false;
			b.walls[bottom] = false;
		}
}
              
            
!
999px

Console