chart = {
const context = DOM.context2d(width, height);
context.translate(200, 200);
const circlesPerGroup = {};
for (const group of groups) {
circlesPerGroup[group] = d3.packSiblings(
d3
.range(circles.filter((circle) => circle.group === group).length)
.map(() => ({
r: subgroupRadius + padding,
group: group
}))
);
}
const xOffset = 500;
const yOffsetPerGroup = 350;
let movedCircles = [];
for (const [group, data] of Object.entries(circlesPerGroup)) {
const groupNumber = groups.indexOf(group);
data.forEach((circle, index) => {
movedCircles.push({
x: data[index].x + xOffset,
y: data[index].y + yOffsetPerGroup * groupNumber,
fill: circle.group === "NFL" ? "#FC803B" : "#1B81F7",
group: circle.group
});
});
}
const tl = gsap.to(circles, {
x: (index, target, targets) => movedCircles[index].x,
y: (index, target, targets) => movedCircles[index].y,
fill: (index, target, targets) => movedCircles[index].fill,
duration: 1,
ease: "power.3.out",
stagger: { amount: 2 },
onUpdate: animate
});
tl.pause();
function animate() {
context.fillStyle = "white";
context.fillRect(-200, -200, width, height);
for (const { x, y, r, fill } of circles) {
context.beginPath();
context.arc(x, y, r - padding, 0, 2 * Math.PI);
context.fillStyle = fill;
context.fill();
}
}
animate();
tl.play();
return context.canvas;
}