Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="react-container"></div>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Roboto);
* {margin:0;padding:0;border:0;box-sizing:border-box;}
html,body {height:100%;}
body {background:#000;overflow:hidden;color:#fff;font-family:'Roboto',sans-serif;}
canvas {position:fixed;top:0;left:50%;transform:translateX(-50%);}

.kaleidoscopeContainer {
	width:100vw;
  height:100vh;
}

.spaceBackground {
	background:url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/2144/starfield_background_nasa.jpg) center center;
}

#config {
	background:rgba(#111, 0.5);
	width:280px;
	height:100%;
	position:absolute;
	z-index:200;
	padding:14px 40px 14px 14px;
	transition:transform 200ms ease-out;
	
	&.closed {
		transform:translateX(-240px);
	}
	
	a.action {
		position:absolute;
		right:8px;
	}
	
	form {
		
		span {
			display:block;
			margin:4px 0;
		}
		
		p {margin:24px 0 8px 0;}
		
		label {
			text-transform:uppercase;
			display:inline-block;
			padding-bottom:1px;
			border-bottom:2px solid rgba(255,255,255,0.25);
			font-size:12px;
			width:160px;
			margin-left:4px;
		}
		input[type=checkbox] + label {
			border-color:transparent;
		}
		
		input[type=number] {
			width:40px;
			color:#fff;
			background:transparent;
			font-family:'Roboto';
			border-bottom:2px solid rgba(255,255,255,0.25);
			padding-bottom:1px;
			font-size:16px;
			margin-left:4px;
			vertical-align:1px;
			
			&:focus {
				outline:0;
				border-color:rgba(255,255,255,0.75);
			}
		}
		
	}
}
              
            
!

JS

              
                /*

NOTE:

THREE.FlatShading doesn't work on MeshNormalMaterial right 
now, I'm looking into how to recreate that (last time I actively worked
on this was several Three.js versions ago, a lot has changed lol)

NOTE AGAIN:
Now there's just no shading on MeshNormalMaterial any more lol,
but FlatShading is the default

*/

const defaultConfig = {
	// -------------- // 
	number: 20, // number of objects
	boundaries: 10, // example: 20 means position = THREE.Math.randInt(-20,20)
	size: 3, // object size
	// -------------- //
	kaleidoscope: true,
	sides: 6, // number of kaleidoscope sides
	angle: 45, // kaleidoscope angle, in degrees
	// -------------- //
	colorshift: false, // RGB color shift shader filter
	smooth: false, // smooth shading
	wireframe: false, // wireframe mode
	space: false // space background
	// -------------- //
};


class Canvas extends React.PureComponent {
  constructor(props) {
    super(props);
    
    this.init = this.init.bind(this);
    this.animate = this.animate.bind(this);
    this.renderScene = this.renderScene.bind(this);
  }

  componentDidMount() {
		this.init();
		this.animate();
		
    window.addEventListener('resize', function() {

    	this.camera.updateProjectionMatrix();
    	this.renderer.setSize(window.innerHeight, window.innerHeight);

    }.bind(this), false);
  }
		
	componentDidUpdate() {
		this.init();
	}
	
	componentWillUnmount() {
		cancelAnimationFrame(this.animate);
	}
  
  init() {
		
		if (this.renderer && this.container) {
			this.container.removeChild(this.renderer.domElement);
		}

		this.scene = new THREE.Scene();
    this.camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);

    this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
    this.renderer.setSize( window.innerHeight, window.innerHeight );
    this.container.appendChild( this.renderer.domElement );
    
		if (this.props.smooth) {
			this.geometry = new THREE.SphereGeometry(this.props.size, 3, 3);
		} else {
	    this.geometry = new THREE.IcosahedronGeometry(this.props.size, 0);		
		}

    this.material = new THREE.MeshNormalMaterial({
      wireframe: this.props.wireframe
    });

    this.group = new THREE.Object3D();
    
    for (var i = 0; i < this.props.number; i++) {
      
      let mesh = new THREE.Mesh(this.geometry, this.material);

      let b = this.props.boundaries;
      mesh.position.set(
        THREE.Math.randInt(-b, b),
        THREE.Math.randInt(-b, b),
        THREE.Math.randInt(-b, b)
      );
      
      this.group.add(mesh);
      
    }
    
    this.scene.add(this.group);

    this.light = new THREE.DirectionalLight(0xFFFFFF);
    this.light.position.set(0, 0, 250);
    this.scene.add(this.light);

    this.camera.position.set(0, 0, 40); 
    
    // postprocessing
    if (this.props.kaleidoscope || this.props.colorshift) {
      // support transparency thanks to https://codepen.io/SephReed/pen/jWWEQE
      var renderTarget = new THREE.WebGLRenderTarget(
        window.innerHeight,
        window.innerHeight,
        {
					//texture: {
          	minFilter: THREE.LinearFilter, 
          	magFilter: THREE.LinearFilter, 
	          format: THREE.RGBAFormat, 
					//},
          stencilBuffer: false 
        }
      );

      this.composer = new THREE.EffectComposer(this.renderer, renderTarget);
      this.composer.addPass(new THREE.RenderPass(this.scene, this.camera));    
    }

    if (this.props.kaleidoscope) {
      var effect = new THREE.ShaderPass(THREE.KaleidoShader);
      effect.uniforms['sides'].value = this.props.sides;
      effect.uniforms['angle'].value = this.props.angle * Math.PI / 180;
      this.composer.addPass(effect);
    }
    
    if (this.props.colorshift) {
      var effect = new THREE.ShaderPass( THREE.RGBShiftShader );
      effect.uniforms['amount'].value = 0.005;
      this.composer.addPass(effect);
    }
    
    if (this.props.kaleidoscope || this.props.colorshift) {
      effect.renderToScreen = true;
    }
  }

  animate() {
    this.animation = requestAnimationFrame(this.animate);

  	this.group.rotation.x += 0.01;
  	this.group.rotation.y += 0.01;
  	this.group.rotation.z += 0.01;

  	this.renderScene();
  }

  renderScene() {
    if (this.props.kaleidoscope || this.props.colorshift) {
  		this.composer.render();	
  	} else {
  		this.renderer.render(this.scene, this.camera);
  	}
  }
  
  render() {    
    return (<div ref={(ref) => { this.container = ref; }}></div>)
  }
}



class Kaleidoscope extends React.Component {
	
	constructor(props) {
		super();
		
		this.state = Object.assign({}, defaultConfig, {
			open: true
		});
		
		this.toggleConfig = this.toggleConfig.bind(this);
		this.handleInput = this.handleInput.bind(this);
		this.renderNumberInput = this.renderNumberInput.bind(this);
		this.renderCheckboxInput = this.renderCheckboxInput.bind(this);
		
	}
	
	toggleConfig(event) {
		this.setState(function(prevState) {
			return { open: !prevState.open }
		});
	}
	
	handleInput(field, value) {
		var state = Object.assign({}, this.state);
		state[field] = value;
		this.setState(state);
	}
	
	// render
	
	renderNumberInput(field, label) {
		return (<span>
			<label htmlFor={field}>{label}</label>
			<input
				type="number"
				value={this.state[field]}
				id={field}
				data-field={field}
				onChange={(event) => { this.handleInput(field, event.target.value) }}
			/>
		</span>)
	}
	
	renderCheckboxInput(field, label) {
		return (<span>
			<input
				type="checkbox"
				checked={this.state[field]}
				id={field}
				data-field={field}
				onChange={(event) => { this.handleInput(field, event.target.checked) }}
			/>
			<label htmlFor={field}>{label}</label>
		</span>)
	}
	
	renderConfig() {
		let className = this.state.open ? 'open' : 'closed';
		
		return (<aside id="config" className={className}>
			<a className="action" onClick={this.toggleConfig}>
				<svg width="24" height="24" viewBox="0 0 24 24">
					<title>Options</title>
					<path fill="#FFF" d="M3 4h5q0.57 0 0.867 0.5l7.711 13.5h4.422q0.414 0 0.707 0.293t0.293 0.707-0.293 0.707-0.707 0.293h-5q-0.57 0-0.867-0.5l-7.711-13.5h-4.422q-0.414 0-0.707-0.293t-0.293-0.707 0.293-0.707 0.707-0.293zM14 4h5q0.414 0 0.707 0.293t0.293 0.707-0.293 0.707-0.707 0.293h-5q-0.414 0-0.707-0.293t-0.293-0.707 0.293-0.707 0.707-0.293z"></path>
				</svg>
			</a>
			<h1>config</h1>
			<form>
				<p>3d</p>
				{this.renderNumberInput('number', 'number of objects')}
				{this.renderNumberInput('boundaries', 'positioning boundaries')}
				{this.renderNumberInput('size', 'object size')}
				<p>kaleidoscope</p>
				{this.renderCheckboxInput('kaleidoscope', 'kaleidoscope mode')}
				{this.renderNumberInput('sides', 'number of sides')}
				{this.renderNumberInput('angle', 'kaleidoscope angle')}
				<p>style</p>
				{this.renderCheckboxInput('colorshift', 'rgb color shift')}
				{this.renderCheckboxInput('smooth', 'smooth shading')}
				{this.renderCheckboxInput('wireframe', 'wireframe mode')}
				{this.renderCheckboxInput('space', 'space mode')}
			</form>
		</aside>);
	}
	
	render() {
		let canvasProps = Object.assign({}, this.state);
		delete canvasProps.open;
		delete canvasProps.space;
		
		let classNames = ['kaleidoscopeContainer', (this.state.space ? 'spaceBackground' : '')].join(' ');
		
		return (<div className={classNames}>
			{this.renderConfig()}
			<Canvas {...canvasProps} />
		</div>);
	}
	
}


ReactDOM.render(<Kaleidoscope />, document.getElementById('react-container'));


              
            
!
999px

Console