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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                html, body {
	background: black;
}
              
            
!

JS

              
                "use strict";

const particleCount = 2000;
const particleProps = [
    "x",
    "y",
    "z",
    "vx",
    "vy",
    "vz",
    "life",
    "ttl",
    "speed"
];
const rangeY = 100;
const rangeZ = 200;
const baseTTL = 50;
const rangeTTL = 200;
const baseHue = rand(360);
const rangeHue = 100;
const xc = 0.0005;
const yc = 0.0005;
const zc = 0.0005;
const tc = 0.0005;
const noiseSteps = ceil(rand(4)) + 4;
const backgroundColor = "hsla(0,0%,0%,0.5)";
const { buffer, ctx } = createRenderingContext()

let center;
let tick;
let simplex;
let particles;

function setup() {
	center = [];
	resize();
	initParticles();

	draw();
}

function initParticles() {
	tick = 0;
	simplex = new SimplexNoise();
	particles = new PropsArray(particleCount, particleProps);

	let i = 0;
	
	for (; i < particles.length; i += particleProps.length) {
		initParticle(i);
	}
}

function initParticle(i) {
	let x, y, z, vx, vy, vz, life, ttl, speed, radius, hue;

	x = rand(buffer.canvas.width);
	y = center[1] + randIn(-rangeY, rangeY);
	z = rand(rangeZ);
	vx = 0;
	vy = 0;
	vz = 0;
	life = 0;
	ttl = baseTTL + rand(rangeTTL);
	speed = 0;

	particles.set([x, y, z, vx, vy, vz, life, ttl, speed], i);
}

function drawParticles() {
	let i = 0;

	for (; i < particles.length; i += particleProps.length) {
		updateParticle(i);
	}
}

function updateParticle(i) {
	let n, theta, phi, cosPhi, x2, y2, z2, width, hue;

	let [x, y, z, vx, vy, vz, life, ttl, speed] = particles.get(i);

	n = simplex.noise4D(x * xc, y * yc, z * zc, tick);
	theta = n * TAU * noiseSteps;
	phi = (0 - n) * PI * noiseSteps;
	cosPhi = cos(phi);
	vx = lerp(vx, cos(theta) * cosPhi, 0.0725);
	vy = lerp(vy, sin(phi), 0.0725);
	vz = lerp(vz, sin(theta) * cosPhi, 0.0725);
	x2 = x + vx * speed;
	y2 = y + vy * speed;
	z2 = z + vz * speed;
	width = 0.015 * z2 + 2;
	speed = lerp(speed, 0.05 * z2, 0.15);
	hue = baseHue + speed * 0.035 * rangeHue;

	drawParticle(x, y, z, x2, y2, life, ttl, width, hue);

	life++;

	particles.set([x2, y2, z2, vx, vy, vz, life, ttl, speed], i);

	(checkBounds(x, y, width) || life > ttl) && initParticle(i);
}

function drawParticle(x, y, z, x2, y2, life, ttl, width, hue) {
	buffer.save();
	buffer.lineWidth = width;
	buffer.strokeStyle = `hsla(${hue + clamp(z, 0, 180)},${clamp(z, 10, 60)}%,${clamp(z, 10, 50)}%,${fadeInOut(life, ttl) * (0.01 * clamp(z, 50, 100))})`;
	buffer.beginPath();
	buffer.moveTo(x, y);
	buffer.lineTo(x2, y2);
	buffer.stroke();
	buffer.closePath();
	buffer.restore();
}

function checkBounds(x, y, r) {
	return x > r + buffer.canvas.width || x < -r || y > r + buffer.canvas.height || y < -r;
}

function resize() {
	const { innerWidth, innerHeight } = window;

	buffer.canvas.width = innerWidth;
	buffer.canvas.height = innerHeight;

	buffer.drawImage(ctx.canvas, 0, 0);

	ctx.canvas.width = innerWidth;
	ctx.canvas.height = innerHeight;

	ctx.drawImage(buffer.canvas, 0, 0);

	center[0] = 0.5 * buffer.canvas.width;
	center[1] = 0.5 * buffer.canvas.height;
}

function renderGlow() {
	ctx.save();
	ctx.filter = "blur(8px) brightness(200%)";
	ctx.globalCompositeOperation = "lighter";
	ctx.drawImage(buffer.canvas, 0, 0);
	ctx.restore();

	ctx.save();
	ctx.filter = "blur(4px) brightness(200%)";
	ctx.globalCompositeOperation = "lighter";
	ctx.drawImage(buffer.canvas, 0, 0);
	ctx.restore();
}

function renderToScreen() {
	ctx.save();
	ctx.globalCompositeOperation = "lighter";
	ctx.drawImage(buffer.canvas, 0, 0);
	ctx.restore();
}

function drawBackground() {
	buffer.clearRect(0, 0, buffer.canvas.width, buffer.canvas.height);

	ctx.fillStyle = backgroundColor;
	ctx.fillRect(0, 0, buffer.canvas.width, buffer.canvas.height);
}

function draw() {
	tick += tc;

	drawBackground();
	drawParticles();
	renderToScreen();
	renderGlow();

	window.requestAnimationFrame(draw);
}

window.addEventListener("load", setup);
window.addEventListener("resize", resize);

              
            
!
999px

Console