{
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, Math.PI * 2 * width),
pack = d3.pack().size([width, height]).padding(3),
random = d3.randomUniform(0, 1000),
randomEase = d3.randomInt(0, eases.length),
data = {
name: "0",
children: Array.from({ length: count }, (d) => ({
name: `0-${d}`,
value: random()
}))
},
root = pack(d3.hierarchy(data).sum((d) => d.value)),
circles = root.leaves(),
duration1 = 1500,
duration2 = 1000;
const node = SVG.svg({ width, height }, [
...circles.map((d) => {
const [r, g, b, a] = color(d.x, d.y);
const fill = `rgb(${r}, ${g}, ${b}, ${a})`;
return cm.transition(
{
keyframes: [
{
attr: { r: d.r },
delay: 500,
duration: duration1 - 500,
ease: eases[randomEase()]
}
]
},
[SVG.circle({ cx: d.x, cy: d.y, r: 0, fill })]
);
}),
...circles.map((d) => {
const [r, g, b, a] = color(d.x, d.y);
const gray = Math.round((r + g + b) / 3);
const codePoint = offset + gray;
return cm.transition(
{
keyframes: [
{ attr: { opacity: 1 }, delay: duration1, duration: duration2 }
]
},
[
character({
text: String.fromCodePoint(codePoint),
opacity: 1,
x: d.x,
y: d.y,
fill: "black",
opacity: 0,
fill: fontColor,
height: d.r * 2,
"font-weight": "900",
dy: "0.3em",
dx: "-0.3em",
transform: `rotate(${rotate(d.x / width, d.y / height)})`
})
]
);
})
]);
const sentences = generatePoem(node.textContent);
return HTML.div({ style: "display:flex" }, [
node,
HTML.article(
sentences.map((d, i) => {
const wait = 750;
const delay = duration1 + duration2;
return cm.transition(
{
keyframes: [
{ style: { opacity: 1 }, delay: delay + wait * i, duration: wait }
]
},
[
HTML.p(
{
style: [
"font-weight:bold",
"font-size:20px",
"margin-left: 2.5em",
"opacity: 0"
].join(";")
},
[d]
)
]
);
})
)
]);
}