# Attach

> Pin a layer to a sibling every frame, at the drawn tip of a path or line, at any point along it, or right after the last visible character of a text.

Some layers need to follow another layer exactly: a dot on the end of a chart line that draws itself,
an arrow travelling along a route, a cursor after the text being typed. Computing those positions by
hand is tedious and breaks as soon as anything changes. `attach` does it for you, every frame:

```json
{ "id": "dot", "type": "ellipse", "width": 22, "attach": "trend" }
```

**Demo: A dot and a label attached to a line that draws on: both ride the drawn tip** (rendered video: https://vidmake.app/media/attach-path.mp4)

```json
{
  "fonts": [{ "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" }],
  "compositions": [{
    "id": "chart", "description": "A dot and a label attached to a line that draws on: both ride the drawn tip",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "grid", "type": "rect", "x": 640, "y": 380, "width": 900, "height": 1, "fill": "#d8d1c1",
        "repeat": { "count": 5, "layout": "column", "spacing": 90 } },
      { "id": "trend", "type": "line", "x": 640, "y": 380, "stroke": "#161412", "strokeWidth": 5,
        "points": [[0, 340], [150, 300], [300, 320], [450, 200], [600, 230], [750, 90], [900, 20]],
        "drawEnd": { "from": 0, "to": 1, "start": "0.3s", "duration": "2.4s", "ease": "easeInOutSine" }, "exit": "fade" },
      { "id": "dot", "type": "ellipse", "width": 22, "fill": "#c92a26", "attach": "trend", "exit": "fade" },
      { "id": "tag", "type": "text", "text": "+128%", "font": "Schibsted Grotesk", "fontWeight": 860, "fontSize": 40, "letterSpacing": -1.6,
        "fill": "#161412", "attach": "trend", "anchor": "bottom", "y": -26, "start": "0.3s", "enter": "fade", "exit": "fade" }
    ]
  }]
}
```

The dot and the `+128%` label both have `"attach": "trend"`. The line animates `drawEnd` from 0 to 1,
and both layers ride its tip. The label uses `"anchor": "bottom"` and `"y": -26` to sit above the point.

## Syntax

`attach` is either a sibling's id, or an object:

```json
"attach": "trend"
"attach": { "to": "route", "progress": 0.5, "rotate": true }
```

| field | default | notes |
|---|---|---|
| `to` | required | id of a **sibling** layer: one in the same `layers` or `children` list |
| `progress` | the target's `drawEnd` | paths and lines only: the point at this fraction of the path's length, 0..1 (clamped). Animatable, and may be an expression |
| `rotate` | false | paths and lines only: add the direction of the path at that point to the layer's `rotation` |

Unknown fields are rejected. The target can come before or after the attached layer in the list.

## What a layer can attach to

| target type | the point is | direction (for `rotate`) |
|---|---|---|
| [`path`, `line`](https://vidmake.app/docs/paths.md) | its drawn tip (`drawEnd`), or the point at `progress` | the path's direction of travel there |
| [`text`](https://vidmake.app/docs/text.md) | right after the last **visible** character, vertically centered on that line | along the text baseline |
| [`captions`](https://vidmake.app/docs/captions.md) | right after the last character of the caption on screen | along the baseline |

The point follows everything that moves the target: its own `x`/`y`, `rotation`, `scale`, a `width`
or `height` that scales a path, presets, and the group it is in. For text, the point follows `reveal`
and the `typewriter` preset, so it always sits at the end of what has been typed so far.

Attaching to any other layer type, to the layer itself, or to a layer in a different group fails
validation with a message that says why. Chains are allowed (a label attached to a path, a cursor
attached to that label's text...) as long as they don't loop back.

## Offsets and anchor

When a layer is attached, its own `x` and `y` are no longer positions: they are **offsets** from the
attach point, and default to 0. Its `anchor` decides which part of the layer sits on the point.

```json
{ "id": "tag", "type": "text", "text": "+128%", "attach": "trend", "anchor": "bottom", "y": -26 }
{ "id": "cursor", "type": "rect", "width": 4, "height": 40, "attach": "code", "anchor": "left", "x": 6 }
{ "id": "arrow", "type": "path", "d": "M0 0 L44 18 L0 36 L10 18 Z", "attach": "route", "anchor": [-0.2, 0.5] }
```

- `"anchor": "bottom"` with `"y": -26` puts a label's bottom edge 26 px above the point.
- `"anchor": "left"` with `"x": 6` starts a cursor 6 px after the last character.
- An anchor outside 0..1, like `[-0.2, 0.5]`, puts the layer ahead of the point: handy for an arrowhead
  that should lead the line instead of covering its end.

Offsets can be animated like any `x`/`y` (`"y": "=-26 - 6 * sin(t * 4)"` makes the label bob).

## Riding a path

On a path or line, the default point is the drawn tip, so anything attached follows a "draw on"
animation automatically. Set `progress` to choose the point yourself; it overrides `drawEnd` and works
even when the whole path is drawn:

**Demo: attach with progress and rotate: the arrow travels a curved path and turns with it** (rendered video: https://vidmake.app/media/attach-progress.mp4)

```json
{
  "compositions": [{
    "id": "route", "description": "attach with progress and rotate: the arrow travels a curved path and turns with it",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "route", "type": "path", "x": 640, "y": 360, "fill": null, "stroke": "#8a857a", "strokeWidth": 4, "dash": [2, 14],
        "d": "M0 300 C 200 -60, 420 420, 560 150 S 860 -40, 1000 120" },
      { "id": "start", "type": "ellipse", "width": 18, "fill": "#8a857a", "attach": { "to": "route", "progress": 0 } },
      { "id": "end", "type": "ellipse", "width": 18, "fill": "#c92a26", "attach": { "to": "route", "progress": 1 } },
      { "id": "arrow", "type": "path", "d": "M0 0 L44 18 L0 36 L10 18 Z", "fill": "#161412", "stroke": null,
        "attach": { "to": "route", "rotate": true, "progress": { "from": 0, "to": 1, "start": "0.3s", "duration": "3s" } } }
    ]
  }]
}
```

The dotted route is fully drawn; the two end dots use fixed `progress` values of 0 and 1; the arrow
animates `progress` with a tween and has `rotate: true`. The arrow's path points along +x, so the path
direction is all the rotation it needs. A shape that points up (like a `polygon` triangle) needs
`"rotation": 90` added to face forward.

`progress` is measured along the path's length, so a constant-speed tween gives constant speed on
screen, even through curves. Here are the arrow's real position and angle as it travels:

```console
$ vidmake trace attach-progress.json -l arrow --every 15
 FRAME    TIME        X        Y       W       H    ROT  SCALE  OPAC  NOTES
     0   0.00s    140.0    450.0    44.0    36.0  -58.4   1.00  1.00  
    15   0.50s    140.8    448.7    44.0    36.0  -58.4   1.00  1.00  
    30   1.00s    174.3    397.3    44.0    36.0  -52.3   1.00  1.00  
    45   1.50s    396.1    323.5    44.0    36.0   19.5   1.00  1.00  
    60   2.00s    798.2    167.3    44.0    36.0  -40.1   1.00  1.00  
    75   2.50s   1074.2    203.0    44.0    36.0   42.1   1.00  1.00  
    90   3.00s   1136.7    266.3    44.0    36.0   48.0   1.00  1.00  
   105   3.50s   1140.0    270.0    44.0    36.0   48.0   1.00  1.00
```

With [repeat](https://vidmake.app/docs/repeat.md), `progress` can spread copies along a path: each copy reads its own `i`.

```json
{ "id": "bead", "type": "ellipse", "width": 12, "repeat": 9,
  "attach": { "to": "route", "progress": "=i / (n - 1)" } }
```

> [!NOTE]
> On a path with several sub-paths (several `M` commands), the point is measured along all of them in the order
> they appear in `d`, like a pen moving stroke after stroke. That matches what you see with
> [`drawMode: "total"`](https://vidmake.app/docs/paths.md#several-strokes-drawmode). With the default `drawMode: "each"`, every sub-path
> draws at once while the attached layer still travels through them in order, so attach a pen or a cursor to a
> `total` path.

## A typing cursor

Attached to a text layer, a layer lands right after the last visible character. Animate the text's
`reveal` (or give it `"enter": "typewriter"`) and the cursor moves along as it types:

**Demo: A typing cursor: the bar is attached to the text and follows the typewriter's last character** (rendered video: https://vidmake.app/media/attach-cursor.mp4)

```json
{
  "fonts": [{ "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }],
  "compositions": [{
    "id": "cursor", "description": "A typing cursor: the bar is attached to the text and follows the typewriter's last character",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "prompt", "type": "text", "text": "$ vidmake render project.json", "font": "JetBrains Mono", "fontSize": 44, "fill": "#161412",
        "anchor": "left", "x": 220, "y": 340, "reveal": { "from": 0, "to": 1, "start": "0.4s", "duration": "1.8s", "ease": "linear" } },
      { "id": "cursor", "type": "rect", "width": 22, "height": 48, "fill": "#c92a26", "attach": "prompt", "anchor": "left", "x": 4,
        "opacity": "=t < 2.2 ? 1 : floor(t * 2.5) % 2 == 0", "lint": { "ignore": ["invisible"] } },
      { "id": "done", "type": "text", "text": "✓ done in 1.4s", "font": "JetBrains Mono", "fontSize": 32, "fill": "#8a857a",
        "anchor": "left", "x": 220, "y": 420, "start": "2.6s", "enter": "fadeUp" }
    ]
  }]
}
```

```json
{ "id": "cursor", "type": "rect", "width": 22, "height": 48, "fill": "#c92a26", "attach": "prompt", "anchor": "left", "x": 4,
  "opacity": "=t < 2.2 ? 1 : floor(t * 2.5) % 2 == 0", "lint": { "ignore": ["invisible"] } }
```

The cursor stays solid while typing and blinks afterwards. Since a blinking layer can be invisible at
its midpoint, the `invisible` [lint](https://vidmake.app/docs/lint.md) is silenced on it.

On multi-line text the point moves to the line the last visible character is on. It also works on
[captions](https://vidmake.app/docs/captions.md), where it follows the end of the caption currently on screen.

## Timing

An attached layer is only drawn while its target is on screen: if the target hasn't started yet or has
already ended, the attached layer is hidden too, even if its own timing says otherwise. Give both layers
the same `start` and `duration` (or none) unless you want that.

> [!AGENT]
> `vidmake inspect project.json -t 2s` reports an attached layer's resolved `x`/`y` (the attach point plus
> offsets) and its bounding box, so you can confirm it sits where you expect. `trace -l <id>` shows the
> point moving along the path over time.
