# Groups and layout

> Group layers — a local coordinate space for their children, shared timing, clipping, transforms and effects on the whole group, flexbox-like `layout` stacks with gap, align and justify, and `stagger` for children that enter one after another.

A `group` holds other layers in `children`. Everything about the group (its position, rotation, scale,
opacity, effects, presets and timing) applies to all of its children at once, so a card, a scene or a
chart moves, fades and transitions as one piece.

```json
{ "id": "card", "type": "group", "width": 560, "height": 300, "enter": "pop", "children": [
  { "id": "card-bg", "type": "rect", "cornerRadius": 6, "fill": "#161412" },
  { "id": "card-title", "type": "text", "text": "Group", "anchor": "left", "x": 48, "y": 150 }
] }
```

| field | default | notes |
|---|---|---|
| `children` | `[]` | Layers drawn in the group, first at the bottom. |
| `width`, `height` | the parent's size | The group's box: its children's coordinate space. |
| `clip` | `false` | Hide everything outside the box. |
| `layout` | | Stack the children in a column or row instead of placing them by `x`/`y`. See [Layout](#layout). |
| `stagger` | | Start the children's entrances one after another; exits run in reverse. See [Stagger](#stagger). |
| `series` | `false` | Play the children one after another. See [Series and transitions](https://vidmake.app/docs/series.md). |

Plus every [common field](https://vidmake.app/docs/layers.md). A group has no paint of its own: `fill` and `stroke` do nothing,
so give it a background by making a full-size `rect` its first child (a rect with no size fills its
parent).

## Coordinate space

Children are positioned inside the group's box: `0,0` is the box's top-left corner, and percentages are
of the box, so `"x": "50%"` is the middle of the group and `"width": "100%"` its full width. The group
itself is placed like any layer: its anchor point (the center, by default) goes to its `x`/`y`.

Without `width`/`height` a group is exactly as big as its parent and sits on top of it, so wrapping
layers in a size-less group changes nothing about where they are drawn. That makes groups free to use
for organization: a scene, a set of layers that share an exit, or a block to blur.

**Demo: A clipped group: children use the card's coordinates, and the card moves as one** (rendered video: https://vidmake.app/media/groups-card.mp4)

```json
{
  "fonts": [{ "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" }],
  "compositions": [{
    "id": "card", "description": "A clipped group: children use the card's coordinates, and the card moves as one",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "card", "type": "group", "width": 560, "height": 300, "clip": true, "rotation": "=3 * sin(t * pi / 2)",
        "y": "=360 + 10 * sin(t * pi)", "enter": "pop", "shadow": { "color": "#16141233", "blur": 30, "y": 18 },
        "children": [
          { "id": "bg", "type": "rect", "cornerRadius": 6, "fill": { "type": "linear", "angle": 135, "stops": ["#fbf9f4", "#f4f1ea"] },
            "stroke": "#161412", "strokeWidth": 4 },
          { "id": "dot", "type": "ellipse", "x": 64, "y": 64, "width": 40, "fill": "#c92a26" },
          { "id": "title", "type": "text", "text": "Group", "anchor": "left", "x": 48, "y": 150, "font": "Schibsted Grotesk",
            "fontWeight": 860, "fontSize": 96, "letterSpacing": -4, "fill": "#161412" },
          { "id": "sub", "type": "text", "text": "0,0 is the card's top-left", "anchor": "left", "x": 50, "y": 228,
            "font": "Schibsted Grotesk", "fontSize": 24, "fill": "#4a4640" },
          { "id": "shine", "type": "rect", "width": 90, "height": 600, "rotation": 20, "opacity": 0.06, "fill": "#161412",
            "x": { "from": -120, "to": 700, "start": "1s", "duration": "1.2s", "ease": "easeInOutSine" } }
        ] }
    ]
  }]
}
```

`vidmake inspect` indents children under their group and shows their `X`/`Y` in the group's
coordinates, while the `BBOX` column is always in composition pixels:

```console
$ vidmake inspect groups-card.json -t 2s
Frame 60 (2.00s) of `card` (1280x720), 6 visible layer(s), drawn bottom→top:

ID                       TYPE            X        Y       W       H    ROT  SCALE  OPAC  BBOX [x0,y0,x1,y1]          NOTES
card                     group       640.0    360.0   560.0   300.0    0.0   1.00  1.00  [360,210,920,510]           
  bg                     rect        280.0    150.0   560.0   300.0    0.0   1.00  1.00  [360,210,920,510]           
  dot                    ellipse      64.0     64.0    40.0    40.0    0.0   1.00  1.00  [404,254,444,294]           
  title                  text         48.0    150.0   285.9   115.2    0.0   1.00  1.00  [408,302,694,418]           "Group"
  sub                    text         50.0    228.0   265.8    28.8    0.0   1.00  1.00  [410,424,676,452]           "0,0 is the card's top-left"
  shine                  rect        645.1    150.0    90.0   600.0   20.0   1.00  0.06  [860,63,1150,657]
```

## Timing

A child's `start` is relative to the group's start, and a child without `duration` lasts until the
group ends. Animation times inside a child are relative to the child. So a scene can be built at
`start: 0` and moved to any point in the video by changing one number on the group:

```json
{ "id": "scene-2", "type": "group", "start": "6s", "duration": "4s", "children": [
  { "id": "s2-title", "type": "text", "text": "Chapter two", "enter": "fadeUp" },
  { "id": "s2-sub", "type": "text", "text": "starts 0.4s into the scene", "y": "62%", "start": "0.4s", "enter": "fade" }
] }
```

A group cuts off its children when it ends, including sound from video and audio layers inside it.
For scenes that play back to back, use a [series](https://vidmake.app/docs/series.md) group instead of computing starts.

## Clipping

`"clip": true` hides whatever lies outside the group's box: a shine that sweeps across a card, text that
slides in from behind an edge, a ticker that scrolls through a window. The clip is the group's
rectangle, transformed with the group (it rotates and scales along), and it does not follow a child's
`cornerRadius`. For a rounded or any other shape, use a [mask](https://vidmake.app/docs/masks.md).

## Group transforms and effects

Transforms and effects on a group act on the finished picture of all its children:

- `opacity` fades the group as one image, so overlapping children do not show through each other.
- `scale` and `rotation` turn the whole group around the group's anchor.
- `shadow` is cast by the combined shape of the children.
- `blur`, `brightness`, `saturation`, `blendMode`, [glow and the other effects](https://vidmake.app/docs/effects.md), `mask`
  and `rotateX`/`rotateY` all apply to the composite. Blurring a group is the way to blur a whole scene.
- `enter`/`exit` presets animate the group as a unit, and children can still have their own presets.

## Layout

`layout` stacks the children like CSS flexbox, measuring each child every frame, so a title, a row of
chips and a caption line up with no hand-computed coordinates:

**Demo: A column layout with a row of measured chips inside: no hand-computed coordinates** (rendered video: https://vidmake.app/media/groups-layout.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" },
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk-Italic.ttf" }
  ],
  "compositions": [{
    "id": "layout", "description": "A column layout with a row of measured chips inside: no hand-computed coordinates",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "stack", "type": "group", "layout": { "direction": "column", "gap": 28 },
        "children": [
          { "id": "title", "type": "text", "text": "Stacks, measured", "font": "Schibsted Grotesk", "fontWeight": 860,
            "fontSize": 104, "letterSpacing": -4.5, "fill": "#161412", "enter": "fadeUp" },
          { "id": "chips", "type": "group", "height": 70, "start": "0.4s",
            "layout": { "direction": "row", "gap": 16 },
            "children": [
              { "id": "chip", "type": "text", "repeat": { "count": 4, "delay": "0.12s" }, "enter": "pop",
                "text": "${pick(i, ['column', 'row', 'gap · align · justify', 'nested'])}", "font": "Schibsted Grotesk", "fontSize": 26,
                "fontWeight": 600, "fill": "=pick(i, ['#161412', '#161412', '#f4f1ea', '#161412'])",
                "background": "=pick(i, ['#e9e4d8', '#e9e4d8', '#161412', '#e9e4d8'])", "padding": 16, "cornerRadius": 4 }
            ] },
          { "id": "note", "type": "text", "text": "Each child is measured every frame.", "font": "Schibsted Grotesk", "italic": true,
            "fontSize": 28, "fill": "#4a4640", "start": "0.9s", "enter": "fadeUp" }
        ] }
    ]
  }]
}
```

```json
"layout": "column"
"layout": { "direction": "row", "gap": 16, "align": "center", "justify": "start" }
```

| field | default | notes |
|---|---|---|
| `direction` | `column` | `column` (top to bottom) or `row` (left to right). The string shorthand `"layout": "row"` sets only this. |
| `gap` | `0` | Space between children in px. Animatable; a percentage is of the group's height (column) or width (row). |
| `justify` | `center` | Where the whole stack sits along the direction inside the box: `start`, `center` or `end`. |
| `align` | `center` | Where each child sits across the direction: `start`, `center` or `end`. |

How children behave inside a layout:

- **Size** is each child's measured box: the text as it reads this frame, an image's size, a nested
  group's `width`/`height`. Text that changes length (a counter) pushes its neighbors.
- **`x`/`y` become offsets** from the child's slot, default `0`. `"y": -6` nudges one child up without
  leaving the stack.
- **The anchor still applies** within the slot, so children of different sizes line up by their boxes.
- **Time matters:** a child outside its time window takes no space. Items with increasing `start` times
  therefore make the stack grow and re-center as they appear. To keep positions fixed while items
  appear one by one, start them all together and delay their entrance instead
  (`"enter": { "type": "fadeUp", "delay": "0.3s" }`), or keep them present with `opacity: 0`.
- **Presets and `scale` do not affect the layout.** A child can `pop` in without pushing the others around.

The stack is placed within the group's box, which by default is the whole parent. To position a stack
somewhere specific, give the layout group a size and an `x`/`y` (for example a 600×400 group anchored
`top-left`), and use `justify: "start"` to pin it to the top or left of that box.

### Nesting rows in columns

A `row` group inside a `column` group makes chips, buttons, stat cards and tables. Give the nested group
an explicit cross size, because a group without `width`/`height` is as large as its parent and would
take up the whole column:

```json
{ "id": "stack", "type": "group", "layout": { "direction": "column", "gap": 28 }, "children": [
  { "id": "title", "type": "text", "text": "Stacks, measured", "fontSize": 104 },
  { "id": "chips", "type": "group", "height": 70, "layout": { "direction": "row", "gap": 16 }, "children": [
    { "id": "chip-a", "type": "text", "text": "column", "background": "#161412", "cornerRadius": "50%" },
    { "id": "chip-b", "type": "text", "text": "row", "background": "#4a4640", "cornerRadius": "50%" }
  ] }
] }
```

A row inside a column needs a `height`; a column inside a row needs a `width`. Their other dimension
can stay the default, since alignment uses only the measured children. [`repeat`](https://vidmake.app/docs/repeat.md) works
inside a layout too: each copy is a child of its own, which is how the demo makes four chips from one
layer.

`inspect` shows where the layout put each child, in the group's coordinates:

```console
$ vidmake inspect groups-layout.json -t 2s
Frame 60 (2.00s) of `layout` (1280x720), 8 visible layer(s), drawn bottom→top:

ID                       TYPE            X        Y       W       H    ROT  SCALE  OPAC  BBOX [x0,y0,x1,y1]          NOTES
stack                    group       640.0    360.0  1280.0   720.0    0.0   1.00  1.00  [0,0,1280,720]              
  title                  text        640.0    280.2   887.4   124.8    0.0   1.00  1.00  [196,218,1084,343]          "Stacks, measured"
  chips                  group       640.0    405.6  1280.0    70.0    0.0   1.00  1.00  [0,371,1280,441]            
    chip.0               text        389.3     35.0   124.7    63.2    0.0   1.00  1.00  [327,374,452,437]           "column"
    chip.1               text        508.2     35.0    81.3    63.2    0.0   1.00  1.00  [468,374,549,437]           "row"
    chip.2               text        692.2     35.0   254.7    63.2    0.0   1.00  1.00  [565,374,820,437]           "gap · align · justify"
    chip.3               text        894.3     35.0   117.5    63.2    0.0   1.00  1.00  [836,374,953,437]           "nested"
  note                   text        640.0    485.4   461.8    33.6    0.0   1.00  1.00  [409,469,871,502]           "Each child is measured every frame."
```

## Stagger

`stagger` on a group starts its children's entrances one after another. Each child with an `enter` plays it at
its turn, and exits run in reverse: the last child to arrive is the first to leave.

```json
{ "id": "chips", "type": "group", "layout": "row", "stagger": { "over": "0.4s", "from": "center" },
  "children": [ ... every child has "enter": "pop" ... ] }
```

**Demo: A group stagger: chips pop in from the center out, and leave in reverse, edges first** (rendered video: https://vidmake.app/media/groups-stagger.mp4)

```json
{
  "fonts": [{ "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" }],
  "compositions": [{
    "id": "gstagger", "description": "A group stagger: chips pop in from the center out, and leave in reverse, edges first",
    "width": 1280, "height": 720, "fps": 30, "duration": "3.5s", "background": "#f4f1ea",
    "layers": [
      { "id": "chips", "type": "group", "height": 120, "start": "0.2s", "end": "3.3s",
        "layout": { "direction": "row", "gap": 18 }, "stagger": { "over": "0.4s", "from": "center" },
        "children": [
          { "id": "chip", "type": "text", "repeat": 5, "enter": "pop", "exit": { "to": { "opacity": 0, "y": 30 } },
            "text": "${pick(i, ['Plan', 'Write', 'Validate', 'Render', 'Ship'])}", "font": "Schibsted Grotesk", "fontSize": 34,
            "fontWeight": 700, "letterSpacing": -0.5, "fill": "#f4f1ea", "background": "=pick(i, ['#161412', '#161412', '#c92a26', '#161412', '#161412'])",
            "padding": [18, 30], "cornerRadius": 4 }
        ] }
    ]
  }]
}
```

The five chips are one text layer with `"repeat": 5`; each copy counts as a child. They pop in from the
center out, and at the end of the group the edges leave first.

`"stagger": true` uses the defaults and `"stagger": "80ms"` is a fixed gap. The object takes the same fields as a
[text stagger](https://vidmake.app/docs/text.md#stagger), without `by`:

| field | default | notes |
|---|---|---|
| `over` | `0.5s` | total spread from the first child's start to the last one's |
| `max` | `100ms` | the longest gap between two children |
| `each` | | a fixed gap per child instead of `over` and `max` |
| `from` | `first` | `first`, `last`, `center`, `edges`, `random` or an index |
| `spread` | `linear` | an easing for the start times |

The stagger only moves entrances and exits. Each child keeps its own `start` and `duration`, so `timeline`
still shows them all starting together. Only children that enter take a turn: a background rect without an `enter` appears as written and
doesn't delay the others. A staggered text child counts as entering and shifts its own per-letter stagger along with it. A `series` group can't take a `stagger`: its children already play in turn. To space out whole
children (their keyframes and expressions too), give them `start` times, or use a [repeat](https://vidmake.app/docs/repeat.md#delay-and-timing)
with `delay`.

## Series

`"series": true` turns a group into a sequence: each child starts when the previous one ends, and
`transition` on a child overlaps it with the next. Every child needs a fixed length. That is covered on
its own page: [Series and transitions](https://vidmake.app/docs/series.md).

> [!AGENT]
> Prefer a `layout` group over computing `x`/`y` for rows of text, buttons or cards: lengths change when
> you edit copy, and the layout re-measures for free. After changing text, check the result with
> `vidmake inspect -t <time>` rather than assuming positions.
