{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"speedometer-and-rev-counter-pair-in-a-stitched-leather-binnacle-with-a-start-up-needle-sweep-and-a-working-throttle","type":"registry:component","title":"Speedometer and Rev Counter Pair in a Stitched Leather Binnacle with a Start-Up Needle Sweep and a Working Throttle","description":"Instrument cluster on a canvas: a speedometer and rev counter under glass, with a start-up needle sweep and a working throttle. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/speedometer-and-rev-counter-pair-in-a-stitched-leather-binnacle-with-a-start-up-needle-sweep-and-a-working-throttle.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Everything a visitor is likely to change sits here.\nconst TOP_SPEED = 160; // last number on the speedometer, in the unit below\nconst UNIT = \"MPH\";\nconst REDLINE = 6500; // the red band on the rev counter starts here\nconst MAX_RPM = 8000;\nconst RATIOS = [118, 74, 52, 40, 33, 27]; // engine revs per unit of speed in each gear\nconst PULL = [26, 17, 12, 8.5, 6.2, 4.6]; // how hard each gear accelerates at full throttle\nconst NEEDLE = \"#ff4a1f\"; // needle paint, start-button halo and focus rings\nconst ODOMETER = 48213.4; // reading on load\nconst lamps = [\n  { label: \"OIL\", color: \"#ff3b30\" },\n  { label: \"GEN\", color: \"#ffb020\" },\n  { label: \"SHIFT\", color: \"#ffd84a\" },\n];\n\nconst A0 = (135 * Math.PI) / 180, SPAN = (270 * Math.PI) / 180; // both needles sweep three quarters of a turn\nconst layoutFor = (narrow: boolean) =>\n  narrow\n    ? { W: 380, H: 780, rev: { x: 190, y: 178 }, spd: { x: 190, y: 494 }, lamp: [{ x: 62, y: 716 }, { x: 110, y: 716 }, { x: 158, y: 716 }], btn: { x: 290, y: 714 }, top: 150 }\n    : { W: 790, H: 430, rev: { x: 205, y: 222 }, spd: { x: 585, y: 222 }, lamp: [{ x: 395, y: 108 }, { x: 395, y: 150 }, { x: 395, y: 192 }], btn: { x: 395, y: 322 }, top: 190 };\n\nexport default function SpeedometerAndRevCounterPair() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const pedal = useRef<HTMLInputElement>(null);\n  const [narrow, setNarrow] = useState(false);\n  const [on, setOn] = useState(true);\n  const live = useRef({ on: true, thr: 0.5 });\n  const api = useRef<{ engine: (v: boolean) => void; throttle: (v: number) => void; press: (v: boolean) => void } | null>(null);\n\n  useEffect(() => {\n    const el = stage.current;\n    if (!el) return;\n    const ro = new ResizeObserver(() => setNarrow(el.clientWidth < 640));\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n\n  useEffect(() => {\n    const cv = canvas.current;\n    if (!cv) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const L = layoutFor(narrow);\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const hasConic = typeof ctx.createConicGradient === \"function\";\n    let k = 1;\n    const RB = 150, RF = 124; // bezel and face radius\n    const base = document.createElement(\"canvas\"), glass = document.createElement(\"canvas\");\n    const SANS = \"ui-sans-serif, system-ui, 'Helvetica Neue', Arial, sans-serif\", MONO = \"ui-monospace, SFMono-Regular, Menlo, monospace\";\n\n    const hood = (c: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, rt: number, rb: number) => {\n      c.beginPath(); c.moveTo(x + rt, y); c.arcTo(x + w, y, x + w, y + h, rt); c.arcTo(x + w, y + h, x, y + h, rb); c.arcTo(x, y + h, x, y, rb); c.arcTo(x, y, x + w, y, rt); c.closePath();\n    };\n    const rr = (c: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, r: number) => hood(c, x, y, w, h, r, r);\n    const chrome = (c: CanvasRenderingContext2D, x: number, y: number, turn: number, r: number) => {\n      if (!hasConic) { const g = c.createLinearGradient(x - r, y - r, x + r, y + r); g.addColorStop(0, \"#f6f7f8\"); g.addColorStop(0.4, \"#7d8187\"); g.addColorStop(0.6, \"#e3e5e8\"); g.addColorStop(1, \"#44474c\"); return g; }\n      const g = c.createConicGradient(turn, x, y);\n      for (const [p, v] of [[0, \"#f8f9fa\"], [0.06, \"#c9ccd1\"], [0.13, \"#6b6f75\"], [0.2, \"#2d2f33\"], [0.27, \"#8c9096\"], [0.36, \"#eceef0\"], [0.44, \"#a4a8ae\"], [0.5, \"#4b4e53\"], [0.56, \"#9a9ea4\"], [0.64, \"#f3f4f6\"], [0.72, \"#b6b9bf\"], [0.79, \"#3b3d42\"], [0.86, \"#7b7f85\"], [0.94, \"#dfe1e4\"], [1, \"#f8f9fa\"]] as [number, string][]) g.addColorStop(p, v);\n      return g;\n    };\n    const polar = (d: { x: number; y: number }, f: number, r: number) => { const a = A0 + SPAN * f; return { x: d.x + Math.cos(a) * r, y: d.y + Math.sin(a) * r }; };\n\n    const dial = (b: CanvasRenderingContext2D, d: { x: number; y: number }, kind: \"rev\" | \"spd\") => {\n      // Shadow on the hide, then a bezel turned like a ring: bright shoulder, dark throat.\n      b.save(); b.shadowColor = \"rgba(0,0,0,0.85)\"; b.shadowBlur = 22 * k; b.shadowOffsetY = 12 * k;\n      b.beginPath(); b.arc(d.x, d.y, RB - 1, 0, Math.PI * 2); b.fillStyle = \"#000\"; b.fill(); b.restore();\n      b.beginPath(); b.arc(d.x, d.y, RB, 0, Math.PI * 2); b.fillStyle = chrome(b, d.x, d.y, -2.3, RB); b.fill();\n      b.beginPath(); b.arc(d.x, d.y, RB - 9, 0, Math.PI * 2); b.fillStyle = chrome(b, d.x, d.y, 0.9, RB); b.fill();\n      const lipG = b.createLinearGradient(d.x, d.y - RB, d.x, d.y + RB); lipG.addColorStop(0, \"rgba(255,255,255,0.95)\"); lipG.addColorStop(0.5, \"rgba(255,255,255,0.25)\"); lipG.addColorStop(1, \"rgba(0,0,0,0.5)\");\n      b.beginPath(); b.arc(d.x, d.y, RB - 9, 0, Math.PI * 2); b.lineWidth = 1; b.strokeStyle = lipG; b.stroke();\n      b.beginPath(); b.arc(d.x, d.y, RB - 0.5, 0, Math.PI * 2); b.lineWidth = 1; b.strokeStyle = \"rgba(0,0,0,0.6)\"; b.stroke();\n      const throat = b.createLinearGradient(d.x - RF, d.y - RF, d.x + RF, d.y + RF); throat.addColorStop(0, \"#020202\"); throat.addColorStop(0.55, \"#101113\"); throat.addColorStop(1, \"#4a4d52\");\n      b.beginPath(); b.arc(d.x, d.y, RF + 8, 0, Math.PI * 2); b.fillStyle = throat; b.fill();\n      const face = b.createRadialGradient(d.x, d.y - 30, 10, d.x, d.y, RF);\n      face.addColorStop(0, \"#1e1f22\"); face.addColorStop(0.7, \"#131416\"); face.addColorStop(1, \"#0b0b0d\");\n      b.beginPath(); b.arc(d.x, d.y, RF, 0, Math.PI * 2); b.fillStyle = face; b.fill();\n      // The bezel shades the top of the face.\n      b.save(); b.beginPath(); b.arc(d.x, d.y, RF, 0, Math.PI * 2); b.clip();\n      b.beginPath(); b.arc(d.x, d.y, RF + 6, 0, Math.PI * 2); b.arc(d.x + 4, d.y + 12, RF + 2, 0, Math.PI * 2, true);\n      b.shadowColor = \"rgba(0,0,0,0.9)\"; b.shadowBlur = 12 * k; b.fillStyle = \"rgba(0,0,0,0.85)\"; b.fill(\"evenodd\"); b.restore();\n      // A fine turned ring inside the numerals.\n      b.beginPath(); b.arc(d.x, d.y, 62, 0, Math.PI * 2); b.lineWidth = 1; b.strokeStyle = \"rgba(255,255,255,0.07)\"; b.stroke();\n      b.beginPath(); b.arc(d.x, d.y, 63, 0, Math.PI * 2); b.lineWidth = 1; b.strokeStyle = \"rgba(0,0,0,0.6)\"; b.stroke();\n\n      const INK = \"#efe8d6\", max = kind === \"rev\" ? MAX_RPM / 1000 : TOP_SPEED, major = kind === \"rev\" ? 1 : 20, minors = kind === \"rev\" ? 10 : 4;\n      const red = kind === \"rev\" ? REDLINE / 1000 : Infinity;\n      if (kind === \"rev\") {\n        b.beginPath(); b.arc(d.x, d.y, 113.5, A0 + SPAN * (red / max), A0 + SPAN); b.lineWidth = 7; b.strokeStyle = \"#c8261c\"; b.stroke();\n      }\n      b.lineCap = \"butt\";\n      const n = Math.round(max / major) * minors;\n      for (let i = 0; i <= n; i++) {\n        const v = (i / minors) * major, f = v / max, big = i % minors === 0, mid = !big && i % (minors / 2) === 0;\n        const p0 = polar(d, f, big ? 101 : mid ? 106 : 110), p1 = polar(d, f, 117);\n        b.beginPath(); b.moveTo(p0.x, p0.y); b.lineTo(p1.x, p1.y); b.lineWidth = big ? 2.8 : mid ? 1.8 : 1.1;\n        b.strokeStyle = v >= red && !big ? \"#ffd9d2\" : big ? INK : mid ? \"rgba(239,232,214,0.85)\" : \"rgba(239,232,214,0.55)\"; b.stroke();\n        if (big) {\n          const q = polar(d, f, kind === \"rev\" ? 83 : 80);\n          b.font = `700 ${kind === \"rev\" ? 22 : 15}px ${SANS}`; b.textAlign = \"center\"; b.textBaseline = \"middle\";\n          b.fillStyle = v >= red ? \"#ff6a5c\" : INK; b.fillText(String(v), q.x, q.y + 1);\n        }\n      }\n      b.font = `600 8px ${MONO}`; b.textAlign = \"center\"; b.textBaseline = \"middle\"; b.fillStyle = \"rgba(239,232,214,0.6)\";\n      if (kind === \"rev\") { b.fillText(\"RPM  x1000\", d.x, d.y - 42); b.fillText(\"GEAR\", d.x, d.y + 86); } else { b.fillText(UNIT, d.x, d.y - 42); b.fillText(\"MILES\", d.x, d.y + 72); }\n      // Recess for the gear window or the odometer drums.\n      const w = kind === \"rev\" ? 30 : 74, h = kind === \"rev\" ? 28 : 21, wy = d.y + (kind === \"rev\" ? 44 : 38);\n      rr(b, d.x - w / 2 - 2.5, wy - 2.5, w + 5, h + 5, 4.5); b.fillStyle = \"#000\"; b.fill();\n      const wl = b.createLinearGradient(0, wy - 3, 0, wy + h + 3); wl.addColorStop(0, \"rgba(0,0,0,1)\"); wl.addColorStop(1, \"rgba(150,152,158,0.7)\");\n      rr(b, d.x - w / 2 - 2.5, wy - 2.5, w + 5, h + 5, 4.5); b.lineWidth = 1; b.strokeStyle = wl; b.stroke();\n    };\n\n    const jewelSeat = (b: CanvasRenderingContext2D, x: number, y: number, r: number) => {\n      b.save(); b.shadowColor = \"rgba(0,0,0,0.8)\"; b.shadowBlur = 5 * k; b.shadowOffsetY = 2.5 * k;\n      b.beginPath(); b.arc(x, y, r, 0, Math.PI * 2); b.fillStyle = chrome(b, x, y, -2.2, r); b.fill(); b.restore();\n      b.beginPath(); b.arc(x, y, r * 0.72, 0, Math.PI * 2); b.fillStyle = \"#060606\"; b.fill();\n    };\n\n    const build = () => {\n      const b = base.getContext(\"2d\"), g = glass.getContext(\"2d\");\n      if (!b || !g) return;\n      base.width = glass.width = cv.width; base.height = glass.height = cv.height;\n      b.setTransform(k, 0, 0, k, 0, 0); g.setTransform(k, 0, 0, k, 0, 0);\n      let seed = 41;\n      const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n\n      // The binnacle: black hide stretched over a cowl, grained, with a cream saddle stitch.\n      const X = 8, Y = 8, BW = L.W - 16, BH = L.H - 20, RT = L.top, RBm = 44;\n      b.save(); b.shadowColor = \"rgba(0,0,0,0.8)\"; b.shadowBlur = 28 * k; b.shadowOffsetY = 16 * k;\n      hood(b, X, Y, BW, BH, RT, RBm); b.fillStyle = \"#0c0c0d\"; b.fill(); b.restore();\n      const hide = b.createLinearGradient(0, Y, 0, Y + BH);\n      hide.addColorStop(0, \"#3b3c40\"); hide.addColorStop(0.16, \"#27282b\"); hide.addColorStop(0.6, \"#1b1c1e\"); hide.addColorStop(1, \"#111113\");\n      hood(b, X, Y, BW, BH, RT, RBm); b.fillStyle = hide; b.fill();\n      b.save(); hood(b, X, Y, BW, BH, RT, RBm); b.clip();\n      const count = Math.round((L.W * L.H) / 7);\n      for (let i = 0; i < count; i++) {\n        // Pores, the pale shoulder beside each one, and now and then a short crease.\n        const x = rand() * L.W, y = rand() * L.H, r = 0.3 + rand() * 0.75;\n        b.beginPath(); b.arc(x, y, r, 0, Math.PI * 2); b.fillStyle = `rgba(0,0,0,${0.2 + rand() * 0.3})`; b.fill();\n        if (i % 2 === 0) { b.beginPath(); b.arc(x - 0.8, y - 0.9, r * 0.9, 0, Math.PI * 2); b.fillStyle = `rgba(255,255,255,${0.02 + rand() * 0.05})`; b.fill(); }\n        if (i % 22 === 0) { const a = rand() * Math.PI, n = 3 + rand() * 7; b.beginPath(); b.moveTo(x, y); b.quadraticCurveTo(x + Math.cos(a) * n * 0.5 + rand() * 3 - 1.5, y + Math.sin(a) * n * 0.5 + rand() * 3 - 1.5, x + Math.cos(a) * n, y + Math.sin(a) * n); b.lineWidth = 0.5; b.strokeStyle = `rgba(0,0,0,${0.18 + rand() * 0.2})`; b.stroke(); }\n      }\n      for (const [wd, al] of [[64, 0.1], [40, 0.12], [20, 0.16], [8, 0.2]]) { hood(b, X, Y, BW, BH, RT, RBm); b.lineWidth = wd; b.strokeStyle = `rgba(0,0,0,${al})`; b.stroke(); }\n      const sheen = b.createLinearGradient(0, Y, 0, Y + 120);\n      sheen.addColorStop(0, \"rgba(255,255,255,0.13)\"); sheen.addColorStop(0.35, \"rgba(255,255,255,0.04)\"); sheen.addColorStop(1, \"rgba(255,255,255,0)\");\n      b.fillStyle = sheen; b.fillRect(0, 0, L.W, 140);\n      const roll = b.createLinearGradient(0, Y, 0, Y + BH * 0.5); roll.addColorStop(0, \"rgba(255,255,255,0.2)\"); roll.addColorStop(1, \"rgba(255,255,255,0)\");\n      hood(b, X + 5, Y + 5, BW - 10, BH - 10, RT - 5, RBm - 5); b.lineWidth = 9; b.strokeStyle = roll; b.stroke();\n      b.restore();\n      const rim = b.createLinearGradient(0, Y, 0, Y + BH); rim.addColorStop(0, \"rgba(255,255,255,0.42)\"); rim.addColorStop(0.2, \"rgba(255,255,255,0.05)\"); rim.addColorStop(1, \"rgba(0,0,0,0.6)\");\n      hood(b, X + 0.7, Y + 0.7, BW - 1.4, BH - 1.4, RT - 0.7, RBm - 0.7); b.lineWidth = 1.4; b.strokeStyle = rim; b.stroke();\n\n      const SI = 15, sx = X + SI, sy = Y + SI, sw = BW - SI * 2, sh = BH - SI * 2, rt = RT - SI, rb = RBm - SI + 2;\n      hood(b, sx, sy + 1, sw, sh, rt, rb); b.lineWidth = 4.2; b.strokeStyle = \"rgba(255,255,255,0.07)\"; b.stroke();\n      hood(b, sx, sy, sw, sh, rt, rb); b.lineWidth = 3.4; b.strokeStyle = \"rgba(0,0,0,0.6)\"; b.stroke();\n      const pts: { x: number; y: number; a: number }[] = [];\n      const edge = (x0: number, y0: number, x1: number, y1: number) => { const n = Math.hypot(x1 - x0, y1 - y0), a = Math.atan2(y1 - y0, x1 - x0); for (let d = 0; d < n; d += 1) pts.push({ x: x0 + Math.cos(a) * d, y: y0 + Math.sin(a) * d, a }); };\n      const bend = (cx: number, cy: number, r: number, a0: number) => { for (let d = 0; d < (Math.PI / 2) * r; d += 1) { const a = a0 + d / r; pts.push({ x: cx + Math.cos(a) * r, y: cy + Math.sin(a) * r, a: a + Math.PI / 2 }); } };\n      edge(sx + rt, sy, sx + sw - rt, sy); bend(sx + sw - rt, sy + rt, rt, -Math.PI / 2);\n      edge(sx + sw, sy + rt, sx + sw, sy + sh - rb); bend(sx + sw - rb, sy + sh - rb, rb, 0);\n      edge(sx + sw - rb, sy + sh, sx + rb, sy + sh); bend(sx + rb, sy + sh - rb, rb, Math.PI / 2);\n      edge(sx, sy + sh - rb, sx, sy + rt); bend(sx + rt, sy + rt, rt, Math.PI);\n      b.lineCap = \"round\";\n      for (let i = 4; i < pts.length - 4; i += 9) {\n        const p = pts[i], ca = Math.cos(p.a + 0.2) * 3, sa = Math.sin(p.a + 0.2) * 3;\n        b.beginPath(); b.moveTo(p.x - ca + 0.6, p.y - sa + 1.3); b.lineTo(p.x + ca + 0.6, p.y + sa + 1.3); b.lineWidth = 2.4; b.strokeStyle = \"rgba(0,0,0,0.7)\"; b.stroke();\n        b.beginPath(); b.moveTo(p.x - ca, p.y - sa); b.lineTo(p.x + ca, p.y + sa); b.lineWidth = 1.9; b.strokeStyle = \"#d9c9a6\"; b.stroke();\n        b.beginPath(); b.moveTo(p.x - ca * 0.7, p.y - sa * 0.7 - 0.5); b.lineTo(p.x + ca * 0.5, p.y + sa * 0.5 - 0.5); b.lineWidth = 0.7; b.strokeStyle = \"rgba(255,255,255,0.5)\"; b.stroke();\n      }\n      b.lineCap = \"butt\";\n\n      dial(b, L.rev, \"rev\"); dial(b, L.spd, \"spd\");\n\n      // Lamp seats, their legends, and the ring of the start button.\n      lamps.forEach((lamp, i) => {\n        const p = L.lamp[i]; jewelSeat(b, p.x, p.y, 10.5);\n        b.font = `600 7px ${MONO}`; b.textAlign = \"center\"; b.textBaseline = \"middle\";\n        const ly = narrow ? p.y + 22 : p.y + 20;\n        b.fillStyle = \"rgba(0,0,0,0.8)\"; b.fillText(lamp.label, p.x, ly + 0.8); b.fillStyle = \"rgba(226,218,200,0.72)\"; b.fillText(lamp.label, p.x, ly);\n      });\n      b.save(); b.shadowColor = \"rgba(0,0,0,0.85)\"; b.shadowBlur = 9 * k; b.shadowOffsetY = 5 * k;\n      b.beginPath(); b.arc(L.btn.x, L.btn.y, 31, 0, Math.PI * 2); b.fillStyle = chrome(b, L.btn.x, L.btn.y, -2.3, 31); b.fill(); b.restore();\n      b.beginPath(); b.arc(L.btn.x, L.btn.y, 25.5, 0, Math.PI * 2); b.fillStyle = \"#050505\"; b.fill();\n\n      // Glass over each dial: a broad soft crescent, one hard streak of window, a faint rim opposite.\n      for (const d of [L.rev, L.spd]) {\n        g.save(); g.beginPath(); g.arc(d.x, d.y, RF + 7, 0, Math.PI * 2); g.clip();\n        const soft = g.createLinearGradient(d.x - RF, d.y - RF, d.x + RF * 0.3, d.y + RF * 0.4);\n        soft.addColorStop(0, \"rgba(255,255,255,0.2)\"); soft.addColorStop(0.5, \"rgba(255,255,255,0.05)\"); soft.addColorStop(1, \"rgba(255,255,255,0)\");\n        g.beginPath(); g.arc(d.x, d.y, RF + 7, 0, Math.PI * 2); g.arc(d.x + 34, d.y + 50, RF + 18, 0, Math.PI * 2, true); g.fillStyle = soft; g.fill(\"evenodd\");\n        g.lineCap = \"round\";\n        g.shadowColor = \"rgba(255,255,255,0.5)\"; g.shadowBlur = 6 * k;\n        g.beginPath(); g.arc(d.x, d.y, RF - 9, Math.PI * 1.14, Math.PI * 1.36); g.lineWidth = 5; g.strokeStyle = \"rgba(255,255,255,0.3)\"; g.stroke();\n        g.beginPath(); g.arc(d.x, d.y, RF - 9, Math.PI * 1.39, Math.PI * 1.43); g.lineWidth = 5; g.strokeStyle = \"rgba(255,255,255,0.22)\"; g.stroke();\n        g.shadowBlur = 0;\n        g.beginPath(); g.arc(d.x, d.y, RF + 2, Math.PI * 0.12, Math.PI * 0.42); g.lineWidth = 2; g.strokeStyle = \"rgba(255,255,255,0.1)\"; g.stroke();\n        g.restore();\n      }\n    };\n\n    const s = { phase: \"run\" as \"off\" | \"sweep\" | \"run\", pt: 0, v: 46, g: 2, rpm: 46 * RATIOS[2], cut: 0, fr: 0, frv: 0, fs: 0, fsv: 0, odo: ODOMETER, lamp: lamps.map(() => 0), halo: 1, press: 0, pressTo: 0, thr: 0.55 };\n    s.fr = s.rpm / MAX_RPM; s.fs = s.v / TOP_SPEED;\n\n    const needle = (d: { x: number; y: number }, f: number, len: number) => {\n      const a = A0 + SPAN * Math.max(-0.02, Math.min(1.03, f));\n      const shape = () => { ctx.beginPath(); ctx.moveTo(-26, -3.4); ctx.lineTo(len, -0.9); ctx.lineTo(len + 3, 0); ctx.lineTo(len, 0.9); ctx.lineTo(-26, 3.4); ctx.closePath(); };\n      ctx.save(); ctx.translate(d.x + 4, d.y + 8); ctx.rotate(a); shape(); ctx.shadowColor = \"rgba(0,0,0,0.7)\"; ctx.shadowBlur = 5 * k; ctx.fillStyle = \"rgba(0,0,0,0.5)\"; ctx.fill(); ctx.restore();\n      ctx.save(); ctx.translate(d.x, d.y); ctx.rotate(a);\n      shape(); ctx.fillStyle = NEEDLE; ctx.fill();\n      ctx.beginPath(); ctx.moveTo(-26, -3.4); ctx.lineTo(len, -0.9); ctx.lineTo(len, -0.1); ctx.lineTo(-26, -1.2); ctx.closePath(); ctx.fillStyle = \"rgba(255,255,255,0.32)\"; ctx.fill();\n      ctx.beginPath(); ctx.moveTo(-26, 3.4); ctx.lineTo(len, 0.9); ctx.lineTo(len, 0.2); ctx.lineTo(-26, 1.6); ctx.closePath(); ctx.fillStyle = \"rgba(0,0,0,0.22)\"; ctx.fill();\n      ctx.restore();\n      // Hub cap: a black dome with one window in it.\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.8)\"; ctx.shadowBlur = 7 * k; ctx.shadowOffsetY = 4 * k;\n      ctx.beginPath(); ctx.arc(d.x, d.y, 19, 0, Math.PI * 2); ctx.fillStyle = \"#0a0a0b\"; ctx.fill(); ctx.restore();\n      const cap = ctx.createRadialGradient(d.x - 6, d.y - 8, 1, d.x, d.y, 19);\n      cap.addColorStop(0, \"#6a6d73\"); cap.addColorStop(0.3, \"#2b2c30\"); cap.addColorStop(0.8, \"#0d0d0f\"); cap.addColorStop(1, \"#2a2b2f\");\n      ctx.beginPath(); ctx.arc(d.x, d.y, 19, 0, Math.PI * 2); ctx.fillStyle = cap; ctx.fill();\n      ctx.beginPath(); ctx.ellipse(d.x - 6, d.y - 8.5, 6, 3, -0.6, 0, Math.PI * 2); ctx.fillStyle = \"rgba(255,255,255,0.5)\"; ctx.fill();\n      ctx.beginPath(); ctx.arc(d.x, d.y, 18.4, Math.PI * 0.15, Math.PI * 0.7); ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(255,255,255,0.2)\"; ctx.stroke();\n    };\n\n    const draw = () => {\n      ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0, 0, cv.width, cv.height); ctx.drawImage(base, 0, 0);\n      ctx.setTransform(k, 0, 0, k, 0, 0);\n      ctx.textAlign = \"center\"; ctx.textBaseline = \"middle\";\n\n      // Gear window.\n      const gy = L.rev.y + 58;\n      const moving = s.phase === \"run\" && (s.v > 1 || s.thr > 0.03);\n      ctx.font = `700 22px ${MONO}`; ctx.fillStyle = s.phase === \"off\" ? \"rgba(239,232,214,0.25)\" : \"#efe8d6\"; ctx.fillText(moving ? String(s.g + 1) : \"N\", L.rev.x, gy + 1);\n\n      // Odometer: six drums, each one turning only while the drum to its right passes nine.\n      const ox = L.spd.x - 37, oy = L.spd.y + 38, cw = 74 / 6, ch = 21, tenths = s.odo * 10, f0 = tenths % 1;\n      ctx.save(); ctx.beginPath(); ctx.rect(ox, oy, 74, ch); ctx.clip();\n      let carry = true;\n      for (let p = 0; p < 6; p++) {\n        const digit = Math.floor(tenths / Math.pow(10, p)) % 10, roll = p === 0 ? f0 : carry ? f0 : 0, x = ox + (5 - p) * cw;\n        ctx.fillStyle = p === 0 ? \"#e8e1cf\" : \"#0d0d0e\"; ctx.fillRect(x + 0.5, oy, cw - 1, ch);\n        ctx.font = `700 12px ${MONO}`; ctx.fillStyle = p === 0 ? \"#141414\" : \"#efe8d6\";\n        ctx.fillText(String(digit), x + cw / 2, oy + ch / 2 + 1 - roll * ch); ctx.fillText(String((digit + 1) % 10), x + cw / 2, oy + ch / 2 + 1 + ch - roll * ch);\n        carry = carry && digit === 9;\n      }\n      const drum = ctx.createLinearGradient(0, oy, 0, oy + ch);\n      drum.addColorStop(0, \"rgba(0,0,0,0.85)\"); drum.addColorStop(0.3, \"rgba(0,0,0,0)\"); drum.addColorStop(0.5, \"rgba(255,255,255,0.08)\"); drum.addColorStop(0.72, \"rgba(0,0,0,0)\"); drum.addColorStop(1, \"rgba(0,0,0,0.8)\");\n      ctx.fillStyle = drum; ctx.fillRect(ox, oy, 74, ch); ctx.restore();\n\n      needle(L.rev, s.fr, 110); needle(L.spd, s.fs, 110);\n\n      // Warning jewels.\n      lamps.forEach((lamp, i) => {\n        const p = L.lamp[i], lit = s.lamp[i];\n        ctx.save(); ctx.globalAlpha = 0.2 + 0.8 * lit; ctx.shadowColor = lamp.color; ctx.shadowBlur = 18 * lit * k;\n        ctx.beginPath(); ctx.arc(p.x, p.y, 6.6, 0, Math.PI * 2); ctx.fillStyle = lamp.color; ctx.fill(); ctx.restore();\n        const hot = ctx.createRadialGradient(p.x, p.y, 0, p.x, p.y, 6.6); hot.addColorStop(0, `rgba(255,255,255,${0.75 * lit})`); hot.addColorStop(0.6, \"rgba(255,255,255,0)\");\n        ctx.beginPath(); ctx.arc(p.x, p.y, 6.6, 0, Math.PI * 2); ctx.fillStyle = hot; ctx.fill();\n        ctx.beginPath(); ctx.arc(p.x, p.y, 6.6, 0, Math.PI * 2); ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(0,0,0,0.5)\"; ctx.stroke();\n        ctx.beginPath(); ctx.ellipse(p.x - 2, p.y - 2.6, 2.4, 1.3, -0.6, 0, Math.PI * 2); ctx.fillStyle = `rgba(255,255,255,${0.3 + 0.4 * lit})`; ctx.fill();\n      });\n\n      // Start button: a lit ring, then a satin cap that sinks when pressed.\n      const bx = L.btn.x, by = L.btn.y, sink = s.press;\n      ctx.save(); ctx.shadowColor = NEEDLE; ctx.shadowBlur = 12 * s.halo * k; ctx.globalAlpha = 0.18 + 0.82 * s.halo;\n      ctx.beginPath(); ctx.arc(bx, by, 23.6, 0, Math.PI * 2); ctx.lineWidth = 2.6; ctx.strokeStyle = NEEDLE; ctx.stroke(); ctx.restore();\n      const r = 21 - sink * 0.9;\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.9)\"; ctx.shadowBlur = (6 - sink * 4) * k; ctx.shadowOffsetY = (3 - sink * 2.4) * k;\n      ctx.beginPath(); ctx.arc(bx, by, r, 0, Math.PI * 2); ctx.fillStyle = \"#111\"; ctx.fill(); ctx.restore();\n      const capG = ctx.createLinearGradient(bx - r, by - r, bx + r, by + r);\n      capG.addColorStop(0, sink > 0.5 ? \"#55585d\" : \"#7b7e84\"); capG.addColorStop(0.45, \"#2f3135\"); capG.addColorStop(1, \"#151618\");\n      ctx.beginPath(); ctx.arc(bx, by, r, 0, Math.PI * 2); ctx.fillStyle = capG; ctx.fill();\n      ctx.beginPath(); ctx.arc(bx, by, r - 0.6, Math.PI * 1.05, Math.PI * 1.6); ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(255,255,255,0.45)\"; ctx.stroke();\n      ctx.font = `700 6.4px ${MONO}`; ctx.fillStyle = \"rgba(0,0,0,0.8)\"; ctx.fillText(\"ENGINE\", bx, by - 5.2 + sink * 0.6); ctx.fillText(s.phase === \"off\" ? \"START\" : \"STOP\", bx, by + 4.8 + sink * 0.6);\n      ctx.fillStyle = \"#efe8d6\"; ctx.fillText(\"ENGINE\", bx, by - 6 + sink * 0.6); ctx.fillText(s.phase === \"off\" ? \"START\" : \"STOP\", bx, by + 4 + sink * 0.6);\n\n      ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.drawImage(glass, 0, 0);\n    };\n\n    // One slice of driving: throttle in, speed, gear and revs out.\n    const drive = (dt: number, thr: number, brake: number) => {\n      const g = s.g, torque = 0.55 + 0.45 * Math.sin(Math.PI * Math.min(1, s.rpm / 7200));\n      s.cut = Math.max(0, s.cut - dt);\n      const push = s.cut > 0 ? 0 : thr * PULL[g] * torque;\n      const dragged = 0.012 * s.v + 0.00075 * s.v * s.v + (s.v > 0.2 ? 0.5 + (thr < 0.03 ? 2.2 : 0) : 0) + brake;\n      s.v = Math.max(0, Math.min(TOP_SPEED * 0.985, s.v + (push - dragged) * dt));\n      const road = s.v * RATIOS[s.g], slip = 850 + thr * 2100;\n      const target = Math.min(MAX_RPM * 0.93, Math.max(s.g === 0 ? slip : 850, road));\n      s.rpm += (target - s.rpm) * Math.min(1, dt * (s.cut > 0 ? 9 : 14));\n      if (s.cut <= 0) {\n        if (s.g < RATIOS.length - 1 && road > 2200 + thr * 4300) { s.g++; s.cut = 0.22; }\n        else if (s.g > 0 && road < 1250 + thr * 1500) { s.g--; s.cut = 0.16; }\n      }\n      s.odo += (s.v * dt) / 600; // sped up so the drums are seen to turn\n    };\n\n    let raf = 0, last = 0, seen = true, touched = -1e9, t0 = 0, shown = -1, wasOn = true;\n    const frame = (now: number) => {\n      raf = 0;\n      if (!t0) { t0 = now; last = now; }\n      const dt = Math.min(0.033, (now - last) / 1000); last = now;\n      const idle = now - touched > 9000;\n      let brake = 0;\n      if (idle) {\n        // The self-running lap: pull, lift, kick down, stop, switch off, start again.\n        const T = ((now - t0) / 1000) % 27;\n        const wantOn = !(T >= 14.6 && T < 16.2);\n        if (wantOn !== wasOn) { wasOn = wantOn; live.current.on = wantOn; setOn(wantOn); if (wantOn) { s.phase = \"sweep\"; s.pt = 0; } else s.phase = \"off\"; }\n        live.current.thr = T < 6 ? 0.55 : T < 9.5 ? 0.1 : T < 12 ? 0.95 : T < 18.6 ? 0 : 0.7;\n        if (T >= 12 && T < 14.6) brake = 46;\n      } else wasOn = live.current.on;\n      const want = live.current.thr;\n      s.thr += (want - s.thr) * Math.min(1, dt * 10);\n      let fr = 0, fs = 0;\n      if (s.phase === \"run\") { drive(dt, s.thr, brake); fr = s.rpm / MAX_RPM; fs = s.v / TOP_SPEED; }\n      else if (s.phase === \"sweep\") {\n        s.pt += dt; s.v = 0; s.g = 0;\n        fr = fs = s.pt < 0.8 ? 1 : 0;\n        if (s.pt > 1.75) { s.phase = \"run\"; s.rpm = 1700; }\n      } else { s.v = Math.max(0, s.v - 60 * dt); s.rpm = 0; s.g = 0; fs = s.v / TOP_SPEED; }\n      // Needles are sprung and damped: the rev needle is the livelier of the two.\n      const stiff = s.phase === \"sweep\" ? 46 : 150, damp = s.phase === \"sweep\" ? 11 : 17;\n      s.frv += (stiff * (fr - s.fr) - damp * s.frv) * dt; s.fr += s.frv * dt;\n      s.fsv += ((s.phase === \"sweep\" ? 46 : 80) * (fs - s.fs) - (s.phase === \"sweep\" ? 11 : 14) * s.fsv) * dt; s.fs += s.fsv * dt;\n      const test = s.phase === \"sweep\";\n      const wantLamp = [test ? 1 : 0, test ? 1 : 0, test || (s.phase === \"run\" && s.rpm > 5900) ? 1 : 0];\n      s.lamp = s.lamp.map((v, i) => v + (wantLamp[i] - v) * Math.min(1, dt * 16));\n      s.halo += ((s.phase === \"off\" ? 0 : 1) - s.halo) * Math.min(1, dt * 8);\n      s.press += (s.pressTo - s.press) * Math.min(1, dt * 26);\n      if (idle && pedal.current) { const pct = Math.round(s.thr * 100); if (pct !== shown) { shown = pct; pedal.current.value = String(pct); pedal.current.style.setProperty(\"--fill\", `${pct}%`); } }\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    // With motion reduced there is no loop: show where the needles would come to rest.\n    const steady = () => {\n      const thr = live.current.thr;\n      if (!live.current.on) { s.phase = \"off\"; s.v = 0; s.rpm = 0; s.g = 0; s.halo = 0; }\n      else {\n        s.phase = \"run\"; s.halo = 1; s.thr = thr; s.v = TOP_SPEED * 0.9 * Math.sqrt(thr);\n        s.g = 0; while (s.g < RATIOS.length - 1 && s.v * RATIOS[s.g] > 2200 + thr * 4300) s.g++;\n        s.rpm = Math.max(850, s.v * RATIOS[s.g]);\n      }\n      s.fr = s.rpm / MAX_RPM; s.fs = s.v / TOP_SPEED; s.press = s.pressTo; s.lamp = lamps.map(() => 0); draw();\n    };\n\n    const size = () => {\n      const dpr = Math.min(2, window.devicePixelRatio || 1), wpx = Math.max(200, cv.clientWidth);\n      cv.width = Math.round(wpx * dpr); cv.height = Math.round((wpx * dpr * L.H) / L.W); k = cv.width / L.W;\n      build(); draw();\n    };\n    size();\n    const ro = new ResizeObserver(size); ro.observe(cv);\n\n    api.current = {\n      engine: (v) => { touched = performance.now(); live.current.on = v; if (v) { s.phase = \"sweep\"; s.pt = 0; } else s.phase = \"off\"; if (reduce) steady(); else wake(); },\n      throttle: (v) => { touched = performance.now(); live.current.thr = v; if (reduce) steady(); else wake(); },\n      press: (v) => { s.pressTo = v ? 1 : 0; if (reduce) steady(); else wake(); },\n    };\n    if (reduce) steady(); else wake();\n\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 () => { cancelAnimationFrame(raf); api.current = null; ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis); };\n  }, [narrow]);\n\n  const L = layoutFor(narrow);\n  const toggle = () => { const next = !live.current.on; live.current.on = next; setOn(next); api.current?.engine(next); };\n\n  return (\n    <div ref={stage} className=\"flex min-h-[640px] w-full flex-col items-center justify-center bg-[#090b0f] bg-[radial-gradient(110%_80%_at_50%_30%,#1a1f28_0%,#090b0f_62%)] px-4 py-10 sm:min-h-[760px]\">\n      <style>{`\n        .srcp-pedal { -webkit-appearance: none; appearance: none; height: 40px; background: transparent; cursor: pointer; --fill: 55%; }\n        .srcp-pedal::-webkit-slider-runnable-track { height: 6px; border-radius: 3px; background: linear-gradient(90deg, ${NEEDLE} var(--fill), #05060a var(--fill)); box-shadow: inset 0 1px 2px rgba(0,0,0,0.9), 0 1px 0 rgba(255,255,255,0.12); }\n        .srcp-pedal::-moz-range-track { height: 6px; border-radius: 3px; background: linear-gradient(90deg, ${NEEDLE} var(--fill), #05060a var(--fill)); box-shadow: inset 0 1px 2px rgba(0,0,0,0.9), 0 1px 0 rgba(255,255,255,0.12); }\n        .srcp-pedal::-webkit-slider-thumb { -webkit-appearance: none; width: 26px; height: 26px; margin-top: -10px; border-radius: 50%; border: 1px solid #1b1c1f; background: radial-gradient(circle at 34% 28%, #ffffff 0%, #d6d8dc 22%, #8b8f95 52%, #4a4d52 78%, #9da1a7 100%); box-shadow: 0 4px 8px rgba(0,0,0,0.7), inset 0 0 0 1px rgba(255,255,255,0.35); }\n        .srcp-pedal::-moz-range-thumb { width: 26px; height: 26px; border-radius: 50%; border: 1px solid #1b1c1f; background: radial-gradient(circle at 34% 28%, #ffffff 0%, #d6d8dc 22%, #8b8f95 52%, #4a4d52 78%, #9da1a7 100%); box-shadow: 0 4px 8px rgba(0,0,0,0.7), inset 0 0 0 1px rgba(255,255,255,0.35); }\n        .srcp-pedal:focus-visible { outline: 2px solid ${NEEDLE}; outline-offset: 4px; border-radius: 8px; }\n      `}</style>\n      <div className={`relative w-full ${narrow ? \"max-w-[340px]\" : \"max-w-[940px]\"}`} style={{ aspectRatio: `${L.W} / ${L.H}` }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n        <button\n          type=\"button\"\n          aria-pressed={on}\n          aria-label=\"Engine\"\n          onClick={toggle}\n          onPointerDown={() => api.current?.press(true)}\n          onPointerUp={() => api.current?.press(false)}\n          onPointerLeave={() => api.current?.press(false)}\n          onKeyDown={(e) => { if (e.key === \" \" || e.key === \"Enter\") api.current?.press(true); }}\n          onKeyUp={() => api.current?.press(false)}\n          style={{ left: `${(L.btn.x / L.W) * 100}%`, top: `${(L.btn.y / L.H) * 100}%`, width: `${(64 / L.W) * 100}%` }}\n          className=\"absolute aspect-square min-w-[44px] -translate-x-1/2 -translate-y-1/2 rounded-full outline-none focus-visible:ring-2 focus-visible:ring-[#ff4a1f] focus-visible:ring-offset-2 focus-visible:ring-offset-black\"\n        />\n      </div>\n      <div className=\"mt-6 flex w-full max-w-[340px] items-center gap-4 sm:max-w-[420px]\">\n        <label htmlFor=\"srcp-throttle\" className=\"font-mono text-[11px] uppercase tracking-[0.18em] text-slate-300\">Throttle</label>\n        <input\n          id=\"srcp-throttle\"\n          ref={pedal}\n          type=\"range\"\n          min={0}\n          max={100}\n          defaultValue={55}\n          onInput={(e) => { const v = Number(e.currentTarget.value); e.currentTarget.style.setProperty(\"--fill\", `${v}%`); api.current?.throttle(v / 100); }}\n          className=\"srcp-pedal min-w-0 flex-1\"\n        />\n      </div>\n      <p aria-live=\"polite\" className=\"mt-3 min-h-[20px] text-center font-mono text-[11px] uppercase leading-5 tracking-[0.16em] text-slate-400\">\n        <span className=\"text-slate-100\">{on ? \"Engine running\" : \"Engine off\"}</span>\n        <span className=\"mx-2 text-slate-600\">/</span>\n        {on ? \"Slide the throttle\" : \"Press start\"}\n      </p>\n    </div>\n  );\n}\n"}]}
