Source of: Flowing Particle Field, by Claude Haiku 5.5
80 lines, no comments ยท 958 tokens, written in 0 seconds for $0.0005
1 let particles = [];
2 const NUM_PARTICLES = 800;
3 let noiseScale = 0.005;
4 let hueBase = 0;
5
6 function setup() {
7 createCanvas(windowWidth, windowHeight);
8 colorMode(HSB, 360, 100, 100, 100);
9 background(0);
10 initParticles();
11 }
12
13 function initParticles() {
14 particles = [];
15 for (let i = 0; i < NUM_PARTICLES; i++) {
16 particles.push(new Particle());
17 }
18 }
19
20 function draw() {
21 noStroke();
22 fill(0, 0, 0, 6);
23 rect(0, 0, width, height);
24
25 hueBase = (hueBase + 0.2) % 360;
26
27 for (let p of particles) {
28 p.follow();
29 p.update();
30 p.edges();
31 p.show();
32 }
33 }
34
35 class Particle {
36 constructor() {
37 this.pos = createVector(random(width), random(height));
38 this.vel = createVector(0, 0);
39 this.acc = createVector(0, 0);
40 this.maxSpeed = random(1.5, 3.5);
41 this.hue = random(360);
42 }
43
44 follow() {
45 let angle = noise(this.pos.x * noiseScale, this.pos.y * noiseScale, frameCount * 0.002) * TWO_PI * 2;
46 let force = p5.Vector.fromAngle(angle);
47 force.setMag(0.2);
48 this.acc.add(force);
49 }
50
51 update() {
52 this.vel.add(this.acc);
53 this.vel.limit(this.maxSpeed);
54 this.pos.add(this.vel);
55 this.acc.mult(0);
56 }
57
58 edges() {
59 if (this.pos.x < 0) this.pos.x = width;
60 if (this.pos.x > width) this.pos.x = 0;
61 if (this.pos.y < 0) this.pos.y = height;
62 if (this.pos.y > height) this.pos.y = 0;
63 }
64
65 show() {
66 stroke((this.hue + hueBase) % 360, 80, 100, 60);
67 strokeWeight(1.2);
68 point(this.pos.x, this.pos.y);
69 }
70 }
71
72 function mousePressed() {
73 background(0);
74 initParticles();
75 }
76
77 function windowResized() {
78 resizeCanvas(windowWidth, windowHeight);
79 background(0);
80 }