# SVG Drawings

Draw vector art as SVG, then rig and animate it by its element ids.

An `svg` node draws SVG markup as flat shapes, so an agent can draw a character, an icon or a diagram itself instead of generating an image. Every element with an `id` becomes a node you can animate.

```json
{
  "nodes": {
    "pip": {
      "svg": {
        "src": "<svg viewBox='0 0 200 200'><g id='arm' data-pivot='100 80'><rect x='96' y='80' width='8' height='60' fill='#3355ee'/></g><circle id='head' cx='100' cy='60' r='30' fill='#f2c39b' stroke='#201008' stroke-width='4'/></svg>",
        "width": 4,
        "bones": { "arm": { "parent": "head" } }
      }
    }
  },
  "timeline": {
    "duration": 2,
    "tracks": {
      "wave": { "target": "pip#arm", "property": "rotation", "keyframes": [{ "t": 0, "value": [0, 0, 0] }, { "t": 1, "value": [0, 0, 30] }, { "t": 2, "value": [0, 0, 0] }] }
    }
  }
}
```

## What it Draws

- Shapes: `path` (every command, arcs included), `rect` (rounded too), `circle`, `ellipse`, `line`, `polyline`, `polygon`, `g`, nested `svg`, `use`, and `a`/`switch` as groups.
- Transforms: `matrix`, `translate`, `scale`, `rotate`, `skewX`, `skewY`, nested.
- Paint: fills (`nonzero` and `evenodd`, holes included) and strokes with width, round or square caps and round joins. Colours by hex, `rgb()`/`rgba()`, names, `currentColor`.
- Linear and radial gradients (`href` chains, `gradientTransform`, `userSpaceOnUse`), drawn over each shape's box.
- `opacity`, `fill-opacity`, `stroke-opacity`, `display`, `visibility`.
- Styles: presentation attributes, `style="..."`, and a `<style>` block with tag, `.class` and `#id` selectors.
- A `clip-path` that is one rectangle clips the part. A `<pattern>` fill tiles the pattern over the shape: its strokes are cut exactly at the outline and its small filled shapes (dots, scales) are kept when their centre is inside, so hatching, studs and brocade come out as one mesh per colour; a full-tile `rect` in the pattern becomes the background fill. `mix-blend-mode: screen` (or `plus-lighter`, `lighten`, `color-dodge`) makes a shape add light, for glows and flames (see `blend` on [materials](/docs/features/materials)). Text, images, masks, filters and other clip paths are skipped with an `svg_unsupported` warning (draw text with a [text](/docs/features/text) node).

`src` is the markup or, for the CLI, a file path next to the scene (the CLI reads it in). `width` sets the drawing's width in world units; `anchor` (default `[0.5, 0.5]`) is the point of the viewBox at the node's origin. Parts are drawn in document order and use one generated unlit material, so colours match the SVG; set `material` to shade them with your own.

## Parts and Rigs

An element with `id="arm"` in node `pip` becomes node `pip#arm`, with its SVG children inside it. Target it from tracks, `with`, or other nodes' `parent`. Elements without an id still draw, as `pip#3` and so on. A part with both a fill and a stroke has `.fill` and `.stroke` children (`pip#head.fill`).

`bones` turns parts into a rig:

- `pivot`: `[x, y]` in viewBox units to turn and scale about. Default: a `data-pivot="x y"` attribute on the element, or the centre of its box.
- `parent`: the id of the part it hangs from (default: its SVG parent), e.g. a hand hanging from the forearm.

Tracks on a part are relative to where the SVG drew it: rotation `[0, 0, 20]` turns it 20° about its pivot, and position moves it from its drawn place. `with` overrides parts by id, like a [prefab](/docs/features/prefabs): `"with": {"eyes": {"opacity": 0}}`.

## Bending Limbs

Give a bone `joints` and its part bends instead of turning stiffly. The part's shapes are skinned to its pivot and the joints, for example an elbow and a wrist. A `bend` track on the part then curls it: the angle in degrees is shared equally between the joints, and positive bends counter-clockwise.

```json
"svg": {
  "src": "...",
  "bones": {
    "arm-right": { "pivot": [238, 233], "base": [222, 240], "joints": [[260, 214], [276, 186]] },
    "brush": { "parent": "arm-right" }
  }
}
```

```json
"curl": { "target": "painter#arm-right", "property": "bend", "keyframes": [{ "t": 0, "value": 0 }, { "t": 0.75, "value": 18 }] }
```

- `rotation` on the part still swings the whole limb from its pivot. Use both for a wave.
- `base` is a point inside the part it hangs from, such as the torso just behind the shoulder. It stays put, so the top of the sleeve stretches there instead of lifting off as the arm moves. Draw the arm before the body, starting inside it, so the body covers the join.
- A part hanging from a bendable limb (`"parent": "arm-right"`) rides on its last joint, like a brush in the hand. Parts that are bones of their own stay rigid.
- Joints become nodes `<part>.joint1`, `<part>.joint2` and so on. `bend` turns them into rotation tracks when the scene expands, so don't animate the joints directly as well.
- Strokes bend smoothly: their points are spaced out along the bones as needed. Fills bend at their outline points.

## Morphs

`morph` maps a part to the shapes it morphs into, by id. Keep the targets in `<defs>` so they don't draw. The part's `morph` track blends through them: 0 is the part as drawn, 1 the first target, 2 the second.

```json
"svg": {
  "src": "<svg viewBox='0 0 100 100'><defs><path id='grin' d='M30 60 Q50 85 70 60'/><path id='oh' d='M44 64 Q50 54 56 64 Q50 72 44 64'/></defs><path id='mouth' d='M34 62 Q50 72 66 62' fill='none' stroke='#201008' stroke-width='4'/></svg>",
  "morph": { "mouth": ["grin", "oh"] }
}
```

Any two outlines blend: both are resampled to the same number of points and lined up to keep the motion short. A group morphs shape by shape into a group with the same number of shapes. Each shape morphs its first outline, so give holes and extra subpaths their own paths. A `morph` track on a part with both fill and stroke drives both.

## In TypeScript

`svg()` in [`@motion-sdk/scene`](/docs/concepts/typescript) builds the node and types the part ids. Inline markup (any literal string) autocompletes its ids. For a file, `npm run gen:svg-ids -- art/painter.svg` writes `art/painter.svg.ts` with the ids as a type:

```ts
import { defineScene, file, key, parts, svg, track } from "@motion-sdk/scene";
import type { PainterIds } from "./art/painter.svg.ts";

const drawing = file<PainterIds>("./art/painter.svg", import.meta.url);
const part = parts("painter", drawing); // part("arm-right") is "painter#arm-right"

export default defineScene({
  nodes: { painter: svg(drawing, { width: 8, bones: { brush: { parent: "arm-right" } } }) },
  timeline: {
    tracks: { wave: track(part("arm-right"), "rotation", [key(0, [0, 0, 0]), key(1, [0, 0, 22])]) },
  },
});
```

`svg()` throws on an unknown id in `bones`, `morph` or `with`, a pivot, base or joint that isn't `[x, y]`, or bones that hang from each other; `validate` reports the same as [`svg_unknown_id`](/docs/reference/errors#svg_unknown_id) and [`svg_bad_pivot`](/docs/reference/errors#svg_bad_pivot). `bones(drawing, {...})` and `parts(id, drawing)` type a rig or targets on their own.

See [`examples/ts/svg-painter.ts`](https://github.com/brainfish-ai/motion/blob/main/examples/ts/svg-painter.ts) and [`examples/svg/painter.svg`](https://github.com/brainfish-ai/motion/blob/main/examples/svg/painter.svg): a waving painter, drawn and rigged entirely in SVG.

## Where it Runs

SVG drawings run everywhere: in the CLI (`render`, `validate`, `expand`) and in the browser. `createPlayer` sees the `svg` field and loads the authoring bundle, which includes the SVG reader, as does the WebGL2 fallback. The lean WebGPU player doesn't. `motion-sdk expand scene.json -o scene.player.json` turns the drawing into plain 2D shapes with their ids (`painter#arm-right`), if you'd rather not ship the SVG source. Those shapes still need the authoring bundle.

## API Reference

### Svg

An SVG drawing expanded into 2D shape nodes (see the module docs).

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `anchor` | `[x, y]` | — | The point of the viewBox at the node's origin, as fractions from its top-left corner (default: the centre). |
| `bones` | `map of SvgBone` | — | Rig: SVG ids of parts that move as bones, with the pivot they turn and scale about and, optionally, the part they hang from. |
| `material` | `string` | — | Material for every part (node colours carry the SVG fills). Default: a generated unlit material, so colours match the SVG exactly. |
| `morph` | `map of string[]` | — | Shape morphs: a part's SVG id -> the ids of the shapes it morphs into (keep them in `<defs>` so they don't draw). Its `morph` track blends through them: 1 = the first target, 2 = the second. A group morphs shape by shape into a group with as many shapes. Outlines are resampled to match, so any two paths blend. |
| `src` (required) | `string` | — | SVG markup (`<svg viewBox="0 0 200 300">...</svg>`), or a file path relative to the scene (the CLI reads it in). |
| `width` | `number` | — | Width in world units of the drawing's viewBox (its height follows the aspect ratio). Default: 0.01 units per SVG unit. |
### SvgBone

How one SVG part moves (see [`Svg::bones`]).

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `base` | `[x, y]` | — | A point inside the part it hangs from (e.g. in the torso, just before the shoulder) that stays put with it, so the limb's base stretches there instead of lifting off when it turns or bends. |
| `joints` | `[x, y][]` | — | Bendable limb: joints after the pivot along the part, e.g. elbow and wrist, in viewBox units. The part's shapes are skinned to the pivot and these joints, so a `bend` track on the part curls it smoothly instead of turning it stiffly. Joints become nodes `<part>.joint1`, `<part>.joint2`, ... |
| `parent` | `string` | — | SVG id of the part this one hangs from (default: its SVG parent), e.g. a hand hanging from the forearm. |
| `pivot` | `[x, y]` | — | Pivot `[x, y]` in the SVG's own units (viewBox coordinates). Default: a `data-pivot="x y"` attribute on the element (in its own units), or the centre of its bounding box. |
