{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"porcelain-dotted-desk-globe-you-spin-with-inertia-with-flag-pins-that-trail-as-it-turns","type":"registry:block","title":"Porcelain Dotted Desk Globe You Spin with Inertia with Flag Pins That Trail as It Turns","description":"Canvas globe of blue dots on a porcelain ball: drag it and it turns on with inertia while the flags on its pins trail behind. Free React and HTML code.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/porcelain-dotted-desk-globe-you-spin-with-inertia-with-flag-pins-that-trail-as-it-turns.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n/** The made-up company and its small world. */\nconst world = { name: \"Small World No. 3\", owner: \"Aveline and Marrow, shippers\" };\n\n/** Flag pins. lat and lon are in degrees; each pin snaps to the nearest dot of land when the globe is made. */\nconst places = [\n  { id: \"aveline\", name: \"Port Aveline\", role: \"Head office\", lat: 34, lon: 8, color: \"#e2452c\" },\n  { id: \"tessane\", name: \"Tessane\", role: \"Print works\", lat: 8, lon: 96, color: \"#f2b632\" },\n  { id: \"marrow\", name: \"Marrow Quay\", role: \"Warehouse\", lat: -22, lon: 178, color: \"#1f8a5b\" },\n  { id: \"ostrel\", name: \"Ostrel\", role: \"Studio\", lat: 46, lon: 250, color: \"#17171c\" },\n  { id: \"fenwick\", name: \"Fenwick Sound\", role: \"Depot\", lat: -6, lon: 318, color: \"#ef7fa6\" },\n];\n\n// Design pixels.\nconst LW = 640;\nconst LH = 600;\nconst R = 196; // radius of the globe\nconst CX = LW / 2;\nconst CY = 276;\nconst TILT = 0.36; // lean of the axis, in radians\nconst REST = 0.3; // how far the north pole tips toward you at rest\nconst IDLE = 0.17; // radians a second it turns when left alone\nconst GRID = 3; // degrees between dots\nconst DOT = 2.5;\nconst NEEDLE = 0.22;\nconst ASIDE = 0.6; // a chosen pin stops this far left of centre, in radians, so it is seen side on // pin length as a share of the radius\nconst SEA = 0.5; // raise for more ocean, lower for more land\nconst INK = [\"#3b66ea\", \"#2146c0\", \"#152c80\"]; // dot colour in light, half light and shade\nconst LIGHT = [-0.5, 0.6, 0.62];\n\nconst clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));\nconst rad = (d: number) => (d * Math.PI) / 180;\n\nconst hash3 = (x: number, y: number, z: number) => {\n  let n = (x * 374761393 + y * 668265263 + z * 2147483647) | 0;\n  n = (n ^ (n >>> 13)) * 1274126177;\n  n = n ^ (n >>> 16);\n  return ((n >>> 0) % 100000) / 100000;\n};\nconst noise3 = (x: number, y: number, z: number) => {\n  const xi = Math.floor(x);\n  const yi = Math.floor(y);\n  const zi = Math.floor(z);\n  const s = (t: number) => t * t * (3 - 2 * t);\n  const u = s(x - xi);\n  const v = s(y - yi);\n  const w = s(z - zi);\n  const mix = (a: number, b: number, t: number) => a + (b - a) * t;\n  const plane = (k: number) => mix(mix(hash3(xi, yi, zi + k), hash3(xi + 1, yi, zi + k), u), mix(hash3(xi, yi + 1, zi + k), hash3(xi + 1, yi + 1, zi + k), u), v);\n  return mix(plane(0), plane(1), w);\n};\n/** Continents are invented: land is wherever four layers of noise add up past the SEA mark. */\nconst isLand = (x: number, y: number, z: number) => {\n  let sum = 0;\n  let amp = 0.5;\n  let f = 1.45;\n  for (let i = 0; i < 4; i++) {\n    sum += amp * noise3(x * f + 5.3, y * f + 1.7, z * f + 9.1);\n    amp *= 0.5;\n    f *= 2.07;\n  }\n  return sum / 0.9375 > SEA && Math.abs(y) < 0.985;\n};\nconst onSphere = (lat: number, lon: number) => {\n  const c = Math.cos(rad(lat));\n  return [c * Math.sin(rad(lon)), Math.sin(rad(lat)), c * Math.cos(rad(lon))];\n};\n\nfunction buildSprites(S: number) {\n  const make = (w: number, h: number, draw: (c: CanvasRenderingContext2D) => void) => {\n    const cv = document.createElement(\"canvas\");\n    cv.width = Math.ceil(w * S);\n    cv.height = Math.ceil(h * S);\n    const c = cv.getContext(\"2d\")!;\n    c.scale(S, S);\n    draw(c);\n    return cv;\n  };\n  const standTop = CY + R * 0.86;\n  const standRx = R * 0.52;\n  const standRy = standRx * 0.27;\n  const footY = CY + R * 1.07;\n  const footRx = R * 0.66;\n  const footRy = footRx * 0.24;\n  const brass = (c: CanvasRenderingContext2D, x0: number, x1: number) => {\n    const g = c.createLinearGradient(x0, 0, x1, 0);\n    [[0, \"#6f5220\"], [0.12, \"#b08a3e\"], [0.28, \"#f6e3a6\"], [0.4, \"#c9a352\"], [0.58, \"#a07a30\"], [0.74, \"#e8cd86\"], [0.9, \"#8a6828\"], [1, \"#5c4318\"]].forEach(([o, col]) => g.addColorStop(o as number, col as string));\n    return g;\n  };\n\n  // everything behind the ball: shadow on the table and the turned brass foot\n  const back = make(LW, LH, (c) => {\n    c.save();\n    c.translate(CX + R * 0.26, footY + 6);\n    c.scale(1, 0.2);\n    const sh = c.createRadialGradient(0, 0, R * 0.2, 0, 0, R * 1.18);\n    sh.addColorStop(0, \"rgba(60,48,30,0.42)\");\n    sh.addColorStop(0.5, \"rgba(60,48,30,0.16)\");\n    sh.addColorStop(1, \"rgba(60,48,30,0)\");\n    c.fillStyle = sh;\n    c.fillRect(-R * 1.4, -R * 1.4, R * 2.8, R * 2.8);\n    c.restore();\n    c.save();\n    c.translate(CX + 6, footY + 3);\n    c.scale(1, 0.26);\n    const contact = c.createRadialGradient(0, 0, footRx * 0.6, 0, 0, footRx * 1.22);\n    contact.addColorStop(0, \"rgba(40,30,16,0.7)\");\n    contact.addColorStop(1, \"rgba(40,30,16,0)\");\n    c.fillStyle = contact;\n    c.fillRect(-footRx * 1.3, -footRx * 1.3, footRx * 2.6, footRx * 2.6);\n    c.restore();\n    c.fillStyle = brass(c, CX - footRx, CX + footRx);\n    c.beginPath();\n    c.ellipse(CX, standTop, standRx, standRy, 0, Math.PI, 0, true);\n    c.bezierCurveTo(CX + standRx - 2, standTop + 26, CX + footRx, footY - 14, CX + footRx, footY);\n    c.ellipse(CX, footY, footRx, footRy, 0, 0, Math.PI);\n    c.bezierCurveTo(CX - footRx, footY - 14, CX - standRx + 2, standTop + 26, CX - standRx, standTop);\n    c.fill();\n    // lathe lines and the bright lip at the bottom\n    c.lineWidth = 0.9;\n    for (const [y, rx, a] of [[standTop + 15, standRx + 2.5, 0.32], [footY - 9, footRx - 1.5, 0.4]] as const) {\n      c.strokeStyle = `rgba(60,40,10,${a})`;\n      c.beginPath();\n      c.ellipse(CX, y, rx, rx * 0.25, 0, 0.08, Math.PI - 0.08);\n      c.stroke();\n      c.strokeStyle = \"rgba(255,244,205,0.5)\";\n      c.beginPath();\n      c.ellipse(CX, y + 1.1, rx, rx * 0.25, 0, 0.08, Math.PI - 0.08);\n      c.stroke();\n    }\n    c.strokeStyle = \"rgba(255,246,214,0.6)\";\n    c.lineWidth = 1.2;\n    c.beginPath();\n    c.ellipse(CX, footY - 0.6, footRx - 0.6, footRy, 0, 0.15, Math.PI - 0.15);\n    c.stroke();\n  });\n\n  // the porcelain ball with its lamp fixed up and to the left\n  const ball = make(LW, LH, (c) => {\n    c.beginPath();\n    c.arc(CX, CY, R, 0, Math.PI * 2);\n    c.clip();\n    const body = c.createRadialGradient(CX - R * 0.36, CY - R * 0.42, R * 0.05, CX - R * 0.12, CY - R * 0.14, R * 1.2);\n    [[0, \"#ffffff\"], [0.26, \"#fbf8f1\"], [0.52, \"#ebe5d8\"], [0.74, \"#d3ccbc\"], [0.9, \"#b2a997\"], [1, \"#958c7a\"]].forEach(([o, col]) => body.addColorStop(o as number, col as string));\n    c.fillStyle = body;\n    c.fillRect(0, 0, LW, LH);\n    // warm light bounced up off the table and the brass\n    const bounce = c.createRadialGradient(CX + R * 0.2, CY + R * 1.05, 0, CX + R * 0.2, CY + R * 1.05, R * 0.95);\n    bounce.addColorStop(0, \"rgba(255,226,170,0.55)\");\n    bounce.addColorStop(1, \"rgba(255,226,170,0)\");\n    c.fillStyle = bounce;\n    c.fillRect(0, 0, LW, LH);\n    const edge = c.createRadialGradient(CX, CY, R * 0.86, CX, CY, R);\n    edge.addColorStop(0, \"rgba(70,60,45,0)\");\n    edge.addColorStop(1, \"rgba(70,60,45,0.3)\");\n    c.fillStyle = edge;\n    c.fillRect(0, 0, LW, LH);\n  });\n\n  // glaze: a soft bloom and the sharp reflection of a window, laid over the dots\n  const gloss = make(LW, LH, (c) => {\n    c.beginPath();\n    c.arc(CX, CY, R, 0, Math.PI * 2);\n    c.clip();\n    const bloom = c.createRadialGradient(CX - R * 0.4, CY - R * 0.46, 0, CX - R * 0.4, CY - R * 0.46, R * 0.5);\n    bloom.addColorStop(0, \"rgba(255,255,255,0.75)\");\n    bloom.addColorStop(0.4, \"rgba(255,255,255,0.22)\");\n    bloom.addColorStop(1, \"rgba(255,255,255,0)\");\n    c.fillStyle = bloom;\n    c.fillRect(0, 0, LW, LH);\n    c.save();\n    c.translate(CX - R * 0.43, CY - R * 0.5);\n    c.rotate(-0.72);\n    c.fillStyle = \"rgba(255,255,255,0.9)\";\n    for (const dx of [-13, 3]) {\n      c.beginPath();\n      c.ellipse(dx + 6, 0, 6.5, 17, 0, 0, Math.PI * 2);\n      c.fill();\n    }\n    c.restore();\n    // the brass foot mirrored faintly in the underside\n    const under = c.createLinearGradient(0, CY + R * 0.6, 0, CY + R * 0.9);\n    under.addColorStop(0, \"rgba(190,150,70,0)\");\n    under.addColorStop(1, \"rgba(190,150,70,0.32)\");\n    c.fillStyle = under;\n    c.fillRect(0, CY + R * 0.6, LW, R * 0.4);\n  });\n\n  // the near lip of the foot, which sits in front of the ball\n  const lip = make(LW, LH, (c) => {\n    c.strokeStyle = \"rgba(40,28,8,0.5)\";\n    c.lineWidth = 5;\n    c.beginPath();\n    c.ellipse(CX, standTop - 2.4, standRx, standRy, 0, 0.1, Math.PI - 0.1);\n    c.stroke();\n    c.strokeStyle = brass(c, CX - standRx, CX + standRx);\n    c.lineWidth = 4.2;\n    c.beginPath();\n    c.ellipse(CX, standTop, standRx, standRy, 0, 0, Math.PI);\n    c.stroke();\n    c.strokeStyle = \"rgba(255,248,220,0.75)\";\n    c.lineWidth = 0.9;\n    c.beginPath();\n    c.ellipse(CX, standTop - 1.4, standRx, standRy, 0, 0.2, Math.PI - 0.2);\n    c.stroke();\n  });\n\n  const heads = places.map((p) =>\n    make(22, 22, (c) => {\n      c.scale(1.375, 1.375);\n      const g = c.createRadialGradient(6, 5.4, 0.3, 8, 8, 6.2);\n      g.addColorStop(0, \"#ffffff\");\n      g.addColorStop(0.22, p.color);\n      g.addColorStop(1, \"rgba(0,0,0,0.9)\");\n      c.fillStyle = p.color;\n      c.beginPath();\n      c.arc(8, 8, 5.6, 0, Math.PI * 2);\n      c.fill();\n      c.globalAlpha = 0.62;\n      c.fillStyle = g;\n      c.fill();\n      c.globalAlpha = 1;\n      c.fillStyle = \"rgba(255,255,255,0.92)\";\n      c.beginPath();\n      c.ellipse(6.1, 5.6, 1.7, 1.1, -0.6, 0, Math.PI * 2);\n      c.fill();\n    }),\n  );\n  return { back, ball, gloss, lip, heads };\n}\n\nexport default function PorcelainDottedDeskGlobe() {\n  const reduce = !!useReducedMotion();\n  const boxRef = useRef<HTMLDivElement>(null);\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const [near, setNear] = useState(0);\n  const ctl = useRef({ yaw: 0, vel: 0, pitch: REST, pitchTo: REST, goto: -1, hold: 0, dragging: false, touched: false, paused: false, lastX: 0, lastY: 0, lastT: 0, scale: 1 });\n\n  const go = (i: number, byHand: boolean) => {\n    const c = ctl.current;\n    if (byHand) c.touched = true;\n    c.goto = i;\n    c.hold = 0;\n  };\n\n  useEffect(() => {\n    const box = boxRef.current;\n    const canvas = canvasRef.current;\n    if (!box || !canvas) return;\n    const ctx = canvas.getContext(\"2d\")!;\n    const c = ctl.current;\n    let S = 1;\n    let sprites = buildSprites(1);\n    const size = () => {\n      c.scale = box.clientWidth / LW;\n      const next = clamp(c.scale * Math.min(2, window.devicePixelRatio || 1), 0.75, 2);\n      if (Math.abs(next - S) < 0.05 && canvas.width > 300) return;\n      S = next;\n      canvas.width = Math.round(LW * S);\n      canvas.height = Math.round(LH * S);\n      sprites = buildSprites(S);\n    };\n    size();\n    const ro = new ResizeObserver(() => {\n      size();\n      if (reduce) frame(performance.now());\n    });\n    ro.observe(box);\n\n    // dots of land, one ring of latitude at a time\n    const pts: number[] = [];\n    for (let lat = -87; lat <= 87; lat += GRID) {\n      const n = Math.max(1, Math.round((360 * Math.cos(rad(lat))) / GRID));\n      for (let k = 0; k < n; k++) {\n        const p = onSphere(lat, (k + (Math.round(lat / GRID) % 2) * 0.5) * (360 / n));\n        if (isLand(p[0], p[1], p[2])) pts.push(p[0], p[1], p[2]);\n      }\n    }\n    const N = pts.length / 3;\n    const land = new Float32Array(pts);\n    // each pin moves to the nearest dot of land so it never stands in the sea\n    const pins = places.map((pl) => {\n      const want = onSphere(pl.lat, pl.lon);\n      let best = 0;\n      let bestD = -2;\n      for (let i = 0; i < N; i++) {\n        const d = land[i * 3] * want[0] + land[i * 3 + 1] * want[1] + land[i * 3 + 2] * want[2];\n        if (d > bestD) {\n          bestD = d;\n          best = i;\n        }\n      }\n      const p = [land[best * 3], land[best * 3 + 1], land[best * 3 + 2]];\n      return { p, lon: Math.atan2(p[0], p[2]), lat: Math.asin(p[1]), fx: 1, fy: 0, fvx: 0, fvy: 0, px: 0, py: 0, seen: false };\n    });\n    const bins = [0, 1, 2].map(() => new Float32Array(N * 4));\n    // hairlines scored into the glaze: the equator, four parallels and six meridians\n    const lines: { pts: number[][]; a: number }[] = [];\n    for (const lat of [0, 30, -30, 60, -60]) {\n      const pts: number[][] = [];\n      for (let lon = 0; lon <= 360; lon += 4) pts.push(onSphere(lat, lon));\n      lines.push({ pts, a: lat === 0 ? 0.34 : 0.15 });\n    }\n    for (let lon = 0; lon < 360; lon += 30) {\n      const pts: number[][] = [];\n      for (let lat = -84; lat <= 84; lat += 4) pts.push(onSphere(lat, lon));\n      lines.push({ pts, a: 0.15 });\n    }\n    const pole = [0, 1, 0];\n\n    // spin up at the start, then come round to the first pin\n    c.yaw = pins[0].lon * -1 - 2.6;\n    c.vel = reduce ? 0 : 3.3;\n    if (reduce) {\n      c.yaw = -pins[0].lon - ASIDE;\n      c.pitch = c.pitchTo = REST;\n    }\n    let raf = 0;\n    let seen = true;\n    let stopped = false;\n    let last = 0;\n    let born = 0;\n    let shown = -1;\n    let nextTour = 0;\n    let rot = [1, 0, 0, 0, 1, 0, 0, 0, 1];\n    const turn = (p: ArrayLike<number>, o: number, out: number[]) => {\n      out[0] = rot[0] * p[o] + rot[1] * p[o + 1] + rot[2] * p[o + 2];\n      out[1] = rot[3] * p[o] + rot[4] * p[o + 1] + rot[5] * p[o + 2];\n      out[2] = rot[6] * p[o] + rot[7] * p[o + 1] + rot[8] * p[o + 2];\n    };\n    const v = [0, 0, 0];\n\n    function frame(now: number) {\n      raf = 0;\n      if (!born) born = now;\n      const dt = clamp((now - last) / 1000 || 0.016, 0.001, 0.05);\n      last = now;\n      const age = (now - born) / 1000;\n\n      // motion: dragging sets the speed; left alone, friction eases it to a slow idle turn\n      if (!c.dragging && !reduce) {\n        if (c.goto < 0 && !c.touched && !c.paused && age > 0.95 && now > nextTour) {\n          c.goto = shown < 0 ? 0 : (shown + 1) % pins.length;\n          c.hold = 0;\n        }\n        if (c.goto >= 0) {\n          const pin = pins[c.goto];\n          let d = -pin.lon - ASIDE - c.yaw;\n          d = ((((d + Math.PI) % (Math.PI * 2)) + Math.PI * 2) % (Math.PI * 2)) - Math.PI;\n          // a spring that overshoots a little, like a globe caught by hand\n          c.vel += (d * 15 - c.vel * 5.2) * dt;\n          c.pitchTo = clamp(pin.lat * 0.75 - 0.12, -0.3, 0.7);\n          if (Math.abs(d) < 0.02 && Math.abs(c.vel) < 0.04) {\n            c.hold += dt;\n            if (c.hold > (c.touched ? 4 : 2.6)) {\n              shown = c.goto;\n              c.goto = -1;\n              c.pitchTo = REST;\n              nextTour = now + 1500;\n            }\n          }\n        } else c.vel += (IDLE - c.vel) * (1 - Math.exp(-dt / 0.75));\n        c.yaw += c.vel * dt;\n        c.pitch += (c.pitchTo - c.pitch) * (1 - Math.exp(-dt / 0.35));\n      } else if (reduce && c.goto >= 0) {\n        c.yaw = -pins[c.goto].lon - ASIDE;\n        c.pitch = clamp(pins[c.goto].lat * 0.75 - 0.12, -0.3, 0.7);\n        c.goto = -1;\n      }\n\n      const cy = Math.cos(c.yaw);\n      const sy = Math.sin(c.yaw);\n      const ct = Math.cos(TILT);\n      const st = Math.sin(TILT);\n      const cp = Math.cos(c.pitch);\n      const sp = Math.sin(c.pitch);\n      // spin about the axis, lean the axis over, then tip the pole toward the viewer\n      const a = [cy, 0, sy, 0, 1, 0, -sy, 0, cy];\n      const b = [ct * a[0] + st * a[3], ct * a[1] + st * a[4], ct * a[2] + st * a[5], -st * a[0] + ct * a[3], -st * a[1] + ct * a[4], -st * a[2] + ct * a[5], a[6], a[7], a[8]];\n      rot = [b[0], b[1], b[2], cp * b[3] - sp * b[6], cp * b[4] - sp * b[7], cp * b[5] - sp * b[8], sp * b[3] + cp * b[6], sp * b[4] + cp * b[7], sp * b[5] + cp * b[8]];\n\n      ctx.setTransform(S, 0, 0, S, 0, 0);\n      ctx.clearRect(0, 0, LW, LH);\n      ctx.drawImage(sprites.back, 0, 0, LW, LH);\n\n      // pins: work out where each stands, and let its flag trail behind the way it is moving\n      let front = -1;\n      let frontZ = 0.2;\n      const laid = pins.map((pin, i) => {\n        turn(pin.p, 0, v);\n        const bx = CX + v[0] * R;\n        const by = CY - v[1] * R;\n        const tx = CX + v[0] * R * (1 + NEEDLE);\n        const ty = CY - v[1] * R * (1 + NEEDLE);\n        const z = v[2];\n        if (z > frontZ) {\n          frontZ = z;\n          front = i;\n        }\n        const vx = pin.seen ? (tx - pin.px) / dt : 0;\n        const vy = pin.seen ? (ty - pin.py) / dt : 0;\n        pin.px = tx;\n        pin.py = ty;\n        pin.seen = true;\n        const flutter = reduce ? 0 : Math.sin(now / 190 + i * 1.7) * 0.1;\n        let wx = 1 - vx * 0.011;\n        let wy = 0.12 + flutter - vy * 0.011;\n        const wl = Math.hypot(wx, wy) || 1;\n        wx /= wl;\n        wy /= wl;\n        if (reduce) {\n          pin.fx = wx;\n          pin.fy = wy;\n        } else {\n          pin.fvx += ((wx - pin.fx) * 90 - pin.fvx * 9) * dt;\n          pin.fvy += ((wy - pin.fy) * 90 - pin.fvy * 9) * dt;\n          pin.fx += pin.fvx * dt;\n          pin.fy += pin.fvy * dt;\n        }\n        return { i, bx, by, tx, ty, z, nx: v[0], ny: v[1] };\n      });\n      const drawPin = (q: (typeof laid)[number]) => {\n        const pin = pins[q.i];\n        const hx = q.tx - q.bx;\n        const hy = q.ty - q.by;\n        ctx.lineCap = \"round\";\n        ctx.strokeStyle = \"#6d727a\";\n        ctx.lineWidth = 2.3;\n        ctx.beginPath();\n        ctx.moveTo(q.bx, q.by);\n        ctx.lineTo(q.tx, q.ty);\n        ctx.stroke();\n        ctx.strokeStyle = \"#eef1f4\";\n        ctx.lineWidth = 0.7;\n        ctx.beginPath();\n        ctx.moveTo(q.bx - 0.4, q.by - 0.4);\n        ctx.lineTo(q.tx - 0.4, q.ty - 0.4);\n        ctx.stroke();\n        // the flag: hoisted on the outer part of the needle, flying along (fx, fy)\n        let m1x = q.bx + hx * 0.42;\n        let m1y = q.by + hy * 0.42;\n        const m2x = q.bx + hx * 0.86;\n        const m2y = q.by + hy * 0.86;\n        if (Math.hypot(m2x - m1x, m2y - m1y) < 12) {\n          m1x = m2x;\n          m1y = m2y + 12;\n        }\n        const len = 31;\n        const mx = (m1x + m2x) / 2;\n        const my = (m1y + m2y) / 2;\n        const ex = mx + pin.fx * len;\n        const ey = my + pin.fy * len;\n        const wave = reduce ? 1.2 : Math.sin(now / 130 + q.i * 2.3) * 2.2;\n        const kx = -pin.fy * wave;\n        const ky = pin.fx * wave;\n        ctx.beginPath();\n        ctx.moveTo(m2x, m2y);\n        ctx.quadraticCurveTo((m2x + ex) / 2 + kx, (m2y + ey) / 2 + ky, ex, ey - 1.5);\n        ctx.lineTo(ex, ey + 1.5);\n        ctx.quadraticCurveTo((m1x + ex) / 2 + kx, (m1y + ey) / 2 + ky, m1x, m1y);\n        ctx.closePath();\n        ctx.fillStyle = places[q.i].color;\n        ctx.fill();\n        const sh = ctx.createLinearGradient(mx, my, ex, ey);\n        sh.addColorStop(0, \"rgba(255,255,255,0.28)\");\n        sh.addColorStop(0.45, \"rgba(0,0,0,0)\");\n        sh.addColorStop(0.7, \"rgba(0,0,0,0.2)\");\n        sh.addColorStop(1, \"rgba(255,255,255,0.1)\");\n        ctx.fillStyle = sh;\n        ctx.fill();\n        ctx.drawImage(sprites.heads[q.i], q.tx - 11, q.ty - 11, 22, 22);\n      };\n      laid.filter((q) => q.z <= 0).forEach(drawPin);\n\n      ctx.drawImage(sprites.ball, 0, 0, LW, LH);\n\n      // the dots, sorted into three strengths of blue by how much lamp reaches them\n      const count = [0, 0, 0];\n      for (let i = 0; i < N; i++) {\n        turn(land, i * 3, v);\n        if (v[2] <= 0.02) continue;\n        const lit = v[0] * LIGHT[0] + v[1] * LIGHT[1] + v[2] * LIGHT[2];\n        const k = lit > 0.42 ? 0 : lit > -0.05 ? 1 : 2;\n        const o = count[k] * 4;\n        bins[k][o] = CX + v[0] * R;\n        bins[k][o + 1] = CY - v[1] * R;\n        // a round dot on a ball is seen as an ellipse, squashed along the line out from the centre\n        bins[k][o + 2] = DOT * Math.max(0.22, v[2]);\n        bins[k][o + 3] = Math.atan2(-v[1], v[0]);\n        count[k]++;\n      }\n      // a pale crescent under each dot makes it read as pressed into the glaze\n      ctx.fillStyle = \"rgba(255,255,255,0.7)\";\n      ctx.beginPath();\n      for (let k = 0; k < 3; k++)\n        for (let i = 0; i < count[k]; i++) {\n          const o = i * 4;\n          ctx.moveTo(bins[k][o] + 0.8, bins[k][o + 1] + 0.9);\n          ctx.ellipse(bins[k][o] + 0.8, bins[k][o + 1] + 0.9, bins[k][o + 2], DOT, bins[k][o + 3], 0, Math.PI * 2);\n        }\n      ctx.fill();\n      for (let k = 0; k < 3; k++) {\n        ctx.fillStyle = INK[k];\n        ctx.beginPath();\n        for (let i = 0; i < count[k]; i++) {\n          const o = i * 4;\n          ctx.moveTo(bins[k][o], bins[k][o + 1]);\n          ctx.ellipse(bins[k][o], bins[k][o + 1], bins[k][o + 2], DOT, bins[k][o + 3], 0, Math.PI * 2);\n        }\n        ctx.fill();\n      }\n      ctx.lineWidth = 0.9;\n      for (const line of lines) {\n        ctx.strokeStyle = `rgba(33,70,192,${line.a})`;\n        ctx.beginPath();\n        let pen = false;\n        for (const e of line.pts) {\n          turn(e, 0, v);\n          if (v[2] <= 0.03) {\n            pen = false;\n            continue;\n          }\n          pen ? ctx.lineTo(CX + v[0] * R, CY - v[1] * R) : ctx.moveTo(CX + v[0] * R, CY - v[1] * R);\n          pen = true;\n        }\n        ctx.stroke();\n      }\n      // a brass cap where the axis comes out at the north pole\n      turn(pole, 0, v);\n      if (v[2] > 0.05) {\n        const ang = Math.atan2(-v[1], v[0]);\n        const x = CX + v[0] * R;\n        const y = CY - v[1] * R;\n        ctx.fillStyle = \"rgba(60,45,20,0.35)\";\n        ctx.beginPath();\n        ctx.ellipse(x + 1.2, y + 1.6, 7.5 * Math.max(0.25, v[2]), 7.5, ang, 0, Math.PI * 2);\n        ctx.fill();\n        const cap = ctx.createLinearGradient(x - 6, y - 6, x + 6, y + 6);\n        cap.addColorStop(0, \"#fbeab4\");\n        cap.addColorStop(0.5, \"#c59c4a\");\n        cap.addColorStop(1, \"#7a5a22\");\n        ctx.fillStyle = cap;\n        ctx.beginPath();\n        ctx.ellipse(x, y, 6.6 * Math.max(0.25, v[2]), 6.6, ang, 0, Math.PI * 2);\n        ctx.fill();\n        ctx.fillStyle = \"#5d4418\";\n        ctx.beginPath();\n        ctx.ellipse(x, y, 2 * Math.max(0.25, v[2]), 2, ang, 0, Math.PI * 2);\n        ctx.fill();\n      }\n\n      // the shadow each near pin lays across the porcelain, then the pins themselves\n      const near = laid.filter((q) => q.z > 0).sort((p, q) => p.z - q.z);\n      for (const q of near) {\n        turn(pins[q.i].p, 0, v);\n        const sx = v[0] * (1 + NEEDLE) - LIGHT[0] * NEEDLE * 0.9;\n        const syy = v[1] * (1 + NEEDLE) - LIGHT[1] * NEEDLE * 0.9;\n        const sz = v[2] * (1 + NEEDLE) - LIGHT[2] * NEEDLE * 0.9;\n        const sl = Math.hypot(sx, syy, sz);\n        if (sz / sl < 0.05) continue;\n        const g = ctx.createLinearGradient(q.bx, q.by, CX + (sx / sl) * R, CY - (syy / sl) * R);\n        g.addColorStop(0, \"rgba(60,50,35,0.42)\");\n        g.addColorStop(1, \"rgba(60,50,35,0)\");\n        ctx.strokeStyle = g;\n        ctx.lineWidth = 2.6;\n        ctx.lineCap = \"round\";\n        ctx.beginPath();\n        ctx.moveTo(q.bx, q.by);\n        ctx.lineTo(CX + (sx / sl) * R, CY - (syy / sl) * R);\n        ctx.stroke();\n      }\n      ctx.drawImage(sprites.gloss, 0, 0, LW, LH);\n      near.forEach(drawPin);\n      ctx.drawImage(sprites.lip, 0, 0, LW, LH);\n\n      const named = c.goto >= 0 ? c.goto : front;\n      if (named >= 0 && named !== shownName) {\n        shownName = named;\n        setNear(named);\n      }\n      const deg = Math.round((((((c.yaw * 180) / Math.PI) % 360) + 360) % 360) / 5) * 5;\n      if (deg !== lastDeg) {\n        lastDeg = deg;\n        box.setAttribute(\"aria-valuenow\", String(deg));\n      }\n      if (!reduce && seen && !document.hidden && !stopped) raf = requestAnimationFrame(frame);\n    }\n    let shownName = -1;\n    let lastDeg = -1;\n    const kick = () => {\n      if (!raf && !stopped) raf = requestAnimationFrame(frame);\n    };\n    kick();\n    const io = new IntersectionObserver(([e]) => {\n      seen = e.isIntersecting;\n      if (seen) kick();\n    });\n    io.observe(box);\n    const onVis = () => !document.hidden && kick();\n    document.addEventListener(\"visibilitychange\", onVis);\n    // with motion reduced there is no loop: redraw only when something asks for it\n    const poll = reduce ? window.setInterval(() => (c.goto >= 0 || c.dragging || c.vel !== 0) && ((c.vel = 0), kick()), 100) : 0;\n    return () => {\n      stopped = true;\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      if (poll) window.clearInterval(poll);\n      document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, [reduce]);\n\n  const onDown = (e: React.PointerEvent) => {\n    if (e.button !== 0) return;\n    const c = ctl.current;\n    c.dragging = true;\n    c.touched = true;\n    c.goto = -1;\n    c.pitchTo = c.pitch;\n    c.lastX = e.clientX;\n    c.lastY = e.clientY;\n    c.lastT = performance.now();\n    c.vel = reduce ? 1 : 0;\n    e.currentTarget.setPointerCapture(e.pointerId);\n  };\n  const onDrag = (e: React.PointerEvent) => {\n    const c = ctl.current;\n    if (!c.dragging) return;\n    const now = performance.now();\n    const k = 1 / (R * c.scale);\n    const d = (e.clientX - c.lastX) * k;\n    c.yaw += d;\n    c.pitch = clamp(c.pitch + (e.clientY - c.lastY) * k, -0.5, 0.95);\n    c.pitchTo = c.pitch;\n    if (!reduce) c.vel = c.vel * 0.55 + (d / Math.max(0.004, (now - c.lastT) / 1000)) * 0.45;\n    else c.vel = 1;\n    c.lastX = e.clientX;\n    c.lastY = e.clientY;\n    c.lastT = now;\n  };\n  const onUp = () => {\n    const c = ctl.current;\n    if (!c.dragging) return;\n    c.dragging = false;\n    if (performance.now() - c.lastT > 90) c.vel = reduce ? 1 : 0;\n    c.vel = clamp(c.vel, -14, 14);\n  };\n  const onKey = (e: React.KeyboardEvent) => {\n    const c = ctl.current;\n    const step = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }[e.key];\n    if (!step) return;\n    e.preventDefault();\n    c.touched = true;\n    c.goto = -1;\n    if (reduce) {\n      c.yaw += step[0] * 0.26;\n      c.vel = 1;\n    } else c.vel += step[0] * 1.8;\n    c.pitchTo = clamp(c.pitchTo + step[1] * 0.2, -0.5, 0.95);\n    if (reduce) c.pitch = c.pitchTo;\n  };\n\n  return (\n    <section className=\"relative overflow-hidden bg-[#e7e3da]\">\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\" style={{ background: \"radial-gradient(60% 56% at 44% 38%, #f6f3ec 0%, #e9e5dc 48%, #d6d0c3 100%)\" }} />\n      <div className=\"relative mx-auto w-full max-w-[720px] px-2 pt-4 sm:pt-6\">\n        <div\n          ref={boxRef}\n          role=\"slider\"\n          tabIndex={0}\n          aria-label={`Spin the globe of ${world.name}. Arrow keys turn and tip it.`}\n          aria-valuemin={0}\n          aria-valuemax={360}\n          aria-valuenow={0}\n          aria-valuetext={`${places[near].name} is nearest`}\n          onPointerDown={onDown}\n          onPointerMove={onDrag}\n          onPointerUp={onUp}\n          onPointerCancel={onUp}\n          onPointerEnter={() => (ctl.current.paused = true)}\n          onPointerLeave={() => (ctl.current.paused = false)}\n          onKeyDown={onKey}\n          className=\"relative w-full cursor-grab touch-pan-y select-none rounded-[28px] outline-none focus-visible:ring-2 focus-visible:ring-[#2146c0] active:cursor-grabbing\"\n          style={{ aspectRatio: `${LW} / ${LH}` }}\n        >\n          <canvas ref={canvasRef} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n        </div>\n      </div>\n\n      <div className=\"relative flex flex-col gap-3 px-5 pb-6 pt-1 sm:flex-row sm:items-end sm:justify-between sm:px-8 sm:pb-8\">\n        <p className=\"font-mono text-[11px] uppercase leading-relaxed tracking-[0.18em] text-stone-600\">\n          <span className=\"text-stone-900\">\n            {world.name} / {world.owner}\n          </span>\n          <span className=\"block\" aria-live=\"polite\">\n            {places[near].name}: {places[near].role}\n          </span>\n        </p>\n        <div className=\"flex flex-wrap gap-2\">\n          {places.map((p, i) => (\n            <button\n              key={p.id}\n              type=\"button\"\n              aria-pressed={near === i}\n              onClick={() => go(i, true)}\n              className={`flex h-10 items-center gap-2 rounded-full px-3.5 font-mono text-[11px] font-semibold uppercase tracking-[0.12em] outline-none transition-colors focus-visible:ring-2 focus-visible:ring-[#2146c0] focus-visible:ring-offset-2 focus-visible:ring-offset-[#e7e3da] ${near === i ? \"bg-stone-900 text-white\" : \"bg-white/70 text-stone-800 hover:bg-white\"}`}\n            >\n              <span aria-hidden=\"true\" className=\"h-2.5 w-2.5 rounded-full ring-1 ring-black/10\" style={{ background: p.color }} />\n              {p.name}\n            </button>\n          ))}\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
