{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"blind-embossed-paragraph-that-inks-in-under-a-passing-lamp","type":"registry:block","title":"Blind Embossed Paragraph That Inks In Under a Passing Lamp","description":"Text reveal where a paragraph pressed blind into paper takes ink word by word as a lamp passes or your pointer moves. Free React and HTML code to copy.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/blind-embossed-paragraph-that-inks-in-under-a-passing-lamp.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// The paragraph. Edit the sentence here; it is split into words for you.\nconst TEXT =\n  \"A good page is pressed, not printed. The type bites into cotton paper and leaves a small valley at every stroke, and you only see it when the light comes in low from the side.\";\nconst COLOPHON = \"Set by hand, proof no. 14\";\n\nconst PAPER = [236, 229, 214]; // floor of a blind (un-inked) letter where the lamp is brightest\nconst PAPER_SHADE = [190, 168, 140]; // the same floor out in the unlit part of the sheet\nconst INK = [38, 27, 22]; // colour of a fully inked letter\nconst PASS_SECONDS = 3.4; // how long the lamp takes to cross the sheet\nconst HOLD_SECONDS = 2.6; // how long the inked page rests before it starts again\n\nconst GRAIN =\n  \"url(\\\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='220' height='220'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='3' seed='7'/%3E%3CfeColorMatrix values='0 0 0 0 .25 0 0 0 0 .18 0 0 0 0 .1 0 0 0 .5 0'/%3E%3C/filter%3E%3Crect width='220' height='220' filter='url(%23n)'/%3E%3C/svg%3E\\\")\";\n\ntype Box = { x: number; y: number };\n\nexport default function BlindEmbossedParagraph() {\n  const reduce = useReducedMotion();\n  const sheetRef = useRef<HTMLDivElement>(null);\n  const lampRef = useRef<HTMLDivElement>(null);\n  const shadeRef = useRef<HTMLDivElement>(null);\n  const wordRefs = useRef<(HTMLSpanElement | null)[]>([]);\n  const [run, setRun] = useState(0);\n  const words = TEXT.split(\" \");\n  const all = [...words, COLOPHON];\n\n  useEffect(() => {\n    const sheet = sheetRef.current;\n    if (!sheet) return;\n    const els = wordRefs.current.filter(Boolean) as HTMLSpanElement[];\n    const ink = els.map(() => (reduce ? 1 : 0));\n    let boxes: Box[] = [];\n    let W = 1;\n    let H = 1;\n\n    const measure = () => {\n      const r = sheet.getBoundingClientRect();\n      W = r.width;\n      H = r.height;\n      boxes = els.map((el) => {\n        const b = el.getBoundingClientRect();\n        return { x: b.left - r.left + b.width / 2, y: b.top - r.top + b.height / 2 };\n      });\n    };\n\n    // Paints every word for a lamp at (lx, ly): which way its bevel catches light, and how inked it is.\n    const paint = (lx: number, ly: number, power: number) => {\n      const R = Math.max(170, Math.min(W, H) * 0.62);\n      for (let i = 0; i < els.length; i++) {\n        const b = boxes[i];\n        if (!b) continue;\n        const dx = b.x - lx;\n        const dy = b.y - ly;\n        const d = Math.hypot(dx, dy) || 1;\n        const ux = dx / d;\n        const uy = dy / d;\n        const lit = Math.exp(-(d * d) / (R * R)) * power;\n        const k = ink[i];\n        const tone = Math.pow(Math.min(1, lit * 1.1), 1.4);\n        const c = PAPER.map((p, j) => {\n          const floor = (PAPER_SHADE[j] + (p - PAPER_SHADE[j]) * tone) * 0.975;\n          return Math.round(floor + (INK[j] - floor) * k);\n        });\n        // A pressed letter: the wall nearest the lamp falls into shadow, the far wall catches the light.\n        const reach = 0.55 + Math.min(0.85, d / 420);\n        const dark = (0.3 + 0.38 * lit) * (1 - 0.6 * k);\n        const glint = (0.2 + 0.6 * lit) * (1 - 0.78 * k);\n        const s = els[i].style;\n        s.color = `rgb(${c[0]},${c[1]},${c[2]})`;\n        s.textShadow =\n          `${(-ux * reach).toFixed(2)}px ${(-uy * reach).toFixed(2)}px ${(0.25 + reach * 0.35).toFixed(2)}px rgba(52,36,20,${dark.toFixed(3)}),` +\n          `${(ux * 0.8).toFixed(2)}px ${(uy * 0.8).toFixed(2)}px 0.3px rgba(255,252,243,${glint.toFixed(3)})`;\n      }\n      if (lampRef.current) lampRef.current.style.transform = `translate3d(${lx}px, ${ly}px, 0)`;\n      if (shadeRef.current) shadeRef.current.style.transform = `translate3d(${lx}px, ${ly}px, 0)`;\n    };\n\n    measure();\n    const ro = new ResizeObserver(() => {\n      measure();\n      if (reduce) paint(W * 0.3, -H * 0.1, 0.9);\n    });\n    ro.observe(sheet);\n\n    if (reduce) {\n      paint(W * 0.3, -H * 0.1, 0.9);\n      return () => ro.disconnect();\n    }\n\n    let raf = 0;\n    let visible = true;\n    let last = performance.now();\n    let clock = 0; // seconds into the current cycle\n    let lx = -200;\n    let ly = 0;\n    let first = true;\n    const pointer = { on: false, x: 0, y: 0 };\n\n    const frame = (now: number) => {\n      raf = 0;\n      const dt = Math.min(0.05, (now - last) / 1000);\n      last = now;\n      clock += dt;\n      const total = PASS_SECONDS + HOLD_SECONDS + 1.1;\n      if (clock > total) clock = 0;\n\n      // The lamp's own route: along the long side of the sheet, a little off the diagonal.\n      const p = Math.min(1, clock / PASS_SECONDS);\n      const e = p * p * (3 - 2 * p);\n      const wide = W >= H;\n      const m = Math.max(W, H) * 0.34;\n      const fading = clock > PASS_SECONDS + HOLD_SECONDS;\n      // It comes to rest over the page, breathing a little, then glides back while the ink dries.\n      const rest = clock > PASS_SECONDS ? Math.sin((clock - PASS_SECONDS) * 1.1) * 14 : 0;\n      let tx = wide ? -m + (W * 0.68 + m) * e + rest : W * (0.25 + 0.3 * e);\n      let ty = wide ? H * (0.16 + 0.22 * e) : -m + (H * 0.42 + m) * e + rest;\n      if (fading) {\n        tx = wide ? -m : W * 0.25;\n        ty = wide ? H * 0.16 : -m;\n      }\n      if (pointer.on) {\n        tx = pointer.x;\n        ty = pointer.y;\n      }\n      if (first) {\n        lx = tx;\n        ly = ty;\n        first = false;\n      }\n      const follow = 1 - Math.exp(-dt * (pointer.on ? 9 : fading ? 3.5 : 14));\n      lx += (tx - lx) * follow;\n      ly += (ty - ly) * follow;\n\n      // Ink develops where the light has rested, and dries back to blind at the end of the cycle.\n      const R = Math.max(170, Math.min(W, H) * 0.62);\n      for (let i = 0; i < ink.length; i++) {\n        const b = boxes[i];\n        if (!b) continue;\n        if (fading && !pointer.on) {\n          ink[i] = Math.max(0, ink[i] - dt * 1.4);\n          continue;\n        }\n        const d = Math.hypot(b.x - lx, b.y - ly);\n        const lit = Math.exp(-(d * d) / (R * R));\n        if (lit > 0.42) ink[i] = Math.min(1, ink[i] + dt * 3.2 * (lit - 0.3));\n        else if (!pointer.on && clock > PASS_SECONDS) ink[i] = Math.min(1, ink[i] + dt * 2);\n      }\n      paint(lx, ly, 1);\n      if (visible && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const start = () => {\n      if (!raf && visible && !document.hidden) {\n        last = performance.now();\n        raf = requestAnimationFrame(frame);\n      }\n    };\n    const io = new IntersectionObserver(([en]) => {\n      visible = en.isIntersecting;\n      start();\n    });\n    io.observe(sheet);\n    const onVis = () => start();\n    document.addEventListener(\"visibilitychange\", onVis);\n\n    const move = (ev: PointerEvent) => {\n      const r = sheet.getBoundingClientRect();\n      pointer.on = true;\n      pointer.x = ev.clientX - r.left;\n      pointer.y = ev.clientY - r.top;\n    };\n    const leave = () => {\n      pointer.on = false;\n    };\n    sheet.addEventListener(\"pointermove\", move);\n    sheet.addEventListener(\"pointerdown\", move);\n    sheet.addEventListener(\"pointerleave\", leave);\n    sheet.addEventListener(\"pointercancel\", leave);\n    start();\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", onVis);\n      sheet.removeEventListener(\"pointermove\", move);\n      sheet.removeEventListener(\"pointerdown\", move);\n      sheet.removeEventListener(\"pointerleave\", leave);\n      sheet.removeEventListener(\"pointercancel\", leave);\n    };\n  }, [reduce, run]);\n\n  return (\n    <section className=\"relative flex min-h-[640px] w-full items-center justify-center overflow-hidden bg-[#161310] px-4 py-14 sm:px-8 sm:py-20\">\n      <div\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0\"\n        style={{ background: \"radial-gradient(ellipse at 50% 42%, rgba(96,74,48,0.38), rgba(22,19,16,0) 62%)\" }}\n      />\n      <div className=\"relative w-full max-w-[820px]\">\n        <div\n          ref={sheetRef}\n          className=\"relative overflow-hidden rounded-[2px] bg-[#efe8d8] px-7 py-12 shadow-[0_1px_0_rgba(255,255,255,0.55)_inset,0_-1px_0_rgba(90,70,45,0.35)_inset,0_2px_0_#b9ad97,0_3px_0_#8f8470,0_34px_60px_-16px_rgba(0,0,0,0.8),0_10px_18px_-8px_rgba(0,0,0,0.6)] [touch-action:pan-y] sm:px-16 sm:py-20\"\n        >\n          {/* paper tooth */}\n          <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 opacity-[0.5] mix-blend-multiply\" style={{ backgroundImage: GRAIN }} />\n          {/* the room: everything the lamp does not reach sits in warm shade */}\n          <div\n            ref={shadeRef}\n            aria-hidden=\"true\"\n            className=\"pointer-events-none absolute left-0 top-0 h-0 w-0 will-change-transform\"\n            style={{ transform: \"translate3d(30%, -10%, 0)\" }}\n          >\n            <div\n              className=\"absolute -left-[1600px] -top-[1600px] h-[3200px] w-[3200px] mix-blend-multiply\"\n              style={{\n                background:\n                  \"radial-gradient(circle closest-side, rgba(150,108,64,0) 0%, rgba(150,108,64,0) 5%, rgba(150,108,64,0.2) 13%, rgba(150,108,64,0.4) 24%, rgba(140,98,58,0.5) 100%)\",\n              }}\n            />\n          </div>\n          {/* the lamp's warm pool */}\n          <div ref={lampRef} aria-hidden=\"true\" className=\"pointer-events-none absolute left-0 top-0 h-0 w-0 will-change-transform\">\n            <div\n              className=\"absolute -left-[340px] -top-[340px] h-[680px] w-[680px]\"\n              style={{ background: \"radial-gradient(circle closest-side, rgba(255,244,214,0.5), rgba(255,236,196,0.16) 45%, rgba(255,236,196,0) 100%)\" }}\n            />\n          </div>\n\n          <p className=\"relative select-none text-balance font-serif text-[27px] font-medium leading-[1.32] tracking-[-0.012em] text-[#ece5d6] sm:text-[42px] sm:leading-[1.24]\">\n            {words.map((w, i) => (\n              <span key={i}>\n                <span\n                  ref={(el) => {\n                    wordRefs.current[i] = el;\n                  }}\n                  className=\"inline-block\"\n                  style={reduce ? { color: \"rgb(38,27,22)\" } : undefined}\n                >\n                  {w}\n                </span>\n                {i < words.length - 1 ? \" \" : \"\"}\n              </span>\n            ))}\n          </p>\n          <div className=\"relative mt-10 flex items-center gap-4 sm:mt-14\">\n            <span aria-hidden=\"true\" className=\"h-px w-6 shrink-0 bg-[#261b16]/25 sm:w-10\" />\n            <span\n              ref={(el) => {\n                wordRefs.current[all.length - 1] = el;\n              }}\n              className=\"whitespace-nowrap font-serif text-[10.5px] uppercase tracking-[0.2em] text-[#e9e2d3] sm:text-[13px] sm:tracking-[0.28em]\"\n              style={reduce ? { color: \"rgb(38,27,22)\" } : undefined}\n            >\n              {COLOPHON}\n            </span>\n          </div>\n        </div>\n\n        <div className=\"mt-6 flex items-center justify-between gap-4 px-1\">\n          <p className=\"text-[12px] tracking-wide text-[#a89a86]\">Move a light over the page.</p>\n          <button\n            type=\"button\"\n            onClick={() => setRun((n) => n + 1)}\n            className=\"inline-flex h-10 shrink-0 items-center gap-2 whitespace-nowrap rounded-full border border-[#4a4034] px-4 text-[12px] font-medium tracking-wide text-[#e6dccb] transition-colors hover:bg-[#241f1a] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#e6dccb] focus-visible:ring-offset-2 focus-visible:ring-offset-[#161310]\"\n          >\n            <svg aria-hidden=\"true\" viewBox=\"0 0 20 20\" className=\"h-3.5 w-3.5\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n              <path d=\"M4 10a6 6 0 1 0 2-4.5\" />\n              <path d=\"M4 4v3.2h3.2\" />\n            </svg>\n            Pass the lamp again\n          </button>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
