Erstelle mehrere Partikelgruppen, die sich wie Planeten um unsichtbare…
- User
Erstelle mehrere Partikelgruppen, die sich wie Planeten um unsichtbare Gravitationszentren bewegen. Sie beeinflussen sich gegenseitig leicht, sodass ein chaotischer, aber harmonischer Tanz entsteht.
Assistant
Hier ist ein JavaScript-Code, der mehrere Partikelgruppen erstellt, die sich wie Planeten um unsichtbare Gravitationszentren bewegen und sich gegenseitig beeinflussen:
const canvas = document.createElement('canvas'); document.body.appendChild(canvas); canvas.width = window.innerWidth; canvas.height = window.innerHeight; const ctx = canvas.getContext('2d'); // Vollbildmodus canvas.style.position = 'fixed'; canvas.style.top = '0'; canvas.style.left = '0'; canvas.style.background = '#000'; class GravitationCenter { constructor() { this.x = Math.random() * canvas.width; this.y = Math.random() * canvas.height; this.mass = Math.random() * 500 + 200; this.vx = (Math.random() - 0.5) * 0.5; this.vy = (Math.random() - 0.5) * 0.5; } update() { this.x += this.vx; this.y += this.vy; // Einfache Begrenzung if (this.x < 0 || this.x > canvas.width) this.vx *= -1; if (this.y < 0 || this.y > canvas.height) this.vy *= -1; } } class Particle { constructor(centerIndex, color) { this.centerIndex = centerIndex; const center = gravitationCenters[centerIndex]; const angle = Math.random() * Math.PI * 2; const distance = Math.random() * 150 + 50; this.x = center.x + Math.cos(angle) * distance; this.y = center.y + Math.sin(angle) * distance; this.size = Math.random() * 3 + 1; this.color = color; // Initiale Geschwindigkeit für Orbit const speed = Math.sqrt(0.5 * center.mass / distance); this.vx = -Math.sin(angle) * speed; this.vy = Math.cos(angle) * speed; this.trail = []; this.maxTrail = Math.floor(Math.random() * 20) + 10; } update() { // Speichere Position für Trail this.trail.push({x: this.x, y: this.y}); if (this.trail.length > this.maxTrail) { this.trail.shift(); } // Berechne Gravitationskräfte von allen Zentren for (let i = 0; i < gravitationCenters.length; i++) { const center = gravitationCenters[i]; const dx = center.x - this.x; const dy = center.y - this.y; const distance = Math.sqrt(dx * dx + dy * dy); // Verhindere Division durch Null und zu starke Kräfte if (distance > 5) { const force = 0.5 * center.mass / (distance * distance); const multiplier = i === this.centerIndex ? 1 : 0.15; // Andere Zentren beeinflussen weniger this.vx += dx / distance * force * multiplier; this.vy += dy / distance * force * multiplier; } } // Dämpfung this.vx *= 0.995; this.vy *= 0.995; // Bewege Partikel this.x += this.vx; this.y += this.vy; } draw() { // Zeichne Trail ctx.beginPath(); for (let i = 0; i < this.trail.length; i++) { const point = this.trail[i]; const alpha = i / this.trail.length; if (i === 0) { ctx.moveTo(point.x, point.y); } else { ctx.lineTo(point.x, point.y); } } ctx.strokeStyle = this.color.replace(')', `, ${0.5})`); ctx.lineWidth = this.size / 2; ctx.stroke(); // Zeichne Partikel ctx.beginPath(); ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2); ctx.fillStyle = this.color; ctx.fill(); } } // Erstelle Gravitationszentren const gravitationCenters = []; const numCenters = 5; for (let i = 0; i < numCenters; i++) { gravitationCenters.push(new GravitationCenter()); } // Erstelle Partikelgruppen const particles = []; const colors = [ 'rgba(255, 100, 100, 0.8)', 'rgba(100, 255, 100, 0.8)', 'rgba(100, 100, 255, 0.8)', 'rgba(255, 255, 100, 0.8)', 'rgba(255, 100, 255, 0.8)' ]; for (let i = 0; i < numCenters; i++) { const numParticles = Math.floor(Math.random() * 50) + 50; for (let j = 0; j < numParticles; j++) { particles.push(new Particle(i, colors[i])); } } function animate() { // Hintergrund mit Transparenz für Nachzieheffekt ctx.fillStyle = 'rgba(0, 0, 0, 0.05)'; ctx.fillRect(0, 0, canvas.width, canvas.height); // Aktualisiere Gravitationszentren gravitationCenters.forEach(center => center.update()); // Aktualisiere und zeichne Partikel particles.forEach(particle => { particle.update(); particle.draw(); }); requestAnimationFrame(animate); } animate(); // Anpassung bei Größenänderung des Fensters window.addEventListener('resize', () => { canvas.width = window.innerWidth; canvas.height = window.innerHeight; });Möchten Sie, dass ich den Code erkläre oder bestimmte Teile davon näher erläutere?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.