{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"folded-paper-street-map-that-unfolds-panel-by-panel-with-creases-and-a-pin-that-drops-in","type":"registry:block","title":"Folded Paper Street Map That Unfolds Panel by Panel with Creases and a Pin That Drops In","description":"Location section built as a pocket street plan: paper panels unfold on springs, keep their creases, and a red pin drops in. Free React and HTML to copy.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/folded-paper-street-map-that-unfolds-panel-by-panel-with-creases-and-a-pin-that-drops-in.tsx","type":"registry:component","content":"import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { AnimatePresence, motion, useMotionValue, useReducedMotion, useSpring, useTransform } from \"framer-motion\";\n\n/** What is printed on the cover and in the title box of the plan. */\nconst town = { name: \"Quillhaven\", sub: \"Old Town and Harbour\", edition: \"Pocket plan No. 4\", scale: \"1 : 7 500\" };\n\n/** Where the pin can land. x and y are fractions of the open sheet, measured from its top left corner. */\nconst places = [\n  { id: \"lantern\", name: \"Lantern Coffee Roasters\", note: \"Open from seven\", x: 0.165, y: 0.31 },\n  { id: \"tidewater\", name: \"Tidewater Books\", note: \"Second floor maps\", x: 0.345, y: 0.8 },\n  { id: \"glasshouse\", name: \"Glasshouse Gardens\", note: \"Palm house and pond\", x: 0.615, y: 0.3 },\n  { id: \"ferry\", name: \"Ferry Steps\", note: \"Boats to Marrow Isle\", x: 0.875, y: 0.375 },\n];\n\nconst streets = [\"Anchor Row\", \"Bellfounder Ave\", \"Candlewick Lane\", \"Dyers Yard\", \"Eel Pie Stairs\", \"Fathom Street\", \"Gull Passage\", \"Hawser Lane\", \"Inkwell Court\", \"Juniper Rise\", \"Kiln Street\", \"Lantern Row\", \"Mizzen Walk\", \"Netmakers Lane\", \"Oyster Quay\", \"Pilot Street\", \"Quill Bridge\", \"Ropewalk\", \"Saltway\", \"Tanners Green\", \"Umber Lane\", \"Vesper Street\"];\n\n// Design pixels. The sheet is COLS panels wide and two panels tall.\nconst COLS = 4;\nconst PW = 176;\nconst PH = 236;\nconst MW = PW * COLS;\nconst MH = PH * 2;\nconst SCENE_W = 980;\nconst SCENE_H = 610;\nconst TIP = 47; // camera: how far the table leans away\nconst TURN = -15; // camera: how far the sheet is turned on the table\nconst REST = 5; // degrees each panel keeps after unfolding, so the creases still catch the light\nconst FOLD = 176; // degrees a panel swings to lie on its neighbour\n\nconst PAPER = \"#f3ead6\";\nconst BACK = \"#ece2cc\";\nconst RED = \"#e2442b\";\nconst INK = \"#54463a\";\nconst SERIF = 'Georgia, \"Times New Roman\", serif';\n\nconst clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));\n\n/** Draws the whole plan once. Everything here is made up: no map service is involved. */\nfunction drawPlan(ctx: CanvasRenderingContext2D, W: number, H: number) {\n  let seed = 23;\n  const rnd = () => {\n    seed = (seed * 1664525 + 1013904223) >>> 0;\n    return seed / 4294967296;\n  };\n  const coast = () => {\n    ctx.beginPath();\n    ctx.moveTo(W * 0.5, H + 10);\n    ctx.bezierCurveTo(W * 0.6, H * 0.86, W * 0.74, H * 0.9, W * 0.79, H * 0.7);\n    ctx.bezierCurveTo(W * 0.83, H * 0.56, W * 0.93, H * 0.6, W + 10, H * 0.47);\n    ctx.lineTo(W + 10, H + 10);\n    ctx.closePath();\n  };\n  const river = () => {\n    ctx.beginPath();\n    ctx.moveTo(W * 0.29, -10);\n    ctx.bezierCurveTo(W * 0.38, H * 0.22, W * 0.2, H * 0.4, W * 0.36, H * 0.55);\n    ctx.bezierCurveTo(W * 0.46, H * 0.64, W * 0.56, H * 0.66, W * 0.7, H * 0.93);\n  };\n  const label = (t: string, x: number, y: number, o: { size?: number; italic?: boolean; bold?: boolean; color?: string; gap?: number; rot?: number; halo?: string } = {}) => {\n    ctx.save();\n    ctx.translate(x, y);\n    ctx.rotate(o.rot ?? 0);\n    ctx.font = `${o.italic ? \"italic \" : \"\"}${o.bold ? \"700 \" : \"\"}${o.size ?? 8}px ${SERIF}`;\n    (ctx as unknown as { letterSpacing: string }).letterSpacing = `${o.gap ?? 0}px`;\n    ctx.textAlign = \"center\";\n    ctx.textBaseline = \"middle\";\n    if (o.halo !== \"none\") {\n      ctx.lineJoin = \"round\";\n      ctx.lineWidth = 2.6;\n      ctx.strokeStyle = o.halo ?? \"rgba(243,234,214,0.9)\";\n      ctx.strokeText(t, 0, 0);\n    }\n    ctx.fillStyle = o.color ?? INK;\n    ctx.fillText(t, 0, 0);\n    ctx.restore();\n  };\n  const grid = (cx: number, cy: number, ang: number, sx: number, sy: number) => {\n    ctx.save();\n    ctx.translate(cx, cy);\n    ctx.rotate(ang);\n    const n = 22;\n    for (let i = -n; i < n; i++)\n      for (let j = -n; j < n; j++) {\n        const r = rnd();\n        if (r > 0.7) continue;\n        ctx.fillStyle = r < 0.05 ? \"#e0a590\" : r < 0.3 ? \"#dccaa6\" : \"#e4d5b6\";\n        const w = r < 0.3 ? sx - 9 : (sx - 9) * (0.35 + rnd() * 0.5);\n        const h = r < 0.3 ? sy - 9 : (sy - 9) * (0.5 + rnd() * 0.5);\n        ctx.fillRect(i * sx + 4.5, j * sy + 4.5, w, h);\n      }\n    for (const pass of [0, 1]) {\n      ctx.strokeStyle = pass ? \"#fffdf6\" : \"#cdbb99\";\n      for (let i = -n; i <= n; i++) {\n        ctx.lineWidth = (i % 4 === 0 ? 4.6 : 2.8) + (pass ? 0 : 1.3);\n        ctx.beginPath();\n        ctx.moveTo(i * sx, -n * sy);\n        ctx.lineTo(i * sx, n * sy);\n        ctx.stroke();\n        ctx.beginPath();\n        ctx.moveTo(-n * sx, i * sy);\n        ctx.lineTo(n * sx, i * sy);\n        ctx.stroke();\n      }\n    }\n    ctx.restore();\n  };\n\n  // land, then two street grids that meet at the river\n  ctx.fillStyle = \"#efe3c9\";\n  ctx.fillRect(0, 0, W, H);\n  grid(W * 0.1, H * 0.4, -0.2, 34, 27);\n  ctx.save();\n  river();\n  ctx.lineTo(W + 10, H + 10);\n  ctx.lineTo(W + 10, -10);\n  ctx.closePath();\n  ctx.clip();\n  ctx.fillStyle = \"#efe3c9\";\n  ctx.fillRect(0, 0, W, H);\n  grid(W * 0.7, H * 0.3, 0.3, 38, 30);\n  ctx.restore();\n\n  // parks\n  const park = (x: number, y: number, rx: number, ry: number, rot: number, pond: boolean) => {\n    ctx.save();\n    ctx.translate(x, y);\n    ctx.rotate(rot);\n    ctx.beginPath();\n    ctx.ellipse(0, 0, rx, ry, 0, 0, Math.PI * 2);\n    ctx.fillStyle = \"#c3d7a6\";\n    ctx.fill();\n    ctx.lineWidth = 1;\n    ctx.strokeStyle = \"#9db985\";\n    ctx.stroke();\n    ctx.clip();\n    ctx.setLineDash([3, 3]);\n    ctx.strokeStyle = \"#f4ecd8\";\n    ctx.lineWidth = 1.4;\n    ctx.beginPath();\n    ctx.moveTo(-rx, ry * 0.3);\n    ctx.quadraticCurveTo(0, -ry * 0.8, rx, ry * 0.2);\n    ctx.moveTo(-rx * 0.2, -ry);\n    ctx.quadraticCurveTo(rx * 0.3, 0, -rx * 0.1, ry);\n    ctx.stroke();\n    ctx.setLineDash([]);\n    for (let i = 0; i < rx * 0.9; i++) {\n      const a = rnd() * Math.PI * 2;\n      const d = Math.sqrt(rnd());\n      const tx = Math.cos(a) * d * rx * 0.92;\n      const ty = Math.sin(a) * d * ry * 0.9;\n      ctx.fillStyle = \"rgba(70,100,60,0.25)\";\n      ctx.beginPath();\n      ctx.arc(tx + 0.8, ty + 1, 2.3, 0, Math.PI * 2);\n      ctx.fill();\n      ctx.fillStyle = rnd() < 0.5 ? \"#93b77c\" : \"#a4c48b\";\n      ctx.beginPath();\n      ctx.arc(tx, ty, 2.2, 0, Math.PI * 2);\n      ctx.fill();\n    }\n    if (pond) {\n      ctx.beginPath();\n      ctx.ellipse(rx * 0.25, ry * 0.2, rx * 0.26, ry * 0.26, 0.3, 0, Math.PI * 2);\n      ctx.fillStyle = \"#a9cfd8\";\n      ctx.fill();\n      ctx.strokeStyle = \"#7fb0bd\";\n      ctx.lineWidth = 0.8;\n      ctx.stroke();\n    }\n    ctx.restore();\n  };\n  park(W * 0.615, H * 0.3, 66, 42, -0.25, true);\n  park(W * 0.115, H * 0.76, 48, 35, 0.2, false);\n  park(W * 0.47, H * 0.2, 26, 18, 0.5, false);\n\n  // river and sea, each with a darker bank and paler shallows\n  ctx.lineCap = \"round\";\n  river();\n  ctx.strokeStyle = \"#7fb0bd\";\n  ctx.lineWidth = 25;\n  ctx.stroke();\n  river();\n  ctx.strokeStyle = \"#a9cfd8\";\n  ctx.lineWidth = 22.5;\n  ctx.stroke();\n  coast();\n  ctx.fillStyle = \"#a9cfd8\";\n  ctx.fill();\n  ctx.save();\n  coast();\n  ctx.clip();\n  for (const [w, c] of [[64, \"#b3d6dd\"], [40, \"#bedde2\"], [18, \"#cbe5e8\"]] as const) {\n    coast();\n    ctx.strokeStyle = c;\n    ctx.lineWidth = w;\n    ctx.stroke();\n  }\n  coast();\n  ctx.strokeStyle = \"#7fb0bd\";\n  ctx.lineWidth = 2.4;\n  ctx.stroke();\n  // ferry route\n  ctx.setLineDash([5, 4]);\n  ctx.strokeStyle = \"#5d8e9c\";\n  ctx.lineWidth = 1.1;\n  ctx.beginPath();\n  ctx.moveTo(W * 0.885, H * 0.58);\n  ctx.quadraticCurveTo(W * 0.9, H * 0.78, W * 0.8, H + 4);\n  ctx.stroke();\n  ctx.setLineDash([]);\n  ctx.restore();\n  river();\n  ctx.strokeStyle = \"#a9cfd8\";\n  ctx.lineWidth = 22.5;\n  ctx.stroke();\n\n  // piers in the harbour\n  for (const [px, py, len, a] of [[0.772, 0.76, 40, 0.5], [0.795, 0.705, 46, 0.42], [0.83, 0.622, 34, 0.9]] as const) {\n    ctx.save();\n    ctx.translate(W * px, H * py);\n    ctx.rotate(a);\n    ctx.fillStyle = \"#cdbb99\";\n    ctx.fillRect(-0.8, -3.3, len + 1.6, 6.6);\n    ctx.fillStyle = \"#fffdf6\";\n    ctx.fillRect(0, -2.5, len, 5);\n    ctx.restore();\n  }\n\n  // avenues, drawn over the river so they read as bridges; kept off the sea\n  ctx.save();\n  ctx.beginPath();\n  ctx.rect(0, 0, W, H);\n  coast();\n  ctx.clip(\"evenodd\");\n  const avenues = [\n    () => { ctx.moveTo(-10, H * 0.56); ctx.bezierCurveTo(W * 0.2, H * 0.5, W * 0.4, H * 0.36, W * 0.56, H * 0.46); ctx.bezierCurveTo(W * 0.7, H * 0.55, W * 0.85, H * 0.3, W + 10, H * 0.26); },\n    () => { ctx.moveTo(W * 0.5, -10); ctx.bezierCurveTo(W * 0.5, H * 0.3, W * 0.62, H * 0.5, W * 0.76, H * 0.76); },\n    () => { ctx.moveTo(W * 0.06, -10); ctx.bezierCurveTo(W * 0.02, H * 0.5, W * 0.22, H * 0.86, W * 0.48, H + 10); },\n    () => { ctx.moveTo(W * 0.22, H * 0.73); ctx.bezierCurveTo(W * 0.4, H * 0.86, W * 0.5, H * 0.74, W * 0.63, H * 0.62); },\n  ];\n  for (const pass of [0, 1])\n    for (const a of avenues) {\n      ctx.beginPath();\n      a();\n      ctx.strokeStyle = pass ? \"#f7cc66\" : \"#c79a3c\";\n      ctx.lineWidth = pass ? 5.4 : 7.2;\n      ctx.stroke();\n    }\n  ctx.restore();\n\n  // railway and station\n  const rail = () => {\n    ctx.beginPath();\n    ctx.moveTo(-10, H * 0.3);\n    ctx.bezierCurveTo(W * 0.2, H * 0.02, W * 0.56, H * 0.2, W + 10, H * 0.085);\n  };\n  ctx.lineCap = \"butt\";\n  rail();\n  ctx.strokeStyle = INK;\n  ctx.lineWidth = 3;\n  ctx.stroke();\n  rail();\n  ctx.setLineDash([7, 7]);\n  ctx.strokeStyle = \"#fffdf6\";\n  ctx.lineWidth = 1.5;\n  ctx.stroke();\n  ctx.setLineDash([]);\n  ctx.save();\n  ctx.translate(W * 0.795, H * 0.13);\n  ctx.rotate(-0.1);\n  ctx.fillStyle = INK;\n  ctx.fillRect(-17, -5, 34, 10);\n  ctx.restore();\n\n  // lettering\n  label(\"OLD TOWN\", W * 0.15, H * 0.5, { size: 10.5, gap: 4, color: \"#8a745a\" });\n  label(\"LANTERN QUARTER\", W * 0.14, H * 0.16 + 26, { size: 7.5, gap: 2.4, color: \"#8a745a\" });\n  label(\"EASTBANK\", W * 0.8, H * 0.24, { size: 10.5, gap: 4, color: \"#8a745a\" });\n  label(\"THE STAITHES\", W * 0.6, H * 0.62 + 12, { size: 7.5, gap: 2.4, color: \"#8a745a\", rot: 0.5 });\n  label(\"Quill River\", W * 0.305, H * 0.335, { size: 8.5, italic: true, color: \"#3f7482\", rot: 1.25, halo: \"none\" });\n  label(\"Marrow Sound\", W * 0.885, H * 0.85, { size: 13, italic: true, gap: 1.5, color: \"#3f7482\", rot: -0.5, halo: \"none\" });\n  label(\"Glasshouse Gardens\", W * 0.6, H * 0.3 + 30, { size: 7.5, italic: true, color: \"#4f7040\", rot: -0.25, halo: \"rgba(195,215,166,0.9)\" });\n  label(\"Tanners Green\", W * 0.115, H * 0.76 + 2, { size: 7, italic: true, color: \"#4f7040\", rot: 0.2, halo: \"rgba(195,215,166,0.9)\" });\n  label(\"Bellfounder Avenue\", W * 0.445, H * 0.405 - 9, { size: 6.6, color: \"#7a5a1c\", rot: -0.08 });\n  label(\"Saltway\", W * 0.565, H * 0.36, { size: 6.6, color: \"#7a5a1c\", rot: 1.1 });\n  label(\"Ropewalk\", W * 0.115, H * 0.615, { size: 6.6, color: \"#7a5a1c\", rot: 1.02 });\n  label(\"Quillhaven Central\", W * 0.795, H * 0.13 - 13, { size: 6.6, bold: true, rot: -0.1 });\n  label(\"to Marrow Isle\", W * 0.925, H * 0.72, { size: 6.4, italic: true, color: \"#3f7482\", rot: 1.35, halo: \"none\" });\n\n  // numbered spots the pin lands on\n  places.forEach((p, i) => {\n    const x = W * p.x;\n    const y = H * p.y;\n    ctx.beginPath();\n    ctx.arc(x, y, 6.6, 0, Math.PI * 2);\n    ctx.fillStyle = \"#fffdf6\";\n    ctx.fill();\n    ctx.beginPath();\n    ctx.arc(x, y, 5.2, 0, Math.PI * 2);\n    ctx.fillStyle = \"#b5321d\";\n    ctx.fill();\n    label(String(i + 1), x, y + 0.4, { size: 7, bold: true, color: \"#fffdf6\", halo: \"none\" });\n    label(p.name, x, y + 13.5, { size: 6.8, bold: true, color: \"#3b2f26\" });\n  });\n\n  // north point\n  ctx.save();\n  ctx.translate(W * 0.925, H * 0.565 + 44);\n  ctx.strokeStyle = \"#3f7482\";\n  ctx.fillStyle = \"#3f7482\";\n  ctx.lineWidth = 0.8;\n  ctx.beginPath();\n  ctx.arc(0, 0, 12, 0, Math.PI * 2);\n  ctx.stroke();\n  for (let k = 0; k < 4; k++) {\n    ctx.rotate(Math.PI / 2);\n    ctx.beginPath();\n    ctx.moveTo(0, -16);\n    ctx.lineTo(3, 0);\n    ctx.lineTo(-3, 0);\n    ctx.closePath();\n    k === 3 ? ctx.fill() : ctx.stroke();\n  }\n  ctx.restore();\n  label(\"N\", W * 0.925, H * 0.565 + 22, { size: 7.5, bold: true, color: \"#3f7482\", halo: \"none\" });\n\n  // margin, neat line and grid letters\n  const M = 15;\n  ctx.fillStyle = PAPER;\n  ctx.fillRect(0, 0, W, M);\n  ctx.fillRect(0, H - M, W, M);\n  ctx.fillRect(0, 0, M, H);\n  ctx.fillRect(W - M, 0, M, H);\n  ctx.strokeStyle = INK;\n  ctx.lineWidth = 1.3;\n  ctx.strokeRect(M, M, W - M * 2, H - M * 2);\n  ctx.lineWidth = 0.5;\n  ctx.strokeRect(M - 3, M - 3, W - M * 2 + 6, H - M * 2 + 6);\n  for (let c = 0; c < COLS; c++) {\n    label(\"ABCDEFGH\"[c], (c + 0.5) * (W / COLS), 6.4, { size: 6.5, bold: true, halo: \"none\" });\n    label(\"ABCDEFGH\"[c], (c + 0.5) * (W / COLS), H - 5.6, { size: 6.5, bold: true, halo: \"none\" });\n  }\n  for (let r = 0; r < 2; r++) {\n    label(String(r + 1), 6, (r + 0.5) * (H / 2), { size: 6.5, bold: true, halo: \"none\" });\n    label(String(r + 1), W - 6, (r + 0.5) * (H / 2), { size: 6.5, bold: true, halo: \"none\" });\n  }\n\n  // title box and scale bar\n  ctx.fillStyle = PAPER;\n  ctx.fillRect(M + 9, M + 9, 138, 50);\n  ctx.strokeStyle = INK;\n  ctx.lineWidth = 1;\n  ctx.strokeRect(M + 9, M + 9, 138, 50);\n  ctx.lineWidth = 0.4;\n  ctx.strokeRect(M + 11.5, M + 11.5, 133, 45);\n  label(town.name.toUpperCase(), M + 78, M + 24, { size: 12.5, bold: true, gap: 2.6, halo: \"none\" });\n  label(town.sub, M + 78, M + 37, { size: 7.4, italic: true, halo: \"none\" });\n  for (let k = 0; k < 4; k++) {\n    ctx.fillStyle = k % 2 ? PAPER : INK;\n    ctx.fillRect(M + 30 + k * 16, M + 46.5, 16, 2.6);\n  }\n  ctx.strokeStyle = INK;\n  ctx.lineWidth = 0.5;\n  ctx.strokeRect(M + 30, M + 46.5, 64, 2.6);\n  label(town.scale, M + 116, M + 48, { size: 5.6, halo: \"none\" });\n\n  // paper tooth, and fibres rubbed pale along every fold\n  for (let i = 0; i < 9000; i++) {\n    ctx.fillStyle = rnd() < 0.6 ? \"rgba(90,70,40,0.05)\" : \"rgba(255,255,255,0.08)\";\n    ctx.fillRect(rnd() * W, rnd() * H, 1, 1);\n  }\n  const rub = (x0: number, y0: number, x1: number, y1: number) => {\n    const len = Math.hypot(x1 - x0, y1 - y0);\n    for (let i = 0; i < len * 1.6; i++) {\n      const t = rnd();\n      const off = (rnd() - 0.5) * (rnd() < 0.85 ? 1.6 : 5);\n      ctx.fillStyle = `rgba(255,252,244,${0.25 + rnd() * 0.5})`;\n      ctx.fillRect(x0 + (x1 - x0) * t + (x0 === x1 ? off : 0), y0 + (y1 - y0) * t + (y0 === y1 ? off : 0), x0 === x1 ? 1 : 1 + rnd() * 3, y0 === y1 ? 1 : 1 + rnd() * 3);\n    }\n  };\n  for (let c = 1; c < COLS; c++) rub((W / COLS) * c, 0, (W / COLS) * c, H);\n  rub(0, H / 2, W, H / 2);\n}\n\n/** Soft shading at whichever edges of a panel meet a fold. */\nfunction creases(c: number, r: number, back: boolean) {\n  const dark = (dir: string, a: number) => `linear-gradient(${dir}, rgba(70,48,22,${a}) 0, rgba(70,48,22,${a * 0.35}) 5px, rgba(70,48,22,0) 22px)`;\n  const out: string[] = [];\n  // valleys shade both sides, ridges shade only the side facing away from the lamp\n  const ridgeRight = c % 2 === 1; // fold on this panel's right edge is a ridge\n  if (c < COLS - 1) out.push(dark(\"270deg\", ridgeRight ? 0.05 : 0.26));\n  if (c > 0) out.push(dark(\"90deg\", ridgeRight ? 0.24 : 0.3));\n  out.push(r === 0 ? dark(\"0deg\", 0.13) : dark(\"180deg\", 0.2));\n  // the lamp is up and to the left: panels leaning toward it are lifted, the others dimmed\n  out.push(c % 2 === 1 ? \"linear-gradient(100deg, rgba(255,255,255,0.2), rgba(255,255,255,0.04))\" : \"linear-gradient(100deg, rgba(60,40,15,0.05), rgba(60,40,15,0.17))\");\n  if (back) out.push(\"linear-gradient(0deg, rgba(60,40,15,0.06), rgba(60,40,15,0.06))\");\n  return out.join(\", \");\n}\n\nconst face = { backfaceVisibility: \"hidden\", WebkitBackfaceVisibility: \"hidden\" } as const;\n\nexport default function FoldedPaperStreetMap() {\n  const reduce = !!useReducedMotion();\n  const wrapRef = useRef<HTMLDivElement>(null);\n  const sceneRef = useRef<HTMLDivElement>(null);\n  const [mapUrl, setMapUrl] = useState(\"\");\n  const [open, setOpen] = useState(false);\n  const [tucked, setTucked] = useState(true);\n  const [pin, setPin] = useState(-1);\n  const flags = useRef({ paused: false, touched: false, open: false });\n\n  const px = useMotionValue(0);\n  const py = useMotionValue(0);\n  const rz = useSpring(useTransform(px, (v) => TURN + v * 4), { stiffness: 60, damping: 14 });\n  const rx = useSpring(useTransform(py, (v) => TIP - v * 4), { stiffness: 60, damping: 14 });\n\n  // print the plan once and hand it to every panel as one picture\n  useEffect(() => {\n    const cv = document.createElement(\"canvas\");\n    cv.width = MW * 3;\n    cv.height = MH * 3;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    ctx.scale(3, 3);\n    drawPlan(ctx, MW, MH);\n    let url = \"\";\n    cv.toBlob((b) => {\n      if (!b) return;\n      url = URL.createObjectURL(b);\n      setMapUrl(url);\n    }, \"image/png\");\n    return () => {\n      if (url) URL.revokeObjectURL(url);\n    };\n  }, []);\n\n  const unfold = useCallback((to: boolean) => {\n    flags.current.open = to;\n    setOpen(to);\n    if (to) setTucked(false);\n    else setPin(-1);\n  }, []);\n\n  // once it is shut, the lower panels are put away so nothing shows through the cover\n  useEffect(() => {\n    if (open) return;\n    const t = window.setTimeout(() => !flags.current.open && setTucked(true), reduce ? 0 : 1500);\n    return () => window.clearTimeout(t);\n  }, [open, reduce]);\n\n  // self-playing demo: unfold, drop the pin, then move it from place to place\n  useEffect(() => {\n    if (reduce) {\n      unfold(true);\n      setPin(1);\n      return;\n    }\n    const first = window.setTimeout(() => unfold(true), 220);\n    return () => window.clearTimeout(first);\n  }, [reduce, unfold]);\n  useEffect(() => {\n    if (!open || reduce) return;\n    const f = flags.current;\n    const drop = window.setTimeout(() => setPin((p) => (p < 0 ? 1 : p)), 1250);\n    const loop = window.setInterval(() => {\n      if (f.paused || f.touched || document.hidden) return;\n      setPin((p) => (p + 1) % places.length);\n    }, 3400);\n    return () => {\n      window.clearTimeout(drop);\n      window.clearInterval(loop);\n    };\n  }, [open, reduce]);\n\n  useEffect(() => {\n    const wrap = wrapRef.current;\n    const scene = sceneRef.current;\n    if (!wrap || !scene) return;\n    const measure = () => {\n      const sc = clamp(wrap.clientWidth / 870, 0.4, 1.12);\n      wrap.style.height = `${Math.round(SCENE_H * sc)}px`;\n      scene.style.transform = `translateX(-50%) scale(${sc.toFixed(4)})`;\n    };\n    measure();\n    const ro = new ResizeObserver(measure);\n    ro.observe(wrap);\n    return () => ro.disconnect();\n  }, []);\n\n  const onMove = (e: React.PointerEvent) => {\n    if (reduce || e.pointerType === \"touch\") return;\n    const r = e.currentTarget.getBoundingClientRect();\n    px.set(((e.clientX - r.left) / r.width) * 2 - 1);\n    py.set(((e.clientY - r.top) / r.height) * 2 - 1);\n  };\n\n  const spring = (delay: number) => (reduce ? { duration: 0 } : { type: \"spring\" as const, stiffness: 105, damping: 13.5, mass: 1, delay });\n  const colDelay = (i: number) => (open ? Math.max(0, i - 1) * 0.09 : 0.4 + (COLS - 1 - i) * 0.08);\n  const rowDelay = (i: number) => (open ? 0.36 + i * 0.1 : (COLS - 1 - i) * 0.06);\n  const colAngle = (i: number) => (open ? (i === 0 ? REST : i % 2 ? -2 * REST : 2 * REST) : i === 0 ? 0 : i % 2 ? -FOLD : FOLD);\n\n  const active = pin >= 0 ? places[pin] : null;\n  const pinAt = (c: number, r: number) => {\n    if (!active || !open) return null;\n    const x = active.x * MW;\n    const y = active.y * MH;\n    if (Math.floor(x / PW) !== c || Math.floor(y / PH) !== r) return null;\n    return { id: active.id, x: x - c * PW, y: y - r * PH };\n  };\n\n  const front = (c: number, r: number) => (\n    <div className=\"absolute inset-0\" style={{ ...face, backgroundColor: PAPER, backgroundImage: mapUrl ? `url(${mapUrl})` : undefined, backgroundSize: `${MW}px ${MH}px`, backgroundPosition: `${-c * PW}px ${-r * PH}px` }}>\n      <div className=\"absolute inset-0\" style={{ background: creases(c, r, false) }} />\n    </div>\n  );\n  const index = (c: number, r: number) => (\n    <div className=\"absolute inset-0 overflow-hidden\" style={{ ...face, transform: r ? \"rotateX(180deg)\" : \"rotateY(180deg)\", backgroundColor: BACK }}>\n      <div aria-hidden=\"true\" className=\"absolute inset-[14px] font-mono text-[5.6px] leading-[8.6px]\" style={{ color: INK }}>\n        <div className=\"mb-[5px] flex justify-between border-b pb-[3px] text-[6px] font-bold uppercase tracking-[0.2em]\" style={{ borderColor: INK }}>\n          <span>Street index</span>\n          <span>{\"ABCD\"[c]}</span>\n        </div>\n        {Array.from({ length: 21 }, (_, k) => {\n          const s = streets[(k + c * 5 + r * 9) % streets.length];\n          return (\n            <div key={k} className=\"flex justify-between gap-1\">\n              <span className=\"truncate\">{s}</span>\n              <span>{\"ABCD\"[(k + c) % 4]}{(k % 2) + 1}</span>\n            </div>\n          );\n        })}\n      </div>\n      <div className=\"absolute inset-0\" style={{ background: creases(c, r, true) }} />\n    </div>\n  );\n  const cover = (\n    <div className=\"absolute inset-0 overflow-hidden\" style={{ ...face, transform: \"rotateY(180deg)\", backgroundColor: RED }}>\n      <div aria-hidden=\"true\" className=\"absolute -right-[58px] top-[70px] h-[190px] w-[190px] rounded-full\" style={{ border: \"1.5px solid rgba(255,244,226,0.5)\", boxShadow: \"0 0 0 16px rgba(226,68,43,1), 0 0 0 17.5px rgba(255,244,226,0.42), 0 0 0 34px rgba(226,68,43,1), 0 0 0 35.5px rgba(255,244,226,0.32), 0 0 0 54px rgba(226,68,43,1), 0 0 0 55.5px rgba(255,244,226,0.22)\" }} />\n      <div className=\"absolute left-[14px] right-[12px] top-[16px] text-[#fff4e2]\">\n        <div className=\"font-mono text-[6.5px] font-semibold uppercase tracking-[0.26em]\">{town.edition}</div>\n        <div className=\"mt-[9px] text-[25px] font-black uppercase leading-[0.92] tracking-[-0.02em]\">{town.name}</div>\n        <div className=\"mt-[7px] text-[9px] font-medium leading-tight\">{town.sub}</div>\n      </div>\n      <div className=\"absolute bottom-[14px] left-[14px] right-[14px] flex items-end justify-between font-mono text-[6.5px] font-semibold uppercase tracking-[0.2em] text-[#fff4e2]\">\n        <span>Street plan</span>\n        <span>{town.scale}</span>\n      </div>\n      <div aria-hidden=\"true\" className=\"absolute inset-[6px]\" style={{ border: \"1px solid rgba(255,244,226,0.7)\" }} />\n      <div aria-hidden=\"true\" className=\"absolute inset-0\" style={{ background: \"linear-gradient(118deg, rgba(255,255,255,0.26) 0%, rgba(255,255,255,0.03) 34%, rgba(0,0,0,0) 60%, rgba(0,0,0,0.18) 100%)\" }} />\n    </div>\n  );\n\n  const pinNode = (c: number, r: number) => {\n    const at = pinAt(c, r);\n    return (\n      <AnimatePresence>\n        {at && (\n          <div key={at.id} className=\"absolute left-0 top-0\" style={{ transformStyle: \"preserve-3d\", transform: `translate3d(${at.x}px, ${at.y}px, 0.8px)` }}>\n            {/* its shadow slides in across the paper as the pin comes down */}\n            <motion.div aria-hidden=\"true\" className=\"absolute left-0 top-0\" style={{ width: 0, height: 0 }} initial={reduce ? false : { x: 150, y: 120, opacity: 0, scale: 1.7 }} animate={{ x: 0, y: 0, opacity: 1, scale: 1 }} exit={{ x: 80, y: 64, opacity: 0, transition: { duration: 0.16 } }} transition={{ duration: 0.36, ease: [0.5, 0, 0.9, 0.6] }}>\n              <div className=\"absolute rounded-full\" style={{ left: -1, top: -1.5, width: 44, height: 3, transformOrigin: \"0 50%\", transform: \"rotate(37deg)\", background: \"linear-gradient(90deg, rgba(40,25,10,0.55), rgba(40,25,10,0.3))\", filter: \"blur(0.9px)\" }} />\n              <div className=\"absolute rounded-full\" style={{ left: 22, top: 13, width: 26, height: 20, transform: \"rotate(37deg)\", background: \"rgba(40,25,10,0.42)\", filter: \"blur(3.5px)\" }} />\n            </motion.div>\n            <motion.div className=\"absolute left-0 top-0\" style={{ transformStyle: \"preserve-3d\", width: 0, height: 0 }} initial={reduce ? false : { z: 300 }} animate={{ z: 0 }} exit={{ z: 170, transition: { duration: 0.16, ease: \"easeIn\" } }} transition={{ duration: 0.36, ease: [0.5, 0, 0.9, 0.6] }}>\n              <div className=\"absolute left-0 top-0\" style={{ transformStyle: \"preserve-3d\", transform: `rotateZ(${-TURN}deg) rotateX(-80deg)` }}>\n                <motion.div className=\"absolute\" style={{ left: -15, top: -68, width: 30, height: 68, transformOrigin: \"50% 100%\" }} initial={reduce ? false : { rotate: 0, opacity: 0 }} animate={reduce ? { rotate: 4, opacity: 1 } : { rotate: [0, 0, -11, 8, -4, 5, 4], opacity: 1 }} exit={{ opacity: 0, transition: { duration: 0.16 } }} transition={{ rotate: { duration: 1.05, times: [0, 0.34, 0.46, 0.6, 0.74, 0.88, 1], ease: \"easeOut\" }, opacity: { duration: 0.1 } }}>\n                  <div className=\"absolute bottom-0 left-1/2 h-[42px] w-[2.4px] -translate-x-1/2 rounded-b-full\" style={{ background: \"linear-gradient(90deg, #6c7178 0%, #f4f6f8 38%, #b9bec4 60%, #50545a 100%)\" }} />\n                  <div className=\"absolute left-1/2 top-[25px] h-[4px] w-[7px] -translate-x-1/2 rounded-[2px]\" style={{ background: \"linear-gradient(90deg, #7a0f06, #d93a26 45%, #7a0f06)\" }} />\n                  <div className=\"absolute left-1/2 top-0 h-[28px] w-[28px] -translate-x-1/2 rounded-full\" style={{ background: \"radial-gradient(circle at 34% 28%, #ffd9cf 0%, #ff7d66 9%, #ea3f27 30%, #c22512 58%, #7c1207 86%, #9a2a18 100%)\", boxShadow: \"inset -2px -3px 5px rgba(60,5,0,0.55), inset 1px 1px 1px rgba(255,255,255,0.3), 0 1px 2px rgba(0,0,0,0.35)\" }}>\n                    <span className=\"absolute left-[6px] top-[4.5px] h-[6px] w-[9px] rounded-full\" style={{ background: \"rgba(255,255,255,0.9)\", filter: \"blur(1px)\", transform: \"rotate(-28deg)\" }} />\n                    <span className=\"absolute bottom-[3px] right-[4px] h-[7px] w-[12px] rounded-full\" style={{ background: \"rgba(255,170,140,0.45)\", filter: \"blur(2px)\", transform: \"rotate(-30deg)\" }} />\n                  </div>\n                </motion.div>\n              </div>\n            </motion.div>\n          </div>\n        )}\n      </AnimatePresence>\n    );\n  };\n\n  const column = (i: number): React.ReactNode => (\n    <motion.div key={i} className=\"absolute top-0\" style={{ left: i === 0 ? 0 : PW, width: PW, height: PH, transformStyle: \"preserve-3d\", transformOrigin: i === 0 ? \"100% 50%\" : \"0 50%\" }} initial={false} animate={{ rotateY: colAngle(i) }} transition={spring(colDelay(i))}>\n      {front(i, 0)}\n      {i === COLS - 1 ? cover : index(i, 0)}\n      {pinNode(i, 0)}\n      <div className=\"absolute left-0\" style={{ top: PH, width: PW, height: PH, transformStyle: \"preserve-3d\", transform: \"translateZ(0.6px)\", visibility: tucked ? \"hidden\" : \"visible\" }}>\n        <motion.div className=\"absolute inset-0\" style={{ transformStyle: \"preserve-3d\", transformOrigin: \"50% 0\" }} initial={false} animate={{ rotateX: open ? 0 : 179.4 }} transition={spring(rowDelay(i))}>\n          {front(i, 1)}\n          {index(i, 1)}\n          {pinNode(i, 1)}\n        </motion.div>\n      </div>\n      {i < COLS - 1 && column(i + 1)}\n    </motion.div>\n  );\n\n  return (\n    <section className=\"relative overflow-hidden bg-[#1c3fb0]\">\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\" style={{ background: \"radial-gradient(70% 62% at 46% 40%, #3f69e6 0%, #2549c4 42%, #152f8e 100%)\" }} />\n      <div\n        ref={wrapRef}\n        onPointerMove={onMove}\n        onPointerEnter={() => (flags.current.paused = true)}\n        onPointerLeave={() => {\n          flags.current.paused = false;\n          px.set(0);\n          py.set(0);\n        }}\n        className=\"relative mx-auto w-full overflow-hidden\"\n        style={{ height: SCENE_H }}\n      >\n        <div ref={sceneRef} className=\"absolute left-1/2 top-0 select-none\" style={{ width: SCENE_W, height: SCENE_H, transformOrigin: \"50% 0\", transform: \"translateX(-50%)\", perspective: \"2400px\", perspectiveOrigin: \"50% 40%\" }}>\n          <motion.div className=\"absolute\" style={{ left: (SCENE_W - MW) / 2, top: (SCENE_H - MH) / 2 + 6, width: MW, height: MH, transformStyle: \"preserve-3d\", rotateX: rx, rotateZ: rz }}>\n            <motion.div className={`absolute inset-0 ${open ? \"\" : \"cursor-pointer\"}`} style={{ transformStyle: \"preserve-3d\" }} initial={false} animate={{ x: open ? 0 : PW * 1.5, y: open ? 0 : PH * 0.5 }} transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 70, damping: 16 }} onClick={() => !open && ((flags.current.touched = true), unfold(true))}>\n              {/* shadow on the table, growing with the sheet */}\n              <motion.div aria-hidden=\"true\" className=\"absolute left-0 top-0 rounded-[6px] bg-[#040d33]\" style={{ width: MW, height: MH, transformOrigin: \"0 0\", filter: \"blur(22px)\", opacity: 0.55, x: 22, y: 30 }} initial={false} animate={{ scaleX: open ? 1 : 1 / COLS, scaleY: open ? 1 : 0.5 }} transition={reduce ? { duration: 0 } : { duration: 0.9, ease: [0.3, 0.7, 0.3, 1] }} />\n              <motion.div aria-hidden=\"true\" className=\"absolute left-0 top-0 bg-[#040d33]\" style={{ width: MW, height: MH, transformOrigin: \"0 0\", filter: \"blur(5px)\", opacity: 0.5, x: 5, y: 7 }} initial={false} animate={{ scaleX: open ? 1 : 1 / COLS, scaleY: open ? 1 : 0.5 }} transition={reduce ? { duration: 0 } : { duration: 0.9, ease: [0.3, 0.7, 0.3, 1] }} />\n              {column(0)}\n            </motion.div>\n          </motion.div>\n        </div>\n      </div>\n\n      <div className=\"relative flex flex-col gap-3 px-5 pb-6 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-blue-100\">\n          <span className=\"text-white\">\n            {town.name} / {town.edition}\n          </span>\n          <span className=\"block\" aria-live=\"polite\">\n            {active ? `${pin + 1}. ${active.name}, ${active.note}` : open ? \"Pick a place to pin\" : \"Press the cover to unfold\"}\n          </span>\n        </p>\n        <div className=\"flex flex-wrap items-center gap-2\">\n          {places.map((p, i) => (\n            <button\n              key={p.id}\n              type=\"button\"\n              disabled={!open}\n              aria-pressed={pin === i}\n              aria-label={`Drop the pin on ${p.name}`}\n              onClick={() => {\n                flags.current.touched = true;\n                setPin(i);\n              }}\n              className={`h-10 min-w-[40px] rounded-full px-3 font-mono text-[11px] font-semibold uppercase tracking-[0.14em] outline-none transition-colors focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-offset-2 focus-visible:ring-offset-[#1c3fb0] disabled:opacity-50 ${pin === i ? \"bg-white text-[#152f8e]\" : \"bg-[#10277a] text-white hover:bg-[#0c1f66]\"}`}\n            >\n              {i + 1}\n            </button>\n          ))}\n          <button\n            type=\"button\"\n            aria-expanded={open}\n            onClick={() => {\n              flags.current.touched = true;\n              unfold(!open);\n            }}\n            className=\"h-10 rounded-full bg-[#10277a] px-4 font-mono text-[11px] font-semibold uppercase tracking-[0.14em] text-white outline-none transition-colors hover:bg-[#0c1f66] focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-offset-2 focus-visible:ring-offset-[#1c3fb0]\"\n          >\n            {open ? \"Fold it up\" : \"Unfold\"}\n          </button>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
