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. 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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="dashboard">
	<button class="dashboard__regererate" id="regenerate">Regenerate</button> <label for="noOfPoints">Points</label>
	<input type="range" id="noOfPoints" min="11" max="300" step="2" value="100"  />
<label for="polygonEdges">Edges</label>
	<input type="range" id="polygonEdges" min="4" max="20" step="1" value="10"  />
	<label for="showCircle">Show Circle</label><input type="checkbox"  id="showCircle" />
	
	<label for="edgeWidth">Edge width</label>
	<input type="range" id="edgeWidth" min="0" max="4" step="1" value="1"  />
	<label for="ctrlSpeed" title="Speed">Animate</label><input type="checkbox"  id="doAnimate" title="Animate?" />
	<input type="range" id="ctrlSpeed" min=".2" max="6" step=".2" value="2" title="Speed"  />
	<input type="checkbox" id="colorfull" />
		<button id="getSVG">Get SVG</button>
</div>
<svg width="3000" height="3000">
	<text x="0" y="50px">Generated using Pen on CodePen (https://codepen.io/netsi1964/full/PZJEQe/) which was made by Sten Hougaard, Jan. 2016,  @netsi1964</text>
		<rect id="bg" x="0" y="0" width="1478.86" height="522.414" style="fill:rgb(0,0,0);"/>
	<g id="main">

	<g id="polygons"></g>
	<g id="points"></g>
	</g>

</svg>

            
          
!
            
              * {
  transition: all .4s 0.2s;
}

*:hover {
  transition: all .4s 0;
}
html {
	height: 100%;
	width: 100%;
	padding: 0;
	margin: 0;
}
body {
	width: 100%;
	height: 100%;
  margin: 0;
  padding: 0;
  background: #111;
}

svg {
  background: #000;
}

.dashboard {
  padding: 0 5px;
  text-transform: uppercase;
  font-weight: 200;
  letter-spacing: 2px;
	width: 100%;
}
.dashboard__regererate {
  padding: 4px;
  border-radius: 2px;
  border: 0 solid orange;
background: goldenrod;
	color: #333;
}
.dashboard__regererate:hover {
  cursor: pointer;
  opacity: 1;
}
label {
	color: goldenrod;
	font-family: monospace;
	font-size: 1em;
	line-height: 1.25em;
	margin-left: 1em;
	border-left: dotted 1px #999;
	padding-left: 5px;
}
[type="range"] {
	
}
[type="checkbox"] {
	margin: 0 0 0 5px;
	font-size: 3em;
	height: 14px;
}

input[type=range] {
  -webkit-appearance: none;
}

input[type=range]::-webkit-slider-thumb {
	  -webkit-appearance: none;
 background: orange;
  height: 1.25em;
  width: 10px;
  border-radius: 0;
}

input[type=range]:focus {
  outline: none; 
}

input[type=range]::-webkit-slider-runnable-track {
  width: 100%;
  height: 1.25em;
  cursor: pointer;
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #000000;
  background: #555;
  border-radius: 0;
}

            
          
!
            
              var svg = document.querySelector("svg"),
	regenerate = document.getElementById("regenerate"),
	pointGroup = document.getElementById("points"),
	polygons = document.getElementById("polygons"),
	dashboard = document.querySelector(".dashboard"),
	noOfPoints = document.getElementById("noOfPoints"),
	polygonEdges = document.getElementById("polygonEdges"),
	showCircle = document.getElementById("showCircle"),
	edgeWidth = document.getElementById("edgeWidth"),
	btnGetSVG = document.getElementById("getSVG"),
	ctrlSpeed = document.getElementById("ctrlSpeed"),
	doAnimate = document.getElementById("doAnimate"),
	colorfull = document.getElementById("colorfull"),
	body = document.body,
	bg = document.querySelector("#bg"),
	svgNS = svg.namespaceURI,
	points = [],
	themes = [],
	theme = [],
	padding = 20,
	width,
	height,
	rr,
	gg,
	bb;

function setColor() {
	rr = parseInt(Math.random() * 200 + 55);
	gg = parseInt(Math.random() * 200 + 55);
	bb = parseInt(Math.random() * 200 + 55);
	// Disabled Adobe Kuler color
	// var c = theme.swatches[parseInt(Math.random() * theme.swatches.length)];
	// rr = c.r;
	// gg = c.g;
	// bb = c.b;
}

function drawPolygons(pointID, reset) {
	pointID = pointID || 0;
	if (!!reset) {
		polygons.innerHTML = "";
	}
	var poly = [],
		activePoint = points[pointID];
	points.forEach(function(p, i) {
		p.distance = getDistance(activePoint, p);
	});
	var temp = points.map(function(e, i) {
		if (i !== pointID) return e;
	});
	temp.sort(function(a, b) {
		return a.distance > b.distance ? 1 : -1;
	});

	temp = temp.filter(function(e, i) {
		if (i < polygonEdges.value) {
			e.angle = getAngleFromPoint(e, activePoint);
			return e;
		}
	});

	temp.sort(function(a, b) {
		return a.angle > b.angle ? 1 : -1;
	});

	//temp.splice(0,1);

	var d = " ";
	for (var i = 0; i < temp.length; i++) {
		d += i === 0 ? "M " : i === 1 ? "L " : " ";
		d += temp[i].X + " " + temp[i].Y + " ";
	}
	d += " L " + temp[0].X + " " + temp[0].Y + " ";
	var currentPath = d + " Z";
	if (!!colorfull.checked) {
		setColor();
	}
	var polygon = document.createElementNS(svgNS, "path");
	polygon.setAttribute("d", currentPath);
	polygon.setAttribute("stroke", "rgba(" + rr + "," + gg + "," + bb + ",.2)");
	polygon.setAttribute("stroke-width", edgeWidth.value + "px");
	polygon.setAttribute("fill", "rgba(" + rr + "," + gg + "," + bb + ",.2)");
	polygons.appendChild(polygon);
}

function setSVG() {
	width = parseInt(window.getComputedStyle(body).width, 10);
	height = parseInt(window.getComputedStyle(body).height, 10);
	dbh = parseInt(window.getComputedStyle(dashboard).height, 10);
	height -= dbh;
	svg.setAttribute("width", width);
	svg.setAttribute("height", height);
	bg.setAttribute("width", width);
	bg.setAttribute("height", height);
	svg.setAttribute("viewbox", "0 0 " + width + " " + height);
}

function generateNew() {
	delete points;
	points = [];
	for (var i = 0; i < noOfPoints.value; i++) {
		points.push(
			new Point(
				parseInt(Math.random() * (width - padding) + padding / 2),
				parseInt(Math.random() * (height - padding) + padding / 2),
				i
			)
		);
	}
	drawCircles();
	pointGroup.innerHTML = "";
	polygons.innerHTML = "";
	drawAll();
	animate();
}

function drawAll() {
	polygons.innerHTML = "";
	for (var i = 0; i < points.length; i++) {
		drawPolygons(i);
	}
}

function animate() {
	if (!!doAnimate.checked) {
		for (var i = 0; i < points.length; i++) {
			points[i].move(ctrlSpeed.value);
		}
		drawAll();
		drawCircles();
	}
	requestAnimationFrame(animate);
}

function drawCircles() {
	pointGroup.innerHTML = "";
	if (!!showCircle.checked) {
		points.map(function(e, i) {
			var point = document.createElementNS(svgNS, "circle");
			point.setAttribute("cx", e.X + "px");
			point.setAttribute("cy", e.Y + "px");
			point.setAttribute("r", "5px");
			point.setAttribute("title", i);
			point.setAttribute("stroke", "rgba(" + rr + "," + gg + "," + bb + ",.6)");
			point.setAttribute("stroke-width", "2px");
			point.setAttribute("fill", "none");
			pointGroup.appendChild(point);
		});
	}
}

function Point(x, y, id) {
	return {
		move: function(speed) {
			this.X += this.dx * speed;
			this.Y += this.dy * speed;
			if (this.X < 0 || this.X > width) {
				this.X -= this.dx * speed;
				this.dx = -this.dx;
			}
			if (this.Y < 0 || this.Y > height) {
				this.Y -= this.dy * speed;
				this.dy = -this.dy;
			}
		},
		X: x,
		Y: y,
		distance: -1,
		id: id,
		angle: -1,
		dx: Math.random() - Math.random(),
		dy: Math.random() - Math.random()
	};
}

// Credits goes to http://snipplr.com/view/47207/
function getDistance(point1, point2) {
	var xs = 0;
	var ys = 0;

	xs = point2.X - point1.X;
	xs = xs * xs;

	ys = point2.Y - point1.Y;
	ys = ys * ys;

	return Math.sqrt(xs + ys);
}

// Credits goes to Stackoverflow: http://stackoverflow.com/a/14413632
function getAngleFromPoint(point, centerPoint) {
	var dy = point.Y - centerPoint.Y,
		dx = point.X - centerPoint.X;
	var theta = Math.atan2(dy, dx);
	var angle = (theta * 180 / Math.PI) % 360;
	angle = angle < 0 ? 360 + angle : angle;
	return angle;
}

function getSVG() {
	var w = window.open();
	w.document.write(
		"<h1>Copy SVG into say Affinity Designer</h1><textarea style='width: 100%; height: " +
			window.outerHeight +
			"px'>" +
			svg.outerHTML +
			"</textarea>"
	);
}

setSVG();
window.onresize = setSVG;
regenerate.onclick = noOfPoints.oninput = polygonEdges.oninput = edgeWidth.oninput = generateNew;
showCircle.onchange = drawCircles;
colorfull.onchange = function() {
	// Disabled Adobe Kuler color
	// getKulerTheme();
	drawAll();
};
setSVG();
btnGetSVG.onclick = getSVG;

function forEach(domNodes, cb) {
	return Array.prototype.forEach.call(domNodes, function(item, i) {
		cb(item, i);
	});
}

function aRun() {
	setColor();
	generateNew();
}

svg.addEventListener("click", aRun);
aRun();

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console