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

              
                <pre id="info"></pre>
<div id='map' style='width: 100%; height: 300px; cursor: default!important'>
</div>
<div class="container">
  
  <div id="range-wrap">
  <input type="range" id="range" min="1" max="500">
  <output id="bubble"></output>
</div><button id="new-circle" onclick="newCircleButton()">Create New</button>
</div>

<div id="circle-container">
</div>
              
            
!

CSS

              
                #temp{
  width:100%;
   height: 100%;
}
#info {
display: block;
position: relative;
margin: 0px auto;
width: 50%;
padding: 10px;
border: none;
border-radius: 3px;
font-size: 12px;
text-align: center;
color: #222;
background: #fff;
}

/* slider */
#range-wrap {
  position: relative;
  margin: 20px;
  width:100%;
}
#range {
  width: 100%;
}
#bubble {
  background: rgba(0,0,0,0.8);
  color: white;
  padding: 4px 12px;
  position: absolute;
  border-radius: 4px;
  left: 50%;
  transform: translateX(-50%);
}

/* Flexbox */
#circle-container{
  display: flex;
  align-items:center;
  flex-wrap: wrap;
}
.container{
  display: flex;
  align-items:center;
}
.circle{
  border-radius: 50%;
}
#new-circle{
  width:110px;
}
              
            
!

JS

              
                const range = document.getElementById("range");
const bubble = document.getElementById("bubble");
var circleSize = [];
var colours = [];
var circleLoc = [];
var selected = 0;
var count = -1;
var create = false;
var edit = false;
var curCol = randomColour();

setBubble();

// create new token on mapbox if forked please
mapboxgl.accessToken =
	"pk.eyJ1IjoibG9zYm9ybmUyNCIsImEiOiJja2JobXkxZ2EwNnd2MnFsOW1iNnlqOXZlIn0.TtE1fQPKSLLj8KJ0yRXzlw";
var map = new mapboxgl.Map({
	container: "map",
	style: "mapbox://styles/mapbox/streets-v11", // stylesheet location
	center: [-74.5, 40], // starting position [lng, lat]
	zoom: 5 // starting zoom
});

map.on("click", function (e) {
	var ll = e.lngLat;
	var center = [ll.lng, ll.lat];
	var radius = range.value;
	var options = {
		steps: 128,
		units: "kilometers",
		properties: { foo: "bar" }
	};
	if (create) {
		circleLoc.push(center);
		// create new circle on map
		count += 1; // add to count
		var turfCircle = turf.circle(center, radius, options);
		var col = curCol;
		colours.push(curCol);
		circleSize.push(range.value);
		map.addLayer({
			id: "circle-fill-" + count,
			type: "fill",
			source: {
				type: "geojson",
				data: turfCircle
			},
			paint: {
				"fill-color": `rgb(${col[0]},${col[1]},${col[2]})`,
				"fill-opacity": 0.4
			}
		});
		map.addLayer({
			id: "circle-outline-" + count,
			type: "line",
			source: {
				type: "geojson",
				data: turfCircle
			},
			paint: {
				"line-color": `rgb(${col[0]},${col[1]},${col[2]})`,
				"line-opacity": 0.8,
				"line-width": 4
			}
		});
		selected = count;

		// add circle below map
		var icon = document.createElement("div");
		icon.classList.add("circle");
		icon.id = "circle-" + count;
		document.getElementById("circle-container").appendChild(icon);
		icon.addEventListener("click", () => {
			selectCircle(icon.id);
		});
		selectCircle(icon.id);
		//refreshSlider(250, curCol);
	} else if (edit) {
		circleLoc[selected] = center;
		var turfCircle = turf.circle(center, radius, options);
		map.getSource("circle-fill-" + selected).setData(turfCircle);
		map.getSource("circle-outline-" + selected).setData(turfCircle);
	}
});
//color
function randomColour() {
	var rgb = [0, 0, 0];
	for (var i = 0; i < 3; i++) {
		rgb[i] = Math.floor(Math.random() * 200); // not too bright!
	}
	return rgb;
}

//slider
range.addEventListener("input", () => {
	setBubble();
	const circle = document.getElementById("circle-" + selected);
	if (circle != null) {
		const val = range.value; //circleSize[selected];
		const col = colours[selected];
		const size = Number(val / (range.max / 100) + 50);
		circle.style.height = size + "px";
		circle.style.width = size + "px";
		circle.style.backgroundColor = `rgba(${col[0]},${col[1]},${col[2]},0.4)`;
		circleSize[selected] = val;
		if (edit) {
			var options = {
				steps: 128,
				units: "kilometers",
				properties: { foo: "bar" }
			};
			var turfCircle = turf.circle(circleLoc[selected], val, options);
			map.getSource("circle-fill-" + selected).setData(turfCircle);
			map.getSource("circle-outline-" + selected).setData(turfCircle);
		}
	}
});

function setBubble() {
	const val = range.value;
	const min = range.min;
	const max = range.max;
	const newVal = Number(((val - min) * 100) / (max - min));
	bubble.innerHTML = val + "km";
	bubble.style.left = `calc(${newVal}% + (${8 - newVal * 0.15}px))`;
}

function refreshSlider(val, col) {
	bubble.style.backgroundColor = `rgba(${col[0]},${col[1]},${col[2]},0.8)`;
		range.style.backgroundColor = `rgba(${col[0]},${col[1]},${col[2]},0.8)`;

	const min = range.min;
	const max = range.max;
	const newVal = Number(((val - min) * 100) / (max - min));
	bubble.innerHTML = val + "km";
	bubble.style.left = `calc(${newVal}% + (${8 - newVal * 0.15}px))`;
	range.value = val;
}

function selectCircle(id) {
	document.getElementById("new-circle").innerHTML = "Create New";
	create = false;
	edit = true;
	const circle = document.getElementById(id);
	const lastChar = circle.id.substr(id.length - 1);
	const val = circleSize[lastChar];
	const col = colours[lastChar];
	const size = Number(val / (range.max / 100) + 50);
	circle.style.height = size + "px";
	circle.style.width = size + "px";
	circle.style.backgroundColor = `rgba(${col[0]},${col[1]},${col[2]},0.4)`;
	refreshSlider(val, col);
	selected = lastChar;
}

function newCircleButton() {
	var text = document.getElementById("new-circle").innerHTML;
	if (text == "Create New") {
		document.getElementById("new-circle").innerHTML = "Cancel";
		curCol = randomColour();
		refreshSlider(250, curCol);
		create = true;
		edit = false;
	} else {
		document.getElementById("new-circle").innerHTML = "Create New";
		selected = circleSize.length;
		create = false;
	}
}

              
            
!
999px

Console