// Day12 Rainy — 梅雨を表現してみましょう。
/** @param {import('p5')} p */
export default function sketch(p, ctx) {
const { width, height, palette } = ctx;
let drops = [];
let ripples = [];
p.setup = () => {
p.createCanvas(width, height);
drops = Array.from({ length: 120 }, () => newDrop(true));
};
function newDrop(anywhere = false) {
return { x: p.random(width), y: anywhere ? p.random(height) : -20, v: p.random(4, 9), end: p.random(height * 0.55, height) };
}
p.draw = () => {
p.background(palette[0]);
p.stroke(palette[2]);
p.strokeWeight(1.5);
for (const d of drops) {
p.line(d.x, d.y, d.x, d.y + d.v * 2);
d.y += d.v;
if (d.y > d.end) {
ripples.push({ x: d.x, y: d.end, r: 0 });
Object.assign(d, newDrop());
}
}
p.noFill();
ripples = ripples.filter((r) => r.r < 40);
for (const r of ripples) {
const c = p.color(palette[1]);
c.setAlpha(255 * (1 - r.r / 40));
p.stroke(c);
p.ellipse(r.x, r.y, r.r * 2, r.r * 0.6);
r.r += 1;
}
};
}