Source of: Interactive Particle Fountain, by Grok 4.20
78 lines, 5 of them comments ยท 607 tokens, written in 6 seconds for $0.0018
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, -3));
7 this.acc = createVector(0, 0.2);
8 this.life = 255;
9 this.hue = random(0, 360);
10 this.size = random(4, 12);
11 }
12
13 update() {
14 this.vel.add(this.acc);
15 this.vel.x *= 0.98; // slight air resistance
16 this.pos.add(this.vel);
17 this.life -= 4;
18 }
19
20 display() {
21 colorMode(HSB);
22 noStroke();
23 fill(this.hue, 80, 100, this.life / 255);
24 ellipse(this.pos.x, this.pos.y, this.size);
25 colorMode(RGB);
26 }
27
28 isDead() {
29 return this.life <= 0;
30 }
31 }
32
33 function setup() {
34 createCanvas(800, 600);
35 colorMode(HSB);
36 background(0);
37 }
38
39 function draw() {
40 background(0, 0, 0, 25); // trail effect
41
42 // Add particles continuously when mouse is pressed
43 if (mouseIsPressed) {
44 for (let i = 0; i < 5; i++) {
45 particles.push(new Particle(mouseX, mouseY));
46 }
47 }
48
49 // Occasionally add particles at random bottom positions
50 if (random(1) < 0.1) {
51 particles.push(new Particle(random(width), height));
52 }
53
54 for (let i = particles.length - 1; i >= 0; i--) {
55 let p = particles[i];
56 p.update();
57 p.display();
58 if (p.isDead()) {
59 particles.splice(i, 1);
60 }
61 }
62
63 // Title text
64 fill(255, 30);
65 textAlign(CENTER);
66 textSize(14);
67 text("Click and drag to create particle bursts", width / 2, 30);
68 }
69
70 function mousePressed() {
71 for (let i = 0; i < 30; i++) {
72 particles.push(new Particle(mouseX, mouseY));
73 }
74 }
75
76 function windowResized() {
77 resizeCanvas(800, 600);
78 }