# ribbon

A headline printed on waving 3D fabric: a flag in the wind, a twisting satin ribbon or slow rippling silk.

The text becomes a sheet of cloth in real 3D, folded by travelling waves and seen in perspective. The folds are lit by their own normals, so the sides turned away from the light darken, crests catch a glossy highlight, and the cloth casts a soft shadow on the page that moves with it. A flag is pinned at its left edge and swings wider toward the free end; a ribbon runs one strip per line, and a twist rolls along it, turning the strip past edge-on to show its tinted back; silk ripples slowly while a sheen sweeps across. The pointer presses a dent into the cloth that follows it.

Module `text`, category Text. Import `@dioramajs/text/ribbon`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/text/src/ribbon.ts)

A half marathon headline in white capitals on an orange label that waves like a flag over a night-blue page, its folds lit and casting a soft shadow.

```html title="index.html"
<section class="rb-race">
  <p class="rb-kicker">Harbor Half Marathon · 12 April 2027</p>
  <h2 class="rb-label" data-dio="ribbon">Run the bay</h2>
  <p class="rb-line">21.1 kilometers along the waterfront, over two bridges and into the old town. Entries close on 1 March.</p>
</section>
```

## Usage

Put it on: a heading or short text in a large font, optionally with a solid background color and padding. Not img, video, canvas, svg or form fields.

`start()` registers `ribbon`, so the attribute alone puts it on an element.

```html
<h1 class="hero" data-dio="ribbon">Final lap</h1>
```

In page transitions: The cloth fades with the page.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'flag' \| 'ribbon' \| 'silk'` | `'flag'` | flag is pinned at the left; ribbon twists along each line; silk ripples with a sheen. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'loop' \| 'hover' \| 'scroll'` | `'loop'` | loop waves all the time; hover raises the wind under the pointer; scroll follows the scroll speed. |
| `wind` | `data-dio-wind` | `number` 0 to 2 | `1` | How deep the folds are; 0 keeps the cloth flat apart from the twist and dent. |
| `speed` | `data-dio-speed` | `number` 0 to 3 | `1` | How fast the waves travel; 0 freezes them in place. |
| `folds` | `data-dio-folds` | `number` 1 to 8 | `2.5` | Number of waves along the cloth. |
| `twist` | `data-dio-twist` | `number` 0 to 360° | `180` | Largest turn of the ribbon's rolling twist; past 90 its back shows. |
| `gloss` | `data-dio-gloss` | `number` 0 to 2 | `0.6` | Strength of the glossy highlight on the folds; 0 leaves only the shading. |
| `back` (rebuilds) | `data-dio-back` | `string` | `''` | Color of the back of the cloth; empty uses a darker shade of the surface or text color. |
| `color` (rebuilds) | `data-dio-color` | `string` | `''` | Surface color behind the text, drawn at rest too; empty uses the element's own background. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `style`: `flag`, `ribbon`, `silk`
  - `flag` is pinned at the left.
  - `ribbon` twists along each line.
  - `silk` ripples with a sheen.
- `trigger`: `loop`, `hover`, `scroll`
  - `loop` waves all the time.
  - `hover` raises the wind under the pointer.
  - `scroll` follows the scroll speed.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`over`](/docs/api/inputs#input-over), [`pointer`](/docs/api/inputs#pointer), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size), [`width`](/docs/api/inputs#input-width), [`x`](/docs/api/inputs#input-x), [`y`](/docs/api/inputs#input-y). Draws at a steady rate while it is on screen and stops when it leaves. With trigger loop, every frame while on screen; with hover and scroll, only while the cloth moves and settles, none at rest. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. The cloth stays flat and still: the plain text shows, or the flat copy with its color when color is set.

Runs on every device. There is no dent without a mouse. With trigger hover, a tap blows a gust that settles again; scroll works as on desktop.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { ribbon: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 10.5 KB the first time a page uses it. GPU: A grid of a few thousand vertices per heading, each placed by three evaluations of the waves for its normal, drawn twice: once as the cloth and once flattened as its shadow. One texture read per pixel, a blurred one for the shadow. Memory: A canvas of the text at twice its CSS size with mip levels, and the grid geometry.

- low: The text is rastered at 1 device pixel per CSS pixel instead of 2, the cloth grid is half as dense, and it waves at 30 frames per second.
- medium: The cloth grid is a little coarser.

See [the quality plugin](/docs/effects/quality).

## Recipes

### A satin ribbon for a grand opening

`style="ribbon"` turns each line into a strip of satin. The strip waves, and a twist rolls along it from left to right: where it turns past edge-on you see the back of the ribbon in `back`, a darker shade of the surface when you leave it out. `color` paints the ribbon behind the letters and stays on the page at rest too. A larger `twist` turns the strip further; at 360 it makes a full roll and comes back to the printed side.

A bakery's grand opening headline in cream capitals on a red satin ribbon that twists to its dark back as it waves, on a cream page.

```html title="index.html"
<div class="rb-opening">
  <p>Saturday 6 March, from 10 a.m.</p>
  <h2 class="rb-opening-title" data-dio="ribbon" data-dio-style="ribbon" data-dio-color="#c8102e" data-dio-back="#6e0a1c">Grand opening</h2>
  <p>Fieldhouse Bakery, 18 Mill Lane. The first hundred guests take home a loaf.</p>
</div>
```

### Silk for a fashion label

`style="silk"` lays the letters on silk with slow, shallow ripples from three directions while a broad sheen sweeps across. Without a surface color the cloth is the letters themselves, so it suits a light serif or a refined sans in a pale gold. Raise `gloss` for a glossier fabric.

A fashion house name in pale gold letters rippling like silk, with a sheen sliding across, on a dark plum page.

```html title="index.html"
<div class="rb-silk">
  <p class="rb-silk-kicker">Autumn / Winter 2027</p>
  <h2 class="rb-silk-title" data-dio="ribbon" data-dio-style="silk" data-dio-gloss="1.2">Maison Lune</h2>
  <p class="rb-silk-line">Bias-cut slip dresses in washed silk, shown in Paris on 3 March.</p>
</div>
```

### A label that waves while the page scrolls

With `trigger="scroll"` the wind follows the scroll speed: a quick flick sends a wave through the label, and once the page stops the cloth settles flat and hands back to the DOM. A solid background color and padding on the heading become the cloth, so the label looks the same at rest with or without the effect.

```html
<h2 class="label" data-dio="ribbon" data-dio-trigger="scroll">Match day</h2>
```

## Notes

The cloth is a real 3D surface, not a distortion of the picture. A grid of a few thousand points is folded by travelling waves and seen with its own perspective, the normal of each point comes from the shape of the folds, and the light from the top left shades the cloth by them: folds turned away darken, crests catch a highlight whose strength is `gloss`. The same cloth, flattened along the light, is drawn on the page behind as a soft shadow that blurs the higher the cloth lifts. The text is printed on the cloth from a raster of the heading, so the letters bend with it.

A flag is pinned at its left edge and swings wider toward the free end. `folds` sets how many waves run along the cloth and `wind` how deep they are; the folds keep the same steepness on a short word and on a long line. The pointer presses a dent into the cloth that follows it.

The heading stays real text in the DOM: screen readers read it, search engines index it, and find in page works. With `trigger="hover"` and `trigger="scroll"` the cloth lies flat at rest and the DOM text shows, so nothing is drawn until the wind picks up. A solid background color on the heading itself becomes the surface of the cloth, with its border radius. A background image or a border on the heading is hidden while it waves, so put those on a wrapper; a background image on the heading itself keeps the text as it is.

`color` paints a surface the page does not have, so the flat copy of the cloth stays drawn at rest in place of the DOM text. Under reduced motion the cloth stays flat and still. The canvas cannot draw some text the way the browser does: `background-clip: text`, `text-shadow`, `-webkit-text-stroke` and vertical writing modes. An element with any of them keeps its text as it is, and a warning in development names the reason. `style`, `trigger`, `back` and `color` rebuild the effect when they change.

## Related

- [assemble](/docs/effects/assemble.md): Thousands of lit particles fly out of the depth around the page and gather into a heading.
- [blinds](/docs/effects/blinds.md): A heading printed across real 3D slats that turn over in a wave, lit and casting shadows, to show it.
- [counter](/docs/effects/counter.md): Stats count up to their value on real 3D mechanics: odometer drums, slot machine reels or flip cards.
- [dust](/docs/effects/dust.md): A heading crumbles into fine sand that lifts off the page and drifts away in depth as it scrolls out, or on a click.
- [emboss](/docs/effects/emboss.md): Letters pressed into or raised out of paper, leather, brushed metal, stone or plaster, lit by a light that follows the pointer.
- [extrude](/docs/effects/extrude.md): A headline becomes a solid 3D block: beveled letters with lit side walls, seen in perspective, that turn toward the pointer.
- [fire](/docs/effects/fire.md): The letters burn: flames rise from their tops, sparks drift up in depth and the firelight flickers on the page.
- [hologram](/docs/effects/hologram.md): A heading becomes a 3D hologram: projected from a glowing emitter, built of slices in depth, crossed by scan lines.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
