# Repeat

> Stamp out copies of a layer or group with `repeat`, place them in a ring, grid, row or column, stagger them with `delay` or `stagger`, and vary each copy with `i` and `n`.

`repeat` turns one layer into many. It is vidmake's answer to `Array.map` over components in
Remotion: write the layer once, and every copy gets its own index to vary its position, color, timing
or content.

```json
{ "id": "dot", "type": "ellipse", "width": 24, "x": "50%", "y": "50%",
  "repeat": { "count": 14, "layout": "radial", "radius": 200, "delay": "0.05s" },
  "fill": "=hsl(i * 360 / n, 60, 70)", "enter": "pop" }
```

Copies are real layers: they show up in `inspect`, `trace` and `validate` with their own ids, and each
one is evaluated separately on every frame.

## Fields

`"repeat": 8` is shorthand for `{ "count": 8 }`. The object form takes:

| field | default | notes |
|---|---|---|
| `count` | required | number of copies, 1 to 10000 |
| `layout` | `none` | `none`, `radial` (alias `circle`), `grid`, `row` or `column` |
| `radius` | 200 | radial: distance of each copy from the layer's `x`/`y`, in px |
| `startAngle` | 0 | radial: angle of the first copy, in degrees; 0 is 12 o'clock, positive is clockwise |
| `endAngle` | | radial: angle of the last copy. Without it, copies spread evenly around the full circle. With it, they go from `startAngle` to `endAngle` **inclusive** (a sweep of exactly 360° spreads them evenly instead) |
| `rotate` | false | radial: `true` turns each copy by its angle, so it points outwards |
| `cols` | √count, rounded up | grid: copies per row |
| `spacing` | 100 | grid, row, column: distance between copy **centers**, a number or `[x, y]` |
| `delay` | 0 | time added to each copy's `start`: copy `i` starts `i × delay` later |
| `stagger` | | spread the copies' starts over a total time instead, in any order: `{ "over": "0.6s", "from": "center" }`. See [Stagger](#stagger). Not together with `delay` |
| `origin` | `center` | grid, row, column: where `x`/`y` sits in the block. `start` puts the **first** copy at `x`/`y` and the rest follow right and down; `end` puts the last copy there |

All of these are plain values; they can't be animated. To animate the arrangement itself (a ring that
grows, a grid that spreads), use `layout: "none"` and compute the position in an
[expression](#positions-from-expressions).

Unknown fields are rejected, with the list of allowed ones.

## Layouts

Every layout places copies **around the layer's own `x`/`y`**: the offsets are added to the position
you give the layer, which may itself be animated.

- **`none`** puts every copy at the same spot. Combine with expressions of `i` to fan copies out by
  rotation, scale or position.
- **`radial`** places copies on a circle of `radius` around `x`/`y`. Copy 0 is at `startAngle`.
- **`grid`** fills rows of `cols` copies, left to right and top to bottom. The whole block is centered on
  `x`/`y`.
- **`row`** is a single row of all copies, centered on `x`/`y`, `spacing` apart (the x part of `[x, y]`).
- **`column`** is a single column, centered, `spacing` apart (the y part of `[x, y]`).

Grid, row and column center the block on `x`/`y` by default. When copies should line up with something else,
like a list under a left-aligned heading, set `"origin": "start"`: the first copy lands exactly on `x`/`y` and
the others follow at `spacing`, so a column of four rows at `"y": 380` sits at 380, 530, 680 and 830 with no
arithmetic.

```json
{ "id": "row", "type": "text", "text": "${pick(i, ['data entry', 'follow-ups', 'reports'])}", "align": "left",
  "anchor": "left", "x": 140, "y": 380, "repeat": { "count": 3, "layout": "column", "spacing": 150, "origin": "start" } }
```

### Radial

**Demo: Radial repeat: a full ring of dots with a delay, and a gauge from -120° to 120° with rotate: true** (rendered video: https://vidmake.app/media/repeat-radial.mp4)

```json
{
  "compositions": [{
    "id": "radial", "description": "Radial repeat: a full ring of dots with a delay, and a gauge from -120° to 120° with rotate: true",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "dot", "type": "ellipse", "width": 26, "x": 360, "y": 360,
        "repeat": { "count": 12, "layout": "radial", "radius": 170, "delay": "0.08s" },
        "fill": "=lerp('#d8d1c1', '#161412', i / (n - 1))", "enter": "pop", "exit": "scale" },
      { "id": "tick", "type": "rect", "width": 4, "height": "=i % 5 == 0 ? 44 : 22", "x": 920, "y": 380,
        "repeat": { "count": 25, "layout": "radial", "radius": 190, "startAngle": -120, "endAngle": 120, "rotate": true, "delay": "0.03s" },
        "fill": "=i % 5 == 0 ? '#161412' : '#8a857a'", "enter": "fade", "exit": "fade" },
      { "id": "needle", "type": "rect", "width": 6, "height": 170, "cornerRadius": 1, "anchor": "bottom", "x": 920, "y": 380, "fill": "#c92a26", "enter": { "type": "fade", "delay": "0.6s" }, "exit": "fade",
        "rotation": [[0, -120], ["1.2s", -120], ["2.6s", 75, "easeOutBack"], ["3.4s", 75], ["4s", -120, "easeInOutCubic"]] },
      { "id": "hub", "type": "ellipse", "width": 30, "x": 920, "y": 380, "fill": "#161412", "enter": { "type": "pop", "delay": "0.6s" }, "exit": "fade" }
    ]
  }]
}
```

The left ring is 12 dots with the default full circle and a `delay` of 0.08 s, so the `pop` entrances
and `scale` exits run around the ring. The gauge on the right uses `startAngle: -120`, `endAngle: 120`
and `rotate: true`: 25 ticks from the first angle to the last, each turned to face outwards. Its tick
length and color come from `i % 5 == 0`.

> [!TIP]
> With `rotate: true` the copy's own `rotation` is added on top of its angle. A `rect` taller than it is
> wide points away from the center; to make it tangent instead, add `"rotation": 90`.

### Grid, row and column

**Demo: Grid repeat: 70 copies, each reading its column and row from i to ripple out from the center** (rendered video: https://vidmake.app/media/repeat-grid.mp4)

```json
{
  "compositions": [{
    "id": "grid", "description": "Grid repeat: 70 copies, each reading its column and row from i to ripple out from the center",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "cell", "type": "rect", "width": 64, "height": 64, "cornerRadius": 3, "x": 640, "y": 360,
        "repeat": { "count": 70, "layout": "grid", "cols": 10, "spacing": [100, 90] },
        "scale": "=0.35 + 0.55 * (0.5 + 0.5 * sin(t * tau / 2 - sqrt(pow(i % 10 - 4.5, 2) + pow(floor(i / 10) - 3, 2)) * 0.9))",
        "fill": "=lerp('#e9e4d8', '#161412', 0.5 + 0.5 * sin(t * tau / 2 - sqrt(pow(i % 10 - 4.5, 2) + pow(floor(i / 10) - 3, 2)) * 0.9))" }
    ]
  }]
}
```

That grid is 70 copies of one square: `cols: 10` and `spacing: [100, 90]`. Each copy works out
its column as `i % 10` and its row as `floor(i / 10)`, then measures its distance from the center to
delay a sine ripple. Row and column layouts are handy for bars, labels, and dots in a line:

```json
{ "id": "bar", "type": "rect", "width": 60, "anchor": "bottom", "x": "50%", "y": 600,
  "repeat": { "count": 8, "layout": "row", "spacing": 90, "delay": "0.08s" },
  "height": "=pick(i, [120, 340, 260, 420, 380, 520, 470, 610])", "enter": "fadeUp" }
```

### Same spot

With the shorthand `"repeat": 18` every copy sits in the same place, and the expressions do the rest:

**Demo: "repeat": 18 stacks every copy on the same spot; rotation by i fans them into a rosette** (rendered video: https://vidmake.app/media/repeat-shorthand.mp4)

```json
{
  "compositions": [{
    "id": "spiro", "description": "\"repeat\": 18 stacks every copy on the same spot; rotation by i fans them into a rosette",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "petal", "type": "rect", "repeat": 18, "width": 380, "height": 380, "cornerRadius": 90,
        "fill": null, "stroke": "=lerp('#161412', '#1f5fb0', i / n)", "strokeWidth": 1.5,
        "rotation": "=i * 10 + t * 22.5", "scaleX": "=0.6 + 0.4 * sin(t * pi / 2 + i * 0.35)" }
    ]
  }]
}
```

## i and n

Every copy's [expressions](https://vidmake.app/docs/expressions.md) see two extra values:

| variable | value |
|---|---|
| `i` (alias `index`) | the copy's index, from 0 to `n - 1` |
| `n` (alias `count`) | the number of copies |

Common patterns:

| goal | expression |
|---|---|
| a color per copy from a palette | `"fill": "=pick(i, ['#4a4640', '#1f5fb0', '#c92a26'])"` |
| a gradient across copies | `"fill": "=lerp('#161412', '#1f5fb0', i / (n - 1))"` |
| a hue wheel | `"fill": "=hsl(i * 360 / n, 60, 70)"` |
| a value per copy | `"height": "=pick(i, [120, 340, 260])"` |
| a random-looking constant per copy | `"x": "=w * rand(i)"` |
| a phase offset | `"y": "=360 + sin(t * 3 + i * 0.4) * 60"` |
| a label per copy | `"text": "${pad(i + 1, 2)} / ${pad(n, 2)}"` |
| grid column and row | `i % cols` and `floor(i / cols)` |

Outside a repeat, `i` is the layer's index among its siblings and `n` is 1.

### A different file per copy

`src` takes `${...}` too, evaluated once per copy with `i` and `n`, so one repeated image (or video, audio,
captions or waveform) layer can show a different file in each copy:

**Demo: One repeated image layer shows a different file per copy, lined up from the first copy with origin: start** (rendered video: https://vidmake.app/media/repeat-src.mp4)

```json
{
  "fonts": [{ "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }],
  "compositions": [{
    "id": "lane", "description": "One repeated image layer shows a different file per copy, lined up from the first copy with origin: start",
    "width": 1280, "height": 720, "fps": 30, "duration": "3s", "background": "#f4f1ea",
    "layers": [
      { "id": "icon", "type": "image", "src": "icons/${pick(i, ['draft', 'review', 'ship'])}.svg", "width": 150,
        "svgVars": { "currentColor": "#161412", "--accent": "#161412" }, "x": 290, "y": 320,
        "repeat": { "count": 3, "layout": "row", "spacing": 350, "origin": "start", "delay": "0.25s" }, "enter": "pop" },
      { "id": "label", "type": "text", "text": "${pad(i + 1, 2)}  ${pick(i, ['draft', 'review', 'ship'])}", "font": "JetBrains Mono",
        "fontSize": 24, "letterSpacing": 3, "uppercase": true, "fill": "#161412", "x": 290, "y": 470,
        "repeat": { "count": 3, "layout": "row", "spacing": 350, "origin": "start", "delay": "0.25s" }, "enter": "fadeUp" }
    ]
  }]
}
```

```json
{ "id": "icon", "type": "image", "src": "icons/${pick(i, ['draft', 'review', 'ship'])}.svg", "width": 150,
  "x": 290, "y": 320, "repeat": { "count": 3, "layout": "row", "spacing": 350, "origin": "start", "delay": "0.25s" } }
```

## Delay and timing

`delay` shifts each copy's `start` by `i × delay`. Everything about a layer's timing is local to its
start, so this one field staggers presets, keyframes, tweens and `t`-based expressions all at once.

How the copies end depends on how the layer's length is set:

- with `duration`, every copy lasts that long, so they also **end** staggered (exits cascade too);
- with `end`, every copy ends at that time, so they leave **together**;
- with neither, every copy lasts until its parent ends.

`vidmake timeline` lists every copy with its own start, so you can check a stagger at a glance.

## Stagger

`stagger` shifts each copy's `start` like `delay`, but spreads the starts over a **total time** and in any
order, so 15 copies and 50 copies both finish starting within the same budget:

```json
"repeat": { "count": 15, "layout": "row", "spacing": 60, "stagger": { "over": "0.6s", "from": "edges" } }
```

**Demo: repeat.stagger: a row grows in from the edges, a grid pops in a fixed shuffle, both over 0.6 s** (rendered video: https://vidmake.app/media/repeat-stagger.mp4)

```json
{
  "compositions": [{
    "id": "rstagger", "description": "repeat.stagger: a row grows in from the edges, a grid pops in a fixed shuffle, both over 0.6 s",
    "width": 1280, "height": 720, "fps": 30, "duration": "3s", "background": "#f4f1ea",
    "layers": [
      { "id": "bar", "type": "rect", "width": 36, "height": 120, "cornerRadius": 2, "anchor": "bottom", "x": 640, "y": 220,
        "fill": "#161412", "repeat": { "count": 15, "layout": "row", "spacing": 60, "stagger": { "over": "0.6s", "from": "edges" } },
        "enter": { "from": { "scaleY": 0 }, "motion": "snap" } },
      { "id": "dot", "type": "ellipse", "width": 40, "x": 640, "y": 480, "fill": "=pick(i, ['#161412', '#d8d1c1', '#1f5fb0'])",
        "repeat": { "count": 36, "layout": "grid", "cols": 12, "spacing": 70, "stagger": { "over": "0.6s", "from": "random" } },
        "enter": "pop" }
    ]
  }]
}
```

It takes the same fields as a [group stagger](https://vidmake.app/docs/groups.md#stagger):

| field | default | notes |
|---|---|---|
| `over` | `0.5s` | from the first copy's start to the last one's |
| `max` | `100ms` | the longest gap between two copies |
| `each` | | a fixed gap instead (the same as `delay`) |
| `from` | `first` | `first`, `last`, `center`, `edges`, `random` (a fixed shuffle) or an index |
| `spread` | `linear` | an [easing](https://vidmake.app/docs/easing.md) for the start times: `easeIn` starts slowly and speeds up |

`"stagger": true` uses the defaults. `from` follows the copy index, so on a grid `center` means the middle
copies of the list, not the middle of the block. For a 2D ripple, compute each copy's clock from its column
and row in an expression, as the [grid demo](#grid-row-and-column) does.

> [!NOTE]
> For a timing no stagger field gives you, skip `delay` and compute each copy's own clock in an expression:
> `"scale": "=easeOutBack(clamp((t - sqrt(i) * 0.2) / 0.5, 0, 1))"`.

## Repeated groups

`repeat` works on any layer type, including [groups](https://vidmake.app/docs/groups.md) and [components](https://vidmake.app/docs/components.md).
The group is copied with all its children, and **every child sees the group copy's `i` and `n`**, so
one card definition can produce a whole row of different cards:

**Demo: A repeated group: every child sees the copy's i and n, so one card definition makes four** (rendered video: https://vidmake.app/media/repeat-group.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" },
    { "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }
  ],
  "compositions": [{
    "id": "cards", "description": "A repeated group: every child sees the copy's i and n, so one card definition makes four",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "card", "type": "group", "width": 250, "height": 320, "x": 640, "y": 360,
        "repeat": { "count": 4, "layout": "row", "spacing": 280, "delay": "0.15s" }, "enter": "fadeUp", "exit": "fade",
        "children": [
          { "id": "bg", "type": "rect", "cornerRadius": 4, "fill": "#fbf9f4", "stroke": "#161412", "strokeWidth": 1.5 },
          { "id": "num", "type": "text", "text": "${pad(i + 1, 2)} / ${pad(n, 2)}", "font": "JetBrains Mono", "fontSize": 17,
            "letterSpacing": 2, "fill": "#8a857a", "y": 46 },
          { "id": "dot", "type": "ellipse", "width": 90, "y": 150, "fill": "=pick(i, ['#161412', '#d8d1c1', '#1f5fb0', '#c92a26'])" },
          { "id": "word", "type": "text", "text": "${pick(i, ['Write', 'Validate', 'Look', 'Render'])}", "font": "Schibsted Grotesk",
            "fontWeight": 860, "fontSize": 40, "letterSpacing": -1.6, "fill": "#161412", "y": 262 }
        ] }
    ]
  }]
}
```

A child that has its own `repeat` uses its own `i` instead. Groups are also how to give each copy
several parts that move together, or a [mask](https://vidmake.app/docs/masks.md) that follows the copy.

## Ids

Each copy's id is the layer's id plus `.` and the copy index: `dot.0`, `dot.1`, ... Use those ids
with the inspection commands:

```bash
vidmake trace project.json -l dot.3 --every 6
vidmake inspect project.json -t 2s -l card.2
```

```console
$ vidmake trace repeat-radial.json -l dot.3 --every 6 --to 1s
 FRAME    TIME        X        Y       W       H    ROT  SCALE  OPAC  NOTES
     0   0.00s  (not visible)
     6   0.20s  (not visible)
    12   0.40s    530.0    360.0    26.0    26.0    0.0   0.75  1.00  enter:pop
    18   0.60s    530.0    360.0    26.0    26.0    0.0   1.12  1.00  enter:pop
    24   0.80s    530.0    360.0    26.0    26.0    0.0   1.00  1.00  enter:pop
    30   1.00s    530.0    360.0    26.0    26.0    0.0   1.00  1.00  enter:pop
```

Children inside a repeated group keep their own ids (every copy of `card` has a child called `word`);
`inspect` lists them under `card.0`, `card.1`, ... In the project file there is still only one layer, so
`vidmake set project.json dot.repeat.count 20` edits all copies at once.

`validate` lints every copy separately. When copies are meant to be invisible or off-canvas at some point
(a ring rotating out of frame, flickering particles), silence that lint on the layer with
`"lint": { "ignore": ["offCanvas", "invisible"] }`, which covers every copy. See [Lint](https://vidmake.app/docs/lint.md).

## Positions from expressions

When a built-in layout isn't enough, keep `layout: "none"` and place copies yourself:

```json
{ "id": "spiral", "type": "ellipse", "width": 14, "repeat": 60,
  "x": "=640 + cos(i * 0.4 + t) * (40 + i * 5)",
  "y": "=360 + sin(i * 0.4 + t) * (40 + i * 5)",
  "fill": "=hsl(i * 6, 55, 70)" }
```

Expressions can also animate a layout: `"x": "=640 + (i - (n - 1) / 2) * 100 * easeOutCubic(clamp(t, 0, 1))"`
spreads a row out from the center over the first second.
