Source of: Flow Field Particles, by Claude Sonnet 5
104 lines, no comments ยท 1,323 tokens, written in 12 seconds for $0.014
1 let particles = [];
2 let numParticles = 1500;
3 let noiseScale = 0.006;
4 let zoff = 0;
5 let hueShift = 0;
6
7 function setup() {
8 createCanvas(windowWidth, windowHeight);
9 colorMode(HSB, 360, 100, 100, 100);
10 background(0);
11 for (let i = 0; i < numParticles; i++) {
12 particles.push(new Particle());
13 }
14 }
15
16 function draw() {
17 background(220, 10, 5, 8);
18
19 hueShift += 0.15;
20
21 for (let p of particles) {
22 p.follow();
23 p.update();
24 p.edges();
25 p.show();
26 }
27
28 zoff += 0.0015;
29 }
30
31 function windowResized() {
32 resizeCanvas(windowWidth, windowHeight);
33 background(0);
34 }
35
36 class Particle {
37 constructor() {
38 this.pos = createVector(random(width), random(height));
39 this.vel = createVector(0, 0);
40 this.acc = createVector(0, 0);
41 this.maxSpeed = random(1.5, 3.5);
42 this.prevPos = this.pos.copy();
43 this.hue = random(360);
44 this.life = random(100, 300);
45 }
46
47 follow() {
48 let angle = noise(
49 this.pos.x * noiseScale,
50 this.pos.y * noiseScale,
51 zoff
52 ) * TWO_PI * 4;
53 let force = p5.Vector.fromAngle(angle);
54 force.setMag(0.1);
55 this.acc.add(force);
56 }
57
58 update() {
59 this.prevPos = this.pos.copy();
60 this.vel.add(this.acc);
61 this.vel.limit(this.maxSpeed);
62 this.pos.add(this.vel);
63 this.acc.mult(0);
64
65 this.life -= 1;
66 if (this.life <= 0) {
67 this.pos = createVector(random(width), random(height));
68 this.prevPos = this.pos.copy();
69 this.life = random(100, 300);
70 this.hue = random(360);
71 }
72 }
73
74 edges() {
75 if (this.pos.x > width) {
76 this.pos.x = 0;
77 this.prevPos = this.pos.copy();
78 }
79 if (this.pos.x < 0) {
80 this.pos.x = width;
81 this.prevPos = this.pos.copy();
82 }
83 if (this.pos.y > height) {
84 this.pos.y = 0;
85 this.prevPos = this.pos.copy();
86 }
87 if (this.pos.y < 0) {
88 this.pos.y = height;
89 this.prevPos = this.pos.copy();
90 }
91 }
92
93 show() {
94 let displayHue = (this.hue + hueShift) % 360;
95 stroke(displayHue, 80, 95, 60);
96 strokeWeight(1.5);
97 line(this.prevPos.x, this.prevPos.y, this.pos.x, this.pos.y);
98 }
99 }
100
101 function mousePressed() {
102 background(0);
103 }
104