71 lines, no comments ยท 2,263 tokens, written in 10 seconds for $0.0086


 1  let particles = [];
 2  const numParticles = 900;
 3  let noiseScale = 0.005;
 4  
 5  function setup() {
 6    createCanvas(windowWidth, windowHeight);
 7    colorMode(HSB, 360, 100, 100, 1);
 8    for (let i = 0; i < numParticles; i++) {
 9      particles.push(new Particle());
10    }
11    background(0);
12  }
13  
14  function draw() {
15    background(240, 20, 8, 0.08);
16    for (let p of particles) {
17      p.update();
18      p.display();
19    }
20  }
21  
22  function windowResized() {
23    resizeCanvas(windowWidth, windowHeight);
24    background(0);
25  }
26  
27  class Particle {
28    constructor() {
29      this.reset();
30    }
31    reset() {
32      this.pos = createVector(random(width), random(height));
33      this.prevPos = this.pos.copy();
34      this.vel = createVector(0, 0);
35      this.acc = createVector(0, 0);
36      this.maxSpeed = random(2, 4.5);
37      this.hue = random(360);
38      this.life = random(100, 255);
39    }
40    update() {
41      this.prevPos = this.pos.copy();
42      let angle = noise(this.pos.x * noiseScale, this.pos.y * noiseScale, frameCount * 0.0025) * TWO_PI * 4;
43      let flowForce = p5.Vector.fromAngle(angle);
44      flowForce.mult(0.4);
45      this.acc.add(flowForce);
46  
47      let d = dist(mouseX, mouseY, this.pos.x, this.pos.y);
48      if (d < 180) {
49        let pushForce = p5.Vector.sub(this.pos, createVector(mouseX, mouseY));
50        pushForce.setMag(map(d, 0, 180, 3, 0));
51        this.acc.add(pushForce);
52      }
53  
54      this.vel.add(this.acc);
55      this.vel.limit(this.maxSpeed);
56      this.pos.add(this.vel);
57      this.acc.mult(0);
58  
59      this.hue = (this.hue + 0.4) % 360;
60      this.life -= 0.6;
61  
62      if (this.pos.x < 0 || this.pos.x > width || this.pos.y < 0 || this.pos.y > height || this.life <= 0) {
63        this.reset();
64      }
65    }
66    display() {
67      stroke(this.hue, 85, 100, map(this.life, 0, 255, 0, 0.7));
68      strokeWeight(map(this.life, 0, 255, 0.5, 2.5));
69      line(this.prevPos.x, this.prevPos.y, this.pos.x, this.pos.y);
70    }
71  }