Odometer

A number that rolls its digit wheels to each new value, like a car's odometer.

zweiundeins <sb-odometer> number counter animation odometer intl Since 2026-09-23 MIT licence 1.6 kB0 Open in playground Edit on GitHub

Playground

Shows a number whose digits roll to each new value, one wheel per digit. Only the digits that change turn, so a readout that ticks up many times a second (a live distance, a counter, a price) stays calm to watch. It takes its font, size and colour from where you put it.

Put the server-formatted number inside it: that is what shows before the component loads, for search engines, and without JavaScript.

Examples

Driving

A tenth of a kilometre every 3.6 seconds is 100 km/h. Give the roll the same 3.6 seconds and a linear pace, and the last wheel never stops turning, like the one in a car; the kilometre wheel turns over with it on the last tenth.

221180.4 km
<div data-signals="{_odo: 221180.4}" data-on-interval__duration.3600ms.leading="$_odo = Math.round(($_odo + 0.1) * 10) / 10">
  <sb-odometer decimals="1" data-attr:value="$_odo" data-preserve-attr="value" style="font-size: 2.5rem; --sb-odometer-duration: 3.6s; --sb-odometer-easing: linear">221180.4</sb-odometer>
  km
</div>

Drum

drum draws each wheel as a cylinder: you see the digit face-on and its neighbours curving away above and below, and as a wheel turns, the digit on it foreshortens like print on a real drum.

221180.4 km
<div data-signals="{_odo: 221180.4}" data-on-interval__duration.1200ms.leading="$_odo = Math.round(($_odo + 0.1) * 10) / 10" style="display: flex; gap: 0.5rem; align-items: center">
  <sb-odometer drum decimals="1" data-attr:value="$_odo" data-preserve-attr="value" style="font-size: 3rem; --sb-odometer-duration: 1.2s; --sb-odometer-easing: linear">221180.4</sb-odometer>
  <span>km</span>
</div>

--sb-odometer-window sets how much of the drum shows (default 1.5em, the digit plus a sliver of its neighbours), --sb-odometer-perspective how strongly it curves (default 5em; smaller is rounder).

Flight

An altimeter moves fast, and both ways. Updating every 150 ms with a 150 ms linear roll, the lower wheels blur, the hundreds tick past steadily, and on the way down every wheel turns back under.

10000 ft
<div data-signals="{_alt: 10000, _vs: 1}" data-on-interval__duration.150ms="$_vs = $_alt >= 35000 ? -1 : $_alt <= 10000 ? 1 : $_vs; $_alt += $_vs * 61" style="display: flex; gap: 1rem; align-items: baseline">
  <sb-odometer data-attr:value="$_alt" data-preserve-attr="value" style="font-size: 2.5rem; --sb-odometer-duration: 150ms; --sb-odometer-easing: linear">10000</sb-odometer>
  <span>ft</span>
  <span data-text="$_vs > 0 ? '▲ climbing' : '▼ descending'"></span>
</div>

Rolling

221180 +1 +37 +999
<div data-signals="{_km: 221180}" style="display: flex; gap: 1rem; align-items: center">
  <sb-odometer data-attr:value="$_km" data-preserve-attr="value" style="font-size: 2rem">221180</sb-odometer>
  <sb-button data-on:click="$_km += 1">+1</sb-button>
  <sb-button data-on:click="$_km += 37">+37</sb-button>
  <sb-button data-on:click="$_km += 999">+999</sb-button>
</div>

Decimals

decimals fixes the digits after the decimal mark, so a whole-number step still shows its .0 and the layout does not jump.

9.4 +0.3 km
<div data-signals="{_trip: 9.4}" style="display: flex; gap: 1rem; align-items: center">
  <sb-odometer decimals="1" data-attr:value="$_trip" data-preserve-attr="value" style="font-size: 2rem">9.4</sb-odometer>
  <sb-button data-on:click="$_trip = Math.round(($_trip + 0.3) * 10) / 10">+0.3 km</sb-button>
</div>

When the number gains a digit (9.9 → 10.0) the wheels snap to the new layout instead of rolling from the old one.

Locales

Separators follow lang, or else the nearest lang around it, also from outside a component it sits in, then the browser's language. A tag that is not a valid locale falls back to the browser's (en_US is read as en-US). The digits themselves are always 0 to 9, and read left to right, in right-to-left text too.

1234567.5 1234567.5 1234567.5
<div style="display: grid; gap: 0.5rem; font-size: 1.5rem">
  <sb-odometer value="1234567.5" decimals="1" lang="de-CH">1234567.5</sb-odometer>
  <sb-odometer value="1234567.5" decimals="1" lang="fr-CH">1234567.5</sb-odometer>
  <sb-odometer value="1234567.5" decimals="1" lang="en">1234567.5</sb-odometer>
</div>

Styling

The digits inherit the surrounding font; a monospaced or tabular face keeps the wheels evenly spaced. ::part(digit) / ::part(separator) reach the wheels and the static characters.

For a wheel you can see is round, add drum (see above).

The speed is yours: --sb-odometer-duration (default 0.55s) is how long a wheel takes to reach its new digit, and --sb-odometer-easing how it gets there. A quick ease-out suits a counter that jumps; for a value that climbs steadily, set the duration to the interval between updates and the easing to linear, and the wheels move continuously. A wheel always turns the way its digits count: forward over the top (9 → 0) when the number grows, back when it shrinks. Below zero that is its size, not its sign: −15 → −13 turns the units wheel back two digits, as 15 → 13 would. A value that arrives mid-roll takes over from where the wheels are.

Accessibility

Screen readers get the formatted value once, as text. The wheels are hidden from them, since each one holds all ten digits, and copying a selection that includes the odometer copies that text, not the wheels. With prefers-reduced-motion: reduce the digits change without rolling.

Installation

Add Datastar with Rocket and the Starbase autoloader once per page, then use the tag: the autoloader imports each component the first time its tag appears, including tags a Datastar morph adds later. The Datastar here is v1.0.5 with Starbase's patches. The components also run on the official v1.0.4 and v1.0.5.

<script type="importmap">
  { "imports": { "datastar": "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js" } }
</script>
<script type="module" src="https://starbase.zweiundeins.gmbh/c/autoloader.js"></script>

<sb-odometer value="221180.4" decimals="1">221180.4</sb-odometer>

No flash of undefined elements: put class="sb-cloak" on <html> and add .sb-cloak :not(:defined) { visibility: hidden } to your CSS. The autoloader removes the class once the page's components are defined.

Load just this component, pinned to this version: all its modules in one minified file with its integrity hash, so the browser refuses it if a single byte changes.

<script type="importmap">
  {
    "imports": { "datastar": "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js" },
    "integrity": {
      "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js": "sha384-AfXVdZ/qxhMEHK4Nz/pJT6jl9xm3HGMBLBtC9yRuU5A7AQtXHYC0x6ap034vDqBV"
    }
  }
</script>
<script type="module" src="https://starbase.zweiundeins.gmbh/c/odometer@4c9d050f6309/odometer.bundle.min.js" integrity="sha384-aI86K+Hebz0jDwNcalVNnSnoApztLaF5vmLLbTRpNrTTtcGC05ULcjzx94hd6PF1"></script>

<sb-odometer value="221180.4" decimals="1">221180.4</sb-odometer>

Or the minified module files (one file, which the browser requests as it finds their imports), each pinned in the import map:

<script type="importmap">
  {
    "imports": { "datastar": "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js" },
    "integrity": {
      "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js": "sha384-AfXVdZ/qxhMEHK4Nz/pJT6jl9xm3HGMBLBtC9yRuU5A7AQtXHYC0x6ap034vDqBV",
      "https://starbase.zweiundeins.gmbh/c/odometer@4c9d050f6309/odometer.min.js": "sha384-ON7wS6L5jW+lSU5TEOmFR0X/l+MunP/mNPyssll3wm9JSgCtqOZoWPkQlJohcvM3"
    }
  }
</script>
<script type="module" src="https://starbase.zweiundeins.gmbh/c/odometer@4c9d050f6309/odometer.min.js" integrity="sha384-ON7wS6L5jW+lSU5TEOmFR0X/l+MunP/mNPyssll3wm9JSgCtqOZoWPkQlJohcvM3"></script>

<sb-odometer value="221180.4" decimals="1">221180.4</sb-odometer>

In production, pin today's whole catalog: its autoloader and an import map with integrity hashes, so the browser refuses any file that changed. This map covers Datastar and this component; importmap.json has the hashes of every component, to merge into yours.

<script type="importmap">
  {
    "imports": { "datastar": "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js" },
    "integrity": {
      "https://starbase.zweiundeins.gmbh/c/datastar@5f98646d7d3d/datastar-rocket.js": "sha384-AfXVdZ/qxhMEHK4Nz/pJT6jl9xm3HGMBLBtC9yRuU5A7AQtXHYC0x6ap034vDqBV",
      "https://starbase.zweiundeins.gmbh/c/odometer@4c9d050f6309/odometer.bundle.min.js": "sha384-aI86K+Hebz0jDwNcalVNnSnoApztLaF5vmLLbTRpNrTTtcGC05ULcjzx94hd6PF1"
    }
  }
</script>
<script type="module" src="https://starbase.zweiundeins.gmbh/c/@81f24b268adb/autoloader.js" integrity="sha384-bWIhsIq9SpNUiIZyG/lv190MgHA05m8BVYA3LyIoE/pgOF3NUt0437e++4Ht1NcS"></script>

<sb-odometer value="221180.4" decimals="1">221180.4</sb-odometer>

Components import only datastar and their own files, so copying is enough: save each component's one file (and a file it loads on first use, keeping its folder), and point the import map at your own copy of datastar-rocket.js (the /js/ paths stand for yours).

  • <sb-odometer>
    • odometer.bundle.min.js one file 1.6 kB brotli
<script type="importmap">
  { "imports": { "datastar": "/js/datastar-rocket.js" } }
</script>
<script type="module" src="/js/odometer/odometer.bundle.min.js"></script>

<sb-odometer value="221180.4" decimals="1">221180.4</sb-odometer>

Or copy the module files, keeping each component's folder, and load each component's module:

  • <sb-odometer>
<script type="importmap">
  { "imports": { "datastar": "/js/datastar-rocket.js" } }
</script>
<script type="module" src="/js/odometer/odometer.min.js"></script>

<sb-odometer value="221180.4" decimals="1">221180.4</sb-odometer>

In Go, render it with its typed templ wrapper: one field per prop, generated from the component's manifest (the module's README has the type rules). Add the module:

go get github.com/zweiundeins/starbase/templ@latest

Then load it, pinned with integrity hashes to the versions the module was generated with, and use it:

// In <head>: the components you list, pinned,
@sb.Imports(sb.ImportsProps{Components: []sb.Tag{sb.TagOdometer}})
// or the autoloader, which also loads tags a morph adds later: @sb.Imports(sb.ImportsProps{})
// or, around the content of <body>, the components it renders: @sb.Tracked(sb.ImportsProps{}) { … }

@sb.Odometer(sb.OdometerProps{Value: 221180.4, Decimals: 1}) {
	221180.4
}

Size

A page loads this component as one file, its bundle, compressed the way it is served (gzip -9, brotli -11): brotli is what it downloads. Datastar and Rocket are shared by every component and not counted.

FileOriginalgzipbrotli
odometer.bundle.min.js 3.5 kB1.8 kB1.6 kB
As module files: one request, 1.5 kB minified with brotli

For a page that loads the module files as they are, requesting each one as it finds its import, or bundles them itself. Each file compressed on its own; the minified one is what the page downloads, and the readable source is always there too.

FileOriginalgzipbrotliminified
odometer.js 9.7 kB4.3 kB3.7 kB1.5 kB

API reference

Props

AttributeTypeDefaultDescription
valuenumber0The number to show. Change it and the digits roll to the new value.
decimalsnumber0Fixed digits after the decimal mark, so a whole-number step still shows ".0" and the layout does not jump.
groupingtrue | falsetrueGroup thousands the way the locale does (221’180, 221,180, 221 180). On by default: grouping="false" turns it off.
langstring""Locale for the separators (default: the page's lang, then the browser's).
drumbooleanfalseDraw each wheel as a 3D drum: digits curve away above and below, and foreshorten as they roll.