cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              - d = 2000;
- off = -.5*d;

svg(viewbox=[off, off, d, d].join(' '))
	polygon(id='poly')
	
	g(id='dconn')
	
	g(id='tri')
	
	g(id='vx')

form
	button(title='toggle controls') toggle controls
	input(type='range' id='n'
				min='3' max='10' value='3')
	label(for='n') number of edges/ vertices
            
          
!
            
              @import 'compass/css3';

$theme-c: #e18728;
$hl-c: #be4c39;

$poly-c: $theme-c;
$diag-c: #444;
$vx-bg: yellow;
$vx-c: $hl-c;
$vx-txt: purple;
$lbl-bg: $hl-c;
$lbl-c: #eee;

html { overflow: hidden; }

body { margin: 0; }

svg {
	display: block;
	margin: calc(50vh - 50vmin) auto;
	min-width: 10em; min-height: 10em;
	width: 100vmin; height: 100vmin;
	max-width: 100em; max-height: 100em;
	font-size: 1em;
}

text {
	font: 700 100px monospace;
	opacity: .001;
}

svg .hidden {
	opacity: .001;
	pointer-events: none;
}

.q1, .q2 { dominant-baseline: hanging; }
.q2, .q3 { text-anchor: end; }

[id='poly'], [id='dconn'] line, 
[id='tri'] line, [id='vx'] circle {
	vector-effect: non-scaling-stroke;
}

[id='poly'] {
	fill: transparent;
	stroke: $poly-c;
	stroke-width: 4;
}

[id='dconn'] {
	line {
		stroke: $diag-c;
		stroke-width: 2;
	}
}

[id='tri'] {
	line {
		stroke-width: 4;
		stroke-linecap: round;
	}
	rect {
		fill: $lbl-bg;
		opacity: .001;
	}
	text {
		fill: $lbl-c;
		text-anchor: middle;
		dominant-baseline: middle;
	}
}

[id='vx'] {
	circle {
		fill: $vx-bg;
		stroke: $vx-c;
		stroke-width: 5;
	}
	text {
		fill: $vx-txt
	}
}

@media ( min-width: 240px) and 
			 (min-height: 240px) {
	text, [id='tri'] rect { opacity: .999; }
}

@media ( min-width: 320px) and 
			 (min-height: 320px) {	
	text { font-size: 80px; }
}

@media ( min-width: 480px) and 
			 (min-height: 480px) {
	text { font-size: 56px; }
}

@media ( min-width: 800px) and 
			 (min-height: 800px) {
	text { font-size: 40px; }
}


/* =============== CONTROLS =============== */
$ctrl-bg: #262626;
$ctrl-ll: #aaa;
$ctrl-hl: $theme-c;

$track-w: 13em;
$track-h: .25em;

$thumb-d: 1.25em;

@mixin track() {
	width: $track-w; height: $track-h;
	background: $ctrl-ll;
}

@mixin fill() {
	background: $ctrl-hl;
}

@mixin thumb() {
	border: none;
	width: $thumb-d; height: $thumb-d;
	border-radius: 50%;
	background: $ctrl-hl;
}

form {
	position: fixed;
	bottom: 0; left: 0;
	padding: .5em;
	transform-origin: 50% 100%;
	background: $ctrl-bg;
	color: $ctrl-ll;
	font: 1em/1.375em trebuchet ms, sans serif;
	transition: .35s;
	
	&.hidden {
		transform: translateY(100%);
	}
}

button {
	position: absolute;
	right: 0; bottom: 100%; left: 0;
	border: none;
	width: 100%;
	border-radius: .5em .5em 0 0;
	background: $ctrl-ll;
	color: $ctrl-bg;
	text-indent: -40em;
	transition: inherit;
	cursor: pointer;
	
	&:hover {
		color: $ctrl-hl;
	}
	
	&:before {
		$tl: 1em;
		$th: $tl*sqrt(3)/2;
		
		position: absolute;
		top: 50%; left: 50%;
		border: solid 0 transparent;
		border-width: $th .5*$tl 0 .5*$tl;
		border-top-color: currentColor;
		transform: translate(-50%, -50%);
		transition: inherit;
		content: '';
		
		.hidden & {
			transform: translate(-50%, -50%) 
				rotate(180deg);
		}
	}
}

input[type='range'] {
	&, 
	&::-webkit-slider-runnable-track, 
	&::-webkit-slider-thumb {
		-webkit-appearance: none;
	}
	
	display: block;
	margin: 1em auto 0;
	padding: 0;
	width: $track-w; height: 2em;
	background: none;
	font-size: 1em;
	cursor: pointer;
	
	&::-webkit-slider-runnable-track {
		@include track();
	}
	&::-moz-range-track {
		@include track();
	}
	&::-ms-track {
		border: none;
		@include track();
		color: transparent;
	}
	
	&::-moz-range-progress {
		height: $track-h;
		@include fill();
	}
	&::-ms-fill-lower {
		@include fill();
	}
	
	&::-webkit-slider-thumb {
		margin-top: ($track-h - $thumb-d)/2;
		@include thumb();
	}
	&::-moz-range-thumb {
		@include thumb();
	}
	&::-ms-thumb {
		@include thumb();
	}
	
	&::-ms-tooltip { display: none; }
		
	&:focus { outline: none; }
}
            
          
!
            
              /* because IE up to and including 11 */
Math.hypot = Math.hypot || function() {
  var y = 0;
  var length = arguments.length;

  for (var i = 0; i < length; i++) {
    if (arguments[i] === Infinity || arguments[i] === -Infinity) {
      return Infinity;
    }
    y += arguments[i] * arguments[i];
  }
  return Math.sqrt(y);
};

/* because me lazy */
Object.getOwnPropertyNames(Math).map(function(p) {
  window[p] = Math[p];
});

var cm = function(v0, v1, v2) {
	var m0 = {}, mp = {}, k = 1/3;
	
	m0.x = .5*(v1.x + v2.x);
	m0.y = .5*(v1.y + v2.y);
	
	mp.x = (1 - k)*m0.x + k*v0.x;
	mp.y = (1 - k)*m0.y + k*v0.y;
	
	return mp;
};

Node.prototype.setAttrs = function(attr_obj) {
  for(var prop in attr_obj) {
    this.setAttribute(prop, attr_obj[prop]);
  }
};

var NS_URI = 'http://www.w3.org/2000/svg', 
		TO_RAD_F = PI/180, 
		VERTEX_RADIUS = 16, 
		LINE_SPACING = 64, 
		H_PAD = .64, 
		V_PAD = .16, 
		T_VERTEX_REVEAL = 16, 
		T_PULSE = 32, 
		N_PULSES = 3, 
		
		svg = document.querySelector('svg'), 
		vb = svg.getAttribute('viewBox').split(' ')
					.map(function(d) {
						return ~~d;
					}), 
		r = .4*min(vb[2], vb[3]), 
		poly_el = document.getElementById('poly'), 
		dwrap_el = document.getElementById('dconn'), 
		vwrap_el = document.getElementById('vx'), 
		lwrap_el = document.getElementById('tri'), 
		diagonals = [], 
		vertices = [], 
		triangles = [], 
		form = document.querySelector('form'), 
		toggle = form.querySelector('button'), 
		slider = document.getElementById('n'), 
		n = ~~slider.value, n_vis = 0, 
		a_base_deg = 360/n, 
		a_base_rad = TO_RAD_F*a_base_deg, 
		t = 0, req_id = null;

var Point = function(i, x, y, parent) {
	this.i = i || 0;
	this.q = ~~(4*this.i/n);
	this.x = x || 0;
	this.y = y || 0;
	this.parent = parent || svg;
	this.gEl = document.createElementNS(NS_URI, 'g');
	this.pEl = document.createElementNS(NS_URI, 'circle');
	this.tEl = document.createElementNS(NS_URI, 'text');
	
	this.init = function() {
		var j1 = floor(this.q/2), 
				j2 = this.q%2, 
				ttxt = 'translate(' + 
									round(this.x) + ' ' + 
									round(this.y) + 
								')';
				
		// position group
		this.gEl.setAttrs({
			'transform': ttxt, 
			'class': 'hidden'
		});
		
		// position point
		this.pEl.setAttribute('r', VERTEX_RADIUS);
		
		// position text
		this.tEl.textContent = (this.i + 1);
		this.tEl.setAttrs({
			'x': round(2*VERTEX_RADIUS*pow(-1, j2 ^ j1)), 
			'y': round(2*VERTEX_RADIUS*pow(-1, j1)), 
			'class': 'q' + (this.q + 1)
		});
		
		this.gEl.appendChild(this.pEl);
		this.gEl.appendChild(this.tEl);
		this.parent.appendChild(this.gEl);
	};
	
	this.pulse = function() {
		// pulse point
	};
	
	this.init();
};

var Segment = function(s, e, parent) {
	this.s = s || new Point;
	this.e = e || new Point;
	this.len = hypot(this.e.x - this.s.x, 
									 this.e.y - this.s.y);
	this.off = this.len;
	this.parent = parent || svg;
	this.el = document.createElementNS(NS_URI, 'line');
		
	this.init = function() {		
		this.el.setAttrs({
			'x1': round(this.s.x), 
			'y1': round(this.s.y), 
			'x2': round(this.e.x), 
			'y2': round(this.e.y), 
			'stroke-dasharray': this.len, 
			'stroke-dashoffset': this.off
		});
		
		this.parent.appendChild(this.el);
	};
	
	this.draw = function() {
		if(this.off < 0) {
			cancelAnimationFrame(req_id);
			req_id = null;
			
			triangles[this.e.i - 2].fill();
			
			return;
		}
		
		this.off -= .025*this.len;
		this.el.setAttribute(
			'stroke-dashoffset', max(0, this.off)
		);
		
		req_id = 	requestAnimationFrame(
			this.draw.bind(this)
		);
	};
	
	this.init();
};

var Triangle = function(v0, v1, v2, parent, ltxt, poly) {
	this.v0 = v0 || new Point;
	this.v1 = v1 || new Point;
	this.v2 = v2 || new Point;
	this.idx = this.v1.i;
	this.cm = {};
	this.parent = parent || svg;
	this.hEl = document.createElementNS(NS_URI, 'g');
	this.nl = 0;
	this.lIdx = 0;
	this.lOff = 0;
	this.blines = [];
	
	if(ltxt) {
		this.gEl = document.createElementNS(NS_URI, 'g');
		this.bEl = document.createElementNS(NS_URI, 'rect');
		this.tEl = document.createElementNS(NS_URI, 'text');
	}
	
	this.init = function() {
		var bbox, 
				m, vs = 0, ttxt = '';
		
		this.gEl.setAttribute('class', 'hidden');
		
		m = abs((n - 1)*.5 - this.idx)
		vs = .9 - m/(n - 2)*.5;
		
		this.cm = cm(this.v0, this.v1, this.v2);
		
		ttxt = 
			'translate(' + 
				round(this.cm.x) + ' ' + 
				round(this.cm.y) + 
			')' + 
			'scale(' + vs.toFixed(2) + ')' + 
			'translate(' + 
				round(-this.cm.x) + ' ' + 
				round(-this.cm.y) + 
			')';
		
		this.hEl.setAttrs({
			'class': 'fill', 
			'transform': ttxt
		});
		
		this.createFill(m);
		
		if(ltxt) {
			this.tEl.textContent = this.v1.i;
			this.tEl.setAttrs({
				'x': round(this.cm.x), 
				'y': round(this.cm.y)
			});
			
			this.gEl.appendChild(this.hEl);
			this.gEl.appendChild(this.bEl);
			this.gEl.appendChild(this.tEl);
			this.parent.appendChild(this.gEl);
			
			bbox = this.tEl.getBBox();
			this.bEl.setAttrs({
				'x': round(bbox.x - H_PAD*bbox.width), 
				'y': round(bbox.y - V_PAD*bbox.height), 
				'width': round(bbox.width*(1 + 2*H_PAD)), 
				'height': round(bbox.height*(1 + 2*V_PAD)), 
				'rx': round(bbox.width*H_PAD)
			});
		}
	};
	
	this.createFill = function(m) {
		var diffx, diffy, mix, den, num, h, 
				curr_line, pf, x1, y1, x2, y2, 
				stroke = 'hsl(hue,50%,80%)', len;
		
		stroke = stroke.replace('hue', (this.v1.i - 1)*360/(n - 2));
		
		diffx = this.v2.x - this.v1.x;
		diffy = this.v2.y - this.v1.y;
		mix = this.v2.x*this.v1.y - this.v2.y*this.v1.x;

		den = hypot(diffx, diffy);
		num = abs(diffy*this.v0.x - diffx*this.v0.y + mix)

		h = num/den;
		this.nl = ~~(h/LINE_SPACING + .5*m);
		
		for(var j = 0; j <= this.nl; j++) {
			curr_line = document.createElementNS(NS_URI, 'line');
			
			pf = j/this.nl;
			x1 = pf*this.v0.x + (1 - pf)*this.v1.x;
			y1 = pf*this.v0.y + (1 - pf)*this.v1.y;
			x2 = pf*this.v0.x + (1 - pf)*this.v2.x;
			y2 = pf*this.v0.y + (1 - pf)*this.v2.y;
			
			len = hypot(x2 - x1, y2 - y1);
			
			this.blines.push({
				'el': curr_line, 
				'len': len
			});
			
			curr_line.setAttrs({
				'x1': round(x1), 
				'y1': round(y1), 
				'x2': round(x2), 
				'y2': round(y2), 
				'stroke': stroke, 
				'stroke-dasharray': 
					ceil(len), 
				'stroke-dashoffset': 
					ceil(len) + 4
			});
			
			this.hEl.appendChild(curr_line)
		}
	};
	
	this.fill = function() {
		this.gEl.setAttribute('class', '');
		this.lIdx = this.nl - 1;
		this.lOff = this.blines[this.lIdx].len;
		this.drawFill();
	};
	
	this.drawFill = function() {
		if(this.lOff < 0) {
			cancelAnimationFrame(req_id);
			req_id = null;
			
			if(this.lIdx < 1) {
				if(this.idx < n - 2) {
					if(this.idx === n - 3) {
						triangles[this.idx].fill();
					}
					else {
						diagonals[this.idx].draw();
					}
				}
			}
			else {
				this.lIdx--;
				this.lOff = this.blines[this.lIdx].len;
				this.drawFill();
			}
			
			return;
		}
		
		this.lOff -= .1*this.blines[this.lIdx].len;
		this.blines[this.lIdx].el.setAttribute(
			'stroke-dashoffset', max(this.lOff, 0)
		);
		
		req_id = requestAnimationFrame(
			this.drawFill.bind(this)
		);
	};
	
	this.pulse = function() {};
	
	this.init();
};

var init = function() {
	var x, y, pts_txt = '', a_curr_rad;
	
	if(req_id) {
		cancelAnimationFrame(req_id);
		req_id = null;
	}
	
	n_vis = 0;
	a_base_deg = 360/n;
	a_base_rad = TO_RAD_F*a_base_deg;
	
	dwrap_el.textContent = '';
	vwrap_el.textContent = '';
	lwrap_el.textContent = '';
	
	diagonals = [];
	vertices = [];
	triangles = [];
	
	for(var i = 0; i < n; i++) {
		a_curr_rad = i*a_base_rad;
		x = r*cos(a_curr_rad);
		y = r*sin(a_curr_rad);
		
		vertices.push(new Point(i, x, y, vwrap_el));
		
		if(i > 1) {
			if(i < n - 1) {
				diagonals.push(new Segment(
					vertices[0], vertices[i], dwrap_el
				));
			}
			
			triangles.push(new Triangle(
				vertices[0], 
				vertices[i - 1], 
				vertices[i], 
				lwrap_el, 
				i
			));
		}
		
		pts_txt += round(x) + ',' + round(y) + ' ';
	}
	
	poly_el.setAttribute('points', pts_txt);
	
	revealVertices();
};

var revealVertices = function() {
	if(n_vis === n) {
		cancelAnimationFrame(req_id);
		req_id = null;
		
		if(diagonals.length > 0) {
			diagonals[0].draw();
		}
		else {
			triangles[0].fill();
		}
		
		return;
	}
	
	if(t%T_VERTEX_REVEAL === 0) {
		vertices[n_vis].gEl.setAttribute('class', '');
		n_vis++
	}
	
	t++;
	
	req_id = requestAnimationFrame(revealVertices);
};

var sliderChange = function() {
	n = ~~slider.value;
	init();
};

init();

toggle.addEventListener('click', function(e) {
	e.preventDefault();
	form.classList.toggle('hidden');
}, false);

slider.addEventListener('input', sliderChange, false);
slider.addEventListener('change', sliderChange, false);
            
          
!
999px
Loading ..................

Console