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

              
                
              
            
!

CSS

              
                * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    height: 100%;
}

body {
    overflow: hidden;
    background: linear-gradient(to bottom, #3AB795, #37AD8D);
    background-color: #3ab795;
    background-image: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM12 60c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z' fill='%2337ad8d' fill-opacity='1' fill-rule='evenodd'/%3E%3C/svg%3E");
}

@keyframes fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

canvas {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    animation: fade-in 1.5s linear;
    animation-fill-mode: forwards;
    background: #3c3c3c;
    border-radius: .3rem;
    box-shadow: 0 .5rem 2rem rgba(0, 0, 0, .5);
}

              
            
!

JS

              
                // ------------------------------------------------------------
// Global constants
// ------------------------------------------------------------

const SCAN_STEP = 0.4; // Decrease it to increase the number of particles
const RANDOM_MAX = 0.3;
const MIN_DISTANCE = 0.05;
const ACCELERATION_FACTOR = 0.01;



// ------------------------------------------------------------
// Global variables
// ------------------------------------------------------------

let camera, scene, renderer;

let particles = {
    free: null,
    fixed: null,

    old: {
        free: null,
        fixed: null
    }
};


// ------------------------------------------------------------
// Start!
// ------------------------------------------------------------

main();


// ------------------------------------------------------------
// Functions
// ------------------------------------------------------------

function main() {
    init();
    animate();
}


function init() {
    camera = new THREE.PerspectiveCamera(
        100,
        window.innerWidth / window.innerHeight,
        0.01,
        10
    );
    
    camera.position.z = 1;

    scene = new THREE.Scene();

    particles.free  = getFreeParticles();
    particles.fixed = getFixedParticles();

    scene.add(particles.free);

    renderer = new THREE.WebGLRenderer({
        antialias: true,
        alpha: true,
    });

    renderer.setSize(window.innerWidth * 0.8, window.innerHeight * 0.8);
    
    window.addEventListener('resize', () => {
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize(window.innerWidth * 0.8, window.innerHeight * 0.8);
    });

    document.body.appendChild(renderer.domElement);
}


function animate() {
    // Rotate fixed points here (it's alone in this example, but you can add more)
    particles.fixed.geometry.vertices[0].applyAxisAngle(
        new THREE.Vector3(0, 0, 1),
        -0.05
    );

    renderer.render(scene, camera); 

    calculateNextStep();
    requestAnimationFrame(animate);
}


function calculateNextStep() {
    if (!particles.old.free || !particles.old.fixed) {
        particles.old.free  = particles.free.clone();
        particles.old.fixed = particles.fixed.clone();

        return;
    }

    // Move particles to the fixed points with gravity
    particles.free.geometry.vertices.forEach((currentPosition, index) => {
        const acceleration = calculateAcceleration(currentPosition);
        const oldPosition  = particles.old.free.geometry.vertices[index].clone();
        const newPosition  = currentPosition.clone();

        oldPosition.negate();

        newPosition.multiplyScalar(2);
        newPosition.add(oldPosition);
        newPosition.add(acceleration);

        particles.old.free.geometry.vertices[index].copy(currentPosition);
        currentPosition.copy(newPosition);
    });

    // If distance between particles is too short we increase it
    loopPairs(particles.free.geometry.vertices, (A, B) => {
        const L = A.clone().add(B.clone().negate());
        const length = L.length();

        if (length !== 0 && length < MIN_DISTANCE) {
            const d = (length - MIN_DISTANCE) / (2 * length);

            L.multiplyScalar(d);

            B.add(L);
            A.add(L.negate());
        }
    });

    particles.free.geometry.verticesNeedUpdate = true;
}


function calculateAcceleration(position) {
    let point  = position.clone(),
        result = new THREE.Vector3();

    point.negate();

    // And again, it's only one fixed point in this example, but you can add more
    particles.fixed.geometry.vertices.forEach((fixedPoint, index) => {
        const temp = fixedPoint.clone();

        temp.add(point);

        const length = temp.length();

        if (length > 0) {
            temp.multiplyScalar(1 / (length * length));
        }

        result.add(temp);
    });

    result.multiplyScalar(ACCELERATION_FACTOR);

    return result;
}


function getFreeParticles() {
    let geometry = new THREE.Geometry();

    scanXYZ((x, y, z) => {
        let point = new THREE.Vector3(x, y, z);

        point.add(getRandomVector3());
        geometry.vertices.push(point);
    });

    return new THREE.Points(
        geometry,
        getPointMaterial()
    );
}


function getFixedParticles() {
    let geometry = new THREE.Geometry();

    geometry.vertices.push(new THREE.Vector3(1, 0, 0));

    return new THREE.Points(
        geometry,
        getPointMaterial()
    );
}


function getPointMaterial() {
    return new THREE.PointsMaterial({
        color: 0xffffff,
        size: .04,
        side: THREE.DoubleSide,
        transparent: true,
        blending: THREE.AdditiveBlending,
        map: generatePointSprite()
    });
}


function generatePointSprite() {
    let canvas = document.createElement('canvas');

    canvas.width = 16;
    canvas.height = 16;

    let context = canvas.getContext('2d');
    let gradient = context.createRadialGradient(
        canvas.width  / 2,
        canvas.height / 2,
        0,
        canvas.width  / 2,
        canvas.height / 2,
        canvas.width  / 2
    );

    gradient.addColorStop(0,   'rgba(255,255,225,1)');
    gradient.addColorStop(0.2, 'rgba(242,240,225, 1)');
    gradient.addColorStop(0.4, 'rgba(242,240,255,0.1)');
    gradient.addColorStop(1,   'rgba(0,0,0,0)');

    context.fillStyle = gradient;
    context.fillRect(0, 0, canvas.width, canvas.height);

    let texture = new THREE.Texture(canvas);

    texture.needsUpdate = true;

    return texture;
}


function getRandomVector3() {
    const r = RANDOM_MAX;

    return new THREE.Vector3(
        Math.random() * r,
        Math.random() * r,
        Math.random() * r
    );
}


function scanXYZ(callback) {
    const step = SCAN_STEP;

    for (let x = -1; x <= 1; x += step) {
        for (let y = -1; y <= 1; y += step) {
            for (let z = -1; z <= 1; z += step) {
                callback(x, y, z);
            }
        }
    }
}


function loopPairs(arr, callback) {
    for (let i = 0; i < arr.length - 1; i++) {
        for (let j = i + 1; j < arr.length - 1; j++) {
            callback(arr[i], arr[j]);
        }
    }
}
              
            
!
999px

Console