Astonishing Web App: Interactive Particle Universe
Here's a single-file web app that creates an interactive particle universe with gravity, collision physics, and beautiful visual effects. It's a mesmerizing experience that responds to your mouse movements and clicks.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Particle Universe</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: 'Arial', sans-serif;
color: white;
cursor: none;
}
#canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
.controls {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
background: rgba(0, 0, 0, 0.5);
padding: 15px 30px;
border-radius: 30px;
display: flex;
gap: 15px;
z-index: 2;
backdrop-filter: blur(5px);
border: 1px solid rgba(255, 255, 255, 0.1);
}
button {
background: rgba(255, 255, 255, 0.1);
color: white;
border: 1px solid rgba(255, 255, 255, 0.2);
padding: 8px 15px;
border-radius: 20px;
cursor: pointer;
transition: all 0.3s;
font-weight: bold;
}
button:hover {
background: rgba(255, 255, 255, 0.2);
transform: scale(1.05);
}
.custom-cursor {
position: absolute;
width: 30px;
height: 30px;
border-radius: 50%;
background: radial-gradient(circle, rgba(255,255,255,0.8) 0%, rgba(255,255,255,0) 70%);
pointer-events: none;
z-index: 10;
transform: translate(-50%, -50%);
transition: transform 0.1s;
box-shadow: 0 0 15px rgba(255, 255, 255, 0.5);
}
.info-panel {
position: absolute;
top: 20px;
left: 20px;
background: rgba(0, 0, 0, 0.5);
padding: 10px 20px;
border-radius: 10px;
font-size: 14px;
z-index: 2;
backdrop-filter: blur(5px);
border: 1px solid rgba(255, 255, 255, 0.1);
}
.particle-info {
margin-top: 10px;
font-size: 12px;
opacity: 0.8;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div class="custom-cursor"></div>
<div class="info-panel">
<div>Interactive Particle Universe</div>
<div class="particle-info">Particles: <span id="particle-count">0</span> | FPS: <span id="fps">0</span></div>
</div>
<div class="controls">
<button id="add-particles">Add Particles</button>
<button id="clear-particles">Clear Particles</button>
<button id="toggle-gravity">Toggle Gravity</button>
<button id="toggle-collision">Toggle Collision</button>
<button id="randomize-colors">Randomize Colors</button>
</div>
<script>
// Canvas setup
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Custom cursor
const cursor = document.querySelector('.custom-cursor');
let mouseX = 0, mouseY = 0;
let isMouseDown = false;
// Particle system
let particles = [];
let gravityEnabled = true;
let collisionEnabled = true;
let particleCount = 0;
let lastTime = 0;
let fps = 0;
let frameCount = 0;
let lastFpsUpdate = 0;
// Color palette
let colorPalette = [
'#ff7e5f', '#feb47b', '#ff6b6b', '#ffa5ab',
'#6a89cc', '#82ccdd', '#b8e994', '#f8c291'
];
// Particle class
class Particle {
constructor(x, y, size, color) {
this.x = x || Math.random() * canvas.width;
this.y = y || Math.random() * canvas.height;
this.size = size || Math.random() * 3 + 1;
this.baseSize = this.size;
this.color = color || colorPalette[Math.floor(Math.random() * colorPalette.length)];
this.velocity = {
x: (Math.random() - 0.5) * 2,
y: (Math.random() - 0.5) * 2
};
this.mass = this.size * 0.1;
this.life = Math.random() * 1000 + 500;
this.maxLife = this.life;
this.opacity = Math.min(0.8, this.size * 0.2);
this.trail = [];
this.trailLength = Math.floor(Math.random() * 5) + 3;
}
update() {
// Update position
this.x += this.velocity.x;
this.y += this.velocity.y;
// Apply gravity
if (gravityEnabled) {
this.velocity.y += 0.05;
}
// Bounce off walls
if (this.x + this.size > canvas.width) {
this.x = canvas.width - this.size;
this.velocity.x *= -0.8;
} else if (this.x - this.size < 0) {
this.x = this.size;
this.velocity.x *= -0.8;
}
if (this.y + this.size > canvas.height) {
this.y = canvas.height - this.size;
this.velocity.y *= -0.8;
} else if (this.y - this.size < 0) {
this.y = this.size;
this.velocity.y *= -0.8;
}
// Mouse interaction
const dx = mouseX - this.x;
const dy = mouseY - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 100) {
const force = 100 / distance;
this.velocity.x += dx * force * 0.001;
this.velocity.y += dy * force * 0.001;
// Size effect based on distance
this.size = this.baseSize * (1 + (100 - distance) * 0.01);
} else {
this.size = Math.max(this.baseSize, this.size - 0.1);
}
// Add current position to trail
this.trail.push({ x: this.x, y: this.y });
if (this.trail.length > this.trailLength) {
this.trail.shift();
}
// Update life
this.life--;
if (this.life <= 0) {
this.life = this.maxLife;
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.velocity = {
x: (Math.random() - 0.5) * 2,
y: (Math.random() - 0.5) * 2
};
}
}
draw() {
// Draw trail
if (this.trail.length > 1) {
ctx.beginPath();
ctx.moveTo(this.trail[0].x, this.trail[0].y);
for (let i = 1; i < this.trail.length; i++) {
const alpha = i / this.trail.length;
ctx.strokeStyle = this.color.replace(')', `, ${alpha * 0.3})`).replace('rgb', 'rgba');
ctx.lineWidth = this.size * 0.5 * (i / this.trail.length);
ctx.lineTo(this.trail[i].x, this.trail[i].y);
}
ctx.stroke();
}
// Draw particle
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fillStyle = this.color.replace(')', `, ${this.opacity})`).replace('rgb', 'rgba');
ctx.fill();
// Draw glow
const gradient = ctx.createRadialGradient(
this.x, this.y, this.size * 0.2,
this.x, this.y, this.size * 2
);
gradient.addColorStop(0, this.color.replace(')', `, ${this.opacity * 0.5})`).replace('rgb', 'rgba'));
gradient.addColorStop(1, this.color.replace(')', `, 0)`).replace('rgb', 'rgba'));
ctx.beginPath();
ctx.arc(this.x, this.y, this.size * 2, 0, Math.PI * 2);
ctx.fillStyle = gradient;
ctx.fill();
}
}
// Initialize particles
function initParticles(count) {
for (let i = 0; i < count; i++) {
particles.push(new Particle());
}
particleCount = particles.length;
document.getElementById('particle-count').textContent = particleCount;
}
// Handle collisions
function handleCollisions() {
if (!collisionEnabled) return;
for (let i = 0; i < particles.length; i++) {
for (let j = i + 1; j < particles.length; j++) {
const p1 = particles[i];
const p2 = particles[j];
const dx = p1.x - p2.x;
const dy = p1.y - p2.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < p1.size + p2.size) {
// Collision detected
const angle = Math.atan2(dy, dx);
const targetX = p1.x + Math.cos(angle) * (p1.size + p2.size);
const targetY = p1.y + Math.sin(angle) * (p1.size + p2.size);
// Move particles to prevent overlap
const ax = (targetX - p2.x) * 0.2;
const ay = (targetY - p2.y) * 0.2;
p1.velocity.x += ax;
p1.velocity.y += ay;
p2.velocity.x -= ax;
p2.velocity.y -= ay;
// Add some energy to the system
p1.velocity.x *= 1.05;
p1.velocity.y *= 1.05;
p2.velocity.x *= 1.05;
p2.velocity.y *= 1.05;
}
}
}
}
// Animation loop
function animate(timestamp) {
// Calculate FPS
frameCount++;
if (timestamp - lastFpsUpdate >= 1000) {
fps = frameCount;
frameCount = 0;
lastFpsUpdate = timestamp;
document.getElementById('fps').textContent = fps;
}
// Clear canvas with a subtle fade effect
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Update and draw particles
for (let i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].draw();
}
// Handle collisions
handleCollisions();
// Add new particles when mouse is clicked
if (isMouseDown && Math.random() < 0.1) {
const size = Math.random() * 5 + 2;
particles.push(new Particle(mouseX, mouseY, size));
particleCount = particles.length;
document.getElementById('particle-count').textContent = particleCount;
}
requestAnimationFrame(animate);
}
// Event listeners
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
window.addEventListener('mousemove', (e) => {
mouseX = e.clientX;
mouseY = e.clientY;
// Update custom cursor position
cursor.style.left = `${e.clientX}px`;
cursor.style.top = `${e.clientY}px`;
// Add some particles on mouse move
if (Math.random() < 0.05) {
particles.push(new Particle(mouseX, mouseY, Math.random() * 2 + 1));
particleCount = particles.length;
document.getElementById('particle-count').textContent = particleCount;
}
});
window.addEventListener('mousedown', () => {
isMouseDown = true;
cursor.style.transform = 'translate(-50%, -50%) scale(1.5)';
});
window.addEventListener('mouseup', () => {
isMouseDown = false;
cursor.style.transform = 'translate(-50%, -50%) scale(1)';
});
// Button event listeners
document.getElementById('add-particles').addEventListener('click', () => {
const count = Math.floor(Math.random() * 50) + 20;
initParticles(count);
});
document.getElementById('clear-particles').addEventListener('click', () => {
particles = [];
particleCount = 0;
document.getElementById('particle-count').textContent = particleCount;
});
document.getElementById('toggle-gravity').addEventListener('click', () => {
gravityEnabled = !gravityEnabled;
const button = document.getElementById('toggle-gravity');
button.textContent = gravityEnabled ? 'Disable Gravity' : 'Enable Gravity';
});
document.getElementById('toggle-collision').addEventListener('click', () => {
collisionEnabled = !collisionEnabled;
const button = document.getElementById('toggle-collision');
button.textContent = collisionEnabled ? 'Disable Collision' : 'Enable Collision';
});
document.getElementById('randomize-colors').addEventListener('click', () => {
colorPalette = [];
for (let i = 0; i < 8; i++) {
const r = Math.floor(Math.random() * 255);
const g = Math.floor(Math.random() * 255);
const b = Math.floor(Math.random() * 255);
colorPalette.push(`rgb(${r}, ${g}, ${b})`);
}
// Update particle colors
particles.forEach(particle => {
particle.color = colorPalette[Math.floor(Math.random() * colorPalette.length)];
});
});
// Initialize
initParticles(50);
animate();
</script>
</body>
</html>
Features of this Astonishing Web App:
- Interactive Particle System: Hundreds of colorful particles that respond to your mouse movements
- Physics Simulation: Realistic gravity and collision physics
- Beautiful Visual Effects: Glowing particles with trails and smooth animations
- Custom Cursor: A stylish custom cursor that enhances the experience
- Interactive Controls:
- Add more particles
- Clear all particles
- Toggle gravity on/off
- Toggle collision physics
- Randomize color palettes
- Performance Monitoring: Shows particle count and FPS
- Responsive Design: Works on any screen size
- Mesmerizing Effects: Particles grow when you approach them and leave beautiful trails
To experience the full effect:
- Move your mouse around to attract particles
- Click and hold to create a particle explosion
- Play with the controls to change the behavior
- Watch as particles collide and interact with each other
The app combines physics simulation, beautiful visuals, and interactive elements to create a truly awe-inspiring experience in a single HTML file!