xxxxxxxxxx
207
let pen = "#a8a883";
let border;
const numParticles = 500;
function setup() {
createCanvas(1000, 1000);
border = width * 0.05;
pixelDensity(1);
let g = createGraphics(width, height);
g.background(220);
drawShadow(0, 0, 30, color(0), g);
g.stroke(pen);
g.strokeWeight(2);
g.noFill();
g.rect(border, border, width - 2 * border, height - 2 * border);
let particles = [];
for (let i = 0; i < numParticles; i++) {
let c = pen;
let l = random(20, 1500);
if (random() > 0.9) c = "#99000066";
particles.push({
x: random(border + random(20), width - border - random(20)),
y: height - border, // - random(20),
vx: random(-1, 1),
vy: random(-1, 0),
life: l,
olife: l,
col: c,
});
particles.push({
x: random(border + random(20), width - border - random(20)),
y: border, // - random(20),
vx: random(-1, 1),
vy: random(1, 0),
life: l,
olife: l,
col: c,
});
}
drawShadow(0, 0, 0, 0, g);
// while (particles.length > 0) {
for (let i = particles.length - 1; i >= 0; i--) {
let p = particles[i];
while (p.life > 0) {
p.x += p.vx;
p.y += p.vy;
g.strokeWeight(map(p.life, p.olife, 0, 3.0, 0.2));
g.stroke(color(p.col));
g.point(p.x, p.y);
if (random() > 0.9) p.vx = random(-0.25, 0.25); //1,1);
if (random() > 0.9) {
if (p.vy > 0) p.vy = random(0.5, 0);
else p.vy = random(-0.5, 0);
}
p.life--;
if (
p.life <= 0 ||
p.x < border ||
p.x > width - border ||
p.y < border ||
p.y > height - border
)
break; //particles.splice(i,1);
}
}
dither(g);
let g2 = createGraphics(width, height);
g2.fill(color("#99000066"));
g2.noStroke();
drawShadow(1, 1, 40, color("#330000"), g2);
let s = random(width *0.75, width / 4);
if (random() > 0.5) g2.arc(border, border, s, s, 0, HALF_PI);
else {
let x = random(border + s, width - border - s);
let y = random(border + s, width - border - s);
g2.circle(x, y, s);
}
image(g, 0, 0);
image(g2, 0, 0);
// }
}
function draw() {
noLoop();
}
// https://p5js.org/reference/#/p5/drawingContext
function drawShadow(x, y, b, c, g = null) {
if (g == null) {
drawingContext.shadowOffsetX = x;
drawingContext.shadowOffsetY = y;
drawingContext.shadowBlur = b; // * scale;
drawingContext.shadowColor = c;
} else {
g.drawingContext.shadowOffsetX = x;
g.drawingContext.shadowOffsetY = y;
g.drawingContext.shadowBlur = b; // * scale;
g.drawingContext.shadowColor = c;
}
}
function index(g, x, y) {
if (g == null) return (x + y * width) * 4;
else return (x + y * g.width) * 4;
}
function DivideBy255(value) {
return (value + 1 + (value >> 8)) >> 8;
}
function dither(g) {
let referenceSize = 1000;
let hasMaxSize = false;
if (g == null) {
let _scale = Math.ceil(1, map(width, 0, referenceSize, 0, 1, hasMaxSize));
loadPixels();
for (let y = 0; y < height - _scale; y++) {
for (let x = _scale; x < width - _scale; x++) {
let oldr = pixels[index(g, x, y)];
let oldg = pixels[index(g, x, y) + 1];
let oldb = pixels[index(g, x, y) + 2];
let newr = (DivideBy255(oldr) * 255) | 0;
let newg = (DivideBy255(oldg) * 255) | 0;
let newb = (DivideBy255(oldb) * 255) | 0;
pixels[index(g, x, y)] = newr;
pixels[index(g, x, y) + 1] = newg;
pixels[index(g, x, y) + 2] = newb;
for (let _y = 1; _y <= _scale; _y++) {
for (let _x = 1; _x <= _scale; _x++) {
pixels[index(g, x + _x, y)] += ((oldr - newr) * 7) >> 4;
pixels[index(g, x + _x, y) + 1] += ((oldr - newr) * 7) >> 4;
pixels[index(g, x + _x, y) + 2] += ((oldr - newr) * 7) >> 4;
pixels[index(g, x - _x, y + _y)] += ((oldr - newr) * 3) >> 4;
pixels[index(g, x - _x, y + _y) + 1] += ((oldr - newr) * 3) >> 4;
pixels[index(g, x - _x, y + _y) + 2] += ((oldr - newr) * 3) >> 4;
pixels[index(g, x, y + _y)] += ((oldr - newr) * 5) >> 4;
pixels[index(g, x, y + _y) + 1] += ((oldr - newr) * 5) >> 4;
pixels[index(g, x, y + _y) + 2] += ((oldr - newr) * 5) >> 4;
pixels[index(g, x + _x, y + _y)] += ((oldr - newr) * 1) >> 4;
pixels[index(g, x + _x, y + _y) + 1] += ((oldr - newr) * 1) >> 4;
pixels[index(g, x + _x, y + _y) + 2] += ((oldr - newr) * 1) >> 4;
}
}
}
}
updatePixels();
} else {
let _scale = Math.ceil(1, map(g.width, 0, referenceSize, 0, 1, hasMaxSize));
g.loadPixels();
for (let y = 0; y < g.height - _scale; y++) {
for (let x = _scale; x < g.width - _scale; x++) {
let oldr = g.pixels[index(g, x, y)];
let oldg = g.pixels[index(g, x, y) + 1];
let oldb = g.pixels[index(g, x, y) + 2];
let newr = (DivideBy255(oldr) * 255) | 0;
let newg = (DivideBy255(oldg) * 255) | 0;
let newb = (DivideBy255(oldb) * 255) | 0;
g.pixels[index(g, x, y)] = newr;
g.pixels[index(g, x, y) + 1] = newg;
g.pixels[index(g, x, y) + 2] = newb;
for (let _y = 1; _y <= _scale; _y++) {
for (let _x = 1; _x <= _scale; _x++) {
g.pixels[index(g, x + _x, y)] += ((oldr - newr) * 7) >> 4;
g.pixels[index(g, x + _x, y) + 1] += ((oldr - newr) * 7) >> 4;
g.pixels[index(g, x + _x, y) + 2] += ((oldr - newr) * 7) >> 4;
g.pixels[index(g, x - _x, y + _y)] += ((oldr - newr) * 3) >> 4;
g.pixels[index(g, x - _x, y + _y) + 1] += ((oldr - newr) * 3) >> 4;
g.pixels[index(g, x - _x, y + _y) + 2] += ((oldr - newr) * 3) >> 4;
g.pixels[index(g, x, y + _y)] += ((oldr - newr) * 5) >> 4;
g.pixels[index(g, x, y + _y) + 1] += ((oldr - newr) * 5) >> 4;
g.pixels[index(g, x, y + _y) + 2] += ((oldr - newr) * 5) >> 4;
g.pixels[index(g, x + _x, y + _y)] += ((oldr - newr) * 1) >> 4;
g.pixels[index(g, x + _x, y + _y) + 1] += ((oldr - newr) * 1) >> 4;
g.pixels[index(g, x + _x, y + _y) + 2] += ((oldr - newr) * 1) >> 4;
}
}
}
}
g.updatePixels();
}
}