Circle packing in groups
Bubbles of a made-up shop packed in one cluster per department, each with an outline and a name. Switch the groups off to pack all bubbles together.
// Demonstrates groups in Pack.circlesAround: products of a made-up shop,
// sized by their sales, packed in one cluster per department. The clusters
// are kept groupPadding apart; Circles.enclosing draws an outline around
// each, with the department's name. Switch groups off to pack all bubbles
// together. The bubbles follow their new places with
// Animate.shape.followAll.
import {
VisQuill, Reactive, Boxes, Segments, Points, Circles, Values, Pack, Animate, Svg,
type Circle,
} from "@visquill/visquill-gdk"
import { departments, generateProducts } from "./shop"
import './styles.css'
// ─── Layout ───────────────────────────────────────────────────────────────
const maxRadius = 27 // radius of the best-selling product
const padding = 2
const groupPadding = 36 // between two departments
const alpha = 0.12 // follow: share of the remaining way per frame
const buttonWidth = 70
const buttonHeight = 24
const buttonGap = 6
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "demos-pack-groups-")
const canvas = rvg.canvas
// ─── Instruction label ────────────────────────────────────────────
const instructions = canvas.text.label("Switch the groups on and off", "instructions")
Reactive.do([rvg.frame], () => {
Points.moveDown(Segments.midPoint(Boxes.topSide(rvg.frame)), 25, instructions)
})
// ─── Products: sizes, packed targets, visuals ─────────────────────
const products = generateProducts(2)
const largest = Math.max(...products.map(p => p.sales))
const sizes: Circle[] = products.map(p => Circles.circleAt([0, 0], Values.sqrtScale(p.sales, largest, maxRadius)))
const groups = products.map(p => p.department)
const targets = products.map(() => canvas.shapes.circle())
const outlines = departments.map(() => canvas.visuals.circle("outline"))
const names = departments.map(name => canvas.text.label(name, "name"))
const bubbles = products.map(p => canvas.visuals.circle(`bubble-${p.department}`))
// An outline around the drawn bubbles of each department, its name
// above it.
departments.forEach((_, d) => {
const members = bubbles.filter((_, i) => groups[i] === d)
Reactive.do(members, () => {
Circles.enclosing(members, outlines[d])
Points.moveUp(Circles.center(outlines[d]), Circles.radius(outlines[d]).value + 12, names[d])
})
})
// ─── Pack ─────────────────────────────────────────────────────────
const grouped = canvas.values.bool(true)
let placed = false
Reactive.do([rvg.frame, grouped], () => {
Pack.circlesAround(sizes, Points.moveDown(Boxes.center(rvg.frame), 40), {
padding,
groups: grouped.value ? groups : undefined,
groupPadding,
}, targets)
// The first packing is shown at once; after that the bubbles
// follow their new places.
if (!placed) targets.forEach((target, i) => Circles.copy(target, bubbles[i]))
placed = true
Animate.shape.followAll(bubbles, targets, { alpha })
const directive = grouped.value ? "@class-remove hidden" : "@class hidden"
outlines.forEach(outline => outline.applyStyleDirective(directive))
names.forEach(name => name.applyStyleDirective(directive))
})
// ─── Buttons: groups on / off ─────────────────────────────────────
const title = canvas.text.label("groups", "row-title")
const buttons = ["on", "off"].map(name =>
({ box: canvas.visuals.box("button"), label: canvas.text.label(name, "button-label") }))
const note = canvas.text.label("", "note")
const notes = [
"One cluster per department, the clusters packed around each other.",
"All products packed together; the departments mix.",
]
Reactive.do([rvg.frame], () => {
const left = Points.move(Boxes.topLeft(rvg.frame), 20, 55)
Points.move(left, 0, buttonHeight / 2, title)
buttons.forEach(({ box, label }, i) => {
Boxes.box(Points.moveRight(left, 80 + i * (buttonWidth + buttonGap)), buttonWidth, buttonHeight, box)
Boxes.center(box, label)
})
Points.move(left, 80 + 2 * (buttonWidth + buttonGap) + 10, buttonHeight / 2, note)
})
const select = (index: number) => {
buttons.forEach(({ box, label }, i) => {
const directive = i === index ? "@class active" : "@class-remove active"
box.applyStyleDirective(directive)
label.applyStyleDirective(directive)
})
note.value = notes[index]
grouped.value = index === 0
}
buttons.forEach(({ box }, i) => Svg.get(box).addEventListener("click", () => select(i)))
select(0)
}Products of a made-up shop as bubbles sized by sales, packed in one cluster per department.
Pack.circlesAround with groups builds the clusters, Circles.enclosing the outlines, and
Animate.shape.followAll moves the bubbles when the groups change.
How it works
groups gives each circle a key; circles with the same key form a cluster, and the clusters are packed with each
other:
Pack.circlesAround(sizes, Points.moveDown(Boxes.center(rvg.frame), 40), {
padding,
groups: grouped.value ? groups : undefined,
groupPadding,
}, targets)
groups: one department index per product.groupPadding: the gap between two clusters, larger thanpadding, so the names fit in between.Circles.enclosing: one outline around the drawn bubbles of each department.
The bubbles follow targets with Animate.shape.followAll.
Notes
The shop is made up (shop.ts).
API
Pack.circlesAround: packs circles around a point, here withgroupsCircles.enclosing: the outline of a departmentAnimate.shape.followAll: the bubbles follow their places
See also
pack/pack-01-bubblespack/pack-03-zoom




