xxxxxxxxxx
132
let particles = [];
let n = 250; //number of particle
let noiseScale = 0.006; //noise scale;
let stroke_r = 80; // size of the "pen"
let img; // source image
let imgW = 1200;
let imgH = 950;
let imgScale = 0.5; // scaling down
let baseImg; // buffer canvas
let showImg = false;
let arr = []; // storing the centers of star rotation
// standard yellow
const R = 180;
const G = 165;
const B = 60;
function preload() {
img = loadImage('sn.jpg');
}
function setup() {
pixelDensity(1);
createCanvas(imgW*imgScale, imgH*imgScale);
background(0);
//generate image
baseImg = createGraphics(imgW*imgScale, imgH*imgScale);
let arr = colorCompare(); // calculate the centers of rotation
baseImg.background(0);
renderImg(arr); // render the base image
// initialize particles
// for (let i = 0; i < n; i++) {
// particles.push(new Particle());
// }
}
function draw() {
background(0, 4);
// tint(255, 10);
// renderImg(stroke_r);
// generate 5 particles every frame
for (let i = 0; i < 5; i++) {
particles.push(new Particle());
}
// if the number reaches limit, pop out the particles
if (particles.length > n) {
particles.splice(0,5);
}
// move the particles
for(let i=0; i<particles.length; i++){
let p = particles[i];
p.calcforce();
p.calcNoise();
p.applyforce();
p.display();
}
}
// find pixels with smaller RGB distance to the standard yellow
function colorCompare() {
for (let i = 0; i < imgW*imgScale; i ++) {
for (let j = 0; j < imgH*imgScale; j ++) {
let c = img.get(i/imgScale, j/imgScale);
let r = c[0];
let g = c[1];
let b = c[2];
let gbdiff = abs(r-g);
if (gbdiff <= 20
&& r > R-20 && r < R+20
&& g > G-20 && g < G+20
&& b > B-30 && b < B+30) {
arr.push([i,j]);
}
}
}
return arr;
}
// render the base image
function renderImg(arr) {
baseImg.loadPixels();
for (let spot of arr) {
let x = spot[0];
let y = spot[1];
if (baseImg.pixels[(x + x*width) * 4] < 150) {
for (let i = 0; i <= imgW*imgScale; i++) {
for (let j = 0; j <= imgH*imgScale; j++) {
let d = dist(x, y, i, j);
if (d <= stroke_r) {
baseImg.pixels[(i + j*width) * 4] += ((stroke_r-d)/stroke_r)*10;
baseImg.pixels[(i + j*width) * 4+1] += ((stroke_r-d)/stroke_r)*10;
baseImg.pixels[(i + j*width) * 4+2] += ((stroke_r-d)/stroke_r)*10;
}
}
}
}
}
baseImg.updatePixels();
if (showImg == true) {
image(baseImg, 0, 0);
}
}
function keyPressed() {
if (key == "i" || key == "I") {
showImg = !showImg;
}
}