{
  "$schema": "../../schema.json",
  "id": "odometer",
  "type": "text",
  "title": "Odometer",
  "summary": "A number counts up with rolling digit wheels, like an odometer.",
  "tags": [
    "text",
    "numbers",
    "stats"
  ],
  "version": "1.0.0",
  "license": "MIT",
  "export": "odometer",
  "signature": "odometer(el, t, { from, to, start, duration, curve, separator })",
  "params": [
    {
      "name": "from",
      "type": "number",
      "default": 0,
      "description": "start value",
      "min": 0,
      "max": 1000000,
      "step": 1,
      "group": "main"
    },
    {
      "name": "to",
      "type": "number",
      "default": 1000,
      "description": "end value",
      "min": 0,
      "max": 1000000,
      "step": 1,
      "group": "main"
    },
    {
      "name": "start",
      "type": "number",
      "default": 0,
      "description": "start time in seconds",
      "hidden": true
    },
    {
      "name": "duration",
      "type": "number",
      "default": 2,
      "description": "length of the transition in seconds; springs use it to keep their feel",
      "min": 0.2,
      "max": 6,
      "step": 0.05,
      "group": "main"
    },
    {
      "name": "curve",
      "type": "function",
      "default": "ease.outExpo",
      "description": "easing function (from `ease`)",
      "control": "ease",
      "group": "main"
    },
    {
      "name": "separator",
      "type": "string",
      "default": ",",
      "description": "thousands separator",
      "group": "advanced"
    }
  ],
  "install": {
    "kind": "code"
  },
  "files": [
    {
      "path": "moves/odometer.js",
      "content": "import { ease, clamp, lerp, progress } from 'paper-motion';\n\n/** Counts from `from` to `to` on rolling digit wheels; a wheel only turns when the one to its right rolls over. */\nexport function odometer(el, t, { from = 0, to = 1000, start = 0, duration = 2, curve = ease.outExpo, separator = ',' } = {}) {\n  const v = lerp(from, to, curve(progress(t, start, start + duration)));\n  if (!el._labOdo) {\n    const nd = String(Math.round(Math.max(from, to))).length;\n    el.textContent = '';\n    const probe = document.createElement('span'); el.append(probe);\n    const w = Math.max(...'0123456789'.split('').map((d) => { probe.textContent = d; return probe.getBoundingClientRect().width; }));\n    probe.remove();\n    el._labOdo = [];\n    for (let i = 0; i < nd; i++) {\n      const k = nd - 1 - i;\n      const col = document.createElement('span');\n      Object.assign(col.style, { display: 'inline-block', overflow: 'hidden', height: '1em', lineHeight: '1em', width: `${w}px`, textAlign: 'center', verticalAlign: 'bottom' });\n      const strip = document.createElement('span');\n      strip.style.display = 'block';\n      strip.innerHTML = '01234567890'.split('').map((d) => `<span style=\"display:block\">${d}</span>`).join('');\n      col.append(strip); el.append(col);\n      el._labOdo.push({ k, col, strip });\n      if (separator && k > 0 && k % 3 === 0) { const s = document.createElement('span'); s.textContent = separator; s.style.verticalAlign = 'bottom'; s.style.lineHeight = '1em'; s.dataset.k = k; el.append(s); el._labOdo.push({ k, sep: s }); }\n    }\n  }\n  for (const d of el._labOdo) {\n    const on = d.k === 0 || v >= 10 ** d.k ? 1 : 0.16;\n    if (d.sep) { d.sep.style.opacity = on; continue; }\n    const pos = d.k === 0 ? v % 10 : (Math.floor(v / 10 ** d.k) % 10) + clamp(((v / 10 ** (d.k - 1)) % 10) - 9);\n    d.strip.style.transform = `translateY(${-pos}em)`;\n    d.col.style.opacity = on;\n  }\n}\n"
    }
  ],
  "requires": {
    "engine": ">=0.2.0"
  },
  "usage": "import { odometer } from './moves/odometer.js';\n\ns.update((t) => odometer(el, t, { start: 0.5 }));",
  "notes": [
    "Counts from `from` to `to` on rolling digit wheels; a wheel only turns when the one to its right rolls over."
  ],
  "preview": {
    "video": "previews/odometer.webm",
    "poster": "previews/odometer.jpg"
  },
  "mood": [
    "technical",
    "editorial",
    "bold"
  ],
  "energy": 3,
  "role": "text",
  "when": [
    "a big number is the message",
    "stats, counts, prices"
  ]
}