xxxxxxxxxx
// By Roni Kaufman
// https://ronikaufman.github.io
// Paintings by Claude Monet
let pd;
let images = [];
let compo;
let toAdd = [];
function preload() {
let haystacks = ["sun_in_the_mist_mia.jpg", "thaw_sunset_aic.jpg", "sunset_snow_effect_aic.jpg", "end_of_autumn_aic.jpg", "end_of_summer_aic.jpg", "snow_aic.jpg", "snow_effect_overcast_day_aic.jpg", "sketch_aic.jpg", "wiki_1.jpg", "wiki_2.jpg", "wiki_3.jpg", "wiki_4.jpg", "wiki_5.jpg", "wiki_6.jpg", "wiki_7.jpg", "wiki_8.jpg", "wiki_9.JPG", "wiki_10.JPG", "wiki_11.jpg", "wiki_12.jpg"];
for (let fileName of haystacks) {
loadImage(fileName, (img) => images.push(img));
}
}
function setup() {
pd = pixelDensity();
let h = pd*floor(windowHeight*0.8);
let wSum = 0;
for (let img of images) {
img.resize(0, h);
wSum += img.width;
}
let w = pd*floor(wSum/(pd*images.length));
for (let img of images) {
img.resize(w, h);
}
createCanvas(w/pd, h/pd, WEBGL);
frameRate(15);
image(random(images), -width/2, -height/2, width, height);
compo = [{
x: -width/2,
y: -height/2,
w: width,
h: height,
size: 16,
parent: undefined,
children: []
}];
}
function draw() {
if (toAdd.length == 0) {
let idx, el;
do {
idx = ~~random(compo.length);
el = compo[idx];
} while (el.children.length > 0)
if ((el.size != 1 && random() > 1/8) || !el.parent) {
// create 4 children
toAdd.push(
{
x: el.x,
y: el.y,
w: el.w/2,
h: el.h/2,
size: el.size/2,
parent: el,
children: []
},
{
x: el.x + el.w/2,
y: el.y,
w: el.w/2,
h: el.h/2,
size: el.size/2,
parent: el,
children: []
},
{
x: el.x,
y: el.y + el.h/2,
w: el.w/2,
h: el.h/2,
size: el.size/2,
parent: el,
children: []
},
{
x: el.x + el.w/2,
y: el.y + el.h/2,
w: el.w/2,
h: el.h/2,
size: el.size/2,
parent: el,
children: []
}
);
shuffle(toAdd, true);
el.children = [toAdd];
compo.push(toAdd);
} else {
// restore parent and kill all its decendants
killDescendants(el.parent);
toAdd.push(el.parent);
}
}
let {x, y, w, h} = toAdd.pop();
let img = random(images);
image(img, x, y, w, h);
}
function killDescendants(el) {
for (let c of el.children) {
if (c.children.length > 0) killDescendants(c);
let childIdx = compo.indexOf(c);
let child = compo.splice(childIdx, 1)[0];
killDescendants(child);
}
el.children = [];
}