{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"layered-paper-cut-word-with-a-photograph-drifting-behind-the-letters","type":"registry:block","title":"Layered Paper Cut Word with a Photograph Drifting Behind the Letters","description":"Text effect with one word cut through stacked sheets of card, each cut a little wider, and a photograph drifting behind. Free React and HTML code to copy.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/layered-paper-cut-word-with-a-photograph-drifting-behind-the-letters.tsx","type":"registry:component","content":"import { useEffect, useId, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// The word that is cut through the sheets, and the pictures that pass behind it.\nconst WORD = \"BLOOM\";\nconst CAPTION = \"Four sheets of card, one word\";\nconst photos = [\n  {\n    src: \"https://images.pexels.com/photos/33966527/pexels-photo-33966527.jpeg?auto=compress&cs=tinysrgb&w=1400\",\n    alt: \"Pink chrysanthemums in full bloom, photographed close up\",\n  },\n  {\n    src: \"https://images.pexels.com/photos/38017379/pexels-photo-38017379.jpeg?auto=compress&cs=tinysrgb&w=1400\",\n    alt: \"Red poppies and wildflowers in a green meadow\",\n  },\n  {\n    src: \"https://images.pexels.com/photos/33978371/pexels-photo-33978371.jpeg?auto=compress&cs=tinysrgb&w=1400\",\n    alt: \"A lilac chrysanthemum surrounded by bright yellow flowers\",\n  },\n];\n\n// Top sheet first. `grow` widens the cut (in drawing units) so each sheet above shows a rim of the one below.\nconst sheets = [\n  { fill: \"#fbfaf5\", grow: 27, depth: 0 },\n  { fill: \"#f5f2ea\", grow: 18, depth: 1 },\n  { fill: \"#eeeadf\", grow: 9, depth: 2 },\n  { fill: \"#e6e1d3\", grow: 0, depth: 3 },\n];\nconst VB_W = 1200;\nconst VB_H = 560;\nconst SECONDS_PER_PHOTO = 4.2;\n\nexport default function LayeredPaperCutWord() {\n  const reduce = useReducedMotion();\n  const uid = useId().replace(/[^a-zA-Z0-9]/g, \"\");\n  const boxRef = useRef<HTMLDivElement>(null);\n  const layerRefs = useRef<(HTMLDivElement | null)[]>([]);\n  const photoRef = useRef<HTMLDivElement>(null);\n  const tiltRef = useRef<HTMLDivElement>(null);\n  const [shown, setShown] = useState(0);\n\n  // Pictures change on a timer; with motion reduced the first one stays.\n  useEffect(() => {\n    if (reduce) return;\n    const t = window.setInterval(() => {\n      if (!document.hidden) setShown((n) => (n + 1) % photos.length);\n    }, SECONDS_PER_PHOTO * 1000);\n    return () => window.clearInterval(t);\n  }, [reduce]);\n\n  // Looking around the box: deeper sheets slide further, the photograph furthest of all.\n  useEffect(() => {\n    const box = boxRef.current;\n    if (!box || reduce) return;\n    let raf = 0;\n    let visible = true;\n    let last = performance.now();\n    let t = 0;\n    const view = { x: 0, y: 0, vx: 0, vy: 0 };\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      t += dt;\n      // Idle: a slow look from side to side, as if someone were walking past the frame.\n      const tx = pointer.on ? pointer.x : Math.sin(t * 0.9) * 0.85;\n      const ty = pointer.on ? pointer.y : Math.sin(t * 0.55 + 1) * 0.45;\n      // A soft spring, so the sheets lag and settle instead of tracking the pointer rigidly.\n      view.vx += ((tx - view.x) * 60 - view.vx * 13) * dt;\n      view.vy += ((ty - view.y) * 60 - view.vy * 13) * dt;\n      view.x += view.vx * dt;\n      view.y += view.vy * dt;\n      const unit = box.clientWidth / 100;\n      for (let i = 0; i < sheets.length; i++) {\n        const el = layerRefs.current[i];\n        if (!el) continue;\n        const k = sheets[i].depth * 0.34 * unit;\n        el.style.transform = `translate3d(${(-view.x * k).toFixed(2)}px, ${(-view.y * k).toFixed(2)}px, 0)`;\n      }\n      if (tiltRef.current) {\n        // the whole stack leans a few degrees toward the viewer's side\n        tiltRef.current.style.transform = `perspective(1400px) rotateY(${(view.x * 5).toFixed(2)}deg) rotateX(${(-view.y * 4).toFixed(2)}deg)`;\n      }\n      if (photoRef.current) {\n        const k = 2.3 * unit;\n        photoRef.current.style.transform = `translate3d(${(-view.x * k).toFixed(2)}px, ${(-view.y * k).toFixed(2)}px, 0)`;\n      }\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(box);\n    const onVis = () => start();\n    document.addEventListener(\"visibilitychange\", onVis);\n    const move = (ev: PointerEvent) => {\n      const r = box.getBoundingClientRect();\n      pointer.on = true;\n      pointer.x = Math.max(-1, Math.min(1, ((ev.clientX - r.left) / r.width) * 2 - 1));\n      pointer.y = Math.max(-1, Math.min(1, ((ev.clientY - r.top) / r.height) * 2 - 1));\n    };\n    const leave = () => {\n      pointer.on = false;\n    };\n    box.addEventListener(\"pointermove\", move);\n    box.addEventListener(\"pointerdown\", move);\n    box.addEventListener(\"pointerleave\", leave);\n    box.addEventListener(\"pointercancel\", leave);\n    start();\n    return () => {\n      cancelAnimationFrame(raf);\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", onVis);\n      box.removeEventListener(\"pointermove\", move);\n      box.removeEventListener(\"pointerdown\", move);\n      box.removeEventListener(\"pointerleave\", leave);\n      box.removeEventListener(\"pointercancel\", leave);\n    };\n  }, [reduce]);\n\n  return (\n    <section className=\"relative flex min-h-[620px] w-full flex-col items-center justify-center overflow-hidden bg-[#1f3329] px-4 py-14 sm:px-8 sm:py-20\">\n      <style>{`\n        @keyframes layered-paper-cut-word-drift { 0% { transform: scale(1.14) translate3d(-2.5%, 1.5%, 0); } 100% { transform: scale(1.24) translate3d(2.5%, -2.5%, 0); } }\n        .layered-paper-cut-word-photo { animation: layered-paper-cut-word-drift 9s ease-in-out infinite alternate; }\n        @media (prefers-reduced-motion: reduce) { .layered-paper-cut-word-photo { animation: none; transform: scale(1.16); } }\n      `}</style>\n\n      {/* The word itself, for screen readers and search. The cut sheets below are decoration. */}\n      <h2 className=\"sr-only\">{WORD.charAt(0) + WORD.slice(1).toLowerCase()}</h2>\n\n      <div\n        ref={boxRef}\n        className=\"relative w-full max-w-[940px] [container-type:inline-size] [touch-action:pan-y]\"\n        style={{ aspectRatio: `${VB_W} / ${VB_H}` }}\n      >\n        {/* shadow the whole stack throws on the table */}\n        <div aria-hidden=\"true\" className=\"absolute inset-x-[3%] bottom-[-5%] top-[12%] rounded-[50%] bg-black/55 blur-3xl\" />\n\n        <div ref={tiltRef} className=\"absolute inset-0 will-change-transform\">\n        {/* the edges of the lower sheets, seen along the bottom of the stack */}\n        {[3, 2, 1].map((n) => (\n          <div\n            key={n}\n            aria-hidden=\"true\"\n            className=\"absolute inset-0 rounded-[6px] shadow-[0_1px_0_rgba(0,0,0,0.22)]\"\n            style={{ transform: `translateY(${n * 3}px)`, background: sheets[n].fill, filter: `brightness(${1 - n * 0.045})` }}\n          />\n        ))}\n        <div className=\"absolute inset-0 overflow-hidden rounded-[6px] bg-[#2a1a12]\">\n          {/* the photograph at the back of the box */}\n          <div ref={photoRef} className=\"absolute inset-0 will-change-transform\">\n            {photos.map((p, i) => (\n              <img\n                key={p.src}\n                src={p.src}\n                alt={p.alt}\n                loading={i === 0 ? \"eager\" : \"lazy\"}\n                width={1400}\n                height={933}\n                draggable={false}\n                className=\"layered-paper-cut-word-photo absolute inset-0 h-full w-full object-cover transition-opacity duration-[1400ms] ease-in-out motion-reduce:transition-none\"\n                style={{ opacity: i === shown ? 1 : 0, animationDelay: `${-i * 3}s`, animationPlayState: i === shown || i === (shown + photos.length - 1) % photos.length ? \"running\" : \"paused\" }}\n              />\n            ))}\n            <div aria-hidden=\"true\" className=\"absolute inset-0 bg-[radial-gradient(ellipse_at_50%_50%,rgba(0,0,0,0)_40%,rgba(20,10,0,0.3)_100%)]\" />\n          </div>\n\n          {/* the sheets, deepest first so the top sheet is painted last */}\n          {[...sheets].reverse().map((s) => {\n            const i = sheets.indexOf(s);\n            const maskId = `${uid}-cut-${i}`;\n            const grainId = `${uid}-grain-${i}`;\n            const lightId = `${uid}-light-${i}`;\n            return (\n              <div\n                key={i}\n                ref={(el) => {\n                  layerRefs.current[i] = el;\n                }}\n                aria-hidden=\"true\"\n                className=\"absolute -inset-[4%] will-change-transform\"\n                style={{\n                  filter:\n                    i === 0\n                      ? \"drop-shadow(0 0.2cqw 0.2cqw rgba(38,30,18,0.34)) drop-shadow(0 0.95cqw 1.05cqw rgba(38,30,18,0.3))\"\n                      : \"drop-shadow(0 0.2cqw 0.2cqw rgba(38,30,18,0.36)) drop-shadow(0 0.85cqw 0.95cqw rgba(38,30,18,0.32))\",\n                }}\n              >\n                <svg viewBox={`${-VB_W * 0.04} ${-VB_H * 0.04} ${VB_W * 1.08} ${VB_H * 1.08}`} preserveAspectRatio=\"none\" className=\"block h-full w-full\">\n                  <defs>\n                    <mask id={maskId} maskUnits=\"userSpaceOnUse\" x={-VB_W * 0.04} y={-VB_H * 0.04} width={VB_W * 1.08} height={VB_H * 1.08}>\n                      <rect x={-VB_W * 0.04} y={-VB_H * 0.04} width={VB_W * 1.08} height={VB_H * 1.08} fill=\"#fff\" />\n                      <text\n                        x={VB_W / 2}\n                        y={VB_H * 0.705}\n                        textAnchor=\"middle\"\n                        textLength={VB_W * 0.8}\n                        lengthAdjust=\"spacingAndGlyphs\"\n                        fontSize={300}\n                        fontWeight={900}\n                        fontFamily=\"'Arial Black', 'Helvetica Neue', Impact, 'Segoe UI', system-ui, sans-serif\"\n                        fill=\"#000\"\n                        stroke=\"#000\"\n                        strokeWidth={s.grow * 2}\n                        strokeLinejoin=\"miter\"\n                        strokeMiterlimit={6}\n                      >\n                        {WORD}\n                      </text>\n                    </mask>\n                    <filter id={grainId} x=\"0\" y=\"0\" width=\"100%\" height=\"100%\">\n                      <feTurbulence type=\"fractalNoise\" baseFrequency=\"0.85\" numOctaves=\"2\" seed={11 + i * 7} />\n                      <feColorMatrix values=\"0 0 0 0 0.3  0 0 0 0 0.26  0 0 0 0 0.2  0 0 0 0.5 0\" />\n                    </filter>\n                    <linearGradient id={lightId} x1=\"0\" y1=\"0\" x2=\"1\" y2=\"1\">\n                      <stop offset=\"0\" stopColor=\"#fff\" stopOpacity=\"0.28\" />\n                      <stop offset=\"0.55\" stopColor=\"#fff\" stopOpacity=\"0\" />\n                      <stop offset=\"1\" stopColor=\"#4a3c22\" stopOpacity=\"0.12\" />\n                    </linearGradient>\n                  </defs>\n                  <g mask={`url(#${maskId})`}>\n                    <rect x={-VB_W * 0.04} y={-VB_H * 0.04} width={VB_W * 1.08} height={VB_H * 1.08} fill={s.fill} />\n                    <rect x={-VB_W * 0.04} y={-VB_H * 0.04} width={VB_W * 1.08} height={VB_H * 1.08} filter={`url(#${grainId})`} opacity=\"0.35\" />\n                    <rect x={-VB_W * 0.04} y={-VB_H * 0.04} width={VB_W * 1.08} height={VB_H * 1.08} fill={`url(#${lightId})`} />\n                  </g>\n                </svg>\n              </div>\n            );\n          })}\n          {/* a soft bevel around the top sheet */}\n          <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 rounded-[6px] shadow-[inset_0_1px_0_rgba(255,255,255,0.9)]\" />\n        </div>\n        </div>\n      </div>\n\n      <p className=\"relative mt-9 text-center text-[12px] uppercase tracking-[0.24em] text-[#a9bcae] sm:mt-12\">{CAPTION}</p>\n    </section>\n  );\n}\n"}]}
