Source of: Interactive Particle System, by Qwen3 235B A22B Instruct 2507
82 lines, 10 of them comments ยท 660 tokens, written in 14 seconds for $0.0005
1 function setup() {
2 createCanvas(800, 600);
3 colorMode(HSB, 360, 100, 100, 1);
4 noStroke();
5
6 // Array to store particles
7 particles = [];
8 }
9
10 function draw() {
11 // Semi-transparent background for trail effect
12 fill(0, 0.1);
13 rect(0, 0, width, height);
14
15 // Add new particles based on mouse position
16 if (mouseIsPressed) {
17 for (let i = 0; i < 5; i++) {
18 particles.push(new Particle(mouseX, mouseY));
19 }
20 } else if (mouseX > 0 && mouseY > 0) {
21 // Add fewer particles when not pressing
22 if (frameCount % 3 === 0) {
23 particles.push(new Particle(mouseX, mouseY));
24 }
25 }
26
27 // Update and display all particles
28 for (let i = particles.length - 1; i >= 0; i--) {
29 particles[i].update();
30 particles[i].display();
31
32 // Remove dead particles
33 if (particles[i].isDead()) {
34 particles.splice(i, 1);
35 }
36 }
37 }
38
39 // Particle class
40 class Particle {
41 constructor(x, y) {
42 this.pos = createVector(x, y);
43 this.vel = p5.Vector.random2D();
44 this.vel.mult(random(2, 5));
45 this.acc = createVector(0, 0);
46 this.life = 255;
47 this.decay = random(3, 7);
48 this.size = random(3, 8);
49 this.hue = random(360);
50 }
51
52 update() {
53 this.vel.add(this.acc);
54 this.pos.add(this.vel);
55 this.acc.mult(0);
56 this.life -= this.decay;
57
58 // Add slight gravity
59 this.applyForce(createVector(0, 0.05));
60
61 // Slowly drift apart
62 this.vel.mult(0.97);
63 }
64
65 applyForce(force) {
66 this.acc.add(force);
67 }
68
69 display() {
70 fill(this.hue, 80, 90, this.life / 255);
71 ellipse(this.pos.x, this.pos.y, this.size);
72 }
73
74 isDead() {
75 return this.life <= 0;
76 }
77 }
78
79 // Reset particles on key press
80 function keyPressed() {
81 particles = [];
82 }