Separate

Lava lamp with circles that pass each other

Blobs rise and sink in a band around a vertical axis and pass each other in any direction. Separate.circles keeps them apart in every frame.

// Demonstrates Separate.circles in an animation: blobs rise and sink
// around a vertical axis, and in every frame Separate.circles moves them
// apart in any direction. Each blob's own position is its height from the
// animation and its last place across, kept inside a band around the axis;
// so the blobs drift freely within the band and pass each other on the
// way up and down. Drag the edge of the band to make it wider or narrower.
// The blobs follow their places with Animate.shape.followAll.

import {
    VisQuill, Reactive, Boxes, Segments, Points, Circles, Separate, Animate, Svg,
    type Circle, type RvgAnimation,
} from "@visquill/visquill-gdk"
import { generateBlobs } from "./sample"
import './styles.css'

// ─── Layout ───────────────────────────────────────────────────────────────
const axisMargin   = 70      // from the frame top and bottom to the axis ends
const minRadius    = 6
const maxRadius    = 30
const padding      = 2
const alpha        = 0.12    // follow: share of the remaining way per frame
const blobCount    = 32
const loop         = 40000   // ms; one loop of the animation, then it repeats
const maxHalfBand  = 250     // px
const buttonWidth  = 70
const buttonHeight = 24

export function run(div: HTMLDivElement) {
    const rvg    = VisQuill.create(div, "demos-separate-lava-lamp-2d-")
    const canvas = rvg.canvas

    // ─── Instruction label ────────────────────────────────────────────
    const instructions = canvas.text.label("The blobs rise and sink · drag the edge of the band · pause and play", "instructions")
    Reactive.do([rvg.frame], () => {
        Points.moveDown(Segments.midPoint(Boxes.topSide(rvg.frame)), 25, instructions)
    })

    // ─── Vertical axis, and a band around it with a handle on its edge ─
    const band   = canvas.visuals.box("band")
    const axis   = canvas.visuals.segment("axis")
    const handle = canvas.handles.disk("handle")
    const half   = canvas.values.real(100)   // half the width of the band
    Reactive.do([rvg.frame], () => {
        const bottom = Points.moveUp(Segments.midPoint(Boxes.bottomSide(rvg.frame)), axisMargin)
        const top    = Points.moveDown(Segments.midPoint(Boxes.topSide(rvg.frame)), axisMargin + 30)
        Segments.segment(bottom, top, axis)
        Points.moveRight(Segments.midPoint(axis), half, handle)
    })
    // On drag: the handle moves only sideways; its distance to the axis is
    // half the band.
    Reactive.do([handle], () => {
        const center = Segments.midPoint(axis)
        half.value   = Math.min(maxHalfBand, Math.max(0, handle.x - center.x))
        Points.moveRight(center, half, handle)
    })
    Reactive.do([axis, half], () => {
        const top = Segments.pointAt(axis, 1)
        Boxes.box(Points.moveLeft(top, half), 2 * half.value, Segments.pointAt(axis, 0).y - top.y, band)
    })

    // ─── Blobs ────────────────────────────────────────────────────────
    const blobs   = generateBlobs(11, blobCount).sort((a, b) => b.size - a.size)
    const circles = blobs.map(b => canvas.visuals.circle(`blob-${b.color}`))
    const radii   = blobs.map(b => minRadius + (maxRadius - minRadius) * b.size)
    // Weights: a larger blob moves less.
    const weights = radii.map(r => r * r)
    const homes: Circle[] = blobs.map(() => canvas.shapes.circle())
    const targets = blobs.map(() => canvas.shapes.circle())
    let placed = false

    // Progress t of the loop (0 to 1): each blob's own position is its
    // height on the axis and its last place across, clamped into the band.
    // At the start all homes lie on the axis.
    const place = (t: number) => {
        const center = Segments.midPoint(axis)
        blobs.forEach((b, i) => {
            const height = b.center + b.swing * Math.sin(2 * Math.PI * (b.turns * t + b.phase))
            const across = placed ? Circles.center(targets[i]).x - center.x : 0
            const sideways = Math.min(half.value, Math.max(-half.value, across))
            Circles.circleAt(Points.moveRight(Segments.pointAt(axis, height), sideways), radii[i], homes[i])
        })
        Separate.circles(homes, { padding, weights, previous: placed ? targets : undefined }, targets)
        if (!placed) targets.forEach((target, i) => Circles.copy(target, circles[i]))
        placed = true
        Animate.shape.followAll(circles, targets, { alpha })
    }

    // ─── Animation, with pause and play ───────────────────────────────
    let progress = 0
    let animation: RvgAnimation | null = null
    const play = () => {
        const start = progress
        animation = Animate.progress.repeat(loop, t => {
            progress = (start + t) % 1
            place(progress)
        })
    }
    Reactive.do([axis, half], () => place(progress))

    const button = canvas.visuals.box("button")
    const label  = canvas.text.label("pause", "button-label")
    Reactive.do([rvg.frame], () => {
        Boxes.box(Points.move(Boxes.topLeft(rvg.frame), 20, 55), buttonWidth, buttonHeight, button)
        Boxes.center(button, label)
    })
    Svg.get(button).addEventListener("click", () => {
        if (animation) {
            animation.cancel()
            animation   = null
            label.value = "play"
        } else {
            play()
            label.value = "pause"
        }
    })
    play()
}

Blobs of different sizes rise and sink around a vertical axis and pass each other inside a band whose edge can be dragged. Separate.circles moves them apart in every frame, and Animate.shape.followAll lets the drawn blobs follow.

How it works

In each frame, a blob's own position is its height from the animation and its last place across, kept inside the band:

const across = placed ? Circles.center(targets[i]).x - center.x : 0
const sideways = Math.min(half.value, Math.max(-half.value, across))
Circles.circleAt(Points.moveRight(Segments.pointAt(axis, height), sideways), radii[i], homes[i])

Separate.circles moves the blobs apart from there; the visible blobs follow:

Separate.circles(homes, { padding, weights, previous: placed ? targets : undefined }, targets)
Animate.shape.followAll(circles, targets, { alpha })
  • The band keeps the blobs near the axis; inside it they drift freely.
  • weights: the area of each blob; a larger blob moves less.

Notes

At the start all homes lie on the axis; the blobs spread sideways from there. The blobs are generated (sample.ts).

API

  • Separate.circles: moves circles apart in any direction
  • Animate.progress.repeat: the animation loop
  • Animate.shape.followAll: the blobs follow their places

See also

  • separate/separate-07-vertical-beeswarm

API used

Also used (28)