Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <html>
    <head>
        <title>Exploding Triangles</title>     
    </head>
    <body>
    </body> 
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                 let scene, camera, renderer;
    let fragments = [];
    let ADD = 0.05;
    const dt = 0.02;
    
    class Fragment {
        constructor(position, velocity, g) {
            this.velocity = velocity;
            this.velocity.multiplyScalar(dt);
   
            let material = new THREE.MeshPhongMaterial({
                            side: THREE.DoubleSide,
                            color: 0xffffff,
                            emissive: 0xfafafa,
                            emissiveIntensity: 0.4,
                            shininess: 100,
                            specular: 0x9d0a00,
                            vertexColors: true
                            });
            
            this.shape = new THREE.Mesh(g, material);
            this.shape.position.copy(position);
        }
        
        move() {
            this.shape.position.add(this.velocity);
            this.shape.rotation.x += ADD;
        }
    };
    
    let createTriangle = function(p1, p2, p3) {
        let geometry = new THREE.Geometry();
        geometry.vertices.push(p1, p2, p3);
        geometry.faces.push( new THREE.Face3(0, 1, 2));
        geometry.computeFaceNormals();
        geometry.computeVertexNormals();
        return geometry;
    };
    
    
    let createGeometry = function() {
        let p1 = new THREE.Vector3(0, 1, 0);
        let p2 = new THREE.Vector3(1, 0, 1);
        let p3 = new THREE.Vector3(-1, 0, 1);
        let p4 = new THREE.Vector3(-1, 0, -1);
        let p5 = new THREE.Vector3(1, 0, -1);
        let p6 = new THREE.Vector3(0, -1, 0);
        
        fragments.push(new Fragment(new THREE.Vector3(0, 0, 0), 
                    new THREE.Vector3(0, 0, 6), createTriangle(p1, p2, p3)));
        
        fragments.push(new Fragment(new THREE.Vector3(0, 0, 0),
                    new THREE.Vector3(-2, 4, 0), createTriangle(p1, p3, p4)));
        fragments.push(new Fragment(new THREE.Vector3(0, 0, 0), 
                    new THREE.Vector3(0, 5, -4), createTriangle(p1, p4, p5)));
        fragments.push(new Fragment(new THREE.Vector3(0, 0, 0), 
                    new THREE.Vector3(2, 3, 0), createTriangle(p1, p5, p2)));
        fragments.push(new Fragment(new THREE.Vector3(0, 0, 0), 
                    new THREE.Vector3(0, -5, 3), createTriangle(p3, p2, p6)));
        fragments.push(new Fragment(new THREE.Vector3(0, 0, 0), 
                    new THREE.Vector3(-4, -3, 0), createTriangle(p6, p3, p4)));
        fragments.push(new Fragment(new THREE.Vector3(0, 0, 0), 
                    new THREE.Vector3(0, -4, -4), createTriangle(p6, p4, p5)));
        fragments.push(new Fragment(new THREE.Vector3(0, 0, 0), 
                    new THREE.Vector3(3, -3, 0), createTriangle(p6, p2, p5)));
        
        fragments.forEach(f => scene.add(f.shape));  
    };
    
    
    // set up the environment - 
    // initiallize scene, camera, objects and renderer
    let init = function() {
        // create the scene
        scene = new THREE.Scene();
        scene.background = new THREE.Color(0x000000);
        
        // create an locate the camera
        camera = new THREE.PerspectiveCamera(75, 
                        window.innerWidth / window.innerHeight, 
                        1, 1000);
        camera.position.z = 20;
        
        let directionalLightUp = new THREE.DirectionalLight( 0xffffff);
        scene.add(directionalLightUp);
        
        createGeometry();
        
        // create the renderer   
        renderer = new THREE.WebGLRenderer();   
        renderer.setSize(window.innerWidth, window.innerHeight);
        
        document.body.appendChild(renderer.domElement);
        
    };
   
    
    // main animation loop - calls 50-60 times per second.
    let mainLoop = function() { 
        fragments.forEach(f => f.move());
         
        renderer.render(scene, camera);
        requestAnimationFrame(mainLoop);
    };
    
    ///////////////////////////////////////////////
    init();
    mainLoop();
              
            
!
999px

Console