{
regenerate;
const width = 640,
height = 640,
image = await (upload ?? FileAttachment("bear.png")).image(),
imageData = getImageDate(image),
color = imageColor(imageData, width, height),
position = cm.randomNoise(3, 8),
rotate = cm.randomNoise(0, cm.TWO_PI * width),
filter = useFilter === "Open" ? filterColor(cm.random()) : (d) => d,
background = theme === "Dark" ? "black" : "white",
pack = d3.pack().size([width, height]).padding(3),
data = {
name: "0",
children: cm
.range(count)
.map((d) => ({ name: `0-${d}`, value: cm.random(0, 1000) }))
};
function draw(app) {
const root = pack(d3.hierarchy(data).sum((d) => d.value));
const circles = root.leaves();
app.append(cm.clear, { fill: background });
app
.data(circles)
.process(cm.derive, {
rotate: (d) => rotate(d.x / width, d.y / height),
position: (d) => {
const vertex = position(d.x / width, d.y / height) | 0;
const T = cm.range(vertex, 0, cm.TWO_PI);
const X = T.map((t) => d.r * Math.cos(t));
const Y = T.map((t) => d.r * Math.sin(t));
return [X, Y];
}
})
.append(cm.group, {
x: (d) => d.x,
y: (d) => d.y,
rotate: (d) => d.rotate
})
.append(cm.polygon, {
x: (d) => d.position[0],
y: (d) => d.position[1],
fill: (d) => filter(color(d.x, d.y))
});
}
function dispose(app) {
invalidation.then(() => app.dispose());
}
return cm.app({ width, height }).call(draw).call(dispose).start().node();
}