{
const { width = 800, height = 600 } = {};
const mainPolygon = subdividePolygon(
[
[0, 0],
[width, 0],
[width, height],
[0, height]
],
5
);
const polygonPerimeter = (polygon) => {
let q = polygon[polygon.length - 1];
let sum = 0;
for (let p of polygon) {
sum += vec2.dist(q, p);
q = p;
}
return sum;
};
const curvePartition = noiseCurvePartition(simplexParams, { width, height });
const rand = makeRandFunction(randomSeed);
const {
maxRecursionLevel,
minArea,
maxPerimeterAreaRatio,
framesPerLevel,
colorVariation,
avgLuminosity,
lineWidth,
lineColor
} = partitionParam;
const recursivePartition = (
polygon,
baseColor,
level,
sideways,
result = []
) => {
const box = boundingBox(polygon);
const boxSides = [box.max[0] - box.min[0], box.max[1] - box.min[1]];
const smallestSide = Math.min(...boxSides);
const boxArea = boxSides[0] * boxSides[1];
const polyArea = Math.abs(polygonArea(polygon));
const polyPerimeter = polygonPerimeter(polygon);
if (
--level < 0 ||
smallestSide < 10 ||
polyArea < minArea ||
polyPerimeter / Math.sqrt(polyArea) > maxPerimeterAreaRatio
) {
result.push([polygon, baseColor]);
return result;
}
let polyA, polyB;
try {
[polyA, polyB] = curvePartition(polygon, sideways);
} catch (error) {
console.log({ error, polygon });
result.push([polygon, baseColor]);
return result;
}
recursivePartition(
polyA,
baseColor, //colorVary(baseColor, rand(-0.2, 0.2), rand(-0.05, 0.05), 0),
level,
!sideways,
result
);
recursivePartition(
polyB,
colorVaryLuv(
baseColor,
rand(-colorVariation, colorVariation),
rand(-colorVariation, colorVariation),
rand(-colorVariation * 0.2, colorVariation * 0.2)
),
level,
!sideways,
result
);
return result;
};
const ctx = DOM.context2d(width, height);
ctx.strokeStyle = lineColor;
ctx.lineWidth = lineWidth;
const dpr = window.devicePixelRatio;
const baseColor = `hsl(${rand(0, 360)},${rand(50, 60)}%,${
avgLuminosity * 100
}%)`;
ctx.fillStyle = baseColor;
ctx.fillRect(0, 0, width, height);
let imgData = ctx.getImageData(0, 0, width * dpr, height * dpr);
for (let level = 1; level <= maxRecursionLevel; level++) {
let pairs = recursivePartition(mainPolygon, baseColor, level);
for (let i = 0; i < framesPerLevel; i++) {
ctx.putImageData(imgData, 0, 0);
let opacity = i / (framesPerLevel - 1);
ctx.globalAlpha = opacity;
for (let [poly, color] of pairs) {
ctx.fillStyle = color;
ctx.beginPath();
for (let p of poly) ctx.lineTo(...p);
ctx.closePath();
ctx.fill();
if (lineWidth > 0) ctx.stroke();
// let q = findPointInPolygon(poly);
// ctx.fillStyle = "black";
// ctx.beginPath();
// ctx.arc(...q, 2, 0, Math.PI * 2);
// ctx.fill();
}
yield ctx.canvas;
}
imgData = ctx.getImageData(0, 0, width * dpr, height * dpr);
}
}