Source of: Noise Flow Field, by Claude Fable 5.1
64 lines, 1 of them comments ยท 1,506 tokens, written in 18 seconds for $0.079
1 let particles = [];
2 const NUM = 1500;
3 let t = 0;
4 const BG = [230, 30, 8];
5
6 function setup() {
7 createCanvas(windowWidth, windowHeight);
8 colorMode(HSB, 360, 100, 100, 100);
9 background(BG[0], BG[1], BG[2]);
10 for (let i = 0; i < NUM; i++) {
11 particles.push(makeParticle());
12 }
13 }
14
15 function makeParticle() {
16 return {
17 x: random(width),
18 y: random(height),
19 px: 0,
20 py: 0,
21 hue: random(180, 320),
22 speed: random(0.8, 2.2),
23 life: random(100, 500)
24 };
25 }
26
27 function draw() {
28 // fade previous frame slightly to create trails
29 noStroke();
30 fill(BG[0], BG[1], BG[2], 4);
31 rect(0, 0, width, height);
32
33 t += 0.0025;
34 strokeWeight(1.2);
35
36 for (let p of particles) {
37 const angle = noise(p.x * 0.0018, p.y * 0.0018, t) * TWO_PI * 4;
38 p.px = p.x;
39 p.py = p.y;
40 p.x += cos(angle) * p.speed;
41 p.y += sin(angle) * p.speed;
42 p.life--;
43
44 const brightness = map(p.speed, 0.8, 2.2, 70, 100);
45 stroke(p.hue, 60, brightness, 45);
46 line(p.px, p.py, p.x, p.y);
47
48 if (p.x < 0 || p.x > width || p.y < 0 || p.y > height || p.life <= 0) {
49 Object.assign(p, makeParticle());
50 }
51 }
52 }
53
54 function mousePressed() {
55 noiseSeed(floor(random(100000)));
56 background(BG[0], BG[1], BG[2]);
57 for (let p of particles) Object.assign(p, makeParticle());
58 }
59
60 function windowResized() {
61 resizeCanvas(windowWidth, windowHeight);
62 background(BG[0], BG[1], BG[2]);
63 }
64