Pen Settings



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


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


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.


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.


                <canvas id="canvas" width="200" height="200"></canvas>




                (function() {
	let canvas = document.getElementById("canvas");
	let context = canvas.getContext("2d");

	/*  Initialize variables necessary to run the script.  “allPixels” is an array of 50 randomly-placed 
	pixels on the canvas; “unvisited” is an array of unvisited pixels; “path” is the TSP tour.  */
	let allPixels = [],
		unvisited = [],
		path = [];

	//Returns the distance between two points
	let getDistance = function(p1, p2){
		var a = Math.pow(p2.x-p1.x,2);
		var b = Math.pow(p2.y-p1.y,2);
		return Math.sqrt((a+b));

	/*  Returns the given pixel’s nearest neighbor.  Accepts a pixel and an array of unvisited pixels.  */
	let getNearestNeighbor = function(pixel, unvisited){
		var shortestDistance = Infinity;

		//initialize the nearest pixel (the return value) and the nearest index
		var nearest = unvisited[0];
		var nearestIndex = 0;

		for(var i=0; i<unvisited.length; i++){
			if((pixel.x != unvisited[i].x) 
				&& (pixel.y != unvisited[i].y)){	//make sure they're not the same pixel
				var d = getDistance(pixel, unvisited[i]);
	    		if(d <= shortestDistance){
	    			shortestDistance = d;
	    			nearest = unvisited[i];
	    			nearestIndex = i;
		//remove this pixel from the “unvisited” array since it's now been visited
		return nearest;

	//Finds the TSP tour and assigns it to the "path" variable
	let getTSPTour = function() {
		var prevPixel = unvisited[0];

		 while(unvisited.length > 0){   	
			var closest = (getNearestNeighbor(prevPixel, unvisited)); 
			prevPixel = closest;

	//Draws the path using nearest neighbors heuristic
	let draw = function(){	
	    for(let i=0; i<path.length; i++){   	
	    	context.moveTo(path[i].x, path[i].y);
			context.lineTo(path[(i+1)%path.length].x, path[(i+1)%path.length].y);

	//Simple Pixel data structure
	function Pixel(x,y) {
		this.x = x;
		this.y = y;

	//Place random points on canvas
	for(let i=0; i<50; i++){
		let x = Math.floor(Math.random() * canvas.width);
		let y = Math.floor(Math.random() * canvas.height);
		let pixel = new Pixel(x,y);

	//Populate the array of unvisited pixels
	for(let i=0; i<allPixels.length; i++){