Kdkdbfjdkkd's picture
A beautiful background with space effects, moonlight and moonlight, very detailed and beautiful, lights flying, stars glowing, grass moving a little
790b877 verified
Raw History Blame Contribute Delete
7.85 kB
document.addEventListener('DOMContentLoaded', () => {
const spaceContainer = document.getElementById('space-container');
const toggleDarkBtn = document.getElementById('toggle-dark');
const changeEffectBtn = document.getElementById('change-effect');
let currentEffect = 'particles';
let particles = [];
let cursorX = 0;
let cursorY = 0;
// Create custom cursor
const cursor = document.createElement('div');
cursor.className = 'custom-cursor';
document.body.appendChild(cursor);
// Track mouse position
document.addEventListener('mousemove', (e) => {
cursorX = e.clientX;
cursorY = e.clientY;
cursor.style.left = `${cursorX}px`;
cursor.style.top = `${cursorY}px`;
if (currentEffect === 'particles') {
createParticle();
} else if (currentEffect === 'stars') {
createStar();
} else if (currentEffect === 'orbit') {
createOrbiter();
}
});
// Toggle dark/light mode
toggleDarkBtn.addEventListener('click', () => {
document.documentElement.classList.toggle('dark');
if (document.documentElement.classList.contains('dark')) {
document.body.style.backgroundColor = 'black';
cursor.style.backgroundColor = 'rgba(255, 255, 255, 0.5)';
} else {
document.body.style.backgroundColor = 'white';
cursor.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
}
});
// Change effect
changeEffectBtn.addEventListener('click', () => {
const effects = ['particles', 'stars', 'orbit'];
const currentIndex = effects.indexOf(currentEffect);
currentEffect = effects[(currentIndex + 1) % effects.length];
clearParticles();
});
// Create different effects
function createParticle() {
const particle = document.createElement('div');
particle.className = 'particle';
const size = Math.random() * 10 + 5;
const color = `hsl(${Math.random() * 60 + 200}, 80%, 60%)`;
particle.style.width = `${size}px`;
particle.style.height = `${size}px`;
particle.style.backgroundColor = color;
particle.style.left = `${cursorX}px`;
particle.style.top = `${cursorY}px`;
particle.style.animation = `float ${Math.random() * 2 + 1}s forwards`;
spaceContainer.appendChild(particle);
particles.push(particle);
setTimeout(() => {
particle.remove();
particles = particles.filter(p => p !== particle);
}, 1000);
}
function createStar() {
const star = document.createElement('div');
star.className = 'particle';
const size = Math.random() * 4 + 2;
const color = `hsl(${Math.random() * 360}, 100%, 80%)`;
star.style.width = `${size}px`;
star.style.height = `${size}px`;
star.style.backgroundColor = color;
star.style.boxShadow = `0 0 ${size * 2}px ${size}px ${color}`;
star.style.left = `${cursorX + (Math.random() * 40 - 20)}px`;
star.style.top = `${cursorY + (Math.random() * 40 - 20)}px`;
star.style.animation = `sparkle ${Math.random() * 1 + 0.5}s forwards`;
spaceContainer.appendChild(star);
particles.push(star);
setTimeout(() => {
star.remove();
particles = particles.filter(s => s !== star);
}, 500);
}
function createOrbiter() {
const orbitCenter = document.createElement('div');
orbitCenter.className = 'particle';
orbitCenter.style.width = '10px';
orbitCenter.style.height = '10px';
orbitCenter.style.backgroundColor = 'white';
orbitCenter.style.left = `${cursorX}px`;
orbitCenter.style.top = `${cursorY}px`;
const orbiter = document.createElement('div');
orbiter.className = 'particle';
orbiter.style.width = '6px';
orbiter.style.height = '6px';
orbiter.style.backgroundColor = `hsl(${Math.random() * 60 + 200}, 80%, 60%)`;
orbiter.style.transformOrigin = 'center';
orbiter.style.animation = `orbit ${Math.random() * 3 + 2}s linear infinite`;
orbitCenter.appendChild(orbiter);
spaceContainer.appendChild(orbitCenter);
particles.push(orbitCenter);
setTimeout(() => {
orbitCenter.remove();
particles = particles.filter(o => o !== orbitCenter);
}, 3000);
}
function clearParticles() {
particles.forEach(particle => particle.remove());
particles = [];
}
// Create moon
const moon = document.createElement('div');
moon.className = 'moon';
document.body.appendChild(moon);
// Create initial background stars
for (let i = 0; i < 200; i++) {
const star = document.createElement('div');
star.className = 'particle';
const size = Math.random() * 3 + 1;
const x = Math.random() * window.innerWidth;
const y = Math.random() * window.innerHeight;
const opacity = Math.random() * 0.8 + 0.2;
const twinkleDelay = Math.random() * 5;
star.style.width = `${size}px`;
star.style.height = `${size}px`;
star.style.backgroundColor = 'white';
star.style.left = `${x}px`;
star.style.top = `${y}px`;
star.style.opacity = opacity;
star.style.animation = `sparkle ${Math.random() * 3 + 2}s infinite ${twinkleDelay}s`;
star.style.boxShadow = `0 0 ${size * 2}px ${size/2}px white`;
spaceContainer.appendChild(star);
particles.push(star);
}
// Create grass field
for (let i = 0; i < 200; i++) {
const grass = document.createElement('div');
grass.className = 'grass-blade';
const height = Math.random() * 50 + 20;
const left = Math.random() * window.innerWidth;
const delay = Math.random() * 5;
grass.style.height = `${height}px`;
grass.style.left = `${left}px`;
grass.style.animationDelay = `${delay}s`;
spaceContainer.appendChild(grass);
particles.push(grass);
}
// Create floating lights
setInterval(() => {
const light = document.createElement('div');
light.className = 'particle';
const size = Math.random() * 8 + 4;
const color = `hsl(${Math.random() * 60 + 30}, 100%, 80%)`;
const startX = Math.random() * window.innerWidth;
const duration = Math.random() * 10 + 5;
light.style.width = `${size}px`;
light.style.height = `${size}px`;
light.style.backgroundColor = color;
light.style.borderRadius = '50%';
light.style.boxShadow = `0 0 ${size * 2}px ${size}px ${color}`;
light.style.left = `${startX}px`;
light.style.top = `${window.innerHeight + size}px`;
light.style.animation = `float-up ${duration}s linear forwards`;
spaceContainer.appendChild(light);
particles.push(light);
setTimeout(() => {
light.remove();
particles = particles.filter(l => l !== light);
}, duration * 1000);
}, 500);
// Update cursor color when theme changes
const observer = new MutationObserver(() => {
if (document.documentElement.classList.contains('dark')) {
cursor.style.backgroundColor = 'rgba(255, 255, 255, 0.5)';
} else {
cursor.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
}
});
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ['class']
});
});