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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                .re-render
    Random scene
#canvas
              
            
!

CSS

              
                .re-render {
    color: #222;
    padding: 12px 20px;
    border: solid 2px #222;
    border-radius: 5px;
    position: fixed;
    top: 20px; right: 20px;
    &:hover {
        cursor: pointer;
        color: whitesmoke;
        background: #222;
    }
}
              
            
!

JS

              
                var container;
var camera, scene, renderer;

var geometry, group;

var mouseX = 0,
    mouseY = 0;

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

document.addEventListener('mousemove', onDocumentMouseMove, false);

init();
animate();

$(".re-render").click(function() {
    init();
});

function init() {

    ////////////////////////////////////
    // Setup Container
    ////////////////////////////////////
    container = document.createElement('div');
    $("#canvas").html(container);

    ////////////////////////////////////
    // Choose random scene
    ////////////////////////////////////
    var sceneThemes = [
        {
            name: "spheres",
            geometry: new THREE.SphereGeometry(10, 30, 30),
            color: 0x3890b0,
            lightColor: 0xada02e,
            cameraZ: 10,
            createObjects: function() {
                for (var i = 0; i < 5000; i++) {
                    var mesh = new THREE.Mesh(theme.geometry, material);
                    mesh.position.x = Math.random() * 2000 - 1000;
                    mesh.position.y = Math.random() * 2000 - 1000;
                    mesh.position.z = Math.random() * 2000 - 1000;

                    mesh.rotation.x = Math.random() * 2 * Math.PI;
                    mesh.rotation.y = Math.random() * 2 * Math.PI;

                    mesh.matrixAutoUpdate = false;
                    mesh.updateMatrix();

                    group.add(mesh);
                }
            }
        },
        {
            name: "torusKnot",
            geometry: new THREE.TorusKnotGeometry( 400, 70, 400, 16 ),
            color: 0xcccccc,
            lightColor: 0xddddddd,
            cameraZ: 1000,
            createObjects: function() {
                var mesh = new THREE.Mesh(theme.geometry, material);

                mesh.matrixAutoUpdate = false;
                mesh.updateMatrix();

                group.add(mesh);
            }
        },
        {
            name: "cubes",
            geometry: new THREE.CubeGeometry(30, 30, 30),
            color: 0x3890b0,
            lightColor: 0xd9cd42,
            cameraZ: 5,
            createObjects: function() {
                for (var i = 0; i < 2000; i++) {
                    var mesh = new THREE.Mesh(theme.geometry, material);
                    mesh.position.x = Math.random() * 2000 - 1000;
                    mesh.position.y = Math.random() * 2000 - 1000;
                    mesh.position.z = Math.random() * 2000 - 1000;

                    mesh.rotation.x = Math.random() * 2 * Math.PI;
                    mesh.rotation.y = Math.random() * 2 * Math.PI;

                    mesh.matrixAutoUpdate = false;
                    mesh.updateMatrix();

                    group.add(mesh);
                }
            }
        },
          {
            name: "sticks",
            geometry: new THREE.CubeGeometry(3, 3, 100),
            color: 0x3890b0,
            lightColor: 0xada02e,
            cameraZ: 5,
            createObjects: function() {
                for (var i = 0; i < 2000; i++) {
                    var mesh = new THREE.Mesh(theme.geometry, material);
                    mesh.position.x = Math.random() * 2000 - 1000;
                    mesh.position.y = Math.random() * 2000 - 1000;
                    mesh.position.z = Math.random() * 2000 - 1000;

                    mesh.rotation.x = Math.random() * 2 * Math.PI;
                    mesh.rotation.y = Math.random() * 2 * Math.PI;

                    mesh.matrixAutoUpdate = false;
                    mesh.updateMatrix();

                    group.add(mesh);
                }
            }
         }
    ];

    // Select a random theme
    var theme = sceneThemes[Math.floor(Math.random() * sceneThemes.length)];

    ////////////////////////////////////
    // Setup Scene
    ////////////////////////////////////
    scene = new THREE.Scene();
    scene.fog = new THREE.Fog( 0xF5F5F5, 1, 10000 );

    // Set Material
    var material = new THREE.MeshLambertMaterial({
        emissive: theme.color
    });

    // Create the group to place the objects
    group = new THREE.Group();

    // Create the objects
    theme.createObjects();

    // Add Group to scene
    scene.add(group);

    ////////////////////////////////////
    // Setup Camera
    ////////////////////////////////////
    camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 10000);
    camera.position.z = theme.cameraZ;

    ////////////////////////////////////
    // Lighting
    ////////////////////////////////////

    // create a point light
    var pointLight = new THREE.DirectionalLight(theme.lightColor);

    // set its position
    pointLight.position.x = 20;
    pointLight.position.y = 120;
    pointLight.position.z = 0;

    // add to the scene
    scene.add(pointLight);

    ////////////////////////////////////
    // Rendering
    ////////////////////////////////////

    renderer = new THREE.WebGLRenderer({ antialias: true } );
    renderer.setClearColor(0xF5F5F5);
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.sortObjects = false;

    container.appendChild(renderer.domElement);
    window.addEventListener('resize', onWindowResize, false);
}

 /////////////////////////////////
// Feel free to resize that windah, Y'all!
// We all know a good pen should resize when you be adjustin' that little screen dat errythang renders into, knah mean?
////////////////////////////////////
function onWindowResize() {
    windowHalfX = window.innerWidth / 2;
    windowHalfY = window.innerHeight / 2;

    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();

    renderer.setSize(window.innerWidth, window.innerHeight);
}

function onDocumentMouseMove(event) {
    mouseX = (event.clientX - windowHalfX) * 5;
    mouseY = (event.clientY - windowHalfY) * 5;
}

function animate() {
    requestAnimationFrame(animate);

    render();
}

function render() {

    var time = Date.now() * 0.001;

    var rx = Math.sin(time * 0.3) * 0.5,
        ry = Math.sin(time * 0.3) * 0.5,
        rz = Math.sin(time * 0.3) * 0.5;

    camera.position.x += (mouseX - camera.position.x) * .05;
    camera.position.y += (-mouseY - camera.position.y) * .05;

    camera.lookAt(scene.position);

    group.rotation.x = rx / 5;
    group.rotation.y = ry / 5;
    group.rotation.z = rz / 5;

    renderer.render(scene, camera);
}
              
            
!
999px

Console