# Paths and lines

> Draw any vector shape with SVG path data (`path`) or a list of points (`line`), animate it on with `drawStart`/`drawEnd` (one stroke after another with `drawMode`), and scale it with `width`/`height`.

When the [built-in shapes](https://vidmake.app/docs/shapes.md) are not enough, draw your own: a `path` takes SVG path data,
a `line` takes a list of points. Both can be trimmed with `drawStart`/`drawEnd`, which is how you
"draw on" a signature, an underline, a route on a map or a chart line.

**Demo: drawEnd draws a path on, then drawStart erases it from the tail** (rendered video: https://vidmake.app/media/paths-draw.mp4)

```json
{
  "fonts": [{ "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }],
  "compositions": [{
    "id": "draw", "description": "drawEnd draws a path on, then drawStart erases it from the tail",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "ghost", "type": "path", "y": "46%", "fill": null, "stroke": "#e9e4d8", "strokeWidth": 14,
        "d": "M0 160 C 120 0, 240 320, 360 160 S 600 0, 720 160 S 960 320, 1040 160" },
      { "id": "wave", "type": "path", "y": "46%", "fill": null, "strokeWidth": 14,
        "stroke": { "type": "linear", "angle": 90, "stops": ["#161412", "#161412", "#c92a26"] },
        "d": "M0 160 C 120 0, 240 320, 360 160 S 600 0, 720 160 S 960 320, 1040 160",
        "drawEnd": { "from": 0, "to": 1, "duration": "1.8s", "ease": "easeInOutCubic" },
        "drawStart": { "from": 0, "to": 1, "start": "2.1s", "duration": "1.6s", "ease": "easeInOutCubic" } },
      { "id": "label", "type": "text", "y": "84%", "font": "JetBrains Mono", "fontSize": 24, "fill": "#4a4640",
        "text": "drawStart ${clamp(easeInOutCubic(clamp((t - 2.1) / 1.6, 0, 1)), 0, 1):2}   drawEnd ${easeInOutCubic(clamp(t / 1.8, 0, 1)):2}" }
    ]
  }]
}
```

## path

| field | default | notes |
|---|---|---|
| `d` | required | SVG path data: `M L H V C S Q T A Z`, absolute or relative, with arcs |
| `fill` | white | the inside of the path; set `null` for outline-only paths |
| `stroke`, `strokeWidth` | none, `2` | the outline |
| `drawStart`, `drawEnd` | `0`, `1` | trim the stroke, see below |
| `drawMode` | `each` | how the trim treats several sub-paths: `each` or `total`, see [below](https://vidmake.app/docs/paths.md#several-strokes-drawmode) |

Coordinates in `d` are in the layer's local pixels. Paste the `d` attribute from any SVG (an icon set,
a Figma export) and it works as is. Here the natural bounds are 20×18, so 200×180 draws it ten times
larger:

```json
{ "id": "check", "type": "path", "d": "M4 12 L10 18 L20 6", "fill": null, "stroke": "#4a4640",
  "strokeWidth": 3, "width": 200, "height": 180 }
```

## line

| field | default | notes |
|---|---|---|
| `points` | required | `[[x, y], [x, y], ...]`, at least two |
| `closed` | `false` | `true` connects the last point back to the first |
| `stroke`, `strokeWidth` | white, `8` | a line is a stroke by default |

A `line` is drawn as a stroke. If you set only `fill`, that paint becomes the stroke color. To fill the
inside of a closed line as well, set both `fill` and `stroke` (or use a `path` with `Z`, which is filled
by default).

```json
{ "id": "zigzag", "type": "line", "points": [[0, 60], [60, 0], [120, 60], [180, 0]], "stroke": "#161412",
  "strokeWidth": 6, "lineCap": "round" }
```

Corners are always rounded joins; `lineCap` (`round`, `butt`, `square`) shapes the two ends.

## The layer box

The box of a path or line runs from `0,0` to the **largest x and y** in its data, control points of
curves included. That box is what `x`/`y`/`anchor` place, what `inspect` reports and what
`width`/`height` stretch:

```console
$ vidmake inspect paths-draw.json -t 1s -l wave
Frame 30 (1.00s) of `draw` (1280x720), 1 visible layer(s), drawn bottom→top:

ID                       TYPE            X        Y       W       H    ROT  SCALE  OPAC  BBOX [x0,y0,x1,y1]          NOTES
wave                     path        640.0    331.2  1040.0   320.0    0.0   1.00  1.00  [120,171,1160,491]
```

The wave's curve never reaches `y = 320`, but its control points do, so the box is 320 tall. Two
consequences worth knowing:

- Draw in positive coordinates. Anything left of `x = 0` or above `y = 0` is still drawn but hangs
  outside the box, so centering by anchor will be off.
- When several paths should line up (axes, a chart line and the area under it), give them all
  `"anchor": "top-left"` and the same `x`/`y`: their local coordinates then share one origin, whatever
  their individual bounds are.

**Demo: A line chart: a line drawn on with drawEnd, a filled path under it and a dot riding the tip** (rendered video: https://vidmake.app/media/paths-chart.mp4)

```json
{
  "compositions": [{
    "id": "chart", "description": "A line chart: a line drawn on with drawEnd, a filled path under it and a dot riding the tip",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "axes", "type": "line", "anchor": "top-left", "x": 190, "y": 170, "stroke": "#161412", "strokeWidth": 2,
        "points": [[0, 0], [0, 380], [900, 380]] },
      { "id": "area", "type": "path", "anchor": "top-left", "x": 190, "y": 170,
        "d": "M0 380 L0 300 L150 235 L300 262 L450 150 L600 180 L750 85 L900 30 L900 380 Z",
        "fill": { "type": "linear", "angle": 180, "stops": ["#16141222", "#16141200"] },
        "opacity": { "from": 0, "to": 1, "start": "1.6s", "duration": "0.8s" } },
      { "id": "trend", "type": "line", "anchor": "top-left", "x": 190, "y": 170, "stroke": "#161412", "strokeWidth": 5,
        "points": [[0, 300], [150, 235], [300, 262], [450, 150], [600, 180], [750, 85], [900, 30]],
        "drawEnd": { "from": 0, "to": 1, "start": "0.2s", "duration": "1.6s", "ease": "easeInOutSine" } },
      { "id": "dot", "type": "ellipse", "width": 22, "fill": "#c92a26", "attach": "trend",
        "stroke": "#f4f1ea", "strokeWidth": 4 }
    ]
  }]
}
```

## Drawing on: drawStart and drawEnd

`drawStart` and `drawEnd` are fractions (`0..1`) of the path's total length. Only the stroke between
them is drawn. Both are animatable:

| effect | animate |
|---|---|
| draw on from the start | `drawEnd` 0 → 1 |
| erase from the start | `drawStart` 0 → 1, after the draw-on |
| a moving segment ("comet") | both, with `drawEnd` leading `drawStart` |
| draw from the middle out | `drawStart` 0.5 → 0 and `drawEnd` 0.5 → 1 together |

```json
"drawEnd": { "from": 0, "to": 1, "duration": "1.8s", "ease": "easeInOutCubic" },
"drawStart": { "from": 0, "to": 1, "start": "2.1s", "duration": "1.6s", "ease": "easeInOutCubic" }
```

While a path is trimmed (`drawStart > 0` or `drawEnd < 1`), only its stroke is drawn: the fill and any
`dash` pattern are skipped until the path is complete again. To fade a fill in after the outline has
drawn, put the fill on a second layer (as the chart's `area` above does), or animate the fill color's
alpha.

A dot or arrow can ride the drawn tip with [`attach`](https://vidmake.app/docs/attach.md), as the chart's `dot` does:
`"attach": "trend"` follows `drawEnd` every frame.

## Several strokes: drawMode

A `d` with several `M` commands is one path made of several sub-paths: the letters of a word, a sketchy
double outline, hatching, a diagram of boxes and arrows. `drawMode` (on `path` and `line`) decides how
`drawStart`/`drawEnd` trim them:

| `drawMode` | what you see |
|---|---|
| `each` (default) | every sub-path starts drawing at the same moment and at the same speed, measured in fractions of the **whole** path's length. A sub-path shorter than the drawn length shows in full, so short strokes appear almost at once and long ones lag behind, which looks dashed |
| `total` | one continuous stroke through the sub-paths in the order they appear in `d`: the next one starts where the previous one finished, like a pen that lifts between strokes |

Here the same 15-stroke word is drawn with each mode, with the same `drawEnd` tween:

**Demo: One hand-lettered path of 15 strokes: drawMode each (top) starts them all at once, total (bottom) writes them in order** (rendered video: https://vidmake.app/media/paths-drawmode.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk-Italic.ttf" },
    { "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }
  ],
  "compositions": [{
    "id": "drawmode", "description": "One hand-lettered path of 15 strokes: drawMode each (top) starts them all at once, total (bottom) writes them in order",
    "width": 1280, "height": 720, "fps": 30, "duration": "4.5s", "background": "#f4f1ea",
    "layers": [
      { "id": "label", "type": "text", "x": 317, "y": "=pick(i, [107, 407])", "anchor": "left", "repeat": 2,
        "font": "JetBrains Mono", "fontSize": 22, "fill": "=pick(i, ['#8a857a', '#161412'])", "text": "\"drawMode\": \"${pick(i, ['each', 'total'])}\"" },
      { "id": "note", "type": "text", "x": 980, "y": "=pick(i, [107, 407])", "anchor": "right", "repeat": 2,
        "font": "Schibsted Grotesk", "italic": true, "fontSize": 24, "fill": "#4a4640", "text": "${pick(i, ['15 strokes start together', 'one pen, stroke after stroke'])}" },
      { "id": "each", "type": "path", "anchor": "top-left", "x": 300, "y": 140, "width": 680, "height": 185,
        "fill": null, "stroke": "#8a857a", "strokeWidth": 9, "lineCap": "round", "d": "M20 80 L60 180 L100 80 M135 80 L135 180 M135 42 L135 44 M255 100 C240 80 220 78 205 80 C175 82 158 105 158 130 C158 158 178 180 205 180 C228 180 248 165 255 145 M255 20 L255 180 M295 80 L295 180 M295 115 C300 90 315 80 330 80 C350 80 360 95 360 115 L360 180 M360 115 C365 90 380 80 395 80 C415 80 425 95 425 115 L425 180 M545 105 C535 87 520 80 505 80 C478 80 462 105 462 130 C462 158 480 180 505 180 C525 180 540 168 545 150 M545 80 L545 180 M590 20 L590 180 M650 80 L590 140 M610 122 L655 180 M690 130 L780 130 C780 100 760 80 735 80 C705 80 688 105 688 130 C688 160 708 180 735 180 C755 180 770 172 780 160 M20 215 C250 200 550 205 790 210",
        "drawEnd": { "from": 0, "to": 1, "duration": "3s", "ease": "easeInOutSine" }, "exit": { "type": "fade", "duration": "0.4s" } },
      { "id": "total", "type": "path", "drawMode": "total", "anchor": "top-left", "x": 300, "y": 440, "width": 680, "height": 185,
        "fill": null, "stroke": "#161412", "strokeWidth": 9, "lineCap": "round", "d": "M20 80 L60 180 L100 80 M135 80 L135 180 M135 42 L135 44 M255 100 C240 80 220 78 205 80 C175 82 158 105 158 130 C158 158 178 180 205 180 C228 180 248 165 255 145 M255 20 L255 180 M295 80 L295 180 M295 115 C300 90 315 80 330 80 C350 80 360 95 360 115 L360 180 M360 115 C365 90 380 80 395 80 C415 80 425 95 425 115 L425 180 M545 105 C535 87 520 80 505 80 C478 80 462 105 462 130 C462 158 480 180 505 180 C525 180 540 168 545 150 M545 80 L545 180 M590 20 L590 180 M650 80 L590 140 M610 122 L655 180 M690 130 L780 130 C780 100 760 80 735 80 C705 80 688 105 688 130 C688 160 708 180 735 180 C755 180 770 172 780 160 M20 215 C250 200 550 205 790 210",
        "drawEnd": { "from": 0, "to": 1, "duration": "3s", "ease": "easeInOutSine" }, "exit": { "type": "fade", "duration": "0.4s" } },
      { "id": "pen", "type": "ellipse", "attach": "total", "width": 18, "fill": "#c92a26",
        "opacity": { "from": 1, "to": 0, "start": "3s", "duration": "0.3s" } }
    ]
  }]
}
```

With `each`, `drawEnd` 0.1 already means "the first 10% of the whole length *on every stroke*": the `i`
and the `k` arms are complete while the long underline is only a third of the way across. With `total`, 0.1 means the pen has
covered the first 10% of the total length, stroke after stroke. Use `total` for handwriting, lettering,
sketches and diagrams; keep `each` for paths whose parts should grow together (several lines of a chart
racing each other, rays bursting from a center).

```json
{ "id": "word", "type": "path", "drawMode": "total", "d": "M20 80 L60 180 L100 80 M135 80 L135 180 ...",
  "fill": null, "stroke": "#161412", "strokeWidth": 9,
  "drawEnd": { "from": 0, "to": 1, "duration": "3s", "ease": "easeInOutSine" } }
```

Things to know:

- **Order matters in `total`.** Strokes draw in the order they are written in `d`, so write them in the order a
  hand would: left to right, the stem of a letter before its dot, the underline last. Pen-up jumps between
  sub-paths take no time; the speed along the drawn strokes stays constant for a linear ease.
- **`attach` follows the `total` position in both modes.** The pen dot in the demo is
  `"attach": "total"`; on the `each` path it would move through the strokes one after another even though they all
  draw at once. With `total` it sits on the tip that is actually drawing.
- **`drawStart` works the same way.** In `total` mode, animating `drawStart` 0 → 1 after the draw-on erases the
  strokes in the same order.
- `drawMode` takes a plain string (`"each"` or `"total"`), not an expression. Any other value is an error:
  ``layer `word` (...): `drawMode` must be each | total, got `pen` ``.
- A path with a single sub-path looks the same in both modes.

## Scaling with width and height

Setting `width` and/or `height` scales the path's coordinates to that size, and the stroke scales with
them:

**Demo: One 21×21 icon path at three sizes: width and height scale the path and its stroke** (rendered video: https://vidmake.app/media/paths-icon.mp4)

```json
{
  "fonts": [{ "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }],
  "compositions": [{
    "id": "icon", "description": "One 21×21 icon path at three sizes: width and height scale the path and its stroke",
    "width": 1280, "height": 720, "fps": 30, "duration": "3s", "background": "#f4f1ea",
    "layers": [
      { "id": "heart", "type": "path", "x": "=pick(i, [280, 560, 900])", "y": "46%", "width": "=pick(i, [63, 126, 252])", "height": "=pick(i, [63, 126, 252])",
        "repeat": { "count": 3, "delay": "0.15s" }, "fill": null, "stroke": "#161412", "strokeWidth": 1.5, "lineCap": "round",
        "d": "M12 21 C 12 21 3 15 3 8.5 C 3 5.5 5.5 3.5 8 3.5 C 9.8 3.5 11.2 4.5 12 6 C 12.8 4.5 14.2 3.5 16 3.5 C 18.5 3.5 21 5.5 21 8.5 C 21 15 12 21 12 21 Z",
        "drawEnd": { "from": 0, "to": 1, "duration": "1.2s", "ease": "easeInOutCubic" } },
      { "id": "size", "type": "text", "x": "=pick(i, [280, 560, 900])", "y": "78%", "repeat": 3, "font": "JetBrains Mono",
        "fontSize": 20, "letterSpacing": 2.4, "fill": "#8a857a", "text": "${pick(i, [63, 126, 252])} × ${pick(i, [63, 126, 252])}" }
    ]
  }]
}
```

Things to keep in mind:

- Each axis scales independently. Setting only `width` stretches the path horizontally and leaves the
  height at the natural bounds, so set **both** to keep proportions (`width / naturalWidth` must equal
  `height / naturalHeight`).
- `strokeWidth` is in the path's own units: an icon drawn with `"strokeWidth": 1.5` in a 21-unit box and
  shown at 252 px gets a stroke about 18 px wide. Scale it down if you want a thin line on a big shape.
- Non-uniform scaling also stretches the stroke unevenly. For data drawn at a specific size (charts,
  graphs), compute the points in pixels instead of scaling.
- `scale`/`scaleX`/`scaleY` work too and look the same; they scale around the anchor and leave the
  layer's `width`/`height` (what layouts measure) unchanged.

## Tips

- Long paths from design tools are fine; the JSON just gets wide. Keep one path per layer so each can
  have its own `id`, timing and stroke.
- `"lineCap": "round"` (the default) makes a drawing-on stroke look like ink; `"butt"` gives crisp ends.
- Outline text and logos are better as an [SVG image](https://vidmake.app/docs/images.md) if they never animate stroke by
  stroke.
- For progress circles, an [`arc`](https://vidmake.app/docs/shapes.md#arc) is simpler than a circular path.
