Source of: Interactive Particle System, by Command A+
58 lines, 2 of them comments ยท 614 tokens, written in 3 seconds for $0.0009
1 let particles = [];
2
3 function setup() {
4 createCanvas(windowWidth, windowHeight);
5 noStroke();
6 }
7
8 function draw() {
9 background(30, 30, 60);
10
11 // Create a new particle at mouse position
12 let x = mouseX;
13 let y = mouseY;
14 if (mouseIsPressed) {
15 particles.push(new Particle(x, y));
16 }
17
18 // Update and display all particles
19 for (let i = particles.length - 1; i >= 0; i--) {
20 let p = particles[i];
21 p.update();
22 p.display();
23 if (p.isDead()) {
24 particles.splice(i, 1);
25 }
26 }
27 }
28
29 function windowResized() {
30 resizeCanvas(windowWidth, windowHeight);
31 }
32
33 class Particle {
34 constructor(x, y) {
35 this.pos = createVector(x, y);
36 this.vel = createVector(random(-1, 1), random(-2, -0.5));
37 this.acc = createVector(0, 0.05);
38 this.life = 255;
39 this.color = color(random(100, 255), random(100, 255), random(100, 255), this.life);
40 this.size = random(5, 15);
41 }
42
43 update() {
44 this.vel.add(this.acc);
45 this.pos.add(this.vel);
46 this.life -= 2;
47 this.color = color(red(this.color), green(this.color), blue(this.color), this.life);
48 }
49
50 display() {
51 fill(this.color);
52 ellipse(this.pos.x, this.pos.y, this.size);
53 }
54
55 isDead() {
56 return this.life <= 0;
57 }
58 }