Spaces:
Running
Running
File size: 5,491 Bytes
005c99b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | document.addEventListener('DOMContentLoaded', () => {
const canvas = document.createElement('canvas');
const background = document.getElementById('animated-background');
background.appendChild(canvas);
canvas.style.position = 'absolute';
canvas.style.top = '0';
canvas.style.left = '0';
canvas.style.width = '100%';
canvas.style.height = '100%';
// Resize canvas to full window
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
const ctx = canvas.getContext('2d');
// Particle system
let particles = [];
let particleCount = 150;
let speed = 5;
let size = 5;
let effectType = 'cosmic';
// Create particles
function createParticles() {
particles = [];
for (let i = 0; i < particleCount; i++) {
particles.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
size: Math.random() * size + 1,
speedX: (Math.random() - 0.5) * speed,
speedY: (Math.random() - 0.5) * speed,
color: `hsl(${Math.random() * 360}, 70%, 60%)`,
angle: Math.random() * Math.PI * 2,
angleSpeed: (Math.random() - 0.5) * 0.02
});
}
}
// Draw particles
function drawParticles() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
particles.forEach(p => {
// Update position
p.x += p.speedX;
p.y += p.speedY;
p.angle += p.angleSpeed;
// Wrap around edges
if (p.x < 0) p.x = canvas.width;
if (p.x > canvas.width) p.x = 0;
if (p.y < 0) p.y = canvas.height;
if (p.y > canvas.height) p.y = 0;
// Draw based on effect type
if (effectType === 'cosmic') {
drawCosmicParticle(p);
} else if (effectType === 'bubbles') {
drawBubbleParticle(p);
} else if (effectType === 'stars') {
drawStarParticle(p);
}
});
requestAnimationFrame(drawParticles);
}
function drawCosmicParticle(p) {
ctx.save();
ctx.translate(p.x, p.y);
ctx.rotate(p.angle);
// Glow effect
const gradient = ctx.createRadialGradient(0, 0, 0, 0, 0, p.size * 2);
gradient.addColorStop(0, p.color);
gradient.addColorStop(1, 'transparent');
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(0, 0, p.size * 2, 0, Math.PI * 2);
ctx.fill();
// Core
ctx.fillStyle = p.color;
ctx.beginPath();
ctx.arc(0, 0, p.size / 2, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
function drawBubbleParticle(p) {
ctx.fillStyle = `rgba(255, 255, 255, ${p.size / 20})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
ctx.fill();
ctx.strokeStyle = `rgba(255, 255, 255, 0.3)`;
ctx.stroke();
}
function drawStarParticle(p) {
const spikes = 5;
const outerRadius = p.size;
const innerRadius = p.size / 2;
ctx.save();
ctx.translate(p.x, p.y);
ctx.rotate(p.angle);
ctx.fillStyle = p.color;
ctx.beginPath();
for (let i = 0; i < spikes * 2; i++) {
const radius = i % 2 === 0 ? outerRadius : innerRadius;
const angle = (i * Math.PI) / spikes;
ctx.lineTo(Math.cos(angle) * radius, Math.sin(angle) * radius);
}
ctx.closePath();
ctx.fill();
// Glow
ctx.shadowColor = p.color;
ctx.shadowBlur = 10;
ctx.fill();
ctx.restore();
}
// Change effect type
document.getElementById('change-effect').addEventListener('click', () => {
const effects = ['cosmic', 'bubbles', 'stars'];
const currentIndex = effects.indexOf(effectType);
effectType = effects[(currentIndex + 1) % effects.length];
createParticles();
});
// Toggle controls panel
const controlsPanel = document.getElementById('controls-panel');
document.getElementById('toggle-controls').addEventListener('click', () => {
controlsPanel.style.transform = controlsPanel.style.transform === 'translateY(0px)' ?
'translateY(100%)' : 'translateY(0)';
});
// Update settings from controls
document.getElementById('particle-count').addEventListener('input', (e) => {
particleCount = parseInt(e.target.value);
createParticles();
});
document.getElementById('speed').addEventListener('input', (e) => {
speed = parseInt(e.target.value);
particles.forEach(p => {
p.speedX = (Math.random() - 0.5) * speed;
p.speedY = (Math.random() - 0.5) * speed;
});
});
document.getElementById('size').addEventListener('input', (e) => {
size = parseInt(e.target.value);
particles.forEach(p => {
p.size = Math.random() * size + 1;
});
});
// Initialize
createParticles();
drawParticles();
}); |