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>
	<defs>
		<circle id='point' r='4'/>
		<path id='arc'/>
		<text id='txt'></text>
	</defs>
	
	<g class='lines--fixed'></g>
	
	<g class='line--moving'>
		<line/>
		<g class='points--ctrl'></g>
	</g>
	
	<g class='points--inters'></g>
	
	<g class='arcs'></g>
</svg>

<section class='inf hidden'>
	<div class='content'>
		<p>When two or more parallel lines (the horizontal dark lines) are crossed by another transversal line (the orange line), the corresponding angles (the ones in matching corners) are equal.</p>
		<p>Drag the red dots to change the transversal line. Double click to switch to the next pair of corners.</p>
	</div>
	<button id='toggle'>info</button>
</section>
            
          
!
            
              $l-fixed-c: #333;
$l-moving-c: #e18728;
$p-ctrl-c: #be4c39;
$p-inters-c: #4ca454;
$a-inters-c: $p-ctrl-c;

html, body, svg { height: 100%; }

html { overflow: hidden; }

body { margin: 0; }

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

line { stroke-wdth: 2; }

.lines--fixed line { stroke: $l-fixed-c; }

.line--moving {
	line { stroke: $l-moving-c; }
	
	use { /* control points at ends of line */
		fill: $p-ctrl-c;
		stroke: rgba($p-ctrl-c, .1);
		stroke-width: 32;
		cursor: move;
		
		&:hover {
			stroke: rgba($p-ctrl-c, .2);
		}
	}
}

.points--inters use { fill: $p-inters-c; }

.arcs {
	use {
		fill: transparent;
		stroke: $a-inters-c;
	}
}

text {
	fill: purple;
	stroke: none;
	font: 900 1em monospace;
}

.inf {
	position: absolute;
	top: 100%; left: 50%;
	padding: 0 .5em;
	min-width: 20em;
	transform: translate(-50%, -100%);
	background: rgba(#111, .85);
	color: #fff;
	font: 1em trebuchet ms, verdana, sans-serif;
	transition: .35s ease-out;
	
	button {
		position: absolute;
		bottom: 100%; left: 50%;
		border: solid 0 transparent;
		transform: translate(-50%);
		background: $l-moving-c;
		font: inherit;
		font-size: 2em;
		cursor: pointer;
		
		&:before, &:after {
			position: absolute;
			z-index: -1;
			top: 0; bottom: 0;
			width: 1em;
			background: inherit;
			content: '';
		}
		&:before {
			right: 100%;
			border-radius: 33% 0 0 0;
			transform: skewX(-15deg) translate(25%);
		}
		&:after {
			left: 100%;
			border-radius: 0 33% 0 0;
			transform: skewX(15deg) translate(-25%);
		}
	}
}

.hidden {
	transform: translate(-50%, 0);
}
            
          
!
            
              Object.getOwnPropertyNames(Math).map(function(p) {
	window[p] = Math[p];
});

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

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

var N = 2 /* no of fixed parallel lines */, 
		ARC_F = 8, 
		J = 1.75, 
		K = .1, 
		NS_URI = 'http://www.w3.org/2000/svg', 
    XLink_NS = 'http://www.w3.org/1999/xlink', 
		
		svg = document.querySelector('svg'), 
		w, h, 
		
		/* definition for point */
		defp = document.getElementById('point'), 
		/* definition for arc */
		defa = document.getElementById('arc'), 
		/* definition for text */
		deft = document.getElementById('txt'), 
		pt_r, lim, arc_r, yunit, 
		
		/* wrapper for fixed lines */
		wf = svg.querySelector('.lines--fixed'), 
		/* wrapper for moving line end poins */
		we = svg.querySelector('.points--ctrl'), 
		/* wrapper for inters points */
		wi = svg.querySelector('.points--inters'), 
		/* wrapper for arcs */
		wa = svg.querySelector('.arcs'), 
		
		populated = false, 
		
		el_lines_fixed = [], 
		el_pts_ctrl = [], 
		el_line_moving = 
			svg.querySelector('.line--moving line'), 
		el_pts_inters = [], 
		el_arcs = [], 
		el_txts = [], 
		
		lines_fixed = [], 
		line_moving, 
		
		dragged = null, 
		quad = 0, p, q, 
		
		itoggle = document.getElementById('toggle');

var Point = function(x, y, el) {
	this.x = (x === 0 || x) ? x : rand(w, 0, 1);
	this.y = (y === 0 || y) ? y : rand(h, 0, 1);
	this.el = el;
	this.changed = true;
		
	this.test = function() {
		return this.el && this.el.tagName && 
			this.el.tagName.toLowerCase() === 'use';
	};
	
	this.change = function(x, y) {
		if(x === 0 || x) { this.x = x; }
		if(y === 0 || y) { this.y = y; }
		
		this.el.setAttrs({
			'x': this.x, 
			'y': this.y
		});
		
		this.changed = true;
	};
	
	this.init = function() {
		if(this.test()) {
			this.change();
		}
	};
	
	this.init();
};

var Line = function(p1, p2, el) {
	this.p1 = p1 || new Point;
	this.p2 = p2 || new Point;
	this.el = el;
	this.rel = [];
	this.n = 0;
	
	this.test = function() {
		return this.el && this.el.tagName && 
			this.el.tagName.toLowerCase() === 'line';
	};
	
	this.intersection = function(line) {
		var dxa = this.p1.x - this.p2.x, 
				dya = this.p1.y - this.p2.y, 
				dxb = line.p1.x - line.p2.x, 
				dyb = line.p1.y - line.p2.y, 
				dma = this.p1.x*this.p2.y - 
							this.p1.y*this.p2.x, 
				dmb = line.p1.x*line.p2.y - 
							line.p1.y*line.p2.x,  
				den = dxa*dyb - dya*dxb, 
				nx = dma*dxb - dxa*dmb, 
				ny = dma*dyb - dya*dmb;
		
		return {
			'x': round(nx/den), 
			'y': round(ny/den)
		};
	};
	
	this.angle = function() {
		var dy = this.p1.y - this.p2.y, 
				dx = this.p1.x - this.p2.x;
		
		return atan2(dy, dx);
	};
	
	this.arcPath = function(alpha) {
		var d_txt = '', x, y, xf, yf, 
				beta = q*PI;
		
		alpha += (p^q)*PI;
				
		x = round(arc_r*cos(alpha));
		y = round(arc_r*sin(alpha));
		xf = round(arc_r*cos(beta));
		yf = round(arc_r*sin(beta));
						
		d_txt = 'M' + x + ',' + y + 
						'A' + arc_r + ' ' + arc_r + ' 0 0 ' + (~~!p) + ' ' + 
						xf + ' ' + yf;
				
		return d_txt;
	};
	
	this.textPos = function(_deg, dims) {
		var bw = dims.width, bh = dims.height, tt, 
				_deg = p?(_deg + 180):_deg, 
				df = pow(-1, q);
				
		tt = 'translate(' + (-bw/2) + ' ' + bh/2 + ')' + 
		 	 	'rotate(' + _deg/2 + ')' + 
		 	 	'translate(' + df*(arc_r + bw/2 + 5) + ')' + 
		 	 	'rotate(' + _deg/-2 + ')';

		return tt;
	};
	
	this.updateRel = function() {
		var coord = {}, x, y, 
				alpha = this.angle(), 
				_deg = round(alpha*180/PI), bb;
		
		defa.setAttrs({'d': this.arcPath(alpha) });
		deft.textContent = (p?(180 - abs(_deg)):abs(_deg)) + '°';
		
		for(var i = 0; i < this.n; i++) {
			coord = this.intersection(this.rel[i].line);
			this.rel[i].inters.change(coord.x, coord.y);
			
			this.rel[i].arc.change();
			
			if(i === 0) {
				bb = this.rel[i].arc.txt.getBBox();
			}
		}
		
		deft.setAttrs({'transform': this.textPos(_deg, bb)});
	};
	
	this.queueChange = function(o, i) {
		if(this['p' + i].changed) {
			o['x' + i] = this['p' + i].x;
			o['y' + i] = this['p' + i].y;
			
			this['p' + i].changed = false;
		}
	};
	
	this.change = function() {
		var to_apply = {};
		
		this.queueChange(to_apply, 1);
		this.queueChange(to_apply, 2);
		
		this.el.setAttrs(to_apply);
		
		if(this.n > 0) {
			this.updateRel();
		}
	};
	
	this.init = function() {
		if(this.test()) {
			this.change();
		}
	};
	
	this.init();
};

var Arc = function(p1, p2, p3, el, txt) {
	this.p1 = p1 || new Point;
	this.p2 = p2 || new Point;
	this.p3 = p3 || new Point;
	this.el = el;
	this.txt = txt;
	
	this.test = function() {
		return this.el && this.el.tagName && 
			this.el.tagName.toLowerCase() === 'use';
	};
	
	this.change = function(alpha) {
		this.el.setAttrs({
			'x': this.p2.x, 
			'y': this.p2.y
		});
		this.txt.setAttrs({
			'x': this.p2.x, 
			'y': this.p2.y
		});
	};
};

var populate = function(wrapper, n, type, def, o) {
	var frag = document.createDocumentFragment(), 
			curr, a = [];
	
	for(var i = 0; i < n; i++) {
		curr = document.createElementNS(NS_URI, type);
		
		if(type === 'use' && def) {
			curr.setAttributeNS(
				XLink_NS, 
				'xlink:href', 
				'#' + def
			);
		}
		
		if(o) {
			curr.setAttrs({ 'id': o + i });
		}
		
		a.push(curr);
		frag.appendChild(curr);
	}
	
	wrapper.appendChild(frag);
	
	return a;
};

var init = function() {
	var s = getComputedStyle(svg), vb, fy, 
			x1, y1, x2, y2, coord = {};
	
	pt_r = ~~defp.getAttribute('r');
	
	lines_fixed = [];
	p = quad%2;
	q = floor(quad/2);
	
	w = ~~s.width.split('px')[0];
	h = ~~s.height.split('px')[0];
	
	vb = [0, 0, w, h];
	
	svg.setAttribute('viewBox', vb.join(' '));
	
	if(!populated) {
		el_lines_fixed = 
			populate(wf, N, 'line');
		el_pts_ctrl = 
			populate(we, 2, 'use', 'point', 'drag');
		el_pts_inters = 
			populate(wi, N, 'use', 'point');
		el_arcs = 
			populate(wa, N, 'use', 'arc');
		el_txts = 
			populate(wa, N, 'use', 'txt');
		
		populated = true;
	}
	
	yunit = ~~(h/(N + 1));
	arc_r = ARC_F*pt_r;
	lim = J*arc_r;
		
	/* moving line */
	x1 = rand((1 - K)*w, K*w, 1);
	y1 = rand(yunit - lim, pt_r, 1);
	x2 = rand((1 - K)*w, K*w, 1);
	y2 = rand(h - pt_r, N*yunit + lim, 1);
	
	line_moving = new Line(
		new Point(x1, y1, el_pts_ctrl[0]), 
		new Point(x2, y2, el_pts_ctrl[1]), 
		el_line_moving
	);
	
	for(var i = 0; i < N; i++) {
		fy = (i + 1)*yunit;
		
		lines_fixed.push(new Line(
			new Point(0, fy), 
			new Point(w, fy), 
			el_lines_fixed[i]
		));
		
		line_moving.rel.push({
			'line': lines_fixed[i],
			'inters': new Point(0,0,el_pts_inters[i]), 
			'arc': null
		});
		
		line_moving.rel[i].arc = new Arc(
			line_moving.p1, 
			line_moving.rel[i].inters, 
			lines_fixed[i].p2, 
			el_arcs[i], 
			el_txts[i]
		);
		
		line_moving.n++;
	}
	
	line_moving.updateRel();
};

init();

addEventListener('resize', init, false);

addEventListener('mousedown', function(e) {
	var t = e.target, cidx;
	
	if(t.id && t.id.indexOf('drag') !== -1) {
		cidx = ~~t.id.split('drag')[1];
		dragged = line_moving['p' + (cidx + 1)];
	}
	console.log(dragged, t, t.id);
}, false);

addEventListener('mousemove', function(e) {
	var x, y;
	
	if(dragged) {		
		x = min(max(e.clientX, K*w), (1 - K)*w);
		y = e.clientY;
		
		if(dragged.el.id.indexOf('0') !== -1) {
			y = max(min(e.clientY, yunit - lim), pt_r);
		}
		if(dragged.el.id.indexOf('1') !== -1) {
			y = min(max(e.clientY, N*yunit + lim), h - pt_r);
		}
		
		dragged.change(x, y);
		line_moving.change();
	}
}, false);

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

addEventListener('dblclick', function() {
	quad = (quad + 1)%4;
	p = quad%2;
	q = floor(quad/2);
	
	line_moving.updateRel();
}, false);

itoggle.addEventListener('click', function() {
	this.parentNode.classList.toggle('hidden');
}, false);
            
          
!
999px
Loading ..................

Console