{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"steel-tape-measure-range-slider-with-a-blade-that-sags-a-thumb-lock-and-spring-rewind","type":"registry:component","title":"Steel Tape Measure Range Slider with a Blade That Sags, a Thumb Lock and Spring Rewind","description":"Range slider drawn as a pocket tape measure: pull the blade out, watch it sag under its weight and slide the lock off to rewind. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/steel-tape-measure-range-slider-with-a-blade-that-sags-a-thumb-lock-and-spring-rewind.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// What the tape measures. The blade is printed from these numbers, so change them freely.\nconst LABEL = \"Shelf width\";\nconst UNIT = \"cm\";\nconst MIN = 0;\nconst MAX = 60;\nconst STEP = 1;\nconst START = 46;\nconst DEMO = true; // the tape plays by itself when left alone. Set to false inside a real form.\nconst SHELL = [\"#ffd84a\", \"#f4b90c\", \"#d39300\"]; // case plastic: lit, body, shaded\nconst BLADE = [\"#fff0a0\", \"#f6cf3a\", \"#d9a916\"]; // blade lacquer: bright band, body, edge\nconst TAB = \"#e23b2e\"; // the thumb lock\n\nconst snap = (v: number) => Math.max(MIN, Math.min(MAX, Math.round(v / STEP) * STEP));\n\nexport default function SteelTapeMeasureSlider() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const lockBtn = useRef<HTMLButtonElement>(null);\n  const [value, setValue] = useState(START);\n  const [locked, setLocked] = useState(true);\n  const api = useRef<{ set: (v: number) => void; lock: (on: boolean) => void } | null>(null);\n\n  useEffect(() => {\n    const cv = canvas.current;\n    if (!cv) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const dpr = Math.min(2, window.devicePixelRatio || 1);\n    const CW = 172, CH = 164; // the case, before scaling\n    let W = 0, H = 0, k = 1, cx0 = 0, cy0 = 0, mx = 0, my = 0, Lmax = 1, ppu = 1, BH = 30;\n\n    // Fine speckle that stops the plastic and rubber looking like flat fills.\n    const grain = document.createElement(\"canvas\");\n    grain.width = grain.height = 96;\n    const gx = grain.getContext(\"2d\");\n    if (gx) {\n      const im = gx.createImageData(96, 96);\n      let seed = 11;\n      for (let i = 0; i < 96 * 96; i++) {\n        seed = (seed * 16807) % 2147483647;\n        const v = seed % 255;\n        im.data[i * 4] = im.data[i * 4 + 1] = im.data[i * 4 + 2] = v > 127 ? 255 : 0;\n        im.data[i * 4 + 3] = Math.abs(v - 127) * 0.2;\n      }\n      gx.putImageData(im, 0, 0);\n    }\n    const speckle = ctx.createPattern(grain, \"repeat\");\n\n    const s = { u: DEMO ? START - 8 : START, uv: 0, target: DEMO ? START - 8 : START, raw: START, drag: false, held: false, locked: true, lockPos: 1, wob: 0, wobV: 0, kick: 0, kickV: 0, shown: START, lockShown: true, keyed: false };\n\n    const layout = () => {\n      W = cv.clientWidth; H = cv.clientHeight;\n      const small = W < 560;\n      k = small ? 0.8 : W >= 760 ? 1.42 : 1;\n      cx0 = small ? 2 : 30; cy0 = H * 0.43 - CH * k * 0.66;\n      mx = cx0 + (CW + 2) * k; my = cy0 + (CH - 25) * k; BH = 30 * k;\n      Lmax = W - mx - (small ? 14 : 58); ppu = Lmax / (MAX - MIN);\n      cv.width = Math.round(W * dpr); cv.height = Math.round(H * dpr);\n      const b = lockBtn.current;\n      if (b) { b.style.left = `${cx0 + (CW - 24) * k - 24}px`; b.style.top = `${cy0 + 55 * k - 32}px`; }\n    };\n\n    const rr = (x: number, y: number, w: number, h: number, r: number[]) => {\n      ctx.beginPath(); ctx.moveTo(x + r[0], y);\n      ctx.arcTo(x + w, y, x + w, y + h, r[1]); ctx.arcTo(x + w, y + h, x, y + h, r[2]);\n      ctx.arcTo(x, y + h, x, y, r[3]); ctx.arcTo(x, y, x + w, y, r[0]); ctx.closePath();\n    };\n    const OUT = [50, 44, 20, 30];\n\n    // The unsupported blade droops like a beam fixed at one end: flat at the mouth, steepest at the hook.\n    const curve = (L: number, t: number) => {\n      const n = L / Lmax;\n      const def = 0.17 * Lmax * Math.pow(n, 2.2) + (s.wob + Math.sin(t / 1000) * 1.8) * n * n;\n      const y = (x: number) => { const q = Math.max(0, x / Math.max(L, 1)); return (def * q * q * (6 - 4 * q + q * q)) / 3; };\n      const slope = (x: number) => { const q = Math.max(0, x / Math.max(L, 1)); return (def * (12 * q - 12 * q * q + 4 * q * q * q)) / 3 / Math.max(L, 1); };\n      return { y, slope };\n    };\n\n    const drawBlade = (L: number, c: ReturnType<typeof curve>) => {\n      const SEG = 3, h = BH;\n      const g = ctx.createLinearGradient(0, -h / 2, 0, h / 2);\n      g.addColorStop(0, \"#8f6c05\"); g.addColorStop(0.05, BLADE[2]); g.addColorStop(0.2, BLADE[1]); g.addColorStop(0.36, BLADE[0]);\n      g.addColorStop(0.5, BLADE[1]); g.addColorStop(0.82, BLADE[2]); g.addColorStop(0.9, \"#a47d09\"); g.addColorStop(0.93, \"#6b5206\"); g.addColorStop(1, \"#3d2f04\");\n      for (let x = -6; x < L; x += SEG) {\n        ctx.save(); ctx.translate(mx + x, my + c.y(x)); ctx.rotate(Math.atan(c.slope(x)));\n        ctx.fillStyle = g; ctx.fillRect(0, -h / 2, Math.min(SEG, L - x) + 0.7, h);\n        ctx.restore();\n      }\n      // Printed scale. Marks are measured from the hook, so the number at the mouth is the reading.\n      const sub = ppu >= 9 ? 5 : ppu >= 5 ? 2 : 1;\n      const top = -h / 2 + h * 0.05, face = h * 0.86;\n      const count = Math.ceil(L / ppu) + 1;\n      ctx.textAlign = \"center\"; ctx.textBaseline = \"middle\";\n      for (let i = 0; i <= count * sub; i++) {\n        const d = i / sub, x = L - d * ppu;\n        if (x < -2 || d > MAX - MIN + 0.01) continue;\n        const whole = i % sub === 0, n = Math.round(d), ten = whole && n % 10 === 0, five = whole && n % 5 === 0;\n        ctx.save(); ctx.translate(mx + x, my + c.y(x)); ctx.rotate(Math.atan(c.slope(x)));\n        const len = ten ? face * 0.36 : five ? face * 0.44 : whole ? face * 0.32 : face * 0.17;\n        ctx.fillStyle = whole ? \"rgba(24,20,8,0.92)\" : \"rgba(24,20,8,0.7)\";\n        ctx.fillRect(-(whole ? 0.6 : 0.35), top, whole ? 1.2 : 0.7, len);\n        if (!whole) ctx.fillRect(-0.35, top + face - len * 0.8, 0.7, len * 0.8);\n        if (n > 0 && ten) {\n          ctx.font = `800 ${h * 0.4}px ui-sans-serif, system-ui, Helvetica, Arial, sans-serif`;\n          ctx.fillStyle = \"#b3190f\"; ctx.fillText(String(n + MIN), 0, h * 0.14);\n        } else if (n > 0 && whole && (five || ppu >= 14)) {\n          ctx.font = `700 ${h * (five ? 0.29 : 0.24)}px ui-sans-serif, system-ui, Helvetica, Arial, sans-serif`;\n          ctx.fillStyle = \"rgba(24,20,8,0.9)\"; ctx.fillText(String(five ? n + MIN : n % 10), 0, h * 0.2);\n        }\n        ctx.restore();\n      }\n      // A thin run of light along the curl of the steel, and the shade the case throws on the first inch.\n      ctx.lineCap = \"round\";\n      for (const [off, wd, col] of [[-0.19, 1.6, \"rgba(255,255,255,0.55)\"], [0.39, 0.8, \"rgba(255,255,255,0.28)\"]] as [number, number, string][]) {\n        ctx.beginPath();\n        for (let x = 0; x <= L; x += 6) { const a = Math.atan(c.slope(x)); ctx.lineTo(mx + x - Math.sin(a) * h * off, my + c.y(x) + Math.cos(a) * h * off); }\n        ctx.lineWidth = wd * k; ctx.strokeStyle = col; ctx.stroke();\n      }\n      const shade = ctx.createLinearGradient(mx, 0, mx + 46 * k, 0);\n      shade.addColorStop(0, \"rgba(20,12,0,0.5)\"); shade.addColorStop(1, \"rgba(20,12,0,0)\");\n      ctx.fillStyle = shade; ctx.fillRect(mx, my - h / 2 - 1, Math.min(L, 46 * k), h + 4);\n    };\n\n    const drawHook = (L: number, c: ReturnType<typeof curve>) => {\n      const h = BH;\n      ctx.save(); ctx.translate(mx + L, my + c.y(L)); ctx.rotate(Math.atan(c.slope(L)));\n      const steel = ctx.createLinearGradient(0, -h / 2, 0, h / 2);\n      steel.addColorStop(0, \"#f4f5f6\"); steel.addColorStop(0.35, \"#c9cdd2\"); steel.addColorStop(0.7, \"#9097a0\"); steel.addColorStop(1, \"#5c626a\");\n      // The riveted tongue on top of the blade.\n      rr(-24 * k, -h / 2 + h * 0.16, 25 * k, h * 0.64, [3 * k, 0, 0, 3 * k]); ctx.fillStyle = steel; ctx.fill();\n      ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(40,44,50,0.65)\"; ctx.stroke();\n      ctx.fillStyle = \"rgba(255,255,255,0.7)\"; ctx.fillRect(-23 * k, -h / 2 + h * 0.18, 22 * k, 0.9);\n      for (const ry of [-0.12, 0.2]) {\n        const g = ctx.createRadialGradient(-13 * k - 0.8, h * ry - 0.8, 0.2, -13 * k, h * ry, 3.1 * k);\n        g.addColorStop(0, \"#ffffff\"); g.addColorStop(0.5, \"#aab0b8\"); g.addColorStop(1, \"#4b5058\");\n        ctx.beginPath(); ctx.arc(-13 * k, h * ry, 3 * k, 0, Math.PI * 2); ctx.fillStyle = g; ctx.fill();\n      }\n      // The end tab that catches the edge of the work.\n      const tab = ctx.createLinearGradient(-1, 0, 6 * k, 0);\n      tab.addColorStop(0, \"#f7f8f9\"); tab.addColorStop(0.3, \"#d5d9de\"); tab.addColorStop(1, \"#6c727b\");\n      rr(-0.5, -h / 2 - 2 * k, 5.5 * k, h + 17 * k, [1.5, 1.5, 3 * k, 3 * k]); ctx.fillStyle = tab; ctx.fill();\n      ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(40,44,50,0.7)\"; ctx.stroke();\n      ctx.restore();\n    };\n\n    const screw = (x: number, y: number, turn: number) => {\n      ctx.beginPath(); ctx.arc(x, y, 6.6, 0, Math.PI * 2); ctx.fillStyle = \"rgba(60,36,0,0.5)\"; ctx.fill();\n      ctx.beginPath(); ctx.arc(x, y + 0.7, 6.3, 0, Math.PI * 2); ctx.fillStyle = \"rgba(255,240,180,0.55)\"; ctx.fill();\n      const g = ctx.createRadialGradient(x - 1.6, y - 2, 0.4, x, y, 5.4);\n      g.addColorStop(0, \"#fbfbfc\"); g.addColorStop(0.55, \"#aeb3ba\"); g.addColorStop(1, \"#555b63\");\n      ctx.beginPath(); ctx.arc(x, y, 5.3, 0, Math.PI * 2); ctx.fillStyle = g; ctx.fill();\n      ctx.save(); ctx.translate(x, y); ctx.rotate(turn); ctx.fillStyle = \"rgba(20,22,26,0.82)\";\n      ctx.fillRect(-3.5, -0.75, 7, 1.5); ctx.fillRect(-0.75, -3.5, 1.5, 7); ctx.restore();\n    };\n\n    const drawCase = () => {\n      ctx.save(); ctx.translate(cx0 + s.kick, cy0); ctx.scale(k, k);\n      // Rubber jacket.\n      rr(0, 0, CW, CH, OUT);\n      const rub = ctx.createLinearGradient(0, 0, CW * 0.6, CH);\n      rub.addColorStop(0, \"#3d3e42\"); rub.addColorStop(0.35, \"#232427\"); rub.addColorStop(1, \"#0f0f11\");\n      ctx.fillStyle = rub; ctx.fill();\n      if (speckle) { ctx.save(); ctx.clip(); ctx.globalAlpha = 0.5; ctx.fillStyle = speckle; ctx.fillRect(0, 0, CW, CH); ctx.restore(); }\n      rr(0.8, 0.8, CW - 1.6, CH - 1.6, OUT);\n      const rim = ctx.createLinearGradient(0, 0, CW * 0.5, CH);\n      rim.addColorStop(0, \"rgba(255,255,255,0.5)\"); rim.addColorStop(0.3, \"rgba(255,255,255,0.06)\"); rim.addColorStop(1, \"rgba(0,0,0,0.6)\");\n      ctx.lineWidth = 1.6; ctx.strokeStyle = rim; ctx.stroke();\n      // Grip ribs moulded into the heel.\n      for (let i = 0; i < 6; i++) {\n        const x = 16 + i * 8.5;\n        ctx.fillStyle = \"rgba(0,0,0,0.6)\"; ctx.fillRect(x, CH - 30, 3.2, 20);\n        ctx.fillStyle = \"rgba(255,255,255,0.13)\"; ctx.fillRect(x + 3.2, CH - 30, 1, 20);\n      }\n      // The plastic shell that shows through the jacket.\n      rr(11, 10, CW - 22, CH - 56, [41, 35, 12, 12]);\n      const sh = ctx.createLinearGradient(20, 10, CW - 20, CH - 40);\n      sh.addColorStop(0, SHELL[0]); sh.addColorStop(0.45, SHELL[1]); sh.addColorStop(1, SHELL[2]);\n      ctx.fillStyle = sh; ctx.fill();\n      ctx.save(); ctx.clip();\n      if (speckle) { ctx.globalAlpha = 0.22; ctx.fillStyle = speckle; ctx.fillRect(0, 0, CW, CH); ctx.globalAlpha = 1; }\n      const gloss = ctx.createRadialGradient(46, 22, 4, 46, 22, 120);\n      gloss.addColorStop(0, \"rgba(255,255,255,0.55)\"); gloss.addColorStop(0.4, \"rgba(255,255,255,0.1)\"); gloss.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = gloss; ctx.fillRect(0, 0, CW, CH);\n      const under = ctx.createLinearGradient(0, CH - 80, 0, CH - 46);\n      under.addColorStop(0, \"rgba(120,60,0,0)\"); under.addColorStop(1, \"rgba(120,60,0,0.3)\");\n      ctx.fillStyle = under; ctx.fillRect(0, CH - 80, CW, 40);\n      ctx.restore();\n      rr(11.8, 10.8, CW - 23.6, CH - 57.6, [40, 34, 11, 11]);\n      const bev = ctx.createLinearGradient(11, 10, CW - 30, CH - 40);\n      bev.addColorStop(0, \"rgba(255,255,255,0.85)\"); bev.addColorStop(0.35, \"rgba(255,255,255,0.1)\"); bev.addColorStop(1, \"rgba(90,50,0,0.55)\");\n      ctx.lineWidth = 1.5; ctx.strokeStyle = bev; ctx.stroke();\n      rr(10.4, 9.4, CW - 20.8, CH - 54.8, [41.5, 35.5, 12.5, 12.5]);\n      ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(0,0,0,0.7)\"; ctx.stroke();\n\n      // Centre badge: a spun steel ring around a black label.\n      const bx = 72, by = 62;\n      ctx.save(); ctx.shadowColor = \"rgba(70,40,0,0.5)\"; ctx.shadowBlur = 7 * dpr; ctx.shadowOffsetY = 3 * dpr;\n      ctx.beginPath(); ctx.arc(bx, by, 37, 0, Math.PI * 2); ctx.fillStyle = \"#777\"; ctx.fill(); ctx.restore();\n      let ring: CanvasGradient;\n      if (typeof ctx.createConicGradient === \"function\") {\n        ring = ctx.createConicGradient(-2.3, bx, by);\n        for (const [p, v] of [[0, 250], [0.08, 190], [0.2, 96], [0.32, 170], [0.45, 240], [0.5, 205], [0.58, 110], [0.72, 80], [0.85, 160], [0.94, 228], [1, 250]] as [number, number][]) ring.addColorStop(p, `rgb(${v - 3},${v},${v + 4})`);\n      } else { ring = ctx.createLinearGradient(bx - 37, by - 37, bx + 37, by + 37); ring.addColorStop(0, \"#f4f5f6\"); ring.addColorStop(1, \"#6a6f77\"); }\n      ctx.beginPath(); ctx.arc(bx, by, 37, 0, Math.PI * 2); ctx.fillStyle = ring; ctx.fill();\n      ctx.lineWidth = 0.8; ctx.strokeStyle = \"rgba(0,0,0,0.5)\"; ctx.stroke();\n      const disc = ctx.createRadialGradient(bx - 10, by - 14, 2, bx, by, 32);\n      disc.addColorStop(0, \"#3a3b3f\"); disc.addColorStop(0.6, \"#18181a\"); disc.addColorStop(1, \"#0b0b0c\");\n      ctx.beginPath(); ctx.arc(bx, by, 31, 0, Math.PI * 2); ctx.fillStyle = disc; ctx.fill();\n      ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(0,0,0,0.85)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(bx, by, 26.5, 0, Math.PI * 2); ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(244,185,12,0.75)\"; ctx.stroke();\n      ctx.textAlign = \"center\"; ctx.textBaseline = \"middle\"; ctx.fillStyle = SHELL[1];\n      ctx.font = \"800 21px ui-sans-serif, system-ui, Helvetica, Arial, sans-serif\"; ctx.fillText(String(MAX), bx, by - 3);\n      ctx.font = \"700 6.5px ui-monospace, SFMono-Regular, Menlo, monospace\"; ctx.fillStyle = \"rgba(255,255,255,0.72)\";\n      ctx.fillText(UNIT.toUpperCase() + \"  CLASS II\", bx, by + 13);\n      const glint = ctx.createLinearGradient(bx - 30, by - 30, bx + 10, by + 6);\n      glint.addColorStop(0, \"rgba(255,255,255,0.2)\"); glint.addColorStop(0.5, \"rgba(255,255,255,0)\");\n      ctx.beginPath(); ctx.arc(bx, by, 31, 0, Math.PI * 2); ctx.fillStyle = glint; ctx.fill();\n\n      screw(28, 100, 0.5); screw(132, 100, 1.2); screw(120, 22, 0.2);\n\n      // Steel guard where the blade leaves the case.\n      const gd = ctx.createLinearGradient(CW - 15, 0, CW + 3, 0);\n      gd.addColorStop(0, \"#8d939b\"); gd.addColorStop(0.35, \"#eef0f2\"); gd.addColorStop(0.7, \"#b9bec5\"); gd.addColorStop(1, \"#5d636b\");\n      rr(CW - 15, CH - 45, 18, 41, [2, 2, 2, 2]); ctx.fillStyle = gd; ctx.fill();\n      ctx.lineWidth = 0.8; ctx.strokeStyle = \"rgba(0,0,0,0.6)\"; ctx.stroke();\n      ctx.fillStyle = \"#0a0a0b\"; ctx.fillRect(CW - 0.5, CH - 41, 3.5, 33);\n\n      // Thumb lock: a slot in the shoulder and a ribbed tab that slides down to pinch the blade.\n      rr(CW - 31, 31, 14, 48, [7, 7, 7, 7]); ctx.fillStyle = \"#070708\"; ctx.fill();\n      ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(255,255,255,0.16)\"; ctx.stroke();\n      const ty = 30 + s.lockPos * 25;\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.6)\"; ctx.shadowBlur = 5 * dpr; ctx.shadowOffsetY = 2.5 * dpr;\n      rr(CW - 37, ty, 43, 25, [6, 9, 9, 6]);\n      const tg = ctx.createLinearGradient(0, ty, 0, ty + 25);\n      tg.addColorStop(0, \"#ff8a7c\"); tg.addColorStop(0.16, TAB); tg.addColorStop(0.75, \"#b7241a\"); tg.addColorStop(1, \"#7f150e\");\n      ctx.fillStyle = tg; ctx.fill(); ctx.restore();\n      for (let i = 0; i < 4; i++) {\n        ctx.fillStyle = \"rgba(70,0,0,0.6)\"; ctx.fillRect(CW - 30, ty + 5 + i * 4.4, 28, 1.5);\n        ctx.fillStyle = \"rgba(255,255,255,0.35)\"; ctx.fillRect(CW - 30, ty + 6.5 + i * 4.4, 28, 0.7);\n      }\n      ctx.font = \"700 6px ui-monospace, SFMono-Regular, Menlo, monospace\"; ctx.textAlign = \"right\"; ctx.fillStyle = \"rgba(60,36,0,0.8)\";\n      ctx.fillText(\"LOCK\", CW - 41, 87);\n      ctx.beginPath(); ctx.moveTo(CW - 54, 92); ctx.lineTo(CW - 46, 92); ctx.lineTo(CW - 50, 97); ctx.closePath(); ctx.fill();\n      ctx.restore();\n    };\n\n    const draw = (t: number) => {\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.clearRect(0, 0, W, H);\n      const L = Math.max(0, (s.u - MIN) * ppu), c = curve(L, t);\n      // Shadows on the backdrop a hand's width behind: the case, then the blade.\n      ctx.save();\n      ctx.shadowColor = \"rgba(70,52,20,0.34)\"; ctx.shadowBlur = 34 * dpr; ctx.shadowOffsetX = 14 * dpr; ctx.shadowOffsetY = 30 * dpr;\n      ctx.translate(cx0 + s.kick, cy0); ctx.scale(k, k); rr(4, 4, CW - 8, CH - 8, OUT); ctx.fillStyle = \"#222\"; ctx.fill();\n      ctx.restore();\n      if (L > 2) {\n        ctx.save();\n        ctx.shadowColor = \"rgba(70,52,20,0.3)\"; ctx.shadowBlur = 20 * dpr; ctx.shadowOffsetX = 12 * dpr; ctx.shadowOffsetY = 34 * dpr;\n        ctx.beginPath(); for (let x = 0; x <= L; x += 8) ctx.lineTo(mx + x, my + c.y(x)); ctx.lineTo(mx + L, my + c.y(L));\n        ctx.lineWidth = BH * 0.5; ctx.strokeStyle = \"#caa21c\"; ctx.stroke();\n        ctx.restore();\n      }\n      drawBlade(L, c);\n      drawHook(L, c);\n      drawCase();\n    };\n\n    let raf = 0, last = 0, t0 = 0, seen = true, touched = -1e9, demoAt = 0, step = 0;\n    // The self-running demo: it opens on the finished pull, then shows the lock and the rewind.\n    const script: [number, () => void][] = [\n      [650, () => { s.target = START; }],\n      [4300, () => { s.locked = false; s.held = false; }],\n      [5900, () => { s.held = true; s.target = MIN + (MAX - MIN) * 0.38; }],\n      [7300, () => { s.locked = true; s.held = false; }],\n      [8700, () => { s.target = START; }],\n      [12600, () => { demoAt += 8300; step = 1; }],\n    ];\n    const publish = () => {\n      const v = s.keyed ? s.target : snap(s.u); // a key press shows its result at once, the blade catches up\n      if (v !== s.shown) { s.shown = v; setValue(v); }\n      if (s.locked !== s.lockShown) { s.lockShown = s.locked; setLocked(s.locked); }\n    };\n\n    const frame = (now: number) => {\n      raf = 0;\n      if (!t0) { t0 = now; last = now; demoAt = now; }\n      const dt = Math.min(0.033, (now - last) / 1000); last = now;\n      if (DEMO && now - touched > 6000) {\n        if (touched > 0) { touched = -1e9; s.keyed = false; demoAt = now - 7300; step = 4; s.locked = true; s.held = false; s.target = snap(s.u); }\n        while (step < script.length && now - demoAt >= script[step][0]) { const run = script[step][1]; step++; run(); }\n      }\n      const before = s.uv;\n      if (s.drag) {\n        const nu = s.u + (s.raw - s.u) * Math.min(1, dt * 28);\n        s.uv = (nu - s.u) / Math.max(dt, 0.001); s.u = nu;\n      } else if (s.locked || s.held) {\n        s.uv += (120 * (s.target - s.u) - 17 * s.uv) * dt; s.u += s.uv * dt;\n      } else if (s.u > MIN || s.uv !== 0) {\n        // Lock off and nobody holding: the spring in the case wins.\n        s.uv += (-26 * (s.u - MIN) - 1.6 * s.uv - (MAX - MIN) * 4.2) * dt; s.u += s.uv * dt;\n        if (s.u <= MIN) { s.kickV += s.uv * ppu * 0.05; s.wobV -= 60; s.u = MIN; s.uv = Math.abs(s.uv) > (MAX - MIN) * 0.5 ? -s.uv * 0.16 : 0; s.target = MIN; }\n      }\n      s.wobV += (-150 * s.wob - 7 * s.wobV) * dt + (s.uv - before) * ppu * 0.05;\n      s.wob = Math.max(-46, Math.min(46, s.wob + s.wobV * dt));\n      s.kickV += (-700 * s.kick - 30 * s.kickV) * dt; s.kick += s.kickV * dt;\n      s.lockPos += ((s.locked ? 1 : 0) - s.lockPos) * Math.min(1, dt * 22);\n      publish();\n      draw(now - t0);\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    const still = () => {\n      if (!s.locked && !s.drag && !s.held) s.target = MIN;\n      if (!s.drag) s.u = s.target;\n      s.uv = 0; s.wob = 0; s.kick = 0; s.lockPos = s.locked ? 1 : 0; publish(); draw(0);\n    };\n    const refresh = () => { if (reduce) still(); else wake(); };\n\n    api.current = {\n      // Arrow keys always engage the lock, so a keyboard user never watches a value rewind.\n      set: (v) => { touched = performance.now(); s.locked = true; s.held = false; s.keyed = true; s.target = snap(v); publish(); refresh(); },\n      lock: (on) => { touched = performance.now(); s.locked = on; s.held = false; s.keyed = false; if (on) s.target = snap(s.u); refresh(); },\n    };\n\n    layout();\n    if (reduce) { s.u = s.target = START; still(); } else wake();\n\n    let startX = 0, startU = 0;\n    const onDown = (e: PointerEvent) => {\n      touched = performance.now(); s.drag = true; s.keyed = false; startX = e.clientX; startU = s.u; s.raw = s.u;\n      cv.setPointerCapture(e.pointerId); wake();\n    };\n    const onMove = (e: PointerEvent) => {\n      if (!s.drag) return;\n      touched = performance.now();\n      let u = startU + (e.clientX - startX) / ppu;\n      if (u > MAX) u = MAX + (u - MAX) * 0.12; // the blade is riveted to the drum, so the end gives only a little\n      if (u < MIN) u = MIN;\n      s.raw = u;\n      if (reduce) { s.u = u; still(); }\n    };\n    const onUp = () => {\n      if (!s.drag) return;\n      s.drag = false; touched = performance.now(); s.target = snap(s.u);\n      if (reduce) still();\n    };\n    cv.addEventListener(\"pointerdown\", onDown);\n    cv.addEventListener(\"pointermove\", onMove);\n    cv.addEventListener(\"pointerup\", onUp);\n    cv.addEventListener(\"pointercancel\", onUp);\n    const ro = new ResizeObserver(() => { layout(); if (reduce) still(); else { draw(performance.now() - t0); wake(); } });\n    ro.observe(cv);\n    const io = new IntersectionObserver(([entry]) => { seen = entry.isIntersecting; if (seen) wake(); });\n    io.observe(cv);\n    const onVis = () => { if (!document.hidden) wake(); };\n    document.addEventListener(\"visibilitychange\", onVis);\n    return () => {\n      cancelAnimationFrame(raf); api.current = null;\n      cv.removeEventListener(\"pointerdown\", onDown); cv.removeEventListener(\"pointermove\", onMove);\n      cv.removeEventListener(\"pointerup\", onUp); cv.removeEventListener(\"pointercancel\", onUp);\n      ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  return (\n    <div className=\"flex min-h-[620px] w-full flex-col items-center justify-center bg-[#e9e3d6] bg-[radial-gradient(90%_70%_at_38%_30%,#f6f1e7_0%,#e9e3d6_55%,#dcd4c3_100%)] px-3 py-10 sm:min-h-[740px] sm:px-8\">\n      <div className=\"relative w-full max-w-[920px]\">\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"block h-[300px] w-full cursor-grab touch-pan-y select-none active:cursor-grabbing sm:h-[470px]\" />\n        <button\n          ref={lockBtn}\n          type=\"button\"\n          aria-pressed={locked}\n          aria-label=\"Blade lock\"\n          onClick={() => api.current?.lock(!locked)}\n          className=\"absolute h-16 w-12 rounded-xl outline-none focus-visible:ring-2 focus-visible:ring-stone-900 focus-visible:ring-offset-2 focus-visible:ring-offset-[#f4b90c]\"\n        />\n      </div>\n      <div className=\"flex w-full max-w-[920px] items-end justify-between gap-4 px-2 sm:px-9\">\n        <div>\n          <input\n            id=\"tape-measure-range\"\n            type=\"range\"\n            min={MIN}\n            max={MAX}\n            step={STEP}\n            value={value}\n            aria-valuetext={`${value} ${UNIT}`}\n            onChange={(e) => api.current?.set(Number(e.target.value))}\n            className=\"peer sr-only\"\n          />\n          <label htmlFor=\"tape-measure-range\" className=\"block rounded-md px-1 font-mono text-[11px] font-semibold uppercase tracking-[0.2em] text-stone-600 peer-focus-visible:ring-2 peer-focus-visible:ring-stone-900\">\n            {LABEL}\n          </label>\n          <output htmlFor=\"tape-measure-range\" className=\"mt-1 block px-1 text-5xl font-semibold tabular-nums tracking-tight text-stone-900 sm:text-6xl\">\n            {value}\n            <span className=\"ml-2 text-xl font-medium text-stone-500 sm:text-2xl\">{UNIT}</span>\n          </output>\n        </div>\n        <p aria-live=\"polite\" className=\"max-w-[190px] pb-2 text-right font-mono text-[11px] uppercase leading-5 tracking-[0.14em] text-stone-600\">\n          <span className=\"text-stone-900\">{locked ? \"Lock on\" : \"Lock off\"}</span>\n          <br />\n          {locked ? \"Pull the blade or use the arrow keys\" : \"Let go and it rewinds to zero\"}\n        </p>\n      </div>\n    </div>\n  );\n}\n"}]}
