{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"wireframe-topographic-terrain-background-gliding-under-a-low-camera","type":"registry:block","title":"Wireframe Terrain Glide","description":"","dependencies":[],"files":[{"path":"components/we-inc/wireframe-topographic-terrain-background-gliding-under-a-low-camera.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// The values worth changing.\nconst SETTINGS = {\n  paper: \"#f3f1ec\", // the page colour; also hides the lines behind each hill\n  line: [38, 44, 54], // graphite line colour as [red, green, blue]\n  accent: \"rgba(184, 84, 48, 0.85)\", // the one contour line picked out in rust\n  speed: 1.1, // how fast the ground glides toward you\n  relief: 1, // height of the hills; 0.5 is rolling farmland, 1.6 is alpine\n  rows: 46, // lines from front to back\n  cols: 96, // lines from left to right\n};\n\nfunction terrainHeight(x: any, z: any) {\n  const a = Math.sin(x * 0.18 + z * 0.07) * 1.1 + Math.sin(z * 0.13 - x * 0.05) * 1.4;\n  const b = Math.sin(x * 0.41 + z * 0.29 + 1.7) * 0.45 + Math.sin(x * 0.07 - z * 0.21) * 1.6;\n  const ridge = 1 - Math.abs(Math.sin(x * 0.09 + z * 0.045));\n  return (a + b + ridge * ridge * 2.4) * SETTINGS.relief;\n}\n\nconst SCENE: any = {\n  start: 0,\n  still: 0,\n  init() {\n    return {};\n  },\n  draw(ctx: any, w: any, h: any, t: any) {\n    ctx.fillStyle = SETTINGS.paper;\n    ctx.fillRect(0, 0, w, h);\n    const horizon = h * (w < 700 ? 0.56 : 0.5);\n    const focal = Math.max(w, 700) * 0.9;\n    const camY = 7.5;\n    const dz = 1;\n    const scroll = t * SETTINGS.speed;\n    const shift = scroll % dz;\n    const rows = SETTINGS.rows;\n    const cols = SETTINGS.cols;\n    const span = 90;\n    const pts: any[] = [];\n    // Project a grid of ground points; row 0 is nearest the viewer.\n    for (let r = 0; r < rows; r++) {\n      const z = 1.6 + r * dz - shift;\n      const row: any[] = [];\n      for (let c = 0; c <= cols; c++) {\n        const x = (c / cols - 0.5) * span;\n        const y = terrainHeight(x, z + scroll);\n        row.push([w / 2 + (x * focal) / (z * 3.2), horizon + ((camY - y) * focal) / (z * 4.2), y]);\n      }\n      pts.push({ z, row });\n    }\n    const [lr, lg, lb] = SETTINGS.line;\n    ctx.lineJoin = \"round\";\n    // Draw from back to front so the fog fades distant lines into the paper.\n    for (let r = rows - 1; r >= 0; r--) {\n      const { z, row } = pts[r];\n      const fade = Math.max(0, Math.min(1, (rows - z) / (rows * 0.75))) * Math.min(1, (z - 1.2) * 1.2);\n      // Paper under this row hides whatever lies behind the ridge.\n      ctx.fillStyle = SETTINGS.paper;\n      ctx.beginPath();\n      ctx.moveTo(row[0][0], h + 2);\n      for (let c = 0; c <= cols; c++) ctx.lineTo(row[c][0], row[c][1]);\n      ctx.lineTo(row[cols][0], h + 2);\n      ctx.fill();\n      ctx.strokeStyle = `rgba(${lr},${lg},${lb},${0.62 * fade})`;\n      ctx.lineWidth = 0.6 + 0.9 * fade;\n      ctx.beginPath();\n      for (let c = 0; c <= cols; c++) {\n        if (c === 0) ctx.moveTo(row[c][0], row[c][1]);\n        else ctx.lineTo(row[c][0], row[c][1]);\n      }\n      ctx.stroke();\n      if (r > 0) {\n        const near = pts[r - 1].row;\n        ctx.strokeStyle = `rgba(${lr},${lg},${lb},${0.34 * fade})`;\n        ctx.lineWidth = 0.6 + 0.5 * fade;\n        ctx.beginPath();\n        for (let c = 0; c <= cols; c++) {\n          ctx.moveTo(row[c][0], row[c][1]);\n          ctx.lineTo(near[c][0], near[c][1]);\n        }\n        ctx.stroke();\n        // One contour height traced in rust where the ground crosses it.\n        ctx.strokeStyle = SETTINGS.accent.replace(/[\\d.]+\\)$/, `${0.85 * fade})`);\n        ctx.lineWidth = 1.4;\n        ctx.beginPath();\n        const level = 3.2;\n        for (let c = 0; c < cols; c++) {\n          const a = row[c];\n          const b = row[c + 1];\n          const d = near[c];\n          const cross = (p: any, q: any) => {\n            if ((p[2] - level) * (q[2] - level) >= 0) return null;\n            const s = (level - p[2]) / (q[2] - p[2]);\n            return [p[0] + (q[0] - p[0]) * s, p[1] + (q[1] - p[1]) * s];\n          };\n          const hits = [cross(a, b), cross(a, d), cross(b, near[c + 1]), cross(d, near[c + 1])].filter(Boolean) as any;\n          if (hits.length >= 2) {\n            ctx.moveTo(hits[0][0], hits[0][1]);\n            ctx.lineTo(hits[1][0], hits[1][1]);\n          }\n        }\n        ctx.stroke();\n      }\n    }\n  },\n};\n\n// Runs a scene on a canvas: sizes it to the block, pauses off screen and in hidden tabs, cleans up after itself.\nfunction runScene(root: any, canvas: any, scene: any, reduced: boolean) {\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) return () => {};\n  let w = 0;\n  let h = 0;\n  let st: any = null;\n  let raf = 0;\n  let last = 0;\n  let acc = 0;\n  let onScreen = true;\n  let time = reduced ? (scene.still ?? scene.start ?? 0) : (scene.start ?? 0);\n  const ptr: any = { x: 0, y: 0, on: false, down: 0 };\n  const step = 1 / (scene.fps || 60);\n  const resize = () => {\n    const r = root.getBoundingClientRect();\n    const nw = Math.max(1, Math.round(r.width));\n    const nh = Math.max(1, Math.round(r.height));\n    if (nw === w && nh === h && st) return;\n    w = nw;\n    h = nh;\n    // Device pixels are capped at 1.5 and scaled by the scene's own quality factor.\n    const d = Math.min(window.devicePixelRatio || 1, 1.5) * (scene.scale || 1);\n    canvas.width = Math.max(1, Math.round(w * d));\n    canvas.height = Math.max(1, Math.round(h * d));\n    ctx.setTransform(canvas.width / w, 0, 0, canvas.height / h, 0, 0);\n    st = scene.init(ctx, w, h, canvas.width / w);\n    scene.draw(ctx, w, h, time, st, ptr, 0);\n  };\n  const tick = (now: number) => {\n    raf = requestAnimationFrame(tick);\n    const dt = last ? Math.min(0.1, (now - last) / 1000) : step;\n    last = now;\n    acc += dt;\n    if (acc < step * 0.95) return;\n    const used = Math.min(acc, 0.1);\n    acc = 0;\n    time += used;\n    scene.draw(ctx, w, h, time, st, ptr, used);\n  };\n  const play = () => {\n    if (raf || reduced || !onScreen || document.hidden) return;\n    last = 0;\n    raf = requestAnimationFrame(tick);\n  };\n  const stop = () => {\n    cancelAnimationFrame(raf);\n    raf = 0;\n  };\n  const ro = new ResizeObserver(resize);\n  ro.observe(root);\n  const io = new IntersectionObserver((entries: any) => {\n    onScreen = entries[0].isIntersecting;\n    if (onScreen) play();\n    else stop();\n  });\n  io.observe(root);\n  const onVis = () => (document.hidden ? stop() : play());\n  const onMove = (e: any) => {\n    const r = root.getBoundingClientRect();\n    ptr.x = e.clientX - r.left;\n    ptr.y = e.clientY - r.top;\n    ptr.on = true;\n  };\n  const onLeave = () => {\n    ptr.on = false;\n  };\n  const onDown = (e: any) => {\n    onMove(e);\n    ptr.down += 1;\n  };\n  document.addEventListener(\"visibilitychange\", onVis);\n  root.addEventListener(\"pointermove\", onMove);\n  root.addEventListener(\"pointerleave\", onLeave);\n  root.addEventListener(\"pointerdown\", onDown);\n  resize();\n  play();\n  return () => {\n    stop();\n    ro.disconnect();\n    io.disconnect();\n    document.removeEventListener(\"visibilitychange\", onVis);\n    root.removeEventListener(\"pointermove\", onMove);\n    root.removeEventListener(\"pointerleave\", onLeave);\n    root.removeEventListener(\"pointerdown\", onDown);\n  };\n}\n\nexport default function WireframeTerrainBackground() {\n  const rootRef = useRef<HTMLElement>(null);\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  useEffect(() => {\n    const root = rootRef.current;\n    const canvas = canvasRef.current;\n    if (!root || !canvas) return;\n    // Visitors who prefer reduced motion get one settled frame and no loop.\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    return runScene(root, canvas, SCENE, reduced);\n  }, []);\n\n  return (\n    <section ref={rootRef} className=\"relative h-[640px] w-full overflow-hidden bg-[#f3f1ec] text-[#1f242c]\">\n      <canvas ref={canvasRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 block h-full w-full\" />\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\" style={{ background: \"linear-gradient(180deg, rgba(243,241,236,0.96) 0%, rgba(243,241,236,0.85) 42%, rgba(243,241,236,0) 62%)\" }} />\n      <div className=\"relative z-10 mx-auto flex h-full max-w-6xl flex-col justify-start items-center text-center px-6 py-14 sm:px-10\">\n        <div className=\"max-w-xl\">\n          <p className=\"text-xs font-semibold uppercase tracking-[0.2em] text-[#9a4424]\">Ridgeline Survey Co.</p>\n          <h2 className=\"mt-4 text-4xl font-semibold leading-[1.05] tracking-tight sm:text-5xl lg:text-6xl\">Ground models you can build on.</h2>\n          <p className=\"mt-5 text-base leading-relaxed text-[#4a515c] sm:text-lg\">Measured terrain for architects and landscape designers, walked on foot and flown by drone.</p>\n          <a href=\"#\" className=\"mt-8 inline-flex h-12 items-center rounded-[4px] bg-[#1f242c] px-6 text-sm font-semibold text-[#f3f1ec] transition-opacity hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#1f242c] focus-visible:ring-offset-2 focus-visible:ring-offset-[#f3f1ec]\">Request a survey</a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
