{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"mechanical-watch-movement-loader-with-swinging-balance-wheel-and-breathing-hairspring","type":"registry:component","title":"Mechanical Watch Movement Loader with Swinging Balance Wheel and Breathing Hairspring","description":"Loader drawn on a canvas as a watch movement, with a swinging balance wheel, a breathing hairspring and a ticking escape wheel. Free React and HTML code.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/mechanical-watch-movement-loader-with-swinging-balance-wheel-and-breathing-hairspring.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// What the loader says underneath. The first line is also what a screen reader announces.\nconst LABEL = \"Setting things in motion\";\nconst HINT = \"Press the movement to wind it\";\n\n// Timing of the escapement.\nconst BEATS_PER_SECOND = 2.5; // ticks a second; the balance swings once for every two\nconst REST_SWING = 1.9; // balance swing, in radians, when the spring is nearly run down\nconst FULL_SWING = 2.9; // swing straight after a wind\nconst AUTO_WIND_EVERY = 4.6; // seconds between the winds it gives itself\n\n// Where the parts sit, on a 400 by 400 drawing.\nconst C = 200;\nconst PLATE_R = 172;\nconst BARREL = { x: 214, y: 147, r: 62 };\nconst CROWN = { x: 148, y: 88, r: 24 };\nconst THIRD = { x: 155, y: 184, r: 50 };\nconst FOURTH = { x: 135, y: 238, r: 44 };\nconst ESCAPE = { x: 165, y: 280, r: 27 };\nconst FORK = { x: 213, y: 287 };\nconst BALANCE = { x: 272, y: 262, r: 64 };\n\nconst GOLD = [\"#fbeab2\", \"#d9ae4f\", \"#8c6524\", \"#ecd08a\", \"#a67c30\"];\nconst STEEL = [\"#fbfbfc\", \"#c3c7ce\", \"#737984\", \"#e6e8ec\", \"#8d939d\"];\nconst ROSE = [\"#ffe1cf\", \"#dba083\", \"#8f5a44\", \"#f1c2aa\", \"#a9705a\"];\n\ntype Ctx = CanvasRenderingContext2D;\n\nfunction sheen(ctx: Ctx, x: number, y: number, light: number, p: string[]) {\n  const g = ctx.createConicGradient(light, x, y);\n  g.addColorStop(0, p[0]);\n  g.addColorStop(0.1, p[1]);\n  g.addColorStop(0.27, p[2]);\n  g.addColorStop(0.42, p[1]);\n  g.addColorStop(0.5, p[3]);\n  g.addColorStop(0.6, p[1]);\n  g.addColorStop(0.77, p[4]);\n  g.addColorStop(0.92, p[1]);\n  g.addColorStop(1, p[0]);\n  return g;\n}\n\n// A spoked wheel: toothed rim, hub, and windows cut between the spokes.\nfunction wheelPath(ctx: Ctx, r: number, teeth: number, depth: number, spokes: number, hub: number, rim: number, spokeW: number) {\n  const p = (Math.PI * 2) / teeth;\n  ctx.beginPath();\n  for (let i = 0; i < teeth; i++) {\n    const a = i * p;\n    const pts: [number, number][] = [\n      [a, r - depth],\n      [a + p * 0.22, r - depth],\n      [a + p * 0.38, r],\n      [a + p * 0.62, r],\n      [a + p * 0.78, r - depth],\n    ];\n    for (const [ang, rad] of pts) {\n      const X = Math.cos(ang) * rad;\n      const Y = Math.sin(ang) * rad;\n      if (i === 0 && ang === 0) ctx.moveTo(X, Y);\n      else ctx.lineTo(X, Y);\n    }\n  }\n  ctx.closePath();\n  if (spokes > 0) {\n    const ro = r - depth - rim;\n    const mo = spokeW / (2 * ro);\n    const mi = spokeW / (2 * hub);\n    for (let s = 0; s < spokes; s++) {\n      const a = (s / spokes) * Math.PI * 2;\n      const b = ((s + 1) / spokes) * Math.PI * 2;\n      ctx.moveTo(Math.cos(a + mo) * ro, Math.sin(a + mo) * ro);\n      ctx.arc(0, 0, ro, a + mo, b - mo);\n      ctx.arc(0, 0, hub, b - mi, a + mi, true);\n      ctx.closePath();\n    }\n  }\n}\n\n// The escape wheel: long, hooked teeth that the pallets catch.\nfunction escapePath(ctx: Ctx, r: number, teeth: number) {\n  const p = (Math.PI * 2) / teeth;\n  const root = r * 0.6;\n  ctx.beginPath();\n  for (let i = 0; i < teeth; i++) {\n    const a = i * p;\n    const pts: [number, number][] = [\n      [a, root],\n      [a + p * 0.5, r],\n      [a + p * 0.68, r - 0.6],\n      [a + p * 0.62, root + 2],\n    ];\n    for (const [ang, rad] of pts) {\n      const X = Math.cos(ang) * rad;\n      const Y = Math.sin(ang) * rad;\n      if (i === 0 && ang === 0) ctx.moveTo(X, Y);\n      else ctx.lineTo(X, Y);\n    }\n  }\n  ctx.closePath();\n  const ro = root - 3.5;\n  for (let s = 0; s < 4; s++) {\n    const a = (s / 4) * Math.PI * 2;\n    const b = ((s + 1) / 4) * Math.PI * 2;\n    ctx.moveTo(Math.cos(a + 0.2) * ro, Math.sin(a + 0.2) * ro);\n    ctx.arc(0, 0, ro, a + 0.2, b - 0.2);\n    ctx.arc(0, 0, 5.5, b - 0.5, a + 0.5, true);\n    ctx.closePath();\n  }\n}\n\nfunction jewel(ctx: Ctx, x: number, y: number, s: number, light: number) {\n  ctx.save();\n  ctx.translate(x, y);\n  ctx.beginPath();\n  ctx.arc(0, 0, 7.4 * s, 0, Math.PI * 2);\n  ctx.fillStyle = sheen(ctx, 0, 0, light, GOLD);\n  ctx.fill();\n  ctx.lineWidth = 0.6;\n  ctx.strokeStyle = \"rgba(90,60,10,0.55)\";\n  ctx.stroke();\n  const g = ctx.createRadialGradient(-1.4 * s, -1.6 * s, 0.4, 0, 0, 4.9 * s);\n  g.addColorStop(0, \"#ff9db0\");\n  g.addColorStop(0.35, \"#e0183f\");\n  g.addColorStop(1, \"#5e0318\");\n  ctx.beginPath();\n  ctx.arc(0, 0, 4.9 * s, 0, Math.PI * 2);\n  ctx.fillStyle = g;\n  ctx.fill();\n  ctx.beginPath();\n  ctx.arc(0, 0, 1.2 * s, 0, Math.PI * 2);\n  ctx.fillStyle = \"#2a0009\";\n  ctx.fill();\n  ctx.beginPath();\n  ctx.ellipse(-1.9 * s, -2.1 * s, 1.5 * s, 0.8 * s, -0.7, 0, Math.PI * 2);\n  ctx.fillStyle = \"rgba(255,255,255,0.8)\";\n  ctx.fill();\n  ctx.restore();\n}\n\nfunction screw(ctx: Ctx, x: number, y: number, r: number, turn: number) {\n  ctx.save();\n  ctx.translate(x, y);\n  ctx.beginPath();\n  ctx.arc(0, 0, r + 1.4, 0, Math.PI * 2);\n  ctx.fillStyle = \"rgba(6,8,12,0.55)\";\n  ctx.fill();\n  const g = ctx.createRadialGradient(-r * 0.4, -r * 0.5, 0.5, 0, 0, r);\n  g.addColorStop(0, \"#8fb0ff\");\n  g.addColorStop(0.45, \"#3558c4\");\n  g.addColorStop(1, \"#16235e\");\n  ctx.beginPath();\n  ctx.arc(0, 0, r, 0, Math.PI * 2);\n  ctx.fillStyle = g;\n  ctx.fill();\n  ctx.rotate(turn);\n  ctx.fillStyle = \"rgba(8,12,40,0.85)\";\n  ctx.fillRect(-r * 0.92, -r * 0.16, r * 1.84, r * 0.32);\n  ctx.restore();\n}\n\n// A finger bridge: a polished bar from the edge of the plate to a wheel's pivot.\nfunction bridgePath(ctx: Ctx, x0: number, y0: number, x1: number, y1: number, w0: number, w1: number) {\n  const a = Math.atan2(y1 - y0, x1 - x0);\n  const nx = Math.cos(a + Math.PI / 2);\n  const ny = Math.sin(a + Math.PI / 2);\n  ctx.beginPath();\n  ctx.arc(x0, y0, w0, a + Math.PI / 2, a - Math.PI / 2);\n  ctx.lineTo(x1 - nx * w1, y1 - ny * w1);\n  ctx.arc(x1, y1, w1, a - Math.PI / 2, a + Math.PI / 2);\n  ctx.lineTo(x0 + nx * w0, y0 + ny * w0);\n  ctx.closePath();\n}\n\nconst bridges = [\n  { from: [56, 142], to: [THIRD.x, THIRD.y], w0: 11, w1: 8.5, turn: 0.6 },\n  { from: [46, 266], to: [FOURTH.x, FOURTH.y], w0: 11, w1: 8.5, turn: 2.1 },\n  { from: [146, 358], to: [ESCAPE.x, ESCAPE.y], w0: 10, w1: 8, turn: 1.2 },\n  { from: [356, 300], to: [BALANCE.x, BALANCE.y], w0: 14, w1: 10, turn: 0.2 },\n];\n\n// Perlage: rows of small overlapping circles ground into the plate. It is the costly part, so it is drawn once.\nfunction drawGrain(ctx: Ctx) {\n  const step = 6.5;\n  let row = 0;\n  for (let y = C - PLATE_R - step; y < C + PLATE_R + step; y += step * 0.86) {\n    const off = row % 2 ? step / 2 : 0;\n    for (let x = C - PLATE_R - step + off; x < C + PLATE_R + step; x += step) {\n      const g = ctx.createConicGradient(-1.9, x, y);\n      g.addColorStop(0, \"rgba(255,255,255,0.2)\");\n      g.addColorStop(0.25, \"rgba(10,12,18,0.22)\");\n      g.addColorStop(0.5, \"rgba(255,255,255,0.1)\");\n      g.addColorStop(0.75, \"rgba(10,12,18,0.24)\");\n      g.addColorStop(1, \"rgba(255,255,255,0.2)\");\n      ctx.beginPath();\n      ctx.arc(x, y, step * 0.72, 0, Math.PI * 2);\n      ctx.fillStyle = g;\n      ctx.fill();\n    }\n    row++;\n  }\n}\n\nfunction drawPlate(ctx: Ctx, light: number, grain: HTMLCanvasElement) {\n  // main plate\n  ctx.save();\n  ctx.shadowColor = \"rgba(40,36,30,0.5)\";\n  ctx.shadowBlur = 9;\n  ctx.shadowOffsetY = 5;\n  ctx.beginPath();\n  ctx.arc(C, C, PLATE_R + 8, 0, Math.PI * 2);\n  ctx.fillStyle = \"#6f747d\";\n  ctx.fill();\n  ctx.restore();\n  // polished outer ring\n  ctx.beginPath();\n  ctx.arc(C, C, PLATE_R + 8, 0, Math.PI * 2);\n  ctx.fillStyle = sheen(ctx, C, C, light, STEEL);\n  ctx.fill();\n  ctx.beginPath();\n  ctx.arc(C, C, PLATE_R, 0, Math.PI * 2);\n  ctx.fillStyle = \"#454850\";\n  ctx.fill();\n  // perlage, painted once and reused (see drawGrain)\n  ctx.save();\n  ctx.beginPath();\n  ctx.arc(C, C, PLATE_R - 0.5, 0, Math.PI * 2);\n  ctx.clip();\n  if (grain.width) ctx.drawImage(grain, 0, 0, 400, 400);\n  // the plate is dished toward the edge\n  const v = ctx.createRadialGradient(C - 40, C - 60, 30, C, C, PLATE_R);\n  v.addColorStop(0, \"rgba(255,255,255,0.16)\");\n  v.addColorStop(0.6, \"rgba(255,255,255,0)\");\n  v.addColorStop(1, \"rgba(8,10,16,0.5)\");\n  ctx.fillStyle = v;\n  ctx.fillRect(0, 0, 400, 400);\n  ctx.restore();\n  ctx.beginPath();\n  ctx.arc(C, C, PLATE_R, 0, Math.PI * 2);\n  ctx.lineWidth = 1.2;\n  ctx.strokeStyle = \"rgba(40,44,54,0.55)\";\n  ctx.stroke();\n  // engraving round the empty quarter of the plate\n  ctx.save();\n  ctx.font = \"600 7.5px ui-sans-serif, system-ui, sans-serif\";\n  ctx.textAlign = \"center\";\n  ctx.textBaseline = \"middle\";\n  const words = \"FIVE JEWELS     WOUND BY HAND\";\n  for (let i = 0; i < words.length; i++) {\n    const a = -1.08 + i * 0.043;\n    ctx.save();\n    ctx.translate(C + Math.cos(a) * 156, C + Math.sin(a) * 156);\n    ctx.rotate(a + Math.PI / 2);\n    ctx.fillStyle = \"rgba(0,0,0,0.7)\";\n    ctx.fillText(words[i], 0.4, 0.6);\n    ctx.fillStyle = \"rgba(232,212,150,0.95)\";\n    ctx.fillText(words[i], 0, 0);\n    ctx.restore();\n  }\n  ctx.restore();\n  // sunk wells under the wheels\n  for (const w of [THIRD, FOURTH, ESCAPE, BALANCE, BARREL]) {\n    const g = ctx.createRadialGradient(w.x, w.y, w.r * 0.3, w.x, w.y, w.r + 7);\n    g.addColorStop(0, \"rgba(24,25,31,0.95)\");\n    g.addColorStop(0.85, \"rgba(20,21,27,0.95)\");\n    g.addColorStop(1, \"rgba(8,9,12,0.98)\");\n    ctx.beginPath();\n    ctx.arc(w.x, w.y, w.r + 6, 0, Math.PI * 2);\n    ctx.fillStyle = g;\n    ctx.fill();\n    ctx.lineWidth = 1;\n    ctx.strokeStyle = \"rgba(255,255,255,0.4)\";\n    ctx.stroke();\n  }\n}\n\nfunction drawBridges(ctx: Ctx, light: number, _grain: HTMLCanvasElement) {\n  for (const b of bridges) {\n    const [x0, y0] = b.from;\n    const [x1, y1] = b.to;\n    ctx.save();\n    ctx.shadowColor = \"rgba(10,10,16,0.6)\";\n    ctx.shadowBlur = 9;\n    ctx.shadowOffsetY = 5;\n    bridgePath(ctx, x0, y0, x1, y1, b.w0, b.w1);\n    ctx.fillStyle = \"#b9bdc5\";\n    ctx.fill();\n    ctx.restore();\n    // straight graining along the bar, then a bright chamfer\n    bridgePath(ctx, x0, y0, x1, y1, b.w0, b.w1);\n    const a = Math.atan2(y1 - y0, x1 - x0) + Math.PI / 2;\n    const g = ctx.createLinearGradient(x0 - Math.cos(a) * b.w0, y0 - Math.sin(a) * b.w0, x0 + Math.cos(a) * b.w0, y0 + Math.sin(a) * b.w0);\n    const tilt = Math.cos(a - light);\n    g.addColorStop(0, tilt > 0 ? \"#8b909a\" : \"#e6e8ec\");\n    g.addColorStop(0.5, \"#c2c6cd\");\n    g.addColorStop(1, tilt > 0 ? \"#eceef1\" : \"#8f949e\");\n    ctx.fillStyle = g;\n    ctx.fill();\n    // Geneva stripes across the bar\n    ctx.save();\n    ctx.clip();\n    ctx.translate(x0, y0);\n    ctx.rotate(0.62);\n    for (let x = -320; x < 320; x += 9) {\n      const st = ctx.createLinearGradient(x, 0, x + 9, 0);\n      st.addColorStop(0, \"rgba(255,255,255,0.38)\");\n      st.addColorStop(0.55, \"rgba(255,255,255,0)\");\n      st.addColorStop(1, \"rgba(40,46,60,0.28)\");\n      ctx.fillStyle = st;\n      ctx.fillRect(x, -320, 9, 640);\n    }\n    ctx.restore();\n    bridgePath(ctx, x0, y0, x1, y1, b.w0, b.w1);\n    ctx.lineWidth = 1.3;\n    ctx.strokeStyle = \"rgba(255,255,255,0.85)\";\n    ctx.stroke();\n    ctx.lineWidth = 0.5;\n    ctx.strokeStyle = \"rgba(60,64,74,0.7)\";\n    ctx.stroke();\n    screw(ctx, x0, y0, b.w0 * 0.46, b.turn);\n    jewel(ctx, x1, y1, 0.78, light);\n  }\n  // loose plate screws\n  screw(ctx, 322, 222, 5.5, 0.9);\n  screw(ctx, 96, 318, 6, 2.4);\n  screw(ctx, 250, 352, 6, 0.3);\n  jewel(ctx, FORK.x, FORK.y, 0.62, light);\n}\n\nexport default function MechanicalWatchMovementLoader() {\n  const reduce = useReducedMotion();\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const wrapRef = useRef<HTMLButtonElement>(null);\n  const windRef = useRef(0);\n\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    const wrap = wrapRef.current;\n    if (!canvas || !wrap) return;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n    const plate = document.createElement(\"canvas\");\n    const grain = document.createElement(\"canvas\");\n    const top = document.createElement(\"canvas\");\n    let size = 0;\n    let dpr = 1;\n    let lightDrawn = 99;\n    let light = -2.3;\n    let lightTarget = -2.3;\n    let pointerOn = false;\n\n    const cache = () => {\n      for (const [cv, fn] of [[plate, drawPlate], [top, drawBridges]] as const) {\n        cv.width = canvas.width;\n        cv.height = canvas.height;\n        const c = cv.getContext(\"2d\");\n        if (!c) continue;\n        c.setTransform((size * dpr) / 400, 0, 0, (size * dpr) / 400, 0, 0);\n        fn(c, light, grain);\n      }\n      lightDrawn = light;\n    };\n    const resize = () => {\n      const s = Math.round(wrap.clientWidth * 1.12);\n      if (s < 2 || s === size) return;\n      size = s;\n      dpr = Math.min(2, window.devicePixelRatio || 1);\n      canvas.width = Math.round(size * dpr);\n      canvas.height = Math.round(size * dpr);\n      grain.width = canvas.width;\n      grain.height = canvas.height;\n      const gc = grain.getContext(\"2d\");\n      if (gc) {\n        gc.setTransform((size * dpr) / 400, 0, 0, (size * dpr) / 400, 0, 0);\n        drawGrain(gc);\n      }\n      cache();\n      if (reduce) draw();\n    };\n\n    // state of the going train\n    let t = 0.13;\n    let phase = 0.42;\n    let swing = FULL_SWING - 0.3;\n    let ticks = 0;\n    let sinceTick = 1;\n    let ratchet = 0;\n    let ratchetTo = 0;\n    let ratchetV = 0;\n    let sinceWind = AUTO_WIND_EVERY - 0.5;\n    let seenWinds = windRef.current;\n\n    const wheel = (w: { x: number; y: number; r: number }, rot: number, pal: string[], teeth: number, depth: number, spokes: number, hub: number, rim: number, spokeW: number) => {\n      ctx.save();\n      ctx.translate(w.x, w.y);\n      ctx.rotate(rot);\n      ctx.shadowColor = \"rgba(0,0,0,0.6)\";\n      ctx.shadowBlur = 7 * ((size * dpr) / 400);\n      ctx.shadowOffsetY = 4 * ((size * dpr) / 400);\n      wheelPath(ctx, w.r, teeth, depth, spokes, hub, rim, spokeW);\n      ctx.fillStyle = sheen(ctx, 0, 0, light - rot, pal);\n      ctx.fill(\"evenodd\");\n      ctx.shadowColor = \"transparent\";\n      ctx.lineWidth = 0.5;\n      ctx.strokeStyle = \"rgba(60,40,10,0.5)\";\n      ctx.stroke();\n      ctx.restore();\n    };\n\n    function draw() {\n      const k = (size * dpr) / 400;\n      ctx!.setTransform(1, 0, 0, 1, 0, 0);\n      ctx!.clearRect(0, 0, canvas!.width, canvas!.height);\n      ctx!.drawImage(plate, 0, 0);\n      ctx!.setTransform(k, 0, 0, k, 0, 0);\n\n      const step = Math.min(1, sinceTick / 0.085);\n      const back = 1 + 2.2 * Math.pow(step - 1, 3) + 1.2 * Math.pow(step - 1, 2); // lands with a small recoil\n      const escapeRot = (ticks - 1 + back) * ((Math.PI * 2) / 30);\n      const fourthRot = -escapeRot / 6;\n      const thirdRot = -fourthRot / 5;\n      const theta = swing * Math.sin(phase);\n\n      // going train, lowest wheel first\n      wheel(THIRD, thirdRot + 0.4, GOLD, 66, 3.4, 5, 9, 6, 6.5);\n      wheel(FOURTH, fourthRot, GOLD, 58, 3.2, 5, 8, 5.5, 5.5);\n\n      // ratchet wheel on the barrel, with a sunray finish that shows it turning\n      ctx!.save();\n      ctx!.translate(BARREL.x, BARREL.y);\n      ctx!.rotate(ratchet);\n      ctx!.shadowColor = \"rgba(0,0,0,0.6)\";\n      ctx!.shadowBlur = 9 * k;\n      ctx!.shadowOffsetY = 5 * k;\n      wheelPath(ctx!, BARREL.r, 54, 4.2, 0, 0, 0, 0);\n      ctx!.fillStyle = sheen(ctx!, 0, 0, light - ratchet, STEEL);\n      ctx!.fill();\n      ctx!.shadowColor = \"transparent\";\n      ctx!.lineWidth = 0.6;\n      for (let i = 0; i < 96; i++) {\n        const a = (i / 96) * Math.PI * 2;\n        ctx!.beginPath();\n        ctx!.moveTo(Math.cos(a) * 15, Math.sin(a) * 15);\n        ctx!.lineTo(Math.cos(a) * (BARREL.r - 6), Math.sin(a) * (BARREL.r - 6));\n        ctx!.strokeStyle = i % 2 ? \"rgba(255,255,255,0.3)\" : \"rgba(50,56,70,0.2)\";\n        ctx!.stroke();\n      }\n      ctx!.beginPath();\n      ctx!.arc(0, 0, BARREL.r - 5.5, 0, Math.PI * 2);\n      ctx!.strokeStyle = \"rgba(50,56,70,0.45)\";\n      ctx!.stroke();\n      ctx!.beginPath();\n      ctx!.arc(0, 0, 15, 0, Math.PI * 2);\n      ctx!.fillStyle = sheen(ctx!, 0, 0, light - ratchet + 1, STEEL);\n      ctx!.fill();\n      ctx!.stroke();\n      ctx!.restore();\n      screw(ctx!, BARREL.x, BARREL.y, 8.5, ratchet + 0.5);\n\n      // crown wheel that drives it\n      const crownRot = -ratchet * (BARREL.r / CROWN.r) + 0.1;\n      ctx!.save();\n      ctx!.translate(CROWN.x, CROWN.y);\n      ctx!.rotate(crownRot);\n      ctx!.shadowColor = \"rgba(0,0,0,0.55)\";\n      ctx!.shadowBlur = 7 * k;\n      ctx!.shadowOffsetY = 4 * k;\n      wheelPath(ctx!, CROWN.r, 21, 4.2, 0, 0, 0, 0);\n      ctx!.fillStyle = sheen(ctx!, 0, 0, light - crownRot, STEEL);\n      ctx!.fill();\n      ctx!.shadowColor = \"transparent\";\n      ctx!.lineWidth = 0.6;\n      ctx!.strokeStyle = \"rgba(50,56,70,0.5)\";\n      for (const r of [CROWN.r - 6, 9]) {\n        ctx!.beginPath();\n        ctx!.arc(0, 0, r, 0, Math.PI * 2);\n        ctx!.stroke();\n      }\n      ctx!.restore();\n      screw(ctx!, CROWN.x, CROWN.y, 5.5, crownRot);\n\n      // escape wheel\n      ctx!.save();\n      ctx!.translate(ESCAPE.x, ESCAPE.y);\n      ctx!.rotate(escapeRot);\n      ctx!.shadowColor = \"rgba(0,0,0,0.6)\";\n      ctx!.shadowBlur = 5 * k;\n      ctx!.shadowOffsetY = 3 * k;\n      escapePath(ctx!, ESCAPE.r, 15);\n      ctx!.fillStyle = sheen(ctx!, 0, 0, light - escapeRot, ROSE);\n      ctx!.fill(\"evenodd\");\n      ctx!.restore();\n\n      // pallet fork, thrown from one banking pin to the other on every tick\n      const side = ticks % 2 ? 1 : -1;\n      const forkRot = Math.atan2(BALANCE.y - FORK.y, BALANCE.x - FORK.x) + side * (2 * Math.min(1, back) - 1) * 0.14;\n      ctx!.save();\n      ctx!.translate(FORK.x, FORK.y);\n      ctx!.rotate(forkRot);\n      ctx!.shadowColor = \"rgba(0,0,0,0.55)\";\n      ctx!.shadowBlur = 4 * k;\n      ctx!.shadowOffsetY = 2 * k;\n      ctx!.beginPath();\n      ctx!.moveTo(0, -3.2);\n      ctx!.lineTo(40, -2.2);\n      ctx!.lineTo(47, -6.5);\n      ctx!.lineTo(49, -3);\n      ctx!.lineTo(44, 0);\n      ctx!.lineTo(49, 3);\n      ctx!.lineTo(47, 6.5);\n      ctx!.lineTo(40, 2.2);\n      ctx!.lineTo(0, 3.2);\n      ctx!.lineTo(-14, 23);\n      ctx!.lineTo(-20, 20);\n      ctx!.lineTo(-9, 0);\n      ctx!.lineTo(-20, -20);\n      ctx!.lineTo(-14, -23);\n      ctx!.closePath();\n      ctx!.fillStyle = sheen(ctx!, 0, 0, light - forkRot, STEEL);\n      ctx!.fill();\n      ctx!.shadowColor = \"transparent\";\n      ctx!.fillStyle = \"#d3163c\";\n      ctx!.fillRect(-23, 17, 9, 4.5);\n      ctx!.fillRect(-23, -21.5, 9, 4.5);\n      ctx!.restore();\n\n      // hairspring: outer end pinned, inner end turning with the balance, so the coils breathe\n      ctx!.save();\n      ctx!.translate(BALANCE.x, BALANCE.y);\n      const turns = Math.PI * 2 * 6.5;\n      const pin = 0.35;\n      ctx!.beginPath();\n      for (let i = 0; i <= 220; i++) {\n        const u = i / 220;\n        const ang = pin - (1 - u) * (turns - theta);\n        const rad = 8 + (BALANCE.r - 27) * u;\n        const X = Math.cos(ang) * rad;\n        const Y = Math.sin(ang) * rad;\n        if (i === 0) ctx!.moveTo(X, Y);\n        else ctx!.lineTo(X, Y);\n      }\n      ctx!.lineWidth = 1.7;\n      ctx!.strokeStyle = \"#2748b4\";\n      ctx!.stroke();\n      ctx!.lineWidth = 0.6;\n      ctx!.strokeStyle = \"rgba(196,214,255,0.95)\";\n      ctx!.stroke();\n\n      // balance wheel\n      ctx!.rotate(theta);\n      ctx!.shadowColor = \"rgba(0,0,0,0.6)\";\n      ctx!.shadowBlur = 9 * k;\n      ctx!.shadowOffsetY = 5 * k;\n      ctx!.beginPath();\n      ctx!.arc(0, 0, BALANCE.r, 0, Math.PI * 2);\n      ctx!.arc(0, 0, BALANCE.r - 9, 0, Math.PI * 2, true);\n      ctx!.closePath();\n      ctx!.moveTo(-BALANCE.r + 5, -4);\n      ctx!.lineTo(-9, -5.5);\n      ctx!.arc(0, 0, 9, Math.PI + 0.6, -0.6);\n      ctx!.lineTo(BALANCE.r - 5, -4);\n      ctx!.lineTo(BALANCE.r - 5, 4);\n      ctx!.lineTo(9, 5.5);\n      ctx!.arc(0, 0, 9, 0.6, Math.PI - 0.6);\n      ctx!.lineTo(-BALANCE.r + 5, 4);\n      ctx!.closePath();\n      ctx!.fillStyle = sheen(ctx!, 0, 0, light - theta, GOLD);\n      ctx!.fill();\n      ctx!.shadowColor = \"transparent\";\n      ctx!.lineWidth = 0.5;\n      ctx!.strokeStyle = \"rgba(70,46,8,0.6)\";\n      ctx!.stroke();\n      // timing screws round the rim\n      for (let i = 0; i < 10; i++) {\n        const a = (i / 10) * Math.PI * 2 + 0.31;\n        const X = Math.cos(a) * (BALANCE.r - 4.5);\n        const Y = Math.sin(a) * (BALANCE.r - 4.5);\n        const g = ctx!.createRadialGradient(X - 0.8, Y - 0.8, 0.2, X, Y, 2.8);\n        g.addColorStop(0, \"#fff6d4\");\n        g.addColorStop(1, \"#8c6524\");\n        ctx!.beginPath();\n        ctx!.arc(X, Y, 2.7, 0, Math.PI * 2);\n        ctx!.fillStyle = g;\n        ctx!.fill();\n      }\n      ctx!.restore();\n\n      ctx!.setTransform(1, 0, 0, 1, 0, 0);\n      ctx!.drawImage(top, 0, 0);\n\n      // a band of reflection on the glass over the movement, lying across the direction of the light\n      ctx!.setTransform(k, 0, 0, k, 0, 0);\n      ctx!.save();\n      ctx!.beginPath();\n      ctx!.arc(C, C, PLATE_R, 0, Math.PI * 2);\n      ctx!.clip();\n      const lx = Math.cos(light);\n      const ly = Math.sin(light);\n      const gl = ctx!.createLinearGradient(C + lx * 170, C + ly * 170, C - lx * 60, C - ly * 60);\n      gl.addColorStop(0, \"rgba(255,255,255,0.3)\");\n      gl.addColorStop(0.3, \"rgba(255,255,255,0.07)\");\n      gl.addColorStop(0.42, \"rgba(255,255,255,0.16)\");\n      gl.addColorStop(0.5, \"rgba(255,255,255,0.02)\");\n      gl.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx!.fillStyle = gl;\n      ctx!.fillRect(0, 0, 400, 400);\n      ctx!.restore();\n    }\n\n    resize();\n    const ro = new ResizeObserver(resize);\n    ro.observe(wrap);\n\n    if (reduce) {\n      draw();\n      return () => ro.disconnect();\n    }\n\n    let raf = 0;\n    let visible = true;\n    let last = performance.now();\n    const frame = (now: number) => {\n      raf = 0;\n      const dt = Math.min(0.05, (now - last) / 1000);\n      last = now;\n      t += dt;\n\n      // winding: by itself every few seconds, or whenever the button is pressed\n      sinceWind += dt;\n      if (windRef.current !== seenWinds || sinceWind > AUTO_WIND_EVERY) {\n        seenWinds = windRef.current;\n        sinceWind = 0;\n        ratchetTo += (Math.PI * 2) / 54 * 4;\n        swing = Math.min(FULL_SWING, swing + 0.55);\n      }\n      ratchetV += ((ratchetTo - ratchet) * 220 - ratchetV * 17) * dt;\n      ratchet += ratchetV * dt;\n      swing += (REST_SWING - swing) * (1 - Math.exp(-dt * 0.35));\n\n      // the balance, and a tick each time it passes centre\n      const before = Math.floor(phase / Math.PI);\n      phase += dt * Math.PI * BEATS_PER_SECOND;\n      sinceTick += dt;\n      if (Math.floor(phase / Math.PI) !== before) {\n        ticks++;\n        sinceTick = 0;\n      }\n\n      // the light wanders slowly, or comes from wherever the pointer is\n      if (!pointerOn) lightTarget = -2.3 + Math.sin(t * 0.5) * 0.5;\n      let d = lightTarget - light;\n      while (d > Math.PI) d -= Math.PI * 2;\n      while (d < -Math.PI) d += Math.PI * 2;\n      light += d * (1 - Math.exp(-dt * 6));\n      if (Math.abs(light - lightDrawn) > 0.045) cache();\n\n      draw();\n      if (visible && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const start = () => {\n      if (!raf && visible && !document.hidden) {\n        last = performance.now();\n        raf = requestAnimationFrame(frame);\n      }\n    };\n    const io = new IntersectionObserver(([en]) => {\n      visible = en.isIntersecting;\n      start();\n    });\n    io.observe(wrap);\n    const onVis = () => start();\n    document.addEventListener(\"visibilitychange\", onVis);\n    const move = (ev: PointerEvent) => {\n      const r = wrap.getBoundingClientRect();\n      pointerOn = true;\n      lightTarget = Math.atan2(ev.clientY - (r.top + r.height / 2), ev.clientX - (r.left + r.width / 2));\n    };\n    const leave = () => {\n      pointerOn = false;\n    };\n    wrap.addEventListener(\"pointermove\", move);\n    wrap.addEventListener(\"pointerleave\", leave);\n    wrap.addEventListener(\"pointercancel\", leave);\n    start();\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", onVis);\n      wrap.removeEventListener(\"pointermove\", move);\n      wrap.removeEventListener(\"pointerleave\", leave);\n      wrap.removeEventListener(\"pointercancel\", leave);\n    };\n  }, [reduce]);\n\n  return (\n    <section className=\"relative flex min-h-[600px] w-full flex-col items-center justify-center overflow-hidden bg-[#e9e6e0] px-4 py-14\">\n      <div\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0\"\n        style={{ background: \"radial-gradient(ellipse at 50% 38%, rgba(255,255,255,0.9), rgba(233,230,224,0) 60%)\" }}\n      />\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute left-1/2 top-1/2 h-[min(84vw,430px)] w-[min(84vw,430px)] -translate-x-1/2 -translate-y-[48%] rounded-full bg-[#3b352b]/45 blur-2xl\" />\n      <button\n        ref={wrapRef}\n        type=\"button\"\n        aria-label=\"Wind the movement\"\n        onClick={() => {\n          windRef.current += 1;\n        }}\n        className=\"relative aspect-square w-[min(84vw,430px)] cursor-pointer rounded-full transition-transform duration-200 ease-out active:scale-[0.985] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#2b3f8f] focus-visible:ring-offset-8 focus-visible:ring-offset-[#e9e6e0] motion-reduce:transition-none\"\n      >\n        <canvas ref={canvasRef} aria-hidden=\"true\" className=\"absolute -inset-[6%] h-[112%] w-[112%]\" />\n      </button>\n      <div role=\"status\" className=\"relative mt-10 text-center\">\n        <p className=\"text-[13px] font-medium uppercase tracking-[0.26em] text-[#3c3a36]\">{LABEL}</p>\n        <p className=\"mt-2 text-[12px] tracking-wide text-[#6b6860]\">{HINT}</p>\n      </div>\n    </section>\n  );\n}\n"}]}
