|
| 1 | +// Constellation Background |
| 2 | +const canvas = document.getElementById('constellation-canvas'); |
| 3 | +const ctx = canvas.getContext('2d'); |
| 4 | + |
| 5 | +canvas.width = window.innerWidth; |
| 6 | +canvas.height = window.innerHeight; |
| 7 | + |
| 8 | +let particles = []; |
| 9 | +const particleCount = 100; |
| 10 | + |
| 11 | +class Particle { |
| 12 | + constructor(x, y) { |
| 13 | + this.x = x; |
| 14 | + this.y = y; |
| 15 | + this.size = Math.random() * 2 + 1; |
| 16 | + this.speedX = Math.random() * 3 - 1.5; |
| 17 | + this.speedY = Math.random() * 3 - 1.5; |
| 18 | + this.color = '#fff'; |
| 19 | + } |
| 20 | + update() { |
| 21 | + this.x += this.speedX; |
| 22 | + this.y += this.speedY; |
| 23 | + |
| 24 | + if (this.size > 0.2) this.size -= 0.1; |
| 25 | + } |
| 26 | + draw() { |
| 27 | + ctx.fillStyle = this.color; |
| 28 | + ctx.strokeStyle = this.color; |
| 29 | + ctx.lineWidth = 0.5; |
| 30 | + ctx.beginPath(); |
| 31 | + ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2); |
| 32 | + ctx.closePath(); |
| 33 | + ctx.fill(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function handleParticles() { |
| 38 | + for (let i = 0; i < particles.length; i++) { |
| 39 | + particles[i].update(); |
| 40 | + particles[i].draw(); |
| 41 | + |
| 42 | + for (let j = i; j < particles.length; j++) { |
| 43 | + const dx = particles[i].x - particles[j].x; |
| 44 | + const dy = particles[i].y - particles[j].y; |
| 45 | + const distance = Math.sqrt(dx * dx + dy * dy); |
| 46 | + if (distance < 100) { |
| 47 | + ctx.beginPath(); |
| 48 | + ctx.strokeStyle = particles[i].color; |
| 49 | + ctx.lineWidth = 0.2; |
| 50 | + ctx.moveTo(particles[i].x, particles[i].y); |
| 51 | + ctx.lineTo(particles[j].x, particles[j].y); |
| 52 | + ctx.stroke(); |
| 53 | + ctx.closePath(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (particles[i].size <= 0.3) { |
| 58 | + particles.splice(i, 1); |
| 59 | + i--; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function createParticles() { |
| 65 | + for (let i = 0; i < particleCount; i++) { |
| 66 | + particles.push(new Particle(Math.random() * canvas.width, Math.random() * canvas.height)); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function animate() { |
| 71 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 72 | + handleParticles(); |
| 73 | + requestAnimationFrame(animate); |
| 74 | +} |
| 75 | + |
| 76 | +createParticles(); |
| 77 | +animate(); |
| 78 | + |
| 79 | +window.addEventListener('resize', () => { |
| 80 | + canvas.width = window.innerWidth; |
| 81 | + canvas.height = window.innerHeight; |
| 82 | + particles = []; |
| 83 | + createParticles(); |
| 84 | +}); |
| 85 | + |
| 86 | +// 3D Parallax Card Hover Effect |
| 87 | +const cards = document.querySelectorAll('.glass-card'); |
| 88 | +VanillaTilt.init(cards, { |
| 89 | + max: 25, |
| 90 | + speed: 400, |
| 91 | + glare: true, |
| 92 | + 'max-glare': 0.5, |
| 93 | +}); |
| 94 | + |
| 95 | +// Audio Visualizer |
| 96 | +const audio = document.getElementById('audio'); |
| 97 | +const audioCtx = new (window.AudioContext || window.webkitAudioContext)(); |
| 98 | +const analyser = audioCtx.createAnalyser(); |
| 99 | +const source = audioCtx.createMediaElementSource(audio); |
| 100 | +source.connect(analyser); |
| 101 | +analyser.connect(audioCtx.destination); |
| 102 | +analyser.fftSize = 256; |
| 103 | +const bufferLength = analyser.frequencyBinCount; |
| 104 | +const dataArray = new Uint8Array(bufferLength); |
| 105 | + |
| 106 | +function drawVisualizer() { |
| 107 | + analyser.getByteFrequencyData(dataArray); |
| 108 | + // Simple visualizer for now, will be improved later |
| 109 | + let barHeight; |
| 110 | + let x = 0; |
| 111 | + for (let i = 0; i < bufferLength; i++) { |
| 112 | + barHeight = dataArray[i]; |
| 113 | + ctx.fillStyle = 'rgb(' + (barHeight + 100) + ',50,50)'; |
| 114 | + ctx.fillRect(x, canvas.height - barHeight / 2, 2, barHeight / 2); |
| 115 | + x += 2 + 1; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +function animateVisualizer() { |
| 120 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 121 | + handleParticles(); |
| 122 | + drawVisualizer(); |
| 123 | + requestAnimationFrame(animateVisualizer); |
| 124 | +} |
| 125 | + |
| 126 | +audio.addEventListener('play', () => { |
| 127 | + audioCtx.resume(); |
| 128 | + animateVisualizer(); |
| 129 | +}); |
| 130 | + |
| 131 | +// Start audio on user interaction |
| 132 | +document.body.addEventListener('click', () => { |
| 133 | + audio.play(); |
| 134 | +}, { once: true }); |
0 commit comments