88 lines, no comments ยท 1,082 tokens, written in 13 seconds for $0.0055


 1  let particles = [];
 2  let fieldTime = 0;
 3  
 4  function setup() {
 5    createCanvas(windowWidth, windowHeight);
 6    pixelDensity(1);
 7    colorMode(HSB, 360, 100, 100, 100);
 8    background(230, 35, 5);
 9    initializeParticles();
10  }
11  
12  function initializeParticles() {
13    particles = [];
14    const count = constrain(floor(width * height / 700), 400, 1800);
15    for (let i = 0; i < count; i++) {
16      particles.push(new Particle());
17    }
18  }
19  
20  function draw() {
21    blendMode(BLEND);
22    noStroke();
23    fill(230, 35, 5, 12);
24    rect(0, 0, width, height);
25  
26    blendMode(ADD);
27    for (const particle of particles) {
28      particle.update();
29      particle.display();
30    }
31    blendMode(BLEND);
32    fieldTime += 0.003;
33  }
34  
35  class Particle {
36    constructor() {
37      this.reset();
38      this.age = random(this.lifetime);
39    }
40  
41    reset() {
42      this.x = random(width);
43      this.y = random(height);
44      this.previousX = this.x;
45      this.previousY = this.y;
46      this.speed = random(0.8, 2.4);
47      this.lifetime = random(100, 300);
48      this.age = 0;
49    }
50  
51    update() {
52      this.previousX = this.x;
53      this.previousY = this.y;
54  
55      const angle = noise(this.x * 0.0025, this.y * 0.0025, fieldTime) * TWO_PI * 3;
56      this.x += cos(angle) * this.speed;
57      this.y += sin(angle) * this.speed;
58      this.age++;
59  
60      if (this.x < 0 || this.x > width || this.y < 0 || this.y > height || this.age > this.lifetime) {
61        this.reset();
62      }
63    }
64  
65    display() {
66      const hue = (180 + this.x / width * 120 + noise(this.y * 0.003, fieldTime) * 50) % 360;
67      const fade = sin(PI * this.age / this.lifetime);
68      stroke(hue, 70, 100, 30 * fade);
69      strokeWeight(1.2);
70      line(this.previousX, this.previousY, this.x, this.y);
71    }
72  }
73  
74  function mousePressed() {
75    noiseSeed(floor(random(1000000)));
76    background(230, 35, 5);
77    initializeParticles();
78  }
79  
80  function keyPressed() {
81    if (key === 's' || key === 'S') saveCanvas('luminous-flow', 'png');
82  }
83  
84  function windowResized() {
85    resizeCanvas(windowWidth, windowHeight);
86    background(230, 35, 5);
87    initializeParticles();
88  }