Beeswarm that makes room for a dragged disc
Drag a disc through a beeswarm: the dots keep their value on the axis and move up or down to make room. Three options for how the layout follows.
// Demonstrates obstacles in Separate.circlesAlongAxis: a beeswarm makes room for a
// disc that is dragged through it. The dots keep their value on the axis
// and move only up or down. From one move to the next, each dot keeps its
// order from top to bottom ("order"), only its side ("sides"), or is
// placed fresh.
import {
VisQuill, Reactive, Boxes, Segments, Points, Circles, Separate, Svg,
type Circle,
} from "@visquill/visquill-gdk"
import { generateSample } from "./sample"
import './styles.css'
// ─── Layout ───────────────────────────────────────────────────────────────
const axisMargin = 60
const dotRadius = 5
const dotPadding = 1
const discRadius = 45
const buttonWidth = 70
const buttonHeight = 24
const buttonGap = 6
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "demos-separate-make-room-")
const canvas = rvg.canvas
// ─── Instruction label ────────────────────────────────────────────
const instructions = canvas.text.label("Drag the disc through the dots · switch what the dots keep", "instructions")
Reactive.do([rvg.frame], () => {
Points.moveDown(Segments.midPoint(Boxes.topSide(rvg.frame)), 25, instructions)
})
// ─── Axis across the frame ────────────────────────────────────────
const axis = canvas.visuals.segment("axis")
Reactive.do([rvg.frame], () => {
const center = Points.moveDown(Boxes.center(rvg.frame), 40)
const half = Boxes.width(rvg.frame).value / 2 - axisMargin
Segments.segment(Points.moveLeft(center, half), Points.moveRight(center, half), axis)
})
// ─── The disc: a handle with a circle around it ───────────────────
const discVisual = canvas.visuals.circle("disc")
const disc = canvas.handles.disk("handle")
const obstacle = canvas.shapes.circle()
Reactive.do([axis], () => {
// At the start, the disc waits above the dots.
Points.moveUp(Segments.pointAt(axis, 0.3), 140, disc)
})
Reactive.do([disc], () => {
Circles.circleAt(disc, discRadius, obstacle)
Circles.copy(obstacle, discVisual)
})
// ─── Dots: at their value on the axis, then separated ─────────────
const sample = generateSample(5)
const onAxis: Circle[] = sample.map(() => canvas.shapes.circle())
const dots = sample.map(v => canvas.visuals.circle(v.group === 0 ? "dot-a" : "dot-b"))
// How the dots follow the last layout: keep their order along the
// axis normal, keep only their sides, or none (placed fresh).
const modes = ["order", "sides", "fresh"] as const
const mode = canvas.values.real(0)
let placed = false
Reactive.do([axis, disc, mode], () => {
sample.forEach((v, i) => {
Circles.circleAt(Segments.pointAt(axis, v.value / 100), dotRadius, onAxis[i])
})
const keep = modes[mode.value]
Separate.circlesAlongAxis(onAxis, "x", {
padding: dotPadding,
obstacles: [obstacle],
previous: keep !== "fresh" && placed ? { circles: dots, keep } : undefined,
}, dots)
placed = true
})
// ─── Buttons: order / sides / fresh ───────────────────────────────
const title = canvas.text.label("keep", "row-title")
const buttons = modes.map(name =>
({ box: canvas.visuals.box("button"), label: canvas.text.label(name, "button-label") }))
const note = canvas.text.label("", "note")
const notes = [
"Each dot keeps its order across the axis: no dot passes another.",
"Each dot stays on its side of the axis and moves back when there is room.",
"Every change places all dots anew, so dots can jump to the other side.",
]
Reactive.do([rvg.frame], () => {
const left = Points.move(Boxes.topLeft(rvg.frame), 20, 55)
Points.move(left, 0, buttonHeight / 2, title)
Points.move(left, 60, buttonHeight + 16, note)
buttons.forEach(({ box, label }, i) => {
Boxes.box(Points.moveRight(left, 60 + i * (buttonWidth + buttonGap)), buttonWidth, buttonHeight, box)
Boxes.center(box, label)
})
})
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]
mode.value = index
}
buttons.forEach(({ box }, i) => Svg.get(box).addEventListener("click", () => select(i)))
select(0)
}A beeswarm whose dots move across the axis to make room for a disc dragged through it.
Separate.circlesAlongAxis takes the disc as an obstacle, and previous with keep sets what the
dots keep from the last layout.
How it works
In every drag step, Separate.circlesAlongAxis moves the dots across the axis until they overlap neither each other
nor the disc:
Separate.circlesAlongAxis(onAxis, "x", {
padding: dotPadding,
obstacles: [obstacle],
previous: keep !== "fresh" && placed ? { circles: dots, keep } : undefined,
}, dots)
obstacles: kept clear of but not moved, and not part of the result.previous: the last layout;keepsays what the dots keep from it.
Notes
The buttons choose keep:
order: no dot passes another, but a dot can change to the other side of the disc.sides: each dot stays on its side and moves back when there is room.fresh: noprevious; many dots jump from side to side.
The sample is generated (sample.ts).
API
Separate.circlesAlongAxis: moves circles apart across an axisCircles.circleAt: the disc around the handle
See also
separate/separate-02-beeswarm




