# Masks and blend modes

> Track mattes with `mask` and `maskMode` (alpha, inverted, luma), video through text, animated wipes and reveals, and every `blendMode`.

A **mask** (a track matte, in After Effects terms) shows a layer only where another shape is. A
**blend mode** changes how a layer's colors combine with what's behind it. Both are per-layer fields
and work on every layer type, including groups.

## Masks

`mask` takes a complete layer definition. That layer is never drawn; instead, the masked layer is
visible only where the mask layer would be opaque:

```json
{ "id": "clip", "type": "video", "src": "assets/clip.mp4", "width": "100%", "height": "100%", "muted": true,
  "mask": { "type": "text", "text": "VIDMAKE", "font": "Schibsted Grotesk", "fontWeight": 900, "fontSize": 220 } }
```

**Demo: Video through text: a text layer used as the alpha matte of a video** (rendered video: https://vidmake.app/media/masks-text-video.mp4)

```json
{
  "fonts": [{ "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" }],
  "compositions": [{
    "id": "through", "description": "Video through text: a text layer used as the alpha matte of a video",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "outline", "type": "text", "text": "VIDMAKE", "font": "Schibsted Grotesk", "fontWeight": 900, "fontSize": 220, "letterSpacing": -9,
        "fill": null, "stroke": "#d8d1c1", "strokeWidth": 3 },
      { "id": "clip", "type": "video", "src": "assets/clip.mp4", "muted": true, "width": "100%", "height": "100%", "saturation": 0.75,
        "mask": { "type": "text", "text": "VIDMAKE", "font": "Schibsted Grotesk", "fontWeight": 900, "fontSize": 220, "letterSpacing": -9,
                  "enter": { "type": "wipeRight", "duration": "1.2s", "ease": "easeInOutCubic" }, "exit": "fade" } }
    ]
  }]
}
```

The mask can be any layer type: text, a shape, an image with transparency, a video, a gradient, a
path, even a group of several shapes. It can have its own animation, [presets](https://vidmake.app/docs/presets.md) and
[effects](https://vidmake.app/docs/effects.md). In that demo the text mask has a `wipeRight` entrance, so the video is revealed
letter by letter, and a dim outline text (a normal layer) shows where the letters will be.

### Where a mask sits

The mask layer is placed and timed **as if it were a sibling** of the masked layer:

- its `x`, `y`, `width`, `height` and percentages are in the masked layer's **parent** coordinates,
  not relative to the masked layer;
- its `start` and its animation times are relative to the parent too, not to the masked layer's start;
- it defaults to the center of the parent, like any layer.

So if the masked layer moves, the mask stays where it is: the layer slides "behind" a fixed window. To
move a layer and its mask together, wrap the layer in a [group](https://vidmake.app/docs/groups.md), define the mask inside
the group, and move the group. The same goes for [repeat](https://vidmake.app/docs/repeat.md): copies of a masked layer share
one mask, so repeat a group that contains the masked layer instead.

> [!WARNING]
> If the masked layer has `"start": "2s"`, its mask still starts at 0. That rarely matters for a still
> mask, but an animated one (a wipe, a growing circle) has finished before the layer appears. Give the mask
> the same `start`, or put both in a group that starts at 2 s.

### Mask modes

`maskMode` chooses what part of the mask counts:

| `maskMode` | the layer shows where the mask is... |
|---|---|
| `alpha` (default) | opaque. Soft or semi-transparent edges give partial visibility |
| `alphaInverted` (aliases `invert`, `inverted`) | transparent: a cut-out |
| `luma` | bright: white shows the layer fully, black and transparent hide it, grays in between |
| `lumaInverted` | dark: black (and transparent) areas show the layer, white hides it |

**Demo: maskMode: alpha and alphaInverted use the matte's opacity, luma and lumaInverted its brightness** (rendered video: https://vidmake.app/media/masks-modes.mp4)

```json
{
  "fonts": [{ "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }],
  "compositions": [{
    "id": "modes", "description": "maskMode: alpha and alphaInverted use the matte's opacity, luma and lumaInverted its brightness",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "alpha", "type": "rect", "x": 200, "width": 240, "height": 300, "cornerRadius": 2,
        "fill": { "type": "linear", "angle": 160, "stops": ["#4a4640", "#161412"] },
        "mask": { "type": "star", "x": 200, "width": "=200 + 30 * sin(t * pi)", "height": "=200 + 30 * sin(t * pi)", "rotation": "=t * 30" } },
      { "id": "alphaInverted", "type": "rect", "x": 493, "width": 240, "height": 300, "cornerRadius": 2, "maskMode": "alphaInverted",
        "fill": { "type": "linear", "angle": 160, "stops": ["#4a4640", "#161412"] },
        "mask": { "type": "star", "x": 493, "width": "=200 + 30 * sin(t * pi)", "height": "=200 + 30 * sin(t * pi)", "rotation": "=t * 30" } },
      { "id": "luma", "type": "rect", "x": 786, "width": 240, "height": 300, "cornerRadius": 2, "maskMode": "luma",
        "fill": { "type": "linear", "angle": 160, "stops": ["#1f5fb0", "#161412"] },
        "mask": { "type": "rect", "x": 786, "width": 240, "height": 300,
                  "fill": { "type": "linear", "angle": { "from": 0, "to": 360, "duration": "4s", "ease": "linear" }, "stops": ["#fff", "#000"] } } },
      { "id": "lumaInverted", "type": "rect", "x": 1080, "width": 240, "height": 300, "cornerRadius": 2, "maskMode": "lumaInverted",
        "fill": { "type": "linear", "angle": 160, "stops": ["#1f5fb0", "#161412"] },
        "mask": { "type": "rect", "x": 1080, "width": 240, "height": 300,
                  "fill": { "type": "linear", "angle": { "from": 0, "to": 360, "duration": "4s", "ease": "linear" }, "stops": ["#fff", "#000"] } } },
      { "id": "label", "type": "text", "text": "${pick(i, ['alpha', 'alphaInverted', 'luma', 'lumaInverted'])}", "font": "JetBrains Mono",
        "fontSize": 18, "letterSpacing": 1.6, "fill": "#8a857a", "x": 640, "y": 560, "repeat": { "count": 4, "layout": "row", "spacing": 293 } }
    ]
  }]
}
```

Each tile in that demo has its own mask with an `x` that matches the tile's `x`, because masks are
positioned in the parent's coordinates. The alpha tiles use a star; the luma tiles use a white-to-black
gradient whose angle turns, so the visible part sweeps around.

`maskMode` without `mask` is a validation error.

### Wipes and reveals

Animate the mask to reveal a layer or a whole scene. An iris wipe is a circle mask that scales up:

**Demo: An iris wipe: the second scene is masked by a circle that grows from the center** (rendered video: https://vidmake.app/media/masks-wipe.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" },
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk-Italic.ttf" }
  ],
  "compositions": [{
    "id": "iris", "description": "An iris wipe: the second scene is masked by a circle that grows from the center",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s",
    "layers": [
      { "id": "before", "type": "group", "children": [
        { "id": "before-bg", "type": "rect", "fill": "#f4f1ea" },
        { "id": "before-t", "type": "text", "text": "before", "font": "Schibsted Grotesk", "fontWeight": 860, "fontSize": 170, "letterSpacing": -7, "fill": "#161412" } ] },
      { "id": "after", "type": "group",
        "mask": { "type": "ellipse", "width": 100, "scale": [[0, 0], ["0.8s", 0], ["2s", 15, "easeInOutCubic"], ["2.8s", 15], ["3.8s", 0, "easeInOutCubic"]] },
        "children": [
          { "id": "after-bg", "type": "rect", "fill": "#161412" },
          { "id": "after-t", "type": "text", "text": "after", "font": "Schibsted Grotesk", "italic": true, "fontWeight": 400, "fontSize": 190, "letterSpacing": -6, "fill": "#f4f1ea" } ] }
    ]
  }]
}
```

```json
"mask": { "type": "ellipse", "width": 100,
          "scale": [[0, 0], ["0.8s", 0], ["2s", 15, "easeInOutCubic"], ["2.8s", 15], ["3.8s", 0, "easeInOutCubic"]] }
```

The demo animates `scale` rather than `width`: an ellipse with only `width` set is made square from its
frame-0 value (see [which fields animate](https://vidmake.app/docs/animation.md#which-fields-animate)).

Other reveals use the same idea:

| reveal | mask |
|---|---|
| linear wipe | a `rect` whose `width` grows from 0 with `"anchor": "left"`, or the [`wipe*` presets](https://vidmake.app/docs/presets.md) on the mask |
| diagonal wipe | a large `rect` with `rotation: 30` sliding across with `x` keyframes |
| soft wipe | any of the above with `"blur": 40` on the mask, for a feathered edge |
| text reveal | a `text` mask with a `typewriter` or `stagger` entrance |
| spotlight | an `ellipse` mask with `blur` following an [expression](https://vidmake.app/docs/expressions.md) path |
| shape morph | a `star` mask animating `innerRadius` |

For simple directional reveals on a single layer, the [`wipeLeft|Right|Up|Down` presets](https://vidmake.app/docs/presets.md)
are shorter; reach for a mask when the reveal has a custom shape or must cover several layers at once (put
the mask on a group).

> [!TIP]
> `blur` on the mask layer feathers the matte: `"mask": { "type": "rect", ..., "blur": 30 }` gives a soft
> edge. Blur, opacity and other effects on the mask all change the matte.

## Blend modes

`blendMode` sets how the layer's colors mix with the pixels beneath it:

| `blendMode` | effect |
|---|---|
| `normal` | default: paints over what's below |
| `multiply` | darkens; white disappears. Good for shadows, paper textures, ink |
| `screen` | lightens; black disappears. Good for light leaks, glows, flares on black |
| `overlay` | multiply on dark areas and screen on light ones: adds contrast |
| `darken` | keeps the darker of the two colors per channel |
| `lighten` | keeps the lighter of the two colors per channel |
| `color-dodge` (alias `colorDodge`) | brightens the backdrop strongly, for hot highlights |
| `color-burn` (alias `colorBurn`) | darkens the backdrop strongly, with saturated shadows |
| `hard-light` (alias `hardLight`) | like overlay, keyed on the layer instead of the backdrop |
| `soft-light` (alias `softLight`) | a gentle version of hard-light |
| `difference` | the absolute difference: inverts where the layer is bright |
| `exclusion` | like difference, lower contrast |
| `add` (alias `plus`) | adds the colors together, clipping at white |

**Demo: Every blendMode: the same red disc composited over the same backdrop** (rendered video: https://vidmake.app/media/masks-blend.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" },
    { "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }
  ],
  "compositions": [{
    "id": "blend", "description": "Every blendMode: the same red disc composited over the same backdrop",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "normal", "type": "use", "ref": "cell", "x": 160, "y": 150, "props": { "mode": "normal" } },
      { "id": "multiply", "type": "use", "ref": "cell", "x": 400, "y": 150, "props": { "mode": "multiply" } },
      { "id": "screen", "type": "use", "ref": "cell", "x": 640, "y": 150, "props": { "mode": "screen" } },
      { "id": "overlay", "type": "use", "ref": "cell", "x": 880, "y": 150, "props": { "mode": "overlay" } },
      { "id": "darken", "type": "use", "ref": "cell", "x": 1120, "y": 150, "props": { "mode": "darken" } },
      { "id": "lighten", "type": "use", "ref": "cell", "x": 160, "y": 360, "props": { "mode": "lighten" } },
      { "id": "color-dodge", "type": "use", "ref": "cell", "x": 400, "y": 360, "props": { "mode": "color-dodge" } },
      { "id": "color-burn", "type": "use", "ref": "cell", "x": 640, "y": 360, "props": { "mode": "color-burn" } },
      { "id": "hard-light", "type": "use", "ref": "cell", "x": 880, "y": 360, "props": { "mode": "hard-light" } },
      { "id": "soft-light", "type": "use", "ref": "cell", "x": 1120, "y": 360, "props": { "mode": "soft-light" } },
      { "id": "difference", "type": "use", "ref": "cell", "x": 160, "y": 570, "props": { "mode": "difference" } },
      { "id": "exclusion", "type": "use", "ref": "cell", "x": 400, "y": 570, "props": { "mode": "exclusion" } },
      { "id": "add", "type": "use", "ref": "cell", "x": 640, "y": 570, "props": { "mode": "add" } },
      { "id": "title", "type": "text", "text": "blendMode", "font": "Schibsted Grotesk", "fontWeight": 860, "fontSize": 72,
        "letterSpacing": -3.2, "fill": "#161412", "x": 1000, "y": 560 }
    ]
  }, {
    "id": "cell", "width": 220, "height": 190, "duration": "4s", "props": { "mode": "normal" },
    "layers": [
      { "id": "backdrop", "type": "rect", "y": 75, "width": 220, "height": 150, "cornerRadius": 2, "stroke": "#161412", "strokeWidth": 1.5,
        "fill": { "type": "linear", "angle": 90, "stops": ["#161412", "#1f5fb0", "#e9e4d8", "#fbf9f4"] } },
      { "id": "disc", "type": "ellipse", "width": 110, "y": 75, "x": "=110 + 40 * sin(t * pi)", "fill": "#c92a26", "blendMode": "{{mode}}" },
      { "id": "name", "type": "text", "text": "{{mode}}", "font": "JetBrains Mono", "fontSize": 16, "letterSpacing": 1.4, "fill": "#8a857a", "y": 172 }
    ]
  }]
}
```

```json
{ "id": "leak", "type": "image", "src": "light-leak.jpg", "width": "100%", "height": "100%", "blendMode": "screen", "opacity": 0.7 }
```

`blendMode` can't be animated; fade a blended layer's `opacity` instead.

### Blending inside groups

A layer blends with whatever has already been drawn **into the same buffer**. Most of the time that's
the frame below it. But a [group](https://vidmake.app/docs/groups.md) that needs its own buffer (because it has an effect,
`opacity` below 1, `clip`, a `mask` or a `blendMode` of its own; [components](https://vidmake.app/docs/components.md) clip by
default) composites its children first: inside it, children blend only with their siblings, and the group's
result is then composited normally.

That's why each cell in the demo above contains its own backdrop: every cell is a clipped component, so a
disc blended against the page background behind it would see nothing. To blend a group's content with the
scene behind it, give the **group** the `blendMode`.

> [!AGENT]
> Masks and blend modes don't change a layer's reported box: `inspect` still shows the full layer. Check them
> with `vidmake still` or `vidmake storyboard`, and remember the mask is positioned in the parent's
> coordinates when the result looks shifted.
