76 lines, 10 of them comments ยท 746 tokens, written in 21 seconds for $0.0022


 1  // Interactive Particle System in p5.js
 2  // Click and drag to create particles
 3  // Particles fade out over time
 4  
 5  let particles = [];
 6  
 7  function setup() {
 8    createCanvas(800, 600);
 9    colorMode(HSB, 360, 100, 100, 100);
10    background(0);
11  }
12  
13  function draw() {
14    // Semi-transparent background for trail effect
15    background(0, 0, 0, 10);
16    
17    // Add new particles when mouse is pressed
18    if (mouseIsPressed) {
19      for (let i = 0; i < 5; i++) {
20        particles.push(new Particle(mouseX, mouseY));
21      }
22    }
23    
24    // Update and display all particles
25    for (let i = particles.length - 1; i >= 0; i--) {
26      particles[i].update();
27      particles[i].display();
28      
29      // Remove dead particles
30      if (particles[i].isDead()) {
31        particles.splice(i, 1);
32      }
33    }
34    
35    // Display instructions
36    fill(255);
37    noStroke();
38    textSize(16);
39    textAlign(CENTER);
40    text('Click and drag to create particles', width / 2, 30);
41  }
42  
43  // Particle class
44  class Particle {
45    constructor(x, y) {
46      this.pos = createVector(x, y);
47      this.vel = p5.Vector.random2D().mult(random(2, 6));
48      this.acc = createVector(0, 0.1);
49      this.lifespan = 255;
50      this.hue = random(360);
51      this.size = random(5, 15);
52    }
53    
54    update() {
55      this.vel.add(this.acc);
56      this.pos.add(this.vel);
57      this.lifespan -= 3;
58    }
59    
60    display() {
61      noStroke();
62      fill(this.hue, 80, 100, this.lifespan);
63      ellipse(this.pos.x, this.pos.y, this.size);
64    }
65    
66    isDead() {
67      return this.lifespan < 0;
68    }
69  }
70  
71  // Add particles on click even without dragging
72  function mousePressed() {
73    for (let i = 0; i < 10; i++) {
74      particles.push(new Particle(mouseX, mouseY));
75    }
76  }