Attach

Many relationships in a graphic are the same few: a bar stands on a baseline, a label sits above a bar, a marker sits on a circle. Attach has them ready-made. You state the relationship once, and the attached object follows its target from then on.

Attaching one thing to another

Drag the ends of the baseline:

6011080
Each bar stands on a pin on the baseline; each label sits above its bar.
const a = canvas.handles.disk("handle", [120, 230])
const b = canvas.handles.disk("handle", [520, 230])
const base = canvas.visuals.segment("line", a, b)

data.forEach((value, i) => {
    // A pin on the baseline, turned along it
    const foot = canvas.values.pin()
    Attach.pointToSegment(foot, base, { position: (i + 1) / (data.length + 1), orientation: "relative" })

    // A bar standing on the pin
    const bar = canvas.visuals.rectangle("shape")
    Attach.rectangleToPoint(bar, foot, { width: 40, height: value, anchorY: "bottom", orientation: "relative" })

    // A label above the bar
    const label = canvas.text.label(String(value), "label")
    Attach.pointToRectangleEdge(label, bar, { placement: "top", distance: 14 })
})

Every Attach line is one relationship, and they build on each other: the baseline moves the pins, the pins move the bars, the bars move the labels. There is no code that runs on a drag, and nothing computes where a bar goes. That is the point of Attach: you say what belongs where, not how to compute it.

Reading the names

The methods are named after what they attach and what to: Attach.<attached>To<target>(attached, target, options).

Attached Targets
a point a point, a segment, a circle, a rectangle (inside, or on an edge), an edge of a shape
a rectangle a point, a segment, an edge of a shape; several along a polyline
a segment a point, a segment, an edge of a shape
a circle a point, a circle
a polyline or polygon a segment, a circle, an edge of a shape
a whole shape a point (moves and turns along)

A few methods bind one property to a value instead: rectangleHeightToValue, rectangleSizeToValues, circleRadiusToValue. The API reference lists them all with their options.

The options you’ll meet everywhere

Option Means
position where on a segment: "start", "center", "end", or a ratio from 0 to 1
distance how far away from the target, at right angles to it
side which side, "left" or "right", seen in the direction of the target. On a segment drawn left to right, left is up
orientation "relative": turn along with the target; "absolute": keep your own direction
offset a fixed shift, as a vector [dx, dy]
anchorX, anchorY which point of a rectangle sits on the target: "left", "center", "bottom"…, or a ratio
paddingLeft, paddingRight how much of a segment to leave free at its ends

Attach and Reactive.do

An attachment is a Reactive.do that someone has already written: it depends on the target and the options, and writes the attached object. With one addition: the attached object counts as changed afterwards. That is why attachments chain, and why Reactive.do([bar], …) runs when an attachment moves bar.

Use Attach when one of its methods says what you mean, and Reactive.do for everything else. They mix freely: an attachment can target a point that a block writes, and a block can read a shape that an attachment writes.

Values as options

Every option that takes a number also takes a Real. Pass a Real, and the attachment follows it:

The handle sets height; the bar is attached to the baseline with height as its height.
const base = canvas.visuals.segment("line", [160, ground], [480, ground])

// The handle sets the height: it can only move up and down
const top = canvas.handles.disk("handle", [320, 120])
Reactive.do([top], () => {
    top.x = 320
    top.y = Math.min(ground, Math.max(30, top.y))
})
const height = canvas.values.real()
Reactive.do([top], () => {
    height.value = ground - top.y
})

// A Real as an option: the bar follows it
const bar = canvas.visuals.rectangle("shape")
Attach.rectangleToSegment(bar, height, base, { paddingLeft: 120, paddingRight: 120 })

So data can drive a graphic through attachments: change the value, and the bar is updated.

One writer per object

An attached object belongs to its attachment. Attach it twice, and both attachments write it, and whichever target changed last wins. Write it yourself, and the next change of the target overwrites you:

const p = canvas.values.point()
const first = canvas.values.point([0, 0])
const second = canvas.values.point([100, 100])

Attach.pointToPoint(p, first)
Attach.pointToPoint(p, second)   // p is at (100, 100) ...
first.x = 50                     // ... and now at (50, 0): the last change wins

p.x = 999                        // holds only until first or second moves again

If an object should follow two things, compute it in one Reactive.do that reads both.

An attachment can’t be removed again: it lasts as long as its objects. When a part of a graphic goes away, dispose of the group that holds it, and its attachments go with it.