Source of: Flowing Particle Field, by Claude Opus 4.8
85 lines, no comments ยท 1,132 tokens, written in 11 seconds for $0.030
1 let particles = [];
2 let zoff = 0;
3 const num = 900;
4 const scl = 20;
5 let cols, rows;
6
7 function setup() {
8 createCanvas(windowWidth, windowHeight);
9 cols = floor(width / scl) + 1;
10 rows = floor(height / scl) + 1;
11 colorMode(HSB, 360, 100, 100, 100);
12 background(0);
13 for (let i = 0; i < num; i++) {
14 particles.push(new Particle());
15 }
16 }
17
18 function draw() {
19 noStroke();
20 fill(0, 0, 0, 6);
21 rect(0, 0, width, height);
22
23 let yoff = 0;
24 let flow = [];
25 for (let y = 0; y < rows; y++) {
26 let xoff = 0;
27 flow[y] = [];
28 for (let x = 0; x < cols; x++) {
29 let angle = noise(xoff, yoff, zoff) * TWO_PI * 2;
30 let v = p5.Vector.fromAngle(angle);
31 v.setMag(0.4);
32 flow[y][x] = v;
33 xoff += 0.1;
34 }
35 yoff += 0.1;
36 }
37 zoff += 0.003;
38
39 for (let p of particles) {
40 let cx = constrain(floor(p.pos.x / scl), 0, cols - 1);
41 let cy = constrain(floor(p.pos.y / scl), 0, rows - 1);
42 p.applyForce(flow[cy][cx]);
43 p.update();
44 p.edges();
45 p.show();
46 }
47 }
48
49 class Particle {
50 constructor() {
51 this.pos = createVector(random(width), random(height));
52 this.vel = createVector(0, 0);
53 this.acc = createVector(0, 0);
54 this.maxSpeed = 2.5;
55 this.hue = random(360);
56 }
57 applyForce(f) {
58 this.acc.add(f);
59 }
60 update() {
61 this.vel.add(this.acc);
62 this.vel.limit(this.maxSpeed);
63 this.pos.add(this.vel);
64 this.acc.mult(0);
65 this.hue = (this.hue + 0.3) % 360;
66 }
67 edges() {
68 if (this.pos.x > width) this.pos.x = 0;
69 if (this.pos.x < 0) this.pos.x = width;
70 if (this.pos.y > height) this.pos.y = 0;
71 if (this.pos.y < 0) this.pos.y = height;
72 }
73 show() {
74 noStroke();
75 fill(this.hue, 70, 100, 40);
76 ellipse(this.pos.x, this.pos.y, 2.5, 2.5);
77 }
78 }
79
80 function windowResized() {
81 resizeCanvas(windowWidth, windowHeight);
82 cols = floor(width / scl) + 1;
83 rows = floor(height / scl) + 1;
84 background(0);
85 }