xxxxxxxxxx
let palette = ['#cb505f', '#e9a14d', '#ba311b', '#86321d'];
let vmin, vmax;
let cx, cy;
let ctx;
let NUM = 300;
let leafs = [];
function setup() {
createCanvas(1112, 834);
vmin = min(width, height);
vmax = max(width, height);
cx = width / 2;
cy = height / 2;
ctx = drawingContext;
let count = 0;
while (count < 10000) {
count++
let x = random(width);
let y = random(height);
let r = vmax * random(0.2, 0.01);
let a = random(-Math.PI, Math.PI);
let c = shuffle(palette).slice(0, 2)
if (leafs.some((l) => (mag(l.x - x, l.y - y) < (l.r + r) * 0.7))) {
continue
}
leafs.push({
x,
y,
a,
r,
c
})
}
}
function leafPiece(pg, x, y, r) {
pg.beginShape();
let x0 = x;
let y0 = y;
let r1 = r * 0.6;
let a1 = -PI * 0.65;
let x1 = x0 + cos(a1) * r1;
let y1 = y0 + sin(a1) * r1;
let x3 = x0;
let y3 = y0 - r;
let r2 = r * 0.7;
let a2 = PI * 0.5;
let x2 = x3 + cos(a2) * r2;
let y2 = y3 + sin(a2) * r2;
let a4 = PI - a1;
let x4 = x0 + cos(a4) * r1;
let y4 = y0 + sin(a4) * r1;
pg.vertex(x0, y0);
pg.bezierVertex(x1, y1, x2, y2, x3, y3);
pg.bezierVertex(x2, y2, x4, y4, x0, y0);
pg.endShape();
}
function leaf(pg, x, y, r, colors) {
let ctx = pg.drawingContext
let gd = ctx.createLinearGradient(0, -r * 0.8, 0, r * 0.35 * 0.8);
gd.addColorStop(0.0, colors[0]);
gd.addColorStop(1.0, colors[1]);
ctx.fillStyle = gd;
pg.push();
{
pg.translate(x, y);
leafPiece(pg, 0, 0, r);
for (let i of [-1, 1]) {
pg.push();
{
pg.rotate(i * PI * 0.25);
leafPiece(pg, 0, 0, r * 0.8);
}
pg.pop();
pg.push();
{
pg.rotate(i * PI * 0.48);
leafPiece(pg, 0, 0, r * 0.8);
}
pg.pop();
pg.push();
{
pg.rotate(i * PI * 0.68);
leafPiece(pg, 0, 0, r * 0.5);
}
pg.pop();
}
pg.triangle(0, -r * 0.5, -r * 0.015, r * 0.35, r * 0.015, r * 0.35);
}
pg.pop();
}
function draw() {
background('#f9e5ce');
for (let { x, y, a, r, c } of leafs) {
let f = `drop-shadow(0 0 16px rgba(180, 40, 0, 0.5))`
ctx.filter = f
let sz = r * 2;
let pg = createGraphics(sz, sz);
pg.noStroke();
leaf(pg, r, r, r, c);
imageMode(CENTER);
push();
{
translate(x, y);
rotate(a);
image(pg, 0, 0);
}
pop();
}
noLoop();
}
// save jpg
let lapse = 0; // mouse timer
function mousePressed(){
if (millis() - lapse > 400){
save("img_" + month() + '-' + day() + '_' + hour() + '-' + minute() + '-' + second() + ".jpg");
lapse = millis();
}
}