# Core concepts

> How a vidmake project is put together: compositions, the layer tree, coordinates and anchors, layer-local time, draw order, and why every frame is a pure function of time.

A vidmake project is a description of a video, not a program that draws one. This page explains the
handful of ideas everything else builds on. Each section links to the page with the full details.

## Projects, compositions and layers

```output
project.json
├── fonts                      font files used by text layers
└── compositions[]
    ├── main (1920×1080, 30 fps, 10s)
    │   ├── background         a rect
    │   ├── intro              a group
    │   │   ├── title          a text
    │   │   └── logo           an image
    │   ├── music              an audio
    │   └── lowerThird         a use layer → renders the "lowerThird" composition
    └── lowerThird (1920×1080, 3s)
        └── ...
```

- A **project** is one JSON file. Besides compositions, it can list `fonts` to load. Asset paths (`src`,
  font `path`) are relative to the project file.
- A **composition** is a canvas with a size, a frame rate, a duration and a list of layers. The first
  composition is rendered by default; `--comp <id>` picks another. Compositions can also be used as
  reusable components inside other compositions. See [Compositions](https://vidmake.app/docs/compositions.md).
- A **layer** is one visual or audio element: a shape, text, image, video, audio clip, group and so on. Each
  has a `type`, an `id`, a time window and properties that can be animated. See [Layers](https://vidmake.app/docs/layers.md).

The **layer tree** comes from three kinds of nesting:

| nesting | how | children are placed and timed relative to |
|---|---|---|
| [group](https://vidmake.app/docs/groups.md) | `"type": "group"` with `children` | the group's box and the group's start |
| [series](https://vidmake.app/docs/series.md) | a group with `"series": true` | played back to back |
| [component](https://vidmake.app/docs/components.md) | `"type": "use"` with `"ref": "<compositionId>"` | the referenced composition, drawn as a group |

Transforms, opacity, effects and presets on a group apply to everything inside it. Moving a group moves
its children; fading a group fades them all.

**Demo: A group moves, tilts and scales; its children follow in the group's own coordinates** (rendered video: https://vidmake.app/media/start-tree.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" },
    { "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }
  ],
  "compositions": [{
    "id": "tree", "description": "A group moves, tilts and scales; its children follow in the group's own coordinates",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "back", "type": "rect", "x": "50%", "y": "52%", "width": 460, "height": 280, "cornerRadius": 3,
        "fill": "#e9e4d8", "stroke": "#d8d1c1", "strokeWidth": 2, "rotation": -6, "comment": "drawn first, so it sits at the bottom" },
      { "id": "card", "type": "group", "x": "=pw/2 + 150 * sin(t * pi / 2)", "y": "52%", "width": 460, "height": 280,
        "rotation": "=6 * sin(t * pi / 2)", "scale": { "from": 0.85, "to": 1, "duration": "1s", "ease": "easeOutBack" },
        "children": [
          { "id": "panel", "type": "rect", "cornerRadius": 3, "fill": "#fbf9f4", "stroke": "#161412", "strokeWidth": 2 },
          { "id": "badge", "type": "ellipse", "x": "100%", "y": 0, "width": 64, "fill": "#c92a26" },
          { "id": "name", "type": "text", "text": "card", "font": "Schibsted Grotesk", "fontWeight": 860, "fontSize": 84,
            "letterSpacing": -3.5, "fill": "#161412", "y": "44%" },
          { "id": "coords", "type": "text", "text": "badge: x \"100%\", y 0", "font": "JetBrains Mono", "fontSize": 20,
            "fill": "#4a4640", "y": "72%" }
        ] },
      { "id": "caption", "type": "text", "text": "children are placed in the group's box, not the canvas", "font": "Schibsted Grotesk",
        "fontSize": 22, "fill": "#8a857a", "y": "90%" }
    ]
  }]
}
```

## Coordinates

The canvas origin is the top-left corner. `x` grows to the right and `y` grows down, in pixels.
Rotation is in degrees, clockwise.

- **`x` and `y` place the layer's anchor point** in its parent's coordinates. They default to `"50%"`, so a
  layer without a position is centered in its parent.
- **The anchor** is a point in the layer's own box, given as fractions `[ax, ay]` (default `[0.5, 0.5]`,
  the center) or as a name: `center`, `top-left`, `top`, `top-right`, `left`, `right`, `bottom-left`,
  `bottom`, `bottom-right`. `"anchor": "top-left"` with `"x": 0, "y": 0` puts the layer in the corner.
- **Rotation and scale happen around the anchor.** A layer anchored at its top-left corner swings around
  that corner.
- **Inside a group, coordinates are the group's.** `(0, 0)` is the top-left of the group's box, and the
  box defaults to the parent's size.

**Demo: Same x and y, three anchors: each square sits on and rotates around its own anchor** (rendered video: https://vidmake.app/media/start-anchors.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" },
    { "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }
  ],
  "compositions": [{
    "id": "anchors", "description": "Same x and y, three anchors: each square sits on and rotates around its own anchor",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "topLeft", "type": "rect", "anchor": "top-left", "x": "25%", "y": "47%", "width": 190, "height": 110,
        "cornerRadius": 3, "fill": "#161412", "rotation": "=90 * easeInOutCubic(pingpong(max(t - 0.5, 0) / 1.5, 1))" },
      { "id": "center", "type": "rect", "anchor": "center", "x": "50%", "y": "47%", "width": 190, "height": 110,
        "cornerRadius": 3, "fill": "#161412", "rotation": "=90 * easeInOutCubic(pingpong(max(t - 0.5, 0) / 1.5, 1))" },
      { "id": "bottomRight", "type": "rect", "anchor": "bottom-right", "x": "75%", "y": "47%", "width": 190, "height": 110,
        "cornerRadius": 3, "fill": "#161412", "rotation": "=90 * easeInOutCubic(pingpong(max(t - 0.5, 0) / 1.5, 1))" },
      { "id": "dot", "type": "ellipse", "repeat": 3, "x": "=pw * (0.25 + 0.25 * i)", "y": "47%", "width": 20,
        "fill": "#c92a26", "stroke": "#f4f1ea", "strokeWidth": 4 },
      { "id": "label", "type": "text", "repeat": 3, "x": "=pw * (0.25 + 0.25 * i)", "y": "84%", "font": "JetBrains Mono",
        "fontSize": 20, "fill": "#4a4640", "text": "\"anchor\": \"${pick(i, ['top-left', 'center', 'bottom-right'])}\"" },
      { "id": "title", "type": "text", "text": "x and y place the anchor point", "font": "Schibsted Grotesk", "fontWeight": 860,
        "fontSize": 52, "letterSpacing": -2.2, "fill": "#161412", "y": "13%" }
    ]
  }]
}
```

Try the anchor and position combinations here:

*(An interactive anchor widget is shown here on the HTML page.)*

### Percentages

Any numeric field accepts a percentage string, measured against the **parent** (the composition, or the
enclosing group's box):

| field | `"50%"` means |
|---|---|
| `x`, `width` | half the parent's width |
| `y`, `height` | half the parent's height |
| `fontSize` | half the parent's height |
| text `maxWidth` | half the parent's width |

In [expressions](https://vidmake.app/docs/expressions.md), the same sizes are available as `w`/`h` (the composition) and
`pw`/`ph` (the parent): `"x": "=pw * 0.25"`.

### Layer sizes

When you don't set `width` and `height`, a layer takes its natural size:

| layer | default size |
|---|---|
| `rect` | the parent's full size, so `{ "type": "rect", "fill": "#111" }` is a background |
| `ellipse`, `polygon`, `star`, `arc` | 100×100; one of `width`/`height` alone makes it square |
| `text`, `captions` | measured from the content and font |
| `image`, `video` | the media's native size; one of `width`/`height` keeps the aspect ratio |
| `group` | the parent's size |
| `use` | the referenced composition's size |
| `path`, `line` | the extent of the points, from `(0, 0)` |

### Reading positions back

`inspect` shows both coordinate systems: `X`/`Y` are the anchor position in the parent's coordinates
(the numbers you wrote), and `BBOX` is the layer's box on the canvas after every transform of every
parent. In the demo above at 1 second, the card group is shifted right and tilted, the `badge` child is
still at `x: 460, y: 0` in the group, and its canvas box follows the group:

```console
$ vidmake inspect start-tree.json -t 1s
Frame 30 (1.00s) of `tree` (1280x720), 7 visible layer(s), drawn bottom→top:

ID                       TYPE            X        Y       W       H    ROT  SCALE  OPAC  BBOX [x0,y0,x1,y1]          NOTES
back                     rect        640.0    374.4   460.0   280.0   -6.0   1.00  1.00  [397,211,883,538]           
card                     group       790.0    374.4   460.0   280.0    6.0   1.00  1.00  [547,211,1033,538]          
  panel                  rect        230.0    140.0   460.0   280.0    0.0   1.00  1.00  [547,211,1033,538]          
  badge                  ellipse     460.0      0.0    64.0    64.0    0.0   1.00  1.00  [998,224,1069,294]          
  name                   text        230.0    123.2   188.8   100.8    0.0   1.00  1.00  [693,298,891,418]           "card"
  coords                 text        230.0    201.6   240.0    24.0    0.0   1.00  1.00  [663,411,904,460]           "badge: x "100%", y 0"
caption                  text        640.0    648.0   544.4    26.4    0.0   1.00  1.00  [368,635,912,661]           "children are placed in the group's box, "
```

## Time

Time is measured in **frames**. A composition at 30 fps has 30 frames per second, numbered from 0. Every
time field accepts a number of frames or a string: `"1.5s"`, `"500ms"`, `"45f"`, `"1:02.5"`.
[Time](https://vidmake.app/docs/time.md) lists every format.

Each layer has a **time window**:

- `start` (default 0) is when it appears, relative to its parent: the composition, or the group it is in.
- `duration` (or `end`) is how long it stays. The default is the rest of the parent, except for media,
  which defaults to the media's length.
- Outside its window, a layer isn't drawn at all, and neither are its children.

**Animation time is local.** Keyframe times, tween `start`/`duration`, presets and the expression
variable `t` all count from the layer's own start. A fade-in written as
`"opacity": [[0, 0], ["0.5s", 1]]` takes the first half second of the layer, whenever the layer starts.
Children of a group count from the group's start. This is what makes layers and groups movable: change a
`start` and everything inside moves with it.

| expression variable | meaning |
|---|---|
| `t`, `f` | seconds and frames since the layer started |
| `p` | the layer's progress, 0 at its start to 1 at its end |
| `dur`, `durf` | the layer's duration in seconds and frames |
| `gt`, `gf` | seconds and frames since the composition started |

## Every frame is a pure function of time

To draw frame 42, vidmake evaluates every property of every layer at frame 42 and rasterizes the
result. Nothing depends on the frames before it: there is no simulation state, no "previous frame".
Randomness is seeded: `noise()`, `wiggle()`, `rand()`, [particles](https://vidmake.app/docs/particles.md) and film grain
produce the same values for the same frame every time.

That has practical consequences:

- **Any frame can be checked on its own.** `still -t 7.5s`, `inspect -t 7.5s` and a storyboard show
  exactly what the render will contain at that moment.
- **Renders are reproducible.** The same project, props and vidmake version give the same frames.
- **Frames render in parallel** across all CPU cores, and `--frames 1s-3s` renders a slice without
  rendering what comes before it.

The only inputs besides the JSON are the files it references (media, fonts) and the
[props](https://vidmake.app/docs/props.md) you pass.

## Draw order

Layers are drawn in array order: **the first layer is at the bottom, the last on top**. There is no
z-index; to bring a layer forward, move it later in the list (`vidmake add --index` inserts at a
position). A group is drawn at its own position in its parent's list, with its children in their order
inside it. In a [series](https://vidmake.app/docs/series.md), later scenes are drawn above earlier ones, so an incoming scene
covers the outgoing one during a transition. A layer used as a [mask](https://vidmake.app/docs/masks.md) isn't drawn at all.

`inspect` lists layers in draw order, bottom to top.

## From JSON to pixels

When you run any command, vidmake:

1. **Loads** the file, resolves the [theme](https://vidmake.app/docs/theme.md) (with the `--theme` mode, if any), and substitutes
   [props](https://vidmake.app/docs/props.md) (`{{name}}`) and theme tokens (`$color.accent`) with their values.
2. **Parses** it strictly: unknown fields and wrong types are errors, with suggestions.
3. **Compiles** the chosen composition into a render tree: resolves times, lays out series, loads fonts,
   images and media metadata, parses expressions, and computes the duration.
4. **Evaluates** each requested frame: every animated value at that time.
5. **Rasterizes** the frame on the CPU, including text, effects, masks and motion blur.
6. **Encodes** with ffmpeg when rendering a video, GIF or audio file.

`validate`, `timeline`, `inspect` and `trace` stop after steps 3 or 4, which is why they are fast. `still`
and `storyboard` go through step 5 for a few frames, and only `render` does all six for every frame.

## Next steps

- [Compositions](https://vidmake.app/docs/compositions.md) Every composition field, fonts and multiple compositions.
- [Time](https://vidmake.app/docs/time.md) All time formats and how local time works.
- [Layers](https://vidmake.app/docs/layers.md) The fields every layer shares.
- [Animation](https://vidmake.app/docs/animation.md) Keyframes, tweens, springs and expressions.
