# Charts & media

> Video, image, SVG, Lottie and caption layers, the Media chain, and animated charts built from native nodes.

## Media layers

```ts
Layer.video("assets/footage/clip.mp4", { width: 1920, height: 1080, fit: "cover", trimStartSec: 2, muted: true });
Layer.image("assets/images/still.webp", { width: 800, height: 600, fit: "contain" }); // .png .jpg .webp
Layer.svg("assets/images/logo.svg", { width: 400, height: 120, fit: "contain" });
Layer.lottie("assets/lottie/badge.json", { width: 400, height: 400, fit: "contain", loop: true }); // .json .lottie
Layer.caption("assets/captions/subs.srt", { /* bottom-anchored, auto-wrapping cues */ });
```

| Builder | Notes |
| --- | --- |
| `Layer.video` | decoded with mediabunny/WebCodecs; `fit`, `trimStartSec`, `timeRemap`, `muted`, `volume` |
| `Layer.image` | raster only. Passing `.json`/`.lottie`/video/audio is an error (`unsupported_image_format`) |
| `Layer.svg` | vector image |
| `Layer.lottie` | Lottie JSON and dotLottie via ThorVG; `fit: "contain" \| "cover" \| "fill"`, `loop`, `speed`, `autoplay` |
| `Layer.caption` | SRT-driven subtitles |

All of them position, animate, matte, take effects and tilt into 3D like any layer. Asset paths resolve from the working directory; build them from `import.meta.url` if the [preview](/docs/guides/preview/) must load them too.

## Time remapping

:::note[Next release]
`timeRemap` ships in the release after framefields 2.6.2.
:::

`timeRemap` decides which moment of the source plays at each frame of a video or audio layer: slow motion, speed ramps, reverse, freeze frames. The layer needs an explicit `durationFrames`. Picture, embedded audio and `comp.vision(layer)` follow the same mapping. Effects stay on the composition clock.

```ts
// Constant rate: half speed from 3 s in. A negative rate plays backwards.
Layer.video("assets/footage/clip.mp4", {
  durationFrames: 96,
  timeRemap: { kind: "rate", sourceStartSec: 3, rate: 0.5 },
});

// Keyframes: layer frame → source second. Equal source times hold the frame.
Layer.video("assets/footage/clip.mp4", {
  durationFrames: 90,
  timeRemap: { kind: "keyframes", keyframes: [
    { frame: 0, sourceSec: 1, interpolation: "smooth" },
    { frame: 30, sourceSec: 2 },
    { frame: 45, sourceSec: 2 }, // hold
    { frame: 90, sourceSec: 1 }, // reverse back to 1 s
  ] },
});
```

Keyframes start at frame 0, end at `durationFrames` and increase in frame. `interpolation` applies to the segment after a key: `"linear"` (default), `"ease-in"`, `"ease-out"` or `"smooth"`, all without overshoot. Audio is resampled, so its pitch follows the speed, and a hold is silent. Do not combine `timeRemap` with `trimStartSec`. A map that reaches outside the source fails before rendering.

### Driven by a signal

A [signal](/docs/guides/audio-and-signals/#signals) can drive the source clock instead of a fixed map.

```ts
import { Layer, Signal } from "framefields";

const speed = Signal.state(1);
comp.onRequestFrame((ctx) => speed.set(ctx.frame < 48 ? 1 : 0.2));

comp.add(Layer.video("assets/footage/dancer.mp4", {
  durationFrames: 240,
  fit: "cover",
  timeRemap: { kind: "signal", mode: "speed", signal: speed },
}));
```

| Field | Meaning |
| --- | --- |
| `mode: "speed"` | the signal is the playback rate (1 = real time, 0 = freeze, negative = reverse), integrated from `sourceStartSec` (default 0) |
| `mode: "source-time"` | the signal is the absolute source second to show |
| `signal` | a `Signal`, or the name of a signal registered with `comp.addSignal`, including analysis channels |

The signal is read on the composition clock at every frame of the layer and interpolated between frames, so audio samples between frames stay smooth.

- **No frame hooks:** the signal is sampled up front, and frames render in any order.
- **With frame hooks** (`onRequestFrame`, `onPrepareFrame`, `comp.vision`, live physics): the signal is live. Each frame's value is recorded once, after that frame's hooks run.
  - A frame never reads a value ahead of itself.
  - Rendering a later frame first runs the skipped frames' hooks in order. Going back reuses what was already recorded.
  - Source bounds are checked as each frame is recorded.
  - A layer that outlasts the composition holds its last speed.
  - A signal that reads its own layer's current source time throws a circular-dependency error.

## Media chains

Process a single clip outside the layer tree, e.g. to grade footage or extract a still.

```ts
import { ApplyLUT, Media } from "framefields";

const graded = Media.video("assets/footage/raw_log.mp4").apply(new ApplyLUT({ lutUrl: "assets/luts/rec709.cube" }));
await graded.renderFrame({ atMs: 1500 });             // PNG Buffer
await graded.renderVideo({ outputPath: "output/graded.mp4" });
```

`Media.video`, `Media.image`, `Media.lottie`, `Media.audio`; each has `apply`, `renderFrame`, `renderFrameGrid`, `renderVideo`.

## Charts

`Layer.chart(options, boxProps)` builds a chart out of ordinary boxes, paths and text — d3 does the scales and geometry, framefields draws it with your fonts.

```ts
Layer.chart(
  {
    type: "bar",               // line | area | bar | scatter | candlestick | pie | donut
    width: 900, height: 480,
    categories: ["Q1", "Q2", "Q3", "Q4"],
    series: [
      { name: "Revenue", data: [12, 19, 24, 31] },
      { name: "Costs", data: [8, 11, 13, 15] },
    ],
    yAxis: { min: 0, ticks: 4, format: "$,.0f" },          // d3-format string or (v) => string
    fontFamily: "Inter", fontSize: 24, textColor: "#e6e8e6", gridColor: "#2a2b2d",
    animate: { start: 10, duration: 30, stagger: 6, ease: "power3.out" }, // or false
  },
  { id: "revenue", position: "absolute", x: 120, y: 200 },
);
```

| Option | Notes |
| --- | --- |
| `series: [{ name, data, color, type, curve, pointRadius }]` or `data` | `data` for pie/donut slices and candles |
| `stacked` | stack bars/areas |
| `xAxis`, `yAxis` | `min`, `max`, `ticks`, `grid`, `format` |
| `legend`, `valueLabels` | booleans |
| `colors`, `textColor`, `gridColor`, `background`, `cornerRadius` | style |
| `innerRadius` | donut hole |
| `upColor`, `downColor` | candlesticks |
| `animate` | built-in reveal: lines draw on, bars grow, points and slices stagger |

Children get stable ids — `<id>-bar-<series>-<i>`, `<id>-line-<series>`, `<id>-slice-<i>` — so you can find them in lint output. Keep scatter plots to a few hundred points: each point is a node.
