{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"layered-plywood-relief-island-model-with-walking-routes-lit-bulb-by-bulb-on-a-walnut-plinth","type":"registry:block","title":"Layered Plywood Relief Island Model with Walking Routes Lit Bulb by Bulb on a Walnut Plinth","description":"Relief model of an island cut from stacked plywood, drawn on a canvas, with walking routes that light up bulb by bulb. Free React code and HTML to copy.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/layered-plywood-relief-island-model-with-walking-routes-lit-bulb-by-bulb-on-a-walnut-plinth.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { motion, useMotionValue, useReducedMotion, useSpring } from \"framer-motion\";\n\n/** The made-up island and what is engraved on its brass plate. */\nconst island = { name: \"Skerrow\", plate: \"Relief model no. 12\", scale: \"1 : 25 000\" };\n\n/**\n * Walks that can be lit. `path` is a list of [u, v] points across the board, each from -1 to 1\n * (u left to right, v back to front). A smooth line is drawn through them and bulbs are set along it.\n */\nconst routes = [\n  { id: \"summit\", name: \"Summit path\", detail: \"Quay to Hollin Top, 6.4 km\", flag: \"#e2452c\", path: [[0.08, 0.4], [0.2, 0.27], [0.05, 0.17], [-0.12, 0.2], [-0.3, 0.1], [-0.32, -0.05], [-0.13, -0.07]] },\n  { id: \"light\", name: \"Lighthouse walk\", detail: \"Quay to Gannet Light, 5.1 km\", flag: \"#f4f1e8\", path: [[0.08, 0.4], [0.2, 0.27], [0.4, 0.3], [0.6, 0.22], [0.72, 0.05]] },\n  { id: \"shore\", name: \"West shore\", detail: \"Quay to Selkie Cove, 9.8 km\", flag: \"#2f9c95\", path: [[0.08, 0.4], [-0.1, 0.38], [-0.35, 0.3], [-0.55, 0.2], [-0.66, 0], [-0.55, -0.3], [-0.3, -0.5]] },\n];\n\n// Design pixels.\nconst LW = 700; // drawing size\nconst LH = 500;\nconst BW = 520; // board, left to right\nconst BD = 400; // board, back to front\nconst ROT = -0.34; // how far the board is turned on the table, in radians\nconst CY = 0.6; // camera: how much the ground is squashed\nconst SZ = 0.8; // camera: how tall one pixel of height looks\nconst PLINTH = 46;\nconst STEP = 5.4; // thickness of one plywood sheet\nconst NL = 9; // number of sheets at the summit\nconst FRAME = 9;\nconst GAP = 13.5; // distance between bulbs\nconst BULB = 2.9;\n\nconst clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));\nconst cosR = Math.cos(ROT);\nconst sinR = Math.sin(ROT);\nconst GD = BW * Math.abs(sinR) + BD * cosR; // depth of the turned board on the ground\nconst GCY = GD / 2;\nconst TOP0 = 262 - GCY * CY; // screen y of the back of the ground plane\n\nconst hash = (x: number, y: number) => {\n  let n = (x * 374761393 + y * 668265263) | 0;\n  n = (n ^ (n >>> 13)) * 1274126177;\n  n = n ^ (n >>> 16);\n  return ((n >>> 0) % 100000) / 100000;\n};\nconst vnoise = (x: number, y: number) => {\n  const xi = Math.floor(x);\n  const yi = Math.floor(y);\n  const xf = x - xi;\n  const yf = y - yi;\n  const u = xf * xf * (3 - 2 * xf);\n  const v = yf * yf * (3 - 2 * yf);\n  const a = hash(xi, yi);\n  const b = hash(xi + 1, yi);\n  const c = hash(xi, yi + 1);\n  const d = hash(xi + 1, yi + 1);\n  return a + (b - a) * u + (c - a) * v + (a - b - c + d) * u * v;\n};\nconst fbm = (x: number, y: number) => {\n  let s = 0;\n  let a = 0.5;\n  let f = 1;\n  for (let i = 0; i < 4; i++) {\n    s += a * vnoise(x * f, y * f);\n    a *= 0.5;\n    f *= 2.03;\n  }\n  return s / 0.9375;\n};\n\n/** Height of the land at a point on the board. Above zero is land, below is sea bed. */\nfunction elev(u: number, v: number) {\n  const wu = u + 0.22 * (fbm(u * 1.9 + 3, v * 1.9) - 0.5);\n  const wv = v + 0.22 * (fbm(u * 1.9, v * 1.9 + 7) - 0.5);\n  const a = (wu + 0.02) / 0.84;\n  const b = wv / 0.72;\n  let h = 0.72 * (1 - Math.sqrt(a * a + b * b));\n  h += 0.74 * Math.exp(-((wu + 0.26) ** 2 + (wv + 0.1) ** 2) / 0.05); // Hollin Top\n  h += 0.46 * Math.exp(-((wu - 0.4) ** 2 + (wv - 0.02) ** 2) / 0.022); // the east hill\n  h -= 0.6 * Math.exp(-((wu - 0.1) ** 2 + (wv - 0.56) ** 2) / 0.03); // the harbour bay\n  h -= 0.3 * Math.exp(-((wu - 0.1) ** 2 + (wv + 0.62) ** 2) / 0.03);\n  h += 0.44 * (fbm(u * 3.3 + 11, v * 3.3 + 5) - 0.5);\n  h += 0.5 * Math.exp(-((u + 0.76) ** 2 + (v - 0.6) ** 2) / 0.005); // the skerry\n  h -= 3 * (Math.max(0, Math.abs(u) - 0.8) + Math.max(0, Math.abs(v) - 0.8));\n  return h;\n}\nconst sheets = (h: number) => (h > 0 ? Math.min(NL, 1 + Math.floor(h / 0.15)) : 0);\n\n/** Board point (in pixels from its centre) and height to a point on the drawing. */\nconst project = (wx: number, wy: number, z: number) => ({ x: LW / 2 + wx * cosR - wy * sinR, y: TOP0 + (GCY + wx * sinR + wy * cosR) * CY - z * SZ });\n\n/** A smooth line through the route's points, cut into equal steps. */\nfunction along(path: number[][], gap: number) {\n  const pts = path.map(([u, v]) => [(u * BW) / 2, (v * BD) / 2]);\n  const fine: number[][] = [];\n  for (let i = 0; i < pts.length - 1; i++) {\n    const p0 = pts[Math.max(0, i - 1)];\n    const p1 = pts[i];\n    const p2 = pts[i + 1];\n    const p3 = pts[Math.min(pts.length - 1, i + 2)];\n    for (let k = 0; k < 24; k++) {\n      const t = k / 24;\n      const t2 = t * t;\n      const t3 = t2 * t;\n      fine.push([0, 1].map((d) => 0.5 * (2 * p1[d] + (p2[d] - p0[d]) * t + (2 * p0[d] - 5 * p1[d] + 4 * p2[d] - p3[d]) * t2 + (3 * p1[d] - p0[d] - 3 * p2[d] + p3[d]) * t3)));\n    }\n  }\n  fine.push(pts[pts.length - 1]);\n  const out = [fine[0]];\n  let need = gap;\n  for (let i = 1; i < fine.length; i++) {\n    let seg = Math.hypot(fine[i][0] - fine[i - 1][0], fine[i][1] - fine[i - 1][1]);\n    let from = fine[i - 1];\n    while (seg >= need) {\n      const t = need / seg;\n      from = [from[0] + (fine[i][0] - from[0]) * t, from[1] + (fine[i][1] - from[1]) * t];\n      out.push(from);\n      seg -= need;\n      need = gap;\n    }\n    need -= seg;\n  }\n  return { fine, stops: out };\n}\n\ntype Bulb = { x: number; y: number };\n\n/** Builds the whole model once: every pixel of plywood, sea, walnut and shadow. */\nfunction buildModel(S: number) {\n  const CW = Math.ceil(LW * S);\n  const CH = Math.ceil(LH * S);\n  const GH = Math.ceil(GD * S) + 2;\n  const cx = (LW / 2) * S;\n  const gcy = GCY * S;\n  const PL = PLINTH * S;\n  const ST = STEP * S;\n\n  // heights are worked out on a half-size grid and smoothed up, which also rounds the cut edges\n  const EW = (CW >> 1) + 2;\n  const EH = (GH >> 1) + 2;\n  const E = new Float32Array(EW * EH);\n  for (let j = 0; j < EH; j++)\n    for (let i = 0; i < EW; i++) {\n      const dx = i * 2 - cx;\n      const dy = j * 2 - gcy;\n      E[j * EW + i] = elev((dx * cosR + dy * sinR) / ((BW / 2) * S), (-dx * sinR + dy * cosR) / ((BD / 2) * S));\n    }\n  const H = new Float32Array(CW * GH).fill(-1); // height above the table, -1 where there is no board\n  const M = new Uint8Array(CW * GH); // 1 frame, 2 sea, 3 land\n  const D = new Float32Array(CW * GH);\n  for (let gy = 0; gy < GH; gy++)\n    for (let gx = 0; gx < CW; gx++) {\n      const dx = gx - cx;\n      const dy = gy - gcy;\n      const wx = dx * cosR + dy * sinR;\n      const wy = -dx * sinR + dy * cosR;\n      const ax = Math.abs(wx) / S;\n      const ay = Math.abs(wy) / S;\n      if (ax > BW / 2 || ay > BD / 2) continue;\n      const i = gy * CW + gx;\n      if (ax > BW / 2 - FRAME || ay > BD / 2 - FRAME) {\n        H[i] = PL + 3 * S;\n        M[i] = 1;\n        continue;\n      }\n      const fx = gx / 2;\n      const fy = gy / 2;\n      const x0 = fx | 0;\n      const y0 = fy | 0;\n      const tx = fx - x0;\n      const ty = fy - y0;\n      const k = y0 * EW + x0;\n      const e = (E[k] * (1 - tx) + E[k + 1] * tx) * (1 - ty) + (E[k + EW] * (1 - tx) + E[k + EW + 1] * tx) * ty;\n      const n = sheets(e);\n      D[i] = e;\n      M[i] = n ? 3 : 2;\n      H[i] = PL + n * ST;\n    }\n\n  // the three walks, scored into the wood\n  const toGround = (p: number[]) => [cx + (p[0] * cosR - p[1] * sinR) * S, gcy + (p[0] * sinR + p[1] * cosR) * S];\n  const laid = routes.map((r) => along(r.path, GAP));\n  const score = document.createElement(\"canvas\");\n  score.width = CW;\n  score.height = GH;\n  const sc = score.getContext(\"2d\")!;\n  sc.strokeStyle = \"#fff\";\n  sc.lineWidth = 1.5 * S;\n  sc.lineJoin = \"round\";\n  for (const l of laid) {\n    sc.beginPath();\n    l.fine.forEach((p, i) => {\n      const g = toGround(p);\n      i ? sc.lineTo(g[0], g[1]) : sc.moveTo(g[0], g[1]);\n    });\n    sc.stroke();\n  }\n  const scored = sc.getImageData(0, 0, CW, GH).data;\n\n  const img = new ImageData(CW, CH);\n  const px = img.data;\n  const top0 = TOP0 * S;\n  const LX = -0.6; // toward the lamp, on the ground\n  const LY = -0.8;\n  const at = (gx: number, gy: number) => (gx < 0 || gy < 0 || gx >= CW || gy >= GH ? -1 : H[(gy | 0) * CW + (gx | 0)]);\n  const put = (x: number, y: number, r: number, g: number, b: number) => {\n    if (y < 0 || y >= CH) return;\n    const o = (y * CW + x) * 4;\n    px[o] = r;\n    px[o + 1] = g;\n    px[o + 2] = b;\n    px[o + 3] = 255;\n  };\n  for (let gx = 0; gx < CW; gx++) {\n    for (let gy = 0; gy < GH; gy++) {\n      const i = gy * CW + gx;\n      const h = H[i];\n      if (h < 0) continue;\n      const m = M[i];\n      const yTop = top0 + gy * CY - h * SZ;\n      const y0 = Math.floor(yTop);\n      const y1 = Math.floor(yTop + CY);\n      const dx = gx - cx;\n      const dy = gy - gcy;\n      const wx = (dx * cosR + dy * sinR) / S;\n      const wy = (-dx * sinR + dy * cosR) / S;\n      if (y1 > y0) {\n        // how much of the lamp is blocked by taller sheets, and whether this is a lit cut edge\n        let block = 0;\n        for (let k = 1; k <= 8; k++) {\n          const d = k * 1.5 * S;\n          if (at(gx + LX * d, gy + LY * d) - h > d * 0.5) block++;\n        }\n        const shade = 1 - 0.56 * Math.min(1, block / 2.6);\n        const rim = at(gx + LX * 1.6 * S, gy + LY * 1.6 * S) < h - 1 ? 1 : 0;\n        const snug = at(gx - LX * 1.6 * S, gy - LY * 1.6 * S) > h + 1 ? 0.86 : 1;\n        let r = 0;\n        let g = 0;\n        let b = 0;\n        if (m === 3) {\n          const n = Math.round((h - PL) / ST);\n          const t = Math.pow(n / NL, 0.8);\n          const grain = 0.92 + 0.13 * vnoise(wx * 0.05 + n * 31, wy * 1.3 + n * 7) + 0.04 * (hash(gx, gy) - 0.5);\n          const cut = 1 - 0.5 * (scored[i * 4 + 3] / 255);\n          const k = grain * shade * snug * cut * (1 + rim * 0.07);\n          r = (190 + 44 * t) * k;\n          g = (156 + 58 * t) * k;\n          b = (110 + 68 * t) * k;\n        } else if (m === 2) {\n          const e = D[i];\n          const deep = clamp(-e / 0.55, 0, 1);\n          const ring = e < -0.04 && (-e * 8) % 1 < 0.09 ? 1.16 : 1;\n          const edge = e > -0.03 ? 1.25 : 1;\n          const sx = gx / S;\n          const sy = yTop / S;\n          const gloss = Math.exp(-(((sx * 0.62 + sy - 470) / 46) ** 2)) * 30 + Math.exp(-(((sx * 0.62 + sy - 600) / 16) ** 2)) * 16;\n          const k = shade * ring * edge;\n          r = (26 - 18 * deep) * k + gloss * shade;\n          g = (98 - 62 * deep) * k + gloss * shade;\n          b = (108 - 52 * deep) * k + gloss * shade;\n        } else {\n          const k = (0.82 + 0.3 * vnoise((wx + wy) * 0.6, (wx - wy) * 0.03)) * shade * (1 + rim * 0.3);\n          r = 104 * k;\n          g = 66 * k;\n          b = 40 * k;\n        }\n        for (let y = y0; y < y1; y++) put(gx, y, r, g, b);\n      }\n      // the cut face of this column, where the one in front is lower\n      const hf = gy + 1 < GH ? Math.max(0, H[i + CW]) : 0;\n      if (h > hf) {\n        const yEnd = Math.floor(top0 + (gy + 1) * CY - hf * SZ);\n        const turn = clamp((at(gx - 2 * S, gy) - at(gx + 2 * S, gy)) / ST, -1, 1);\n        const u = wx / (BW / 2);\n        const v = wy / (BD / 2);\n        // which side of the plinth this is, for its share of the lamp\n        const side = Math.abs(u) > Math.abs(v);\n        const lit = side ? 1.25 : 0.86;\n        const run = side ? wy : wx;\n        for (let y = y1; y < yEnd; y++) {\n          const z = h - (y - yTop - CY) / SZ;\n          if (z > PL + 0.5 && m === 3) {\n            // plywood edge: three plies to a sheet, browned by the laser\n            const ply = (((z - PL) / ST) * 3) % 1;\n            const k = (0.78 - 0.22 * turn) * (ply < 0.5 ? 1 : 0.66) * (0.9 + 0.2 * hash(gx, y));\n            put(gx, y, 126 * k, 86 * k, 50 * k);\n          } else {\n            const zl = z / S;\n            let k = lit * (0.78 + 0.34 * vnoise(run * 0.55, zl * 0.05 + (side ? 9 : 0))) * (0.62 + 0.38 * clamp(zl / 16, 0, 1));\n            if (zl > PLINTH + 1.6) k *= 1.5;\n            if (zl > 9 && zl < 11.4) put(gx, y, 214 * lit, 170 * lit, 92 * lit);\n            else put(gx, y, 86 * k, 53 * k, 32 * k);\n          }\n        }\n      }\n    }\n  }\n\n  const model = document.createElement(\"canvas\");\n  model.width = CW;\n  model.height = CH;\n  model.getContext(\"2d\")!.putImageData(img, 0, 0);\n\n  const out = document.createElement(\"canvas\");\n  out.width = CW;\n  out.height = CH;\n  const ctx = out.getContext(\"2d\")!;\n  // shadow thrown onto the table\n  const foot = [project(-BW / 2, -BD / 2, 0), project(BW / 2, -BD / 2, 0), project(BW / 2, BD / 2, 0), project(-BW / 2, BD / 2, 0)];\n  for (const [blur, ox, oy, a] of [[34, 30, 20, 0.75], [9, 8, 5, 0.8]] as const) {\n    ctx.save();\n    ctx.shadowColor = `rgba(0,0,0,${a})`;\n    ctx.shadowBlur = blur * S;\n    ctx.shadowOffsetX = (ox + 3000) * S;\n    ctx.shadowOffsetY = oy * S;\n    ctx.beginPath();\n    foot.forEach((p, i) => (i ? ctx.lineTo((p.x - 3000) * S, p.y * S) : ctx.moveTo((p.x - 3000) * S, p.y * S)));\n    ctx.closePath();\n    ctx.fill();\n    ctx.restore();\n  }\n  ctx.drawImage(model, 0, 0);\n\n  // lettering cut into the sea, lying flat on it\n  const flat = project(0, 0, PLINTH);\n  ctx.save();\n  ctx.setTransform(cosR * S, sinR * CY * S, -sinR * S, cosR * CY * S, flat.x * S, flat.y * S);\n  ctx.textAlign = \"center\";\n  ctx.textBaseline = \"middle\";\n  const cutText = (t: string, x: number, y: number, size: number, gap: number, a: number, italic = false) => {\n    ctx.font = `${italic ? \"italic \" : \"600 \"}${size}px Georgia, \"Times New Roman\", serif`;\n    (ctx as unknown as { letterSpacing: string }).letterSpacing = `${gap}px`;\n    ctx.fillStyle = `rgba(0,10,16,${a * 0.9})`;\n    ctx.fillText(t, x + 0.5, y + 0.9);\n    ctx.fillStyle = `rgba(206,236,234,${a})`;\n    ctx.fillText(t, x, y);\n  };\n  cutText(island.name.toUpperCase(), -128, 164, 19, 9, 0.62);\n  cutText(\"Marl Deep\", 176, 150, 10, 1.5, 0.5, true);\n  cutText(\"Gannet Sound\", 150, -158, 10, 1.5, 0.42, true);\n  cutText(\"The Quay\", 62, 128, 7.5, 0.6, 0.6, true);\n  ctx.restore();\n\n  // brass plate on the front of the plinth\n  const plate = project(-98, BD / 2, 40);\n  ctx.save();\n  ctx.setTransform(cosR * S, sinR * CY * S, 0, SZ * S, plate.x * S, plate.y * S);\n  ctx.scale(1.32, 1.32);\n  const brass = ctx.createLinearGradient(0, 0, 0, 20);\n  brass.addColorStop(0, \"#f1d79a\");\n  brass.addColorStop(0.35, \"#c9a05a\");\n  brass.addColorStop(0.7, \"#a37a3a\");\n  brass.addColorStop(1, \"#d6b06a\");\n  ctx.fillStyle = \"rgba(0,0,0,0.5)\";\n  ctx.fillRect(0.6, 1.2, 148, 20);\n  ctx.fillStyle = brass;\n  ctx.fillRect(0, 0, 148, 20);\n  ctx.fillStyle = \"rgba(255,246,214,0.75)\";\n  ctx.fillRect(0, 0, 148, 0.8);\n  for (const sxp of [5.5, 142.5]) {\n    ctx.beginPath();\n    ctx.arc(sxp, 10, 1.7, 0, Math.PI * 2);\n    ctx.fillStyle = \"#7a5a2a\";\n    ctx.fill();\n    ctx.fillStyle = \"#f6e2ae\";\n    ctx.fillRect(sxp - 1.3, 9.7, 2.6, 0.6);\n  }\n  ctx.textAlign = \"center\";\n  ctx.textBaseline = \"middle\";\n  ctx.fillStyle = \"#3a2a10\";\n  ctx.font = '700 7.4px Georgia, \"Times New Roman\", serif';\n  (ctx as unknown as { letterSpacing: string }).letterSpacing = \"2.4px\";\n  ctx.fillText(island.name.toUpperCase(), 74, 7);\n  ctx.font = '5px Georgia, \"Times New Roman\", serif';\n  (ctx as unknown as { letterSpacing: string }).letterSpacing = \"1px\";\n  ctx.fillText(`${island.plate.toUpperCase()}  /  ${island.scale}`, 74, 14.6);\n  ctx.restore();\n\n  // where each bulb sits, and a pennant at the far end of every walk\n  const heightAt = (p: number[]) => PLINTH + Math.max(1, sheets(elev(p[0] / (BW / 2), p[1] / (BD / 2)))) * STEP;\n  const bulbs: Bulb[][] = laid.map((l) => l.stops.map((p) => project(p[0], p[1], heightAt(p))));\n  ctx.setTransform(S, 0, 0, S, 0, 0);\n  laid.forEach((l, k) => {\n    const end = l.fine[l.fine.length - 1];\n    const p = project(end[0], end[1], heightAt(end));\n    ctx.strokeStyle = \"rgba(20,10,0,0.45)\";\n    ctx.lineWidth = 1.2;\n    ctx.beginPath();\n    ctx.moveTo(p.x, p.y);\n    ctx.lineTo(p.x + 13, p.y + 6);\n    ctx.stroke();\n    const pole = ctx.createLinearGradient(p.x - 1, 0, p.x + 1, 0);\n    pole.addColorStop(0, \"#f4f0e6\");\n    pole.addColorStop(1, \"#8d8a84\");\n    ctx.fillStyle = pole;\n    ctx.fillRect(p.x - 0.7, p.y - 21, 1.4, 21);\n    ctx.fillStyle = routes[k].flag;\n    ctx.beginPath();\n    ctx.moveTo(p.x + 0.7, p.y - 21);\n    ctx.lineTo(p.x + 11, p.y - 17.6);\n    ctx.lineTo(p.x + 0.7, p.y - 14.4);\n    ctx.closePath();\n    ctx.fill();\n    ctx.fillStyle = \"rgba(0,0,0,0.18)\";\n    ctx.beginPath();\n    ctx.moveTo(p.x + 0.7, p.y - 17.6);\n    ctx.lineTo(p.x + 11, p.y - 17.6);\n    ctx.lineTo(p.x + 0.7, p.y - 14.4);\n    ctx.closePath();\n    ctx.fill();\n  });\n  return { canvas: out, bulbs };\n}\n\n/** Small pictures of one bulb, dark and lit, and of the light it throws. */\nfunction buildSprites(S: number) {\n  const make = (size: number, draw: (c: CanvasRenderingContext2D) => void) => {\n    const cv = document.createElement(\"canvas\");\n    cv.width = cv.height = Math.ceil(size * S);\n    const c = cv.getContext(\"2d\")!;\n    c.scale(S, S);\n    c.translate(size / 2, size / 2);\n    draw(c);\n    return cv;\n  };\n  const dome = (c: CanvasRenderingContext2D, stops: [number, string][]) => {\n    c.fillStyle = \"rgba(40,20,0,0.4)\";\n    c.beginPath();\n    c.ellipse(1.6, 1.4, BULB + 1.8, (BULB + 1.8) * CY, 0, 0, Math.PI * 2);\n    c.fill();\n    c.fillStyle = \"#6f5222\";\n    c.beginPath();\n    c.ellipse(0, 0.9, BULB + 1.4, (BULB + 1.4) * CY, 0, 0, Math.PI * 2);\n    c.fill();\n    c.fillStyle = \"#d9b469\";\n    c.beginPath();\n    c.ellipse(-0.3, 0.5, BULB + 1.1, (BULB + 1.1) * CY, 0, 0, Math.PI * 2);\n    c.fill();\n    const g = c.createRadialGradient(-1, -2.8, 0.2, 0, -1.4, BULB + 0.4);\n    stops.forEach(([o, col]) => g.addColorStop(o, col));\n    c.fillStyle = g;\n    c.beginPath();\n    c.arc(0, -1.4, BULB, 0, Math.PI * 2);\n    c.fill();\n  };\n  const dark = make(20, (c) => {\n    dome(c, [[0, \"#e6d2ab\"], [0.35, \"#b08c5c\"], [0.8, \"#62431f\"], [1, \"#8a6638\"]]);\n    c.fillStyle = \"rgba(255,244,220,0.85)\";\n    c.beginPath();\n    c.ellipse(-1.2, -2.9, 0.9, 0.55, -0.6, 0, Math.PI * 2);\n    c.fill();\n  });\n  const lit = make(20, (c) => dome(c, [[0, \"#ffffff\"], [0.55, \"#fffbe8\"], [0.85, \"#ffd98a\"], [1, \"#ffa838\"]]));\n  const glow = make(96, (c) => {\n    c.save();\n    c.scale(1, CY);\n    const spill = c.createRadialGradient(0, 2, 0, 0, 2, 46);\n    spill.addColorStop(0, \"rgba(255,190,100,0.42)\");\n    spill.addColorStop(0.3, \"rgba(255,160,70,0.16)\");\n    spill.addColorStop(1, \"rgba(255,130,40,0)\");\n    c.fillStyle = spill;\n    c.fillRect(-48, -48, 96, 96);\n    c.restore();\n    const halo = c.createRadialGradient(0, -1.4, 0, 0, -1.4, 15);\n    halo.addColorStop(0, \"rgba(255,244,214,1)\");\n    halo.addColorStop(0.3, \"rgba(255,206,120,0.5)\");\n    halo.addColorStop(1, \"rgba(255,170,80,0)\");\n    c.fillStyle = halo;\n    c.fillRect(-16, -18, 32, 32);\n  });\n  return { dark, lit, glow };\n}\n\nexport default function ReliefIslandRouteModel() {\n  const reduce = !!useReducedMotion();\n  const wrapRef = useRef<HTMLDivElement>(null);\n  const baseRef = useRef<HTMLCanvasElement>(null);\n  const lampRef = useRef<HTMLCanvasElement>(null);\n  const [route, setRoute] = useState(0);\n  const live = useRef({ route: 0, since: 0, touched: false, paused: false, replay: 0 });\n  const tiltX = useSpring(useMotionValue(0), { stiffness: 70, damping: 15 });\n  const tiltY = useSpring(useMotionValue(0), { stiffness: 70, damping: 15 });\n\n  const pick = (i: number, byHand: boolean) => {\n    if (byHand) live.current.touched = true;\n    live.current.route = i;\n    live.current.replay++;\n    setRoute(i);\n  };\n\n  useEffect(() => {\n    const wrap = wrapRef.current;\n    const base = baseRef.current;\n    const lamp = lampRef.current;\n    if (!wrap || !base || !lamp) return;\n    const S = clamp((wrap.clientWidth / LW) * Math.min(2, window.devicePixelRatio || 1), 1, 2);\n    let raf = 0;\n    let seen = true;\n    let stopped = false;\n    let bulbs: Bulb[][] = [];\n    let level: number[][] = [];\n    const sprites = buildSprites(S);\n    const lc = lamp.getContext(\"2d\")!;\n    const state = live.current;\n    let replay = -1;\n    let last = 0;\n\n    const draw = (now: number) => {\n      raf = 0;\n      const dt = Math.min(0.05, (now - last) / 1000 || 0.016);\n      last = now;\n      if (replay !== state.replay) {\n        replay = state.replay;\n        state.since = now;\n      }\n      const t = (now - state.since) / 1000;\n      lc.setTransform(1, 0, 0, 1, 0, 0);\n      lc.clearRect(0, 0, lamp.width, lamp.height);\n      lc.setTransform(S, 0, 0, S, 0, 0);\n      bulbs.forEach((line, r) => {\n        const on = r === state.route;\n        const n = line.length;\n        // the walk lights from the quay outward, then a brighter pulse keeps travelling along it\n        const reach = reduce ? n : (t - 0.12) * 17;\n        const pulse = ((t * 0.42) % 1.25) * n;\n        line.forEach((b, i) => {\n          let want = 0;\n          if (on && i < reach) want = reduce ? 0.95 : 0.74 + 0.26 * Math.exp(-(((i - pulse) / 1.6) ** 2)) + 0.03 * Math.sin(now / 53 + i * 2.1);\n          const cur = level[r][i];\n          // filaments warm quickly and cool slowly\n          level[r][i] = reduce ? want : cur + (want - cur) * (1 - Math.exp(-dt / (want > cur ? 0.07 : 0.3)));\n        });\n      });\n      // light first, so each glass dome sits on top of the glow around it\n      bulbs.forEach((line, r) =>\n        line.forEach((b, i) => {\n          const v = level[r][i];\n          if (v < 0.01) return;\n          lc.globalAlpha = Math.min(1, v);\n          lc.drawImage(sprites.glow, b.x - 48, b.y - 48, 96, 96);\n        }),\n      );\n      bulbs.forEach((line, r) =>\n        line.forEach((b, i) => {\n          const v = level[r][i];\n          lc.globalAlpha = 1;\n          lc.drawImage(sprites.dark, b.x - 10, b.y - 10, 20, 20);\n          if (v > 0.01) {\n            lc.globalAlpha = Math.min(1, v * 1.5);\n            lc.drawImage(sprites.lit, b.x - 10, b.y - 10, 20, 20);\n          }\n        }),\n      );\n      lc.globalAlpha = 1;\n      if (!reduce && seen && !document.hidden && !stopped) raf = requestAnimationFrame(draw);\n    };\n    const kick = () => {\n      if (!raf && !stopped) raf = requestAnimationFrame(draw);\n    };\n\n    // build after the first paint so the stage shows straight away\n    const start = window.setTimeout(() => {\n      const built = buildModel(S);\n      for (const cv of [base, lamp]) {\n        cv.width = built.canvas.width;\n        cv.height = built.canvas.height;\n      }\n      base.getContext(\"2d\")!.drawImage(built.canvas, 0, 0);\n      bulbs = built.bulbs;\n      level = bulbs.map((l) => l.map(() => 0));\n      state.replay++;\n      kick();\n    }, 30);\n\n    const io = new IntersectionObserver(([e]) => {\n      seen = e.isIntersecting;\n      if (seen) kick();\n    });\n    io.observe(wrap);\n    const onVis = () => !document.hidden && kick();\n    document.addEventListener(\"visibilitychange\", onVis);\n    // with motion reduced there is no loop, so a change of route asks for one redraw\n    const poll = reduce ? window.setInterval(() => replay !== state.replay && kick(), 120) : 0;\n    const tour = window.setInterval(() => {\n      if (reduce || state.touched || state.paused || document.hidden || !seen) return;\n      state.route = (state.route + 1) % routes.length;\n      state.replay++;\n      setRoute(state.route);\n    }, 6200);\n    return () => {\n      stopped = true;\n      window.clearTimeout(start);\n      window.clearInterval(tour);\n      if (poll) window.clearInterval(poll);\n      cancelAnimationFrame(raf);\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, [reduce]);\n\n  const onMove = (e: React.PointerEvent) => {\n    if (reduce || e.pointerType === \"touch\") return;\n    const r = e.currentTarget.getBoundingClientRect();\n    tiltY.set((((e.clientX - r.left) / r.width) * 2 - 1) * 3.2);\n    tiltX.set((((e.clientY - r.top) / r.height) * 2 - 1) * -2.6);\n  };\n\n  return (\n    <section className=\"relative overflow-hidden bg-[#0b0a09]\">\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\" style={{ background: \"radial-gradient(58% 60% at 46% 44%, #2c2620 0%, #17130f 46%, #0b0a09 100%)\" }} />\n      <div\n        onPointerMove={onMove}\n        onPointerEnter={() => (live.current.paused = true)}\n        onPointerLeave={() => {\n          live.current.paused = false;\n          tiltX.set(0);\n          tiltY.set(0);\n        }}\n        className=\"relative mx-auto w-full max-w-[940px] px-2 pt-6 sm:pt-8\"\n        style={{ perspective: \"1600px\" }}\n      >\n        <motion.div ref={wrapRef} className=\"relative w-full\" style={{ aspectRatio: `${LW} / ${LH}`, rotateX: tiltX, rotateY: tiltY }}>\n          <canvas ref={baseRef} role=\"img\" aria-label={`Relief model of the island of ${island.name}, built from stacked plywood sheets on a walnut plinth, with three walking routes marked by rows of small bulbs`} className=\"absolute inset-0 h-full w-full\" />\n          <canvas ref={lampRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 h-full w-full\" />\n        </motion.div>\n      </div>\n\n      <div className=\"relative flex flex-col gap-3 px-5 pb-6 pt-2 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-400\">\n          <span className=\"text-stone-100\">\n            {island.name} / {island.plate}\n          </span>\n          <span className=\"block\" aria-live=\"polite\">\n            {routes[route].name}: {routes[route].detail}\n          </span>\n        </p>\n        <div className=\"flex flex-wrap gap-2\">\n          {routes.map((r, i) => (\n            <button\n              key={r.id}\n              type=\"button\"\n              aria-pressed={route === i}\n              onClick={() => pick(i, true)}\n              className={`flex h-10 items-center gap-2 rounded-full px-4 font-mono text-[11px] font-semibold uppercase tracking-[0.14em] outline-none transition-colors focus-visible:ring-2 focus-visible:ring-amber-300 focus-visible:ring-offset-2 focus-visible:ring-offset-[#0b0a09] ${route === i ? \"bg-amber-200 text-stone-900\" : \"bg-stone-800 text-stone-200 hover:bg-stone-700\"}`}\n            >\n              <span aria-hidden=\"true\" className=\"h-2 w-2 rounded-full\" style={{ background: r.flag }} />\n              {r.name}\n            </button>\n          ))}\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
