xxxxxxxxxx
83
let system;
function setup() {
createCanvas(720, 400);
system = new ParticleSystem(createVector(width / 2, height / 2));
}
function draw() {
background(51);
intensity = map(mouseX, 0, width, 0, 255);
background(0,160,255,intensity);
system.addParticle();
system.run();
}
// A simple Particle class
let Particle = function(position) {
this.acceleration = createVector(0, 0.05);
this.velocity = createVector(random(-2, 2), random(-2, 2));
this.position = position.copy();
this.lifespan = 255;
this.size = random(100,50);
this.size = map(mouseX, 0, width, 10, 100);
this.shape = 'ellipse';
if(random(0,5)<1)
this.shape = 'rect';
};
Particle.prototype.run = function() {
this.update();
this.display();
};
// Method to update position
Particle.prototype.update = function(){
//this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.lifespan -= 2;
if(this.size>10)
this.size --;
};
// Method to display
Particle.prototype.display = function() {
stroke(255, this.lifespan);
strokeWeight(2);
fill(200, this.lifespan);
if(this.shape == 'ellipse')
ellipse(this.position.x, this.position.y, this.size, this.size);
else
{
rectMode(CENTER);
rect(this.position.x, this.position.y, this.size, this.size);
}
};
// Is the particle still useful?
Particle.prototype.isDead = function(){
return this.lifespan < 0;
};
let ParticleSystem = function(position) {
this.origin = position.copy();
this.particles = [];
};
ParticleSystem.prototype.addParticle = function() {
this.particles.push(new Particle(this.origin));
};
ParticleSystem.prototype.run = function() {
for (let i = this.particles.length-1; i >= 0; i--) {
let p = this.particles[i];
p.run();
if (p.isDead()) {
this.particles.splice(i, 1);
}
}
};