xxxxxxxxxx
142
let particles = [];
let n = 400; //number of particle
let stroke_r;
let baseImg;
let showImg = false;
let controller = function() {
this.strokeSize = 80;
}
let panel = new controller();
function setup() {
pixelDensity(1);
createCanvas(600, 400);
background(0);
// blendMode(ADD);
//generate image
baseImg = createGraphics(600, 400);
baseImg.background(0);
// setupImg();
let gui = new dat.GUI();
gui.add(panel, 'strokeSize', 0, 100);
// initialize particles
// for (let i = 0; i < n; i++) {
// particles.push(new Particle());
// }
}
function draw() {
background(0);
// tint(255, 10);
stroke_r = panel.strokeSize;
renderImg(stroke_r);
for (let i = 0; i < 5; i++) {
particles.push(new Particle());
}
if (particles.length > n) {
particles.splice(0,5);
}
// for(let i=0; i<particles.length; i++){
// let p = particles[i];
// p.calcforce();
// p.applyforce();
// p.display();
// }
for(let i=particles.length-1; i>=0; i--){
let p = particles[i];
p.calcforce();
p.applyforce();
p.update();
p.display();
// if (p.age > 50) {
// particles.splice(i, 1);
// }
if (p.age > 50) {
p.startDying();
}
if (p.dead == true) {
particles.splice(i, 1);
}
}
push();
strokeWeight(1);
stroke(255);
fill(255);
text("Press 'i' to show base image", 20,20);
pop();
}
function setupImg() {
baseImg.loadPixels();
let widthd = width * pixelDensity();
let heightd = height * pixelDensity();
for (let i = 0; i < widthd; i++) {
for (let j = 0; j < heightd; j++) {
let x = i / pixelDensity();
let y = j / pixelDensity();
baseImg.pixels[(i + j * widthd) * 4] = 0;
baseImg.pixels[(i + j * widthd) * 4 + 1] = 0;
baseImg.pixels[(i + j * widthd) * 4 + 2] = 0;
baseImg.pixels[(i + j * widthd) * 4 + 3] = 255;
}
}
baseImg.updatePixels();
}
function renderImg(stroke_r) {
baseImg.loadPixels();
// let wl = (mouseX-stroke_r);
// let wr = (mouseX+stroke_r);
// let hu = (mouseY+stroke_r);
// let hd = (mouseY-stroke_r);
if (mouseIsPressed) {
for (let i = 0; i <= width*pixelDensity(); i++) {
for (let j = 0; j <= height*pixelDensity(); j++) {
let d = dist(mouseX, mouseY, i/pixelDensity(), j/pixelDensity());
if (d <= stroke_r) {
baseImg.pixels[(i + j*pixelDensity()*width) * 4] += ((stroke_r-d)/stroke_r)*80;
baseImg.pixels[(i + j*pixelDensity()*width) * 4+1] += ((stroke_r-d)/stroke_r)*80;
baseImg.pixels[(i + j*pixelDensity()*width) * 4+2] += ((stroke_r-d)/stroke_r)*80;
}
}
}
}
baseImg.updatePixels();
if (showImg == true) {
image(baseImg, 0, 0);
}
}
function keyPressed() {
if (key == "i" || key == "I") {
showImg = !showImg;
}
}