Here’s a fun, full-screen, generative “cosmic particles” artwork using HTML, CSS and JavaScript. Just copy the code into an index.html file and open it in your browser. Enjoy! 💖✨
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>✨ Cosmic Particles ✨</title>
<style>
/* reset & full-screen canvas */
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: hidden; background: #000; }
canvas { display: block; }
/* optional button to regenerate colors */
#regen {
position: absolute;
top: 20px; left: 50%; transform: translateX(-50%);
padding: 8px 16px;
font-size: 16px;
background: rgba(255,255,255,0.2);
color: #fff;
border: 1px solid #fff;
border-radius: 4px;
cursor: pointer;
transition: background .3s;
}
#regen:hover { background: rgba(255,255,255,0.4); }
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<button id="regen">🔄 Regenerate</button>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let particles = [];
const PARTICLE_COUNT = 150;
let w, h;
// Resize canvas to full window
function resize() {
w = canvas.width = window.innerWidth;
h = canvas.height = window.innerHeight;
}
window.addEventListener('resize', resize);
resize();
// Particle class
class Particle {
constructor() {
this.reset();
}
reset() {
this.x = Math.random() * w;
this.y = Math.random() * h;
this.vx = (Math.random() - 0.5) * 1.2;
this.vy = (Math.random() - 0.5) * 1.2;
this.size = Math.random() * 3 + 1;
this.hue = Math.random() * 360;
}
update() {
this.x += this.vx;
this.y += this.vy;
this.hue = (this.hue + 0.5) % 360;
// wrap around edges
if (this.x < -50 || this.x > w + 50 ||
this.y < -50 || this.y > h + 50) {
this.reset();
}
}
draw() {
ctx.beginPath();
ctx.fillStyle = `hsl(${this.hue}, 80%, 60%)`;
ctx.arc(this.x, this.y, this.size, 0, Math.PI*2);
ctx.fill();
}
}
// Initialize particles
function initParticles() {
particles = [];
for (let i = 0; i < PARTICLE_COUNT; i++) {
particles.push(new Particle());
}
}
initParticles();
// Animation loop
function animate() {
// semi-transparent fill for fading trails
ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';
ctx.fillRect(0, 0, w, h);
// draw & update each particle
particles.forEach(p => {
p.draw();
p.update();
});
requestAnimationFrame(animate);
}
animate();
// regenerate colors & positions
document.getElementById('regen').addEventListener('click', () => {
initParticles();
});
// optional: interact on mousemove (spawn tiny particles)
canvas.addEventListener('mousemove', (e) => {
const p = new Particle();
p.x = e.clientX;
p.y = e.clientY;
p.size = Math.random()*2 + 1;
particles.push(p);
if (particles.length > PARTICLE_COUNT * 2) {
particles.splice(0, PARTICLE_COUNT/2);
}
});
</script>
</body>
</html>
How it works 🎨
- A
<canvas> covers the entire screen.
- We create an array of “particles,” each with a position, velocity, size and a hue.
- On each animation frame we:
- Draw a semi-transparent black rectangle over the whole canvas (to create trailing/fading effects).
- Update each particle’s position, hue & draw it as a colored circle.
- Click Regenerate to reset the swarm’s colors and positions.
- Move your mouse over the canvas to sprinkle in a few extra sparkles!
Feel free to tweak:
• PARTICLE_COUNT (more/fewer particles)
• velocity multipliers
• color ranges (e.g. saturation/lightness in HSL)
• blend modes or shape types
Enjoy your cosmic dance of particles! 🚀✨