sketch
var img;
function preload() {
img = loadImage('https://i.imgur.com/8WPELuP.jpg');
}
var np = 100;
function particleStep() {
this.age++;
this.x += this.dx;
this.y += this.dy;
if (this.x > width) { this.x = width - (this.x - width);
this.dx = -this.dx;
} else if (this.x < 0) { this.x = -this.x;
this.dx = -this.dx;
}
if (this.y > height) { this.y = height - (this.y - height);
this.dy = -this.dy;
} else if (this.y < 0) { this.y = -this.y;
this.dy = -this.dy;
}
}
function particleDraw() {
this.clr = img.get(this.x, this.y);
stroke(this.clr[0], this.clr[1], this.clr[2], mouseY);
point(this.x, this.y);
}
function makeParticle(px, py, pdx, pdy) {
p = {x: px, y: py,
dx: pdx, dy: pdy,
age: 0,
stepFunction: particleStep,
drawFunction: particleDraw
}
return p;
}
var particles = [];
function setup() {
createCanvas(img.width, img.height);
background(mouseY);
for (var i = 0; i < np; i++) {
var p = makeParticle(600, 600,
random(-10, 10),
random(-10, 10));
particles.push(p);
}
frameRate(15);
}
function draw() {
strokeWeight(random(5));
for (var i = 0; i < np; i++) {
var p = particles[i];
p.stepFunction();
p.drawFunction();
}
}
function mousePressed() {
background(mouseY);
}