1 lines, no comments ยท 5,226 tokens, written in 31 seconds for $0.047


1  let particles = []; let attractors = []; const NUM_PARTICLES = 250; const NUM_ATTRACTORS = 4; function setup() { createCanvas(windowWidth, windowHeight); for (let i = 0; i < NUM_PARTICLES; i++) { particles.push(new Particle(random(width), random(height))); } for (let i = 0; i < NUM_ATTRACTORS; i++) { attractors.push({ pos: createVector(random(width), random(height)), noiseOffset: random(10000) }); } background(10, 10, 25); } function draw() { fill(10, 10, 25, 30); noStroke(); rect(0, 0, width, height); let colorShift = sin(frameCount * 0.01) * 50; for (let i = 0; i < attractors.length; i++) { let a = attractors[i]; a.pos.x = noise(a.noiseOffset + frameCount * 0.003) * width; a.pos.y = noise(a.noiseOffset + 10000 + frameCount * 0.003) * height; } for (let p of particles) { p.update(); p.show(colorShift); for (let a of attractors) { p.attract(a.pos, 0.4); } if (mouseIsPressed) { p.attract(createVector(mouseX, mouseY), 3.0); } else { p.attract(createVector(mouseX, mouseY), 0.3); } } strokeWeight(0.8); for (let i = 0; i < particles.length; i += 2) { for (let j = i + 1; j < particles.length; j += 3) { let d = dist(particles[i].pos.x, particles[i].pos.y, particles[j].pos.x, particles[j].pos.y); if (d < 70) { let alpha = map(d, 0, 70, 150, 0); stroke(particles[i].r, particles[i].g, particles[i].b, alpha); line(particles[i].pos.x, particles[i].pos.y, particles[j].pos.x, particles[j].pos.y); } } } fill(255, 20); noStroke(); ellipse(mouseX, mouseY, 30, 30); } function windowResized() { resizeCanvas(windowWidth, windowHeight); } class Particle { constructor(x, y) { this.pos = createVector(x, y); this.vel = p5.Vector.random2D().mult(random(1, 3)); this.acc = createVector(0, 0); this.maxSpeed = random(2, 5); this.size = random(2, 6); let r = floor(random(3)); if (r === 0) { this.baseR = 0; this.baseG = 242; this.baseB = 254; } else if (r === 1) { this.baseR = 253; this.baseG = 85; this.baseB = 154; } else { this.baseR = 157; this.baseG = 78; this.baseB = 221; } this.r = this.baseR; this.g = this.baseG; this.b = this.baseB; } attract(target, strength) { let force = p5.Vector.sub(target, this.pos); let d = force.mag(); d = constrain(d, 20, 500); force.normalize(); let speed = (strength * 12) / (d * 0.06); force.mult(speed); this.acc.add(force); } update() { this.vel.add(this.acc); this.vel.limit(this.maxSpeed); this.pos.add(this.vel); this.acc.mult(0); if (this.pos.x < 0 || this.pos.x > width) this.vel.x *= -1; if (this.pos.y < 0 || this.pos.y > height) this.vel.y *= -1; } show(shift) { this.r = constrain(this.baseR + shift, 0, 255); this.g = constrain(this.baseG - shift, 0, 255); this.b = constrain(this.baseB + shift * 0.5, 0, 255); noStroke(); fill(this.r, this.g, this.b, 200); ellipse(this.pos.x, this.pos.y, this.size); } }