xxxxxxxxxx
let spark;
function setup() {
createCanvas(windowWidth, windowHeight);
spark = new Particle(random(width), random(height));
background('black');
}
function draw() {
spark.update();
spark.show();
}
function Particle(x, y) {
// The particle position has two components
this.x = x;
this.y = y;
// The particle velocity (position change or "delta")
this.dx = 0;
this.dy = 0;
// Acceleration is the change in velocity
this.ax = 0;
this.ay = 0;
// Accelerate the particle by incrementing its acceleration
this.applyForce = function(fx, fy){
this.ax += fx;
this.ay += fy;
}
// Update the particle position and accelearation
this.update = function(){
let b = 0.99; // viscosity
this.x += this.dx;
this.y += this.dy;
this.dx += this.ax;
this.dy += this.ay;
this.dx *= b;
this.dy *= b;
this.ax = 0;
this.ay = 0;
}
this.show = function(){
fill('red');
noStroke();
circle(this.x, this.y, 5);
}
}
function mousePressed() {
let dx = mouseX - spark.x;
let dy = mouseY - spark.y;
let k = 100; // spring constant
spark.applyForce(dx / k, dy / k);
}