# counter

Stats count up to their value on real 3D mechanics: odometer drums, slot machine reels or flip cards.

When a number such as 12,480 or $4.2M scrolls into view, each of its digits sits on a drum that turns in 3D: digits curve over the top and bottom of the drum, foreshortened and darker as they turn away, with a soft sheen and seams between the drums. The value counts up from 0 like a mechanical odometer, the last drum overshooting its digit and settling back. The slot style spins deeper reels behind window frames, blurred while they spin, and lands them one by one from right to left with a bounce; the flip style drops the top half of each card about its hinge towards the viewer until the digit lands. The text around the number and its separators stay flat. When the digits land, the drums or cards fold back into the page and the element is its own text again.

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

A swim club's season stat whose digits turn on odometer drums from 0 up to 12,480 and fold back into the text.

```html title="index.html"
<div class="cn-stat">
  <p class="cn-kicker">Open water season 2026</p>
  <p class="cn-number" data-dio="counter">12,480 <span>swimmers</span></p>
  <p class="cn-note">crossed the bay with a club coach since the first of May.</p>
</div>
```

## Usage

Put it on: a heading, paragraph or span holding one number, with any text before and after it, such as $4.2M, 99.9% or 12,480 swimmers.

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

```html
<p class="stat" data-dio="counter">12,480 <span>swimmers</span></p>
```

In page transitions: Entering a page the number counts up from its start value; leaving, it counts back down to it.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'odometer' \| 'slot' \| 'flip'` | `'odometer'` | odometer turns flush drums in a row, for stats sections; slot spins deep reels behind window frames and lands them right to left, for totals and prices; flip drops flip cards through the digits, for dashboards and scores. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'view' \| 'scroll'` | `'view'` | view counts once when the number enters; scroll ties the count to the scroll, both ways. |
| `duration` | `data-dio-duration` | `number` 0.3 to 6 s | `2` | Time of the count; with slot and flip, until the last digit lands. |
| `from` | `data-dio-from` | `number` 0 to 100000 | `0` | Value the count starts from, at most what the digits of the number can hold. |
| `stagger` | `data-dio-stagger` | `number` 0 to 0.5 s | `0.12` | Delay between neighboring digits landing, for slot and flip. |
| `delay` | `data-dio-delay` | `number` 0 to 3 s | `0` | Wait after the trigger, for a row of stats. |
| `threshold` | `data-dio-threshold` | `number` 0 to 1 | `0.3` | Share of the element height in view, above the bottom tenth, before it counts. |

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

### Variants

- `style`: `odometer`, `slot`, `flip`
  - `odometer` turns flush drums in a row, for stats sections.
  - `slot` spins deep reels behind window frames and lands them right to left, for totals and prices.
  - `flip` drops flip cards through the digits, for dashboards and scores.
- `trigger`: `view`, `scroll`
  - `view` counts once when the number enters.
  - `scroll` ties the count to the scroll, both ways.

## Inputs and frames

Reads [`height`](/docs/api/inputs#input-height), [`presence`](/docs/api/inputs#presence), [`progress`](/docs/api/inputs#progress), [`size`](/docs/api/inputs#size), [`width`](/docs/api/inputs#input-width), [`x`](/docs/api/inputs#input-x), [`y`](/docs/api/inputs#input-y). Draws only while something on it moves, then stops. Only while the digits turn and fold back, and with the scroll trigger while scrolling; none at rest. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it is off and the element shows as plain HTML. Off: the number is shown as it is, and the engine does not load for it.

Runs on every device. The same as with a mouse: nothing reads the pointer.

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

start({ effects: { counter: { reducedMotion: 'static' } } })
```

## Cost and quality

Loads 12.9 KB the first time a page uses it. GPU: A strip of 24 quads per digit with ten texture reads per pixel for the motion blur, a quad per letter around the number. Memory: A canvas of the text at twice its CSS size and a small sheet of the digits, kept while the element lives.

- low: The text and the digits are rastered at 1 device pixel per CSS pixel instead of 2.

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

## Recipes

### A row of stats that count in turn

Give each stat its own element and a growing `delay`, so the numbers land one after another instead of all at once. Each number can take its own style. Scroll down to this example to see it count.

Three stats of a swim club platform that count up in turn on drums, slot reels and flip cards.

```html title="index.html"
<ul class="cn-row">
  <li><p data-dio="counter">312</p><span>clubs on the platform</span></li>
  <li><p data-dio="counter" data-dio-style="slot" data-dio-delay="0.25">$4.2M</p><span>raised for open water safety</span></li>
  <li><p data-dio="counter" data-dio-style="flip" data-dio-delay="0.5">99.9%</p><span>of sessions booked online</span></li>
</ul>
```

### A total on slot machine reels

`style="slot"` spins every digit on a deep reel behind a window frame and lands them from right to left, each with a bounce. A longer `stagger` makes the wait for the last reel part of the show, which suits fundraising totals and prize pools.

```html
<p class="total" data-dio="counter" data-dio-style="slot" data-dio-stagger="0.25" data-dio-duration="1.6">$4,250,000</p>
```

### A year that follows the scroll

With `data-dio-trigger="scroll"` the count follows the scroll position both ways, from `from` when the number enters to its value when it reaches the middle of the viewport. On a company timeline it turns the years as the visitor reads.

```html
<p class="year" data-dio="counter" data-dio-trigger="scroll" data-dio-from="1998">2026</p>
```

## Notes

The element holds one number, with any text before and after it: `$4.2M`, `99.9%`, `12,480 swimmers` or `Over 1,200 teams`. Only the first number counts; put two stats in two elements. The number keeps the way it is written: its group separators, decimals and leading zeros, as in `1.204.500` or `1 024`. A lone point or comma before three digits, as in `3.142`, is read with the decimal separator of the page language, so set `lang` on the page or the element; without one, `12,480` is twelve thousand. A minus sign stays in front as text.

The DOM text never changes, so screen readers, search engines and find in page always see the final value. The drums, reels and cards are drawn only while the digits turn; when they land they fold back into the page and the element is real, selectable text again. Under reduced motion the effect is off and the number shows at once.

Each style draws its parts from the text color and the background behind the element: dark drums on a dark page, light ones on a light page. While they turn, the drums reach about a third of an em above and below the digits, so give the number a line height of 1.3 or some room around it. While the number counts, the element itself is hidden behind its copy, its own background too: put a card’s background on a parent.

`from` cannot hold more digits than the number, so `from="900"` works for `1,024` but not for `24`. A number on screen when the page opens is hidden until the effect is ready and then counts, as with `text-reveal`; with the scroll trigger it stays as it is instead.

The canvas cannot draw `background-clip: text`, `text-shadow`, `-webkit-text-stroke` or vertical writing modes the way the browser does. An element with any of them keeps its number as it is, and a warning in development names the reason. `style` and `trigger` 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.
- [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.
- [inflate](/docs/effects/inflate.md): Letters puff up into round 3D shapes, like balloons or soft clay, that dent and wobble under the pointer.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
