Source of: Bouncing Orbs, by GPT-5.4
48 lines, no comments ยท 481 tokens, written in 6 seconds for $0.0075
1 let balls = [];
2 const NUM_BALLS = 20;
3
4 function setup() {
5 createCanvas(windowWidth, windowHeight);
6 noStroke();
7 for (let i = 0; i < NUM_BALLS; i++) {
8 balls.push({
9 x: random(width),
10 y: random(height),
11 vx: random(-2, 2),
12 vy: random(-2, 2),
13 r: random(8, 20),
14 c: color(random(100, 255), random(100, 255), random(100, 255), 180)
15 });
16 }
17 }
18
19 function draw() {
20 background(10, 15, 30, 40);
21
22 for (let i = 0; i < balls.length; i++) {
23 let b = balls[i];
24 b.x += b.vx;
25 b.y += b.vy;
26
27 if (b.x < b.r || b.x > width - b.r) b.vx *= -1;
28 if (b.y < b.r || b.y > height - b.r) b.vy *= -1;
29
30 fill(b.c);
31 circle(b.x, b.y, b.r * 2);
32
33 for (let j = i + 1; j < balls.length; j++) {
34 let other = balls[j];
35 let d = dist(b.x, b.y, other.x, other.y);
36 if (d < 140) {
37 stroke(255, map(d, 0, 140, 120, 0));
38 strokeWeight(1);
39 line(b.x, b.y, other.x, other.y);
40 noStroke();
41 }
42 }
43 }
44 }
45
46 function windowResized() {
47 resizeCanvas(windowWidth, windowHeight);
48 }