<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.js" type="text/javascript">
</script><script language="javascript" type="text/javascript">
var system; var oldX=0; var oldY=0; var img;
function setup() {
// img = loadImage("Android05.jpg");
img = loadImage("https://image.jimcdn.com/app/cms/image/transf/dimension=196x10000:format=png/path/sf5d368e95ee23ef6/image/i28726074297b6954/version/1470536319/image.png");
createCanvas(600, 750);
system = new ParticleSystem(createVector(100, 20));
}
function draw() {
background(img);
if (abs(mouseX - oldX)+abs(mouseY - oldY) > 100) {
system = new ParticleSystem(createVector(mouseX, mouseY));
oldX=mouseX; oldY=mouseY;
}
system.addParticle(); system.addParticle();
system.addParticle(); system.addParticle();
system.addParticle(); system.addParticle();
system.addParticle(); system.addParticle();
system.addParticle(); system.addParticle();
system.addParticle(); system.addParticle();
system.addParticle(); system.addParticle();
system.addParticle(); system.addParticle();
system.run();
for (var i = 0; i < height; i += 20) {
fill(125, 150, 10);
rect(0, i, width, 5);
}
}
// A simple Particle class
var Particle = function(position) {
this.acceleration = createVector(0.0, 1.50);
this.velocity = createVector(random(-20, 20), random(-20, 0));
this.position = position.copy();
this.lifespan = 25.0;
};
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 -= 1;
};
// Method to display
Particle.prototype.display = function() {
stroke(20, this.lifespan);
strokeWeight(2);
fill(80, 200, 255); R=random(-1, 1);
ellipse(this.position.x, this.position.y, 10+R, 10+R);
};
// Is the particle still useful?
Particle.prototype.isDead = function(){
if (this.lifespan < 0) {
return true;
} else {
return false;
}
};
var 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 (var i = this.particles.length-1; i >= 0; i--) {
var p = this.particles[i];
p.run();
if (p.isDead()) {
this.particles.splice(i, 1);
}
}
};
</script>