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

              
                
              
            
!

CSS

              
                body{
	margin: 0;
	display: flex;
	width: 100vw;
	height: 100vh;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

.cells{
	width: 30px;
	height: 30px;
	border-bottom: 1px solid rgba(0,0,0,0.2);
	border-right: 1px solid rgba(0,0,0,0.2);
	background: white;
}

.rows:first-of-type{
	border-top: 1px solid rgba(0,0,0,0.2);
}

.rows .cells:first-of-type{
	border-left: 1px solid rgba(0,0,0,0.2);
}

.rows{
	display: flex;
}


/*Different Colors*/

.selected{
	/*Cell that is clicked (TARGET SELECTED)*/
	background-color: blue;
}

.fastest{
	/*Fastest animation color*/
	background-color: rgb(3, 12, 138);
}

.blue{
	/*Fast animation color*/
	background-color: blue;
}

.path{
	/*Slow animation color*/
	background-color: rgb(59, 186, 245);
}

.target{
	/*Color when target cell is found (TARGET ACHIEVED!)*/
	background-color: rgb(14, 12, 84);
}

.showPath{
	/*FINAL PATH COLOR*/
	background-color: rgb(14, 12, 84);
}

.hover{
	/*CELLS HOVER EFFECT*/
	background-color: rgba(87, 218, 247, 0.4);
}
              
            
!

JS

              
                function generateRows(num){
	return '<div class="cells"></div>'.repeat(num);
}
let nrows = 22;
let ncols = 48;
document.body.innerHTML = ('<div class="rows">' + generateRows(ncols) + '</div>').repeat(nrows);

let cells = document.querySelectorAll(".cells");
let isPlay = true;

for (let i=0; i<cells.length; i++){
	cells[i].addEventListener("click", function(){
		if (isPlay){
			for (let j=0; j<cells.length; j++){
				cells[j].classList.remove("selected");
				cells[j].classList.remove("path");
				cells[j].classList.remove("target");
				cells[j].classList.remove("hover");
				cells[j].classList.remove("showPath");
				cells[j].classList.remove("blue");
				cells[j].classList.remove("fastest");
			}
			this.classList.add("selected");
			BFS(i);
		}
	});
}

for (let i=0; i<cells.length; i++){
	cells[i].addEventListener("mouseover", function(){
		if (isPlay)
			cells[i].classList.add("hover");
	});
	cells[i].addEventListener("mouseout", function(){
		cells[i].classList.remove("hover");
	});
}


function BFS(target){
	let start = (Math.floor(nrows/2) * ncols) + Math.floor((ncols)/2);

	isPlay = false;

	let visited = {};
	visited[start] = true;

	let parent = {}
	parent[start] = -1;

	let queue = [start];
	let delay = 200;

	while (queue.length != 0){
		let head = queue.shift();

		if (head==target){
			(function(headCopy,delayCopy){
				setTimeout(function(){
					cells[headCopy].classList.add("target");
					displayPath(target, parent);
				},delayCopy);
			})(head, delay);
			break;
		}

		(function(headCopy,delayCopy){
			setTimeout(function(){
				cells[headCopy].classList.add("fastest");
			},delayCopy);
		})(head, delay-180);

		(function(headCopy,delayCopy){
			setTimeout(function(){
				cells[headCopy].classList.add("blue");
			},delayCopy);
		})(head, delay-80);

		(function(headCopy,delayCopy){
			setTimeout(function(){
				cells[headCopy].classList.add("path");
			},delayCopy);
		})(head, delay);

		delay += 5;

		if ((head%ncols)>0 && !((head-1) in visited)){
		//Left Cell
		queue.push(head-1);
		visited[head-1] = true;
		parent[head-1] = head;
		}


		if ((head%ncols)<(ncols-1) && !((head+1) in visited)){
		//Right Cell
		queue.push(head+1);
		visited[head+1] = true;
		parent[head+1] = head;
		}


		if ( (Math.floor(head/ncols))>0 && !((head-ncols) in visited) ) {
		//Top Cell
		queue.push(head-ncols);
		visited[head-ncols] = true;
		parent[head-ncols] = head;
		}


		if ( (Math.floor(head/ncols))<nrows-1 && !((head+ncols) in visited) ){
		//Bottom Cell
		queue.push(head+ncols);
		visited[head+ncols] = true;
		parent[head+ncols] = head;
		}

	}
}

function displayPath(tar, par){
	let delay = 0;
	let temp = tar;

	while(temp != -1){
		delay += 100;
		temp = par[temp];
	}

	setTimeout(function(){
		isPlay = true;
	},delay+2);

	while (tar != -1){
		(function(tarCopy, delayCopy){
			setTimeout(function(){
				cells[tarCopy].classList.add("showPath");
			},delayCopy);
		})(tar, delay);
		delay -= 100;
		tar = par[tar];
	}

}

              
            
!
999px

Console