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.

            
              <svg></svg>

<pre><span id='m0'>60°</span> + <span id='m1'>60°</span> + <span id='m2'>60°</span> = 180°</pre>
            
          
!
            
              html, body, svg { height: 100%; }

body { margin: 0; }

svg {
	display: block;
	width: 100%;
}

polygon, path {
	fill: none;
	stroke-width: 3;
	vector-effect: non-scaling-stroke;
}

g { cursor: pointer; }

path {
	stroke: #be4c39;
	fill-opacity: .7;
	transition: .3s;
}

polygon { stroke: #e18728; }

circle {
	fill: #be4c39;
	stroke: rgba(#d49b00, .32);
	stroke-width: 25;
}

pre {
	position: absolute;
	top: .5em; right: .5em; left: .5em;
	margin: 0;
	padding: .25em .5em;
  background: #333;
	font-size: 1.25em;
  color: #ddd;
	text-align: center;
	
	span {
		display: inline-block;
    padding: .25em;
    border-radius: .1875em;
		transition: .3s;
		cursor: pointer;
	}
}

@for $i from 0 to 3 {
	$c: hsl($i*120, 100%, 80%);

	span:nth-of-type(#{$i + 1}) {
		color: $c;
	}

	g:nth-of-type(#{$i + 1}) path {
		fill: $c;
	}

	.hl:nth-of-type(#{$i + 1}) {
		background: $c;
		color: #000;

		> path {
			fill: $c;
			stroke-width: 5;
		}
	}
}
            
          
!
            
              Object.getOwnPropertyNames(Math).map(function(p) {
  window[p] = Math[p];
});

var rand = function(max, min, is_int) {
	var max = (max || max === 0) ? max : 1, 
			min = min || 0, 
			gen = min + (max - min)*Math.random();
	
	return (is_int) ? Math.round(gen) : gen;
};

var σ = function(k) {
	return (random() < (k || .5))?-1:1;
};

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', 
		DRAG_TARGET_R = 4, 
		ARC_R = 32, 
		
		svg = document.querySelector('svg'), 
		ml = document.querySelectorAll('[id*=m]'), 
		w, h, r, 
		poly, drag_idx = null;

var Vertex = function(idx, x, y) {
	this.idx = idx || 0;
	this.x = x || 0;
	this.y = y || 0;
	this.next = null; // vertex
	this.prev = null;
	this.el = document.createElementNS(NS_URI, 'g');
	this.pt = document.createElementNS(NS_URI, 'circle');
	this.arc = document.createElementNS(NS_URI, 'path');
	
	this.init = function() {
		this.pt.setAttrs({
			'cx': this.x, 'cy': this.y, 
			'r': DRAG_TARGET_R
		});
		
		this.el.appendChild(this.arc);
		this.el.appendChild(this.pt);
	};
	
	this.distanceTo = function(v) {
		var dx = this.x - v.x, 
        dy = this.y - v.y;
		
		return hypot(dx, dy);
	};
	
	this.moveTo = function(x, y) {
		this.x = x;
		this.y = y;
		
		this.pt.setAttrs({
			'cx': this.x, 'cy': this.y
		});
	};
	
	this.slopeTo = function(v) {
		var dx = this.x - v.x, 
        dy = this.y - v.y;
    
    return atan2(dy, dx);
	};
	
	this.measureAngle = function() {
		var a1 = this.prev.slopeTo(this), 
        a2 = this.next.slopeTo(this), 
        a = (a2 - a1 + 2*PI)%(2*PI);
    
    if(a > PI) { a = 2*PI - a; }
    
    return a;
	};
	
	this.refreshArc = function() {
		var a1 = this.prev.slopeTo(this), 
        a2 = this.next.slopeTo(this), 
				b = 'A' + ARC_R + ' ' + ARC_R + ' 0 0', 
				d_txt = 'M' + this.x + ' ' + this.y + 'L', x, y, 
				flag = ~~((a2 - a1 + 2*PI)%(2*PI) < PI);
		
		
		x = this.x + ARC_R*cos(a1);
		y = this.y + ARC_R*sin(a1);
				
		d_txt += x + ' ' + y + b + ' ' + flag;
		
		x = this.x + ARC_R*cos(a2);
		y = this.y + ARC_R*sin(a2);
		
		d_txt += x + ' ' + y + 'z';
		
		this.arc.setAttrs({ 'd': d_txt });
	};
	
	this.init();
};

var Polygon = function(n) {
	this.n = n || 3;
	this.vertices = []; // array of vertex obj
	this.el = document.createElementNS(NS_URI, 'polygon');
	this.pts = document.createElementNS(NS_URI, 'g');
	
	this.init = function() {
		var α = 2*PI/this.n, β, x, y, pts_txt = '';
		
		this.pts.setAttrs({ 'id': 'vertices' });
		
		svg.appendChild(this.el);
		svg.appendChild(this.pts);
		
		for(var i = 0; i < this.n; i++) {
			β = (i + σ()*rand(.32))*α;
			
			x = ~~(w/2 + r*(1 + σ()*rand(.16))*cos(β));
			y = ~~(h/2 + r*(1 + σ()*rand(.16))*sin(β));
			
			pts_txt += x + ',' + y + ' ';
			
			this.vertices.push(new Vertex(i, x, y));
			
			this.vertices[i].el.setAttrs({ 'id': 'v' + i });
			this.pts.appendChild(this.vertices[i].el);
			
			if(i > 0) {
				this.vertices[i].prev = this.vertices[i - 1];
				this.vertices[i - 1].next = this.vertices[i];
				
				if(i == this.n - 1) {					
					this.vertices[i].next = this.vertices[0];
					this.vertices[0].prev = this.vertices[i];
				}
			}
		}
		
		this.refreshPoly();
		
		this.el.setAttrs({
			'id': 'n' + this.n, 
			'points': pts_txt.trim()
		});
	};
	
	this.moveVertex = function(idx, x, y) {
		var c, v = this.vertices[idx], old;
		
		c = this.el.getAttribute('points')
			.split(' ');
		
		v.moveTo(x, y);
		old = c[idx].split(',');
		
		if(v.distanceTo(v.prev) > 2.5*ARC_R && 
			 v.distanceTo(v.next) > 2.5*ARC_R) {
			c[idx] = x + ',' + y;
			this.el.setAttrs({
				'points': c.join(' ')
			});

			this.refreshPoly();
		}
		else {
			v.moveTo(old[0]*1, old[1]*1);
		}				
	};
	
	this.refreshPoly = function() {
		var a, Σ = 0;
		
		for(var i = 0; i < this.n; i++) {
			a = this.vertices[i].measureAngle();
			a = round(a*180/PI);
			Σ += a;

			if(i === this.n - 1 && Σ !== 180) {
				a += 180 - Σ;
			}

			ml[i].textContent = a + '°';

			this.vertices[i].refreshArc();
		}
	};
	
	this.init();
};

var init = function() {
	var s = getComputedStyle(svg), 
			vb;
	
	if(poly) {
		svg.removeChild(poly.el);
		svg.removeChild(poly.pts);
	}
	
	w = s.width.split('px')[0];
	h = s.height.split('px')[0];
	r = .32*min(w, h);
	
	poly = new Polygon;
	
	vb = [0, 0, w, h];
	
	svg.setAttribute('viewBox', vb.join(' '));
};

var hl = function(e) {
	var t = e.target, ts, idx;
	
	if(t.id && t.id[0] === 'm') {
		idx = ~~t.id.replace('m', '');
		ts = poly.vertices[idx].el;
	}
	if(t.parentNode.id && 
		 t.parentNode.id[0] === 'v') {
		t = t.parentNode;
		idx = ~~t.id.replace('v', '');
		ts = ml[idx];
	}
	
	if(t && ts) {
		t.classList.toggle('hl');
    ts.classList.toggle('hl');
	}
};

init();

addEventListener('resize', init, false);

addEventListener('mousedown', function(e) {
	var t = e.target;
	
	if(t.tagName.toLowerCase() === 'circle') {
		drag_idx = t.parentNode.id.split('v')[1];
	}
}, false);

addEventListener('mousemove', function(e) {
	var x, y;
	
	if(drag_idx) {
		x = e.clientX;
		y = e.clientY;
				
		poly.moveVertex(drag_idx, x, y);
	}
}, false);

addEventListener('mouseup', function() {
	if(drag_idx) { drag_idx = null; }
}, false);

addEventListener('mouseover', hl, false);
addEventListener('mouseout', hl, false);
            
          
!
999px
Loading ..................

Console