Source of: Floating Particles, by Claude Haiku 4.5
61 lines, no comments ยท 644 tokens, written in 4 seconds for $0.0035
1 let particles = [];
2 let numParticles = 50;
3
4 function setup() {
5 createCanvas(800, 600);
6 background(10);
7
8 for (let i = 0; i < numParticles; i++) {
9 particles.push(new Particle());
10 }
11 }
12
13 function draw() {
14 background(10, 25);
15
16 for (let particle of particles) {
17 particle.update();
18 particle.display();
19 particle.checkEdges();
20 }
21 }
22
23 class Particle {
24 constructor() {
25 this.pos = createVector(random(width), random(height));
26 this.vel = createVector(random(-2, 2), random(-2, 2));
27 this.acc = createVector(0, 0);
28 this.size = random(3, 8);
29 this.maxSpeed = 3;
30 this.hue = random(360);
31 }
32
33 update() {
34 let angle = noise(this.pos.x * 0.005, this.pos.y * 0.005) * TWO_PI * 2;
35 let force = p5.Vector.fromAngle(angle);
36 force.mult(0.3);
37
38 this.acc.add(force);
39 this.vel.add(this.acc);
40 this.vel.limit(this.maxSpeed);
41 this.pos.add(this.vel);
42 this.acc.mult(0);
43
44 this.hue = (this.hue + 1) % 360;
45 }
46
47 display() {
48 colorMode(HSB);
49 fill(this.hue, 255, 255, 150);
50 noStroke();
51 circle(this.pos.x, this.pos.y, this.size);
52 colorMode(RGB);
53 }
54
55 checkEdges() {
56 if (this.pos.x < 0) this.pos.x = width;
57 if (this.pos.x > width) this.pos.x = 0;
58 if (this.pos.y < 0) this.pos.y = height;
59 if (this.pos.y > height) this.pos.y = 0;
60 }
61 }