Source of: Interactive Particle Fountain, by Grok 4.20
67 lines, 4 of them comments ยท 550 tokens, written in 5 seconds for $0.0016
1 let particles = [];
2
3 class Particle {
4 constructor(x, y) {
5 this.x = x;
6 this.y = y;
7 this.vx = random(-3, 3);
8 this.vy = random(-8, -4);
9 this.life = 255;
10 this.color = color(random(100, 255), random(100, 255), random(200, 255));
11 this.size = random(4, 10);
12 }
13
14 update() {
15 this.vy += 0.2; // gravity
16 this.x += this.vx;
17 this.y += this.vy;
18 this.life -= 4;
19 this.size *= 0.98;
20 }
21
22 display() {
23 noStroke();
24 fill(this.color.levels[0], this.color.levels[1], this.color.levels[2], this.life);
25 ellipse(this.x, this.y, this.size, this.size);
26 }
27
28 isDead() {
29 return this.life <= 0;
30 }
31 }
32
33 function setup() {
34 createCanvas(windowWidth, windowHeight);
35 background(20);
36 }
37
38 function draw() {
39 background(20, 40);
40
41 // Add new particles at mouse position
42 if (mouseIsPressed || frameCount % 3 === 0) {
43 for (let i = 0; i < 6; i++) {
44 particles.push(new Particle(mouseX, mouseY));
45 }
46 }
47
48 // Update and display particles
49 for (let i = particles.length - 1; i >= 0; i--) {
50 let p = particles[i];
51 p.update();
52 p.display();
53 if (p.isDead()) {
54 particles.splice(i, 1);
55 }
56 }
57
58 // Gentle instructions
59 fill(255, 80);
60 textAlign(CENTER);
61 textSize(14);
62 text("Click and drag to create more particles", width/2, height - 30);
63 }
64
65 function windowResized() {
66 resizeCanvas(windowWidth, windowHeight);
67 }