css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              Math.TWO_PI = Math.PI * 2;

const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');

var tendrils = [];

class Vector {

    constructor(x, y, z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    add(value) {
        if(value instanceof Vector) {
            this.x += value.x;
            this.y += value.y;
        } else {
            this.x += value;
            this.y += value;
        }
        return this;
    }

    multiply(value) {
        this.x *= value;
        this.y *= value;
    }

    divide(value) {
        if(value instanceof Vector) {
            this.x = this.x / value.x;
            this.y = this.y / value.y;
        } else {
            this.x = this.x / value;
            this.y = this.y / value;
        }
        return this;
    }
    
    length() {
        return Math.sqrt(this.dot(this));
    }

    normalize() {
        return this.divide(this.length());
    }
}

class Tendril {

    constructor({ angle, x, y }) {
		
        this.position = new Vector(x, y);
        this.velocity = new Vector(x * 0.025, y * 0.025);
        this.acceleration = new Vector();
        this.hue = 250 + Math.sin(angle) * 15;
        this.alpha = 0.25;
        this.fade = 0.9885;
        this.mass = Math.floor(1 + Math.random() * 10);
    }

    get x() { return this.position.x; }
    get y() { return this.position.y; }

    update(time) {
		
        let sample = noise.simplex2(this.x * 0.005, this.y * 0.005);
        let angle = sample * Math.PI;
        
		this.acceleration.x = Math.cos(angle) * this.mass;
        this.acceleration.y = Math.sin(angle) * this.mass;
        this.velocity.x += ( this.acceleration.x - this.velocity.x ) / this.mass;
        this.velocity.y += ( this.acceleration.y - this.velocity.y ) / this.mass;
        this.position.add(this.velocity);
    }

    draw(context) {

        context.beginPath();
        context.arc(this.x, this.y, 1, 0, Math.TWO_PI);
        context.fillStyle = `hsla(${this.hue},71%,55%,${this.alpha})`;
        context.fill();

        this.alpha *= this.fade;
    }
}

function loop(time) {

    loop.handle = requestAnimationFrame(loop);

    let { x, y } = canvas.center;

    context.save();
    context.translate(x, y);
    context.globalCompositeOperation = 'lighter';

    tendrils = tendrils.filter(function(tendril) {

        tendril.update();
        tendril.draw(context);

        return tendril.alpha > 0.01 && tendril.x > -x && tendril.x < x && tendril.y > -y && tendril.y < y;
    });

    context.restore();

    if( ! tendrils.length) {

        console.log('done');
        cancelAnimationFrame(loop.handle);
        loop.handle = null;
    }
}

function resize() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    canvas.center.x = window.innerWidth * 0.5;
    canvas.center.y = window.innerHeight * 0.5;
}

function reset() {

    let seed = Math.random();
	noise.seed(seed);
	
    let angle = 0;
    let limit = 700;
    let radius = 100;
    let slice = Math.TWO_PI / limit;

    tendrils = [];

    for(let i = 0; i < limit; i++) {

        let x = Math.cos(angle);
        let y = Math.sin(angle);
        let displacement = radius + (noise.simplex2(x, y) * 30);

        x = x * displacement;
        y = y * displacement;

        tendrils.push(new Tendril({ angle, x, y }));
        angle += slice;
    }
	
	context.clearRect(0, 0, canvas.width, canvas.height);
	loop.handle || requestAnimationFrame(loop);
}

function init() {

    canvas.center = {};

    document.body.style.cssText = 'margin:0;padding:0;background:#111;overflow:hidden;';
    document.body.appendChild(canvas);
    document.body.addEventListener('click', reset);
	
	window.addEventListener('resize', resize);
	window.addEventListener('keyup', event => event.which == 32 && reset());
	
	resize();
    reset();
}

init();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console