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.

            
              - var n8hedron = 8, n4hedron = 4, n_opts = 2;

svg(width='0' height='0')
	defs
		while n_opts--
			clipPath(id=`cp-${n_opts}`)
				polygon(id=`pc-${n_opts}`)
			symbol(id=`s-${n_opts}`)
				polygon(id=`ps-${n_opts}`)

.scene
	.manual-rotor
		.s3d
			while n8hedron--
				- var i = ~~(n8hedron/n4hedron);
				.pos
					.s2d(class=`type-${i}`)
						svg
							use(xlink:href=`#s-${i}`)

form
	section
		input#auto(type='checkbox')
		label(for='auto') autorun truncation
	section.multi
		input#prog(type='range' value='0')
		label(for='prog') truncation sequence progress
		output#out(for='prog')
            
          
!
            
              $fa: 180deg/pi(); // angular factor
$gc: #FAAB00 #F90050;
$ctrl-hl: #DAF204;
$ctrl-ll: #aaa;
$track-w: 14em;
$track-h: .25em;
$thumb-d: 1.25em;
$check-d: 1.25em;
$n-opts: 2;

// 3gon = polygon (2D shape) with 3 vertices (edges)
$rc3gon: 20em; // circumradius for triangle
$n3gon: 3; // number of vertices
$dc3gon: 2*$rc3gon;
$ca3gon: 360deg/$n3gon; // central angle
$a3gon: ($n3gon - 2)*180deg/$n3gon; // vertex angle
$l3gon: 2*$rc3gon*sin(.5*$ca3gon); // edge
$ri3gon: $rc3gon*cos(.5*$ca3gon); // inradius
$h3gon: $l3gon*sin($a3gon); // height

// 4hedron = polyhedron (3D shape) with 4 faces
$n4hedron: 4;
$da4hedron: acos($ri3gon/$h3gon)*$fa;
$ri4hedron: $rc3gon/tan($da4hedron);

// 8hedron = polyhedron (3D shape) with 8 faces
$n8hedron: 8;

* { margin: 0; padding: 0; }

html { overflow: hidden; }

polygon {
	fill: rgba(whitesmoke, .35);
	stroke: currentColor;
	stroke-width: 9vmin;
	//vector-effect: non-scaling-stroke;
}

@for $i from 0 to $n-opts {
	[id='ps-#{$i}'] {
		clip-path: url(#cp-#{$i});
		color: nth($gc, $i + 1);
	}
}

.scene {
	overflow: hidden;
	position: absolute;
	top: 0; left: 0;
	width: 100vw; height: 100vh;
	perspective: 20em;
	background: #000;
	
	div {
		position: absolute;
		top: 50%; left: 50%;
		transform-style: preserve-3d;
	}
	
	svg { width: inherit; height: inherit; }
}

.s3d {
	font-size: 1vmin;
	animation: r 20s linear infinite;
}

@keyframes r {
	to { 
		transform: rotateY(1turn) 
							 rotateX(1turn);
	}
}

@for $i from 0 to $n8hedron {
	$j: $i%2;
	$j1: floor($i/2)%2;
	$k: floor($i/$n4hedron);
	$ay: $j*180deg - $j1*90deg + $k*90deg;
	
	.pos:nth-child(#{$i + 1}) {
		transform: 
			rotate($j1*.5turn) 
			rotateY($ay) 
			rotateX(-.5*$da4hedron) 
			translateZ($ri4hedron);
	}
}

.s2d {
	margin: -$rc3gon;
	width: $dc3gon; height: $dc3gon;
	-moz-backface-visibility: hidden;
	//-webkit-filter: drop-shadow(0 0 1px #fff);
	filter: drop-shadow(0 0 1px #fff);
}

@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: absolute;
	top: 0; left: 0;
	padding: 0 .25em;
	background: rgba(#222, .32);
	text-align: center;
	font: 1em trebuchet ms, sans-serif;
}

section {
	margin: .25em auto;
	padding: .5em;
	
	&:hover { background: rgba(#000, .65); }
	
	> * {
		display: block;
		margin: 0 auto .5em;
		width: $track-w;
	}
	
	> label { margin-bottom: 0; }
}

.multi {
	display: flex;
	flex-direction: column;
	
	* { order: 1; }
	
	[type='range'] + label { order: 0; }
}

label { color: $ctrl-ll; }

[type='checkbox'] {
	position: absolute;
	left: -100vw;
	
	+ label {
		display: block;
		border: solid 0 transparent;
		border-width: .125em 1em .25em;
		color: $ctrl-ll;
		line-height: 2;
		text-align: left;
		cursor: pointer;
		
		&:before {
			box-sizing: border-box;
			display: inline-block;
			position: relative;
			top: 50%; left: 0;
			margin: -$check-d/2 $check-d/4;
			border: solid .125em $ctrl-ll;
			padding: .25em;
			width: $check-d; height: $check-d;
			color: transparent;
			font: 900 1em/.5 sans-serif;
			text-indent: -.125em;
			transition: .3s ease-out;
			content: '✓';
		}
	}
	
	&:focus + label {
		outline: solid 1px $ctrl-hl;
	}
	
	&:checked + label {
		&, &:before { color: $ctrl-hl; }
	}
}

[type='range'] {
	border: solid 0 transparent;
	border-width: 0 1em;
	padding: 0;
	width: $track-w; height: $track-h + 2em;
	background: none;
	font-size: 1em;
	cursor: pointer;
	
	&, 
	&::-webkit-slider-runnable-track, 
	&::-webkit-slider-thumb {
		-webkit-appearance: none;
	}
	
	&::-webkit-slider-runnable-track {
		@include track();
	}
	&::-moz-range-track {
		@include track();
	}
	&::-ms-track {
		border: none;
		@include track();
		color: transparent;
	}
	
	&::-moz-range-progress {
		@include fill();
	}
	&::-ms-fill-lower {
		@include fill();
	}
	
	&::-webkit-slider-thumb {
		margin-top: .5*($track-h - $thumb-d);
		@include thumb();
	}
	&::-moz-range-thumb {
		@include thumb();
	}
	&::-ms-thumb {
		@include thumb();
	}
	
	&:focus {
		outline: solid 1px $ctrl-hl;
	}
	
	+ output { order: 0; }
}

output {
	border-radius: 3px;
	background: $ctrl-hl;
	
	&:empty { opacity: 0; }
}
            
          
!
            
              const DIM = 1600, 
			N_TYPES = 2, 
			T_PAUSE = 100;

var k, r = .5*1600, p, data, auto = $$('#auto').checked, 
		dk = .01, rID = null, pause = false, t0, 
		prog = $$('#prog'), out = $$('#out'), 
		style = $.body._add('', null, 'style');

var Polygon = function(f = 0, n = 3) {
	var vertices, sym, poly_s, poly_c, f = f;
	
	this.draw = function(k) {
		var plist = [], j;
		
		if(k > .5) k = 1 - k;
		k *= pow(-1, f);
		
		if(f) {
			plist = vertices.map(c => c.map(c => round(c*k)));
		}
		else {
			for(var i = 0; i < n; i++) {
				j = (i + 1)%n;
				
				plist.push(pmix(vertices[i], vertices[j], k));
				plist.push(pmix(vertices[j], vertices[i], k));
			}
		}
		
		poly_c._attr({
			'points': plist.join(' ')
		});
		poly_s._attr({
			'points': plist.join(' ')
		});
	};
	
	this.switch = function() {
		f = 1 - f;
	};
	
	this.init = (function() {
		var ba = 2*PI/n, ca;
		
		vertices = [];
		poly_s = $$('#ps-' + f);
		poly_c = $$('#pc-' + f);
		sym = $$('#s-' + f);
		sym._attr({'viewBox': [-r, -r, DIM, DIM].join(' ')});
		
		for(var i = 0; i < n; i++) {
			ca = i*ba -.5*PI;
			vertices.push([r*cos(ca), r*sin(ca)]);
		}
	})();
};

var mix = function(a, b, k) {
	return round((1 - k)*a + k*b);
};

var pmix = function(a, b, k) {	
	return [mix(a[0], b[0], k), mix(a[1], b[1], k)];
}

var update = function(fa) {
	var newK = prog.value*.01, 
			f, j, tz, oc = '', err = .005;
	
	if(newK !== k) {
		for(var i = 0; i < N_TYPES; i++) {
			if((newK > .5 && k <= .5) || 
				 (newK <= .5 && k > .5))
				p[i].switch();
			p[i].draw(newK);
		}
		
		if(fa !== 1) pause = false;
		
		k = newK;
		f = (k > .5)*1;
		j = (f ? (1 - k) : k);
		tz = (1 - 2*j)*data.dr4hedron;
		style.textContent = 
			'.type-' + (1 - f) + 
			'{transform: translateZ(' + tz + 'px)}';
		
		switch(true) {
			case j < err:
				oc = 'tetrahedron';
				pause = true;
				break;
			case abs(j - .333) < err:
				oc = 'truncated tetrahedron';
				pause = true;
				break;
			case abs(j - .5) < err:
				oc = 'octahedron';
				pause = true;
		}
		
		out.textContent = oc;
	}
};

var ani = function(t) {
	if(pause) {
		if(!t0) t0 = t;
		else if(t - t0 > T_PAUSE) {
			pause = false;
			t0 = null;
		}
	}
	else {
		if(!(k*(1 - k))) dk *= -1;
		prog.value = round(((k || 0) + dk)*100);
		update(1);
	}
	
	rID = requestAnimationFrame(ani.bind(this, ++t));
};

(function init() {
	var _el = $$('.s2d')[0], 
			_s = getComputedStyle(_el), 
			_dim = _s.width.split('px')[0], _a;
	
	p = [];
	data = {
		'n3gon': 3, 
		'rc3gon': .5*_dim
	};
	data.ca3gon = 2*PI/data.n3gon;
	data.a3gon = (data.n3gon - 2)*PI/data.n3gon;
	_a = .5*data.ca3gon;
	data.ri3gon = data.rc3gon*cos(_a);	
	data.l3gon = 2*data.rc3gon*sin(_a);
	data.h3gon = data.l3gon*sin(data.a3gon);
	data.da4hedron = acos(data.ri3gon/data.h3gon);
	data.ri4hedron = data.rc3gon/tan(data.da4hedron);
	data.rc4hedron = data.rc3gon/sin(data.da4hedron);
	data.dr4hedron = data.rc4hedron - data.ri4hedron;
		
	for(var i = 0; i < N_TYPES; i++)
		p.push(new Polygon(i));
	
	update();
	
	if(auto) ani(0);
})();

prog.addEventListener('input', update, false);
prog.addEventListener('update', update, false);

addEventListener('click', function(e) {
	var tg = e.target;
	
	if(tg.tagName.toLowerCase() === 'label') {
		auto = !auto;
		
		if(auto) ani(0);
		else {
			cancelAnimationFrame(rID);
			rID = null;
		}
	}
}, false);
            
          
!
999px
Loading ..................

Console