111 lines, no comments ยท 1,020 tokens, written in 3 seconds for $0.0026


  1  let particles = [];
  2  let numParticles = 1000;
  3  let inc = 0.1;
  4  let scl = 20;
  5  let cols, rows;
  6  let zoff = 0;
  7  
  8  function setup() {
  9    createCanvas(windowWidth, windowHeight);
 10    cols = floor(width / scl);
 11    rows = floor(height / scl);
 12    background(15);
 13  
 14    for (let i = 0; i < numParticles; i++) {
 15      particles[i] = new Particle();
 16    }
 17  }
 18  
 19  function draw() {
 20    background(15, 10);
 21  
 22    let yoff = 0;
 23    for (let y = 0; y < rows; y++) {
 24      let xoff = 0;
 25      for (let x = 0; x < cols; x++) {
 26        let angle = noise(xoff, yoff, zoff) * TWO_PI * 2;
 27        let v = p5.Vector.fromAngle(angle);
 28        v.setMag(0.5);
 29        xoff += inc;
 30      }
 31      yoff += inc;
 32    }
 33    zoff += 0.003;
 34  
 35    for (let i = 0; i < particles.length; i++) {
 36      particles[i].follow();
 37      particles[i].update();
 38      particles[i].show();
 39      particles[i].edges();
 40    }
 41  }
 42  
 43  class Particle {
 44    constructor() {
 45      this.pos = createVector(random(width), random(height));
 46      this.vel = createVector(0, 0);
 47      this.acc = createVector(0, 0);
 48      this.maxSpeed = 2;
 49      this.prevPos = this.pos.copy();
 50      this.color = color(random(100, 255), random(100, 200), random(200, 255), 100);
 51    }
 52  
 53    update() {
 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  
 60    follow() {
 61      let x = floor(this.pos.x / scl);
 62      let y = floor(this.pos.y / scl);
 63      let index = x + y * cols;
 64      let angle = noise(x * inc, y * inc, zoff) * TWO_PI * 2;
 65      let force = p5.Vector.fromAngle(angle);
 66      force.mult(0.5);
 67      this.applyForce(force);
 68    }
 69  
 70    applyForce(force) {
 71      this.acc.add(force);
 72    }
 73  
 74    show() {
 75      stroke(this.color);
 76      strokeWeight(1.5);
 77      line(this.pos.x, this.pos.y, this.prevPos.x, this.prevPos.y);
 78      this.updatePrev();
 79    }
 80  
 81    updatePrev() {
 82      this.prevPos.x = this.pos.x;
 83      this.prevPos.y = this.pos.y;
 84    }
 85  
 86    edges() {
 87      if (this.pos.x > width) {
 88        this.pos.x = 0;
 89        this.updatePrev();
 90      }
 91      if (this.pos.x < 0) {
 92        this.pos.x = width;
 93        this.updatePrev();
 94      }
 95      if (this.pos.y > height) {
 96        this.pos.y = 0;
 97        this.updatePrev();
 98      }
 99      if (this.pos.y < 0) {
100        this.pos.y = height;
101        this.updatePrev();
102      }
103    }
104  }
105  
106  function windowResized() {
107    resizeCanvas(windowWidth, windowHeight);
108    background(15);
109    cols = floor(width / scl);
110    rows = floor(height / scl);
111  }