78 lines, no comments ยท 1,037 tokens, written in 0 seconds for $0.0006


 1  let particles = [];
 2  let numParticles = 800;
 3  let noiseScale = 0.005;
 4  let hueBase = 0;
 5  
 6  function setup() {
 7    createCanvas(windowWidth, windowHeight);
 8    colorMode(HSB, 360, 100, 100, 100);
 9    background(0);
10    for (let i = 0; i < numParticles; i++) {
11      particles.push(new Particle());
12    }
13  }
14  
15  function draw() {
16    noStroke();
17    fill(0, 0, 0, 6);
18    rect(0, 0, width, height);
19    hueBase = (hueBase + 0.2) % 360;
20    for (let p of particles) {
21      p.follow();
22      p.update();
23      p.edges();
24      p.show();
25    }
26  }
27  
28  class Particle {
29    constructor() {
30      this.pos = createVector(random(width), random(height));
31      this.vel = createVector(0, 0);
32      this.acc = createVector(0, 0);
33      this.maxSpeed = 2.5;
34      this.prev = this.pos.copy();
35      this.hue = random(360);
36    }
37  
38    follow() {
39      let angle = noise(this.pos.x * noiseScale, this.pos.y * noiseScale, frameCount * 0.002) * TWO_PI * 2;
40      let force = p5.Vector.fromAngle(angle);
41      force.mult(0.15);
42      this.acc.add(force);
43    }
44  
45    update() {
46      this.prev = this.pos.copy();
47      this.vel.add(this.acc);
48      this.vel.limit(this.maxSpeed);
49      this.pos.add(this.vel);
50      this.acc.mult(0);
51    }
52  
53    edges() {
54      if (this.pos.x < 0) { this.pos.x = width; this.prev.x = width; }
55      if (this.pos.x > width) { this.pos.x = 0; this.prev.x = 0; }
56      if (this.pos.y < 0) { this.pos.y = height; this.prev.y = height; }
57      if (this.pos.y > height) { this.pos.y = 0; this.prev.y = 0; }
58    }
59  
60    show() {
61      stroke((this.hue + hueBase) % 360, 80, 100, 60);
62      strokeWeight(1.2);
63      line(this.prev.x, this.prev.y, this.pos.x, this.pos.y);
64    }
65  }
66  
67  function mousePressed() {
68    background(0);
69    particles = [];
70    for (let i = 0; i < numParticles; i++) {
71      particles.push(new Particle());
72    }
73  }
74  
75  function windowResized() {
76    resizeCanvas(windowWidth, windowHeight);
77    background(0);
78  }