Source of: Interactive Particle Fountain, by Grok 4.20
99 lines, 3 of them comments ยท 779 tokens, written in 6 seconds for $0.0022
1 let particles = [];
2
3 class Particle {
4 constructor(x, y) {
5 this.pos = createVector(x, y);
6 this.vel = createVector(random(-3, 3), random(-8, -2));
7 this.acc = createVector(0, 0);
8 this.life = 255;
9 this.hue = random(0, 360);
10 this.size = random(4, 12);
11 }
12
13 applyForce(force) {
14 this.acc.add(force);
15 }
16
17 update() {
18 let mouse = createVector(mouseX, mouseY);
19 let dir = p5.Vector.sub(mouse, this.pos);
20 let d = dir.mag();
21 if (d > 0) {
22 dir.normalize();
23 let force = dir.mult(0.2);
24 this.applyForce(force);
25 }
26
27 this.vel.add(this.acc);
28 this.vel.limit(6);
29 this.pos.add(this.vel);
30 this.acc.mult(0);
31
32 this.life -= 3;
33 this.vel.y += 0.15; // gravity
34 }
35
36 edges() {
37 if (this.pos.x < 0 || this.pos.x > width) this.vel.x *= -0.8;
38 if (this.pos.y > height) {
39 this.pos.y = height;
40 this.vel.y *= -0.7;
41 }
42 }
43
44 show() {
45 colorMode(HSB);
46 noStroke();
47 fill(this.hue, 90, 100, this.life / 255);
48 circle(this.pos.x, this.pos.y, this.size);
49 colorMode(RGB);
50 }
51
52 isDead() {
53 return this.life < 0;
54 }
55 }
56
57 function setup() {
58 createCanvas(640, 480);
59 colorMode(HSB);
60 }
61
62 function draw() {
63 background(20, 20, 30, 40);
64
65 // Add new particles continuously
66 if (frameCount % 2 === 0) {
67 for (let i = 0; i < 3; i++) {
68 particles.push(new Particle(width / 2 + random(-40, 40), height - 20));
69 }
70 }
71
72 for (let i = particles.length - 1; i >= 0; i--) {
73 let p = particles[i];
74 p.update();
75 p.edges();
76 p.show();
77 if (p.isDead()) {
78 particles.splice(i, 1);
79 }
80 }
81
82 // Draw emitter
83 fill(200, 30, 100);
84 noStroke();
85 rect(width / 2 - 30, height - 30, 60, 20, 5);
86 }
87
88 function mousePressed() {
89 for (let i = 0; i < 60; i++) {
90 let p = new Particle(mouseX, mouseY);
91 p.vel = createVector(random(-7, 7), random(-9, -3));
92 p.life = 180;
93 particles.push(p);
94 }
95 }
96
97 function windowResized() {
98 resizeCanvas(640, 480);
99 }