{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"glossy-photo-print-that-peels-off-a-second-photograph-with-a-lit-curling-underside","type":"registry:block","title":"Glossy Photo Print That Peels Off a Second Photograph with a Lit Curling Underside","description":"Page peel between photos: a glossy print curls up from its corner after the pointer, shows its lit back and comes away. Free React code and HTML to copy.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/glossy-photo-print-that-peels-off-a-second-photograph-with-a-lit-curling-underside.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport type { PointerEvent as ReactPointerEvent } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// Two prints stacked on the table. Peeling the top one off uncovers the other.\nconst prints = [\n  {\n    name: \"Rosé\",\n    src: \"https://images.pexels.com/photos/28209648/pexels-photo-28209648.jpeg?auto=compress&cs=tinysrgb&w=900\",\n    alt: \"A woman in a red dress, patterned headscarf and cat-eye sunglasses resting her chin on her hand against a pale pink wall\",\n    tone: \"#e3c3cb\",\n  },\n  {\n    name: \"Verdigris\",\n    src: \"https://images.pexels.com/photos/9489589/pexels-photo-9489589.jpeg?auto=compress&cs=tinysrgb&w=900\",\n    alt: \"A woman in a green beret, green jacket and green gloves looking down at a green ball in her hands against a green wall\",\n    tone: \"#2f6b3a\",\n  },\n];\n\n/** Radius of the curl as a share of the print's height. Bigger is a lazier, fatter roll. */\nconst CURL = 0.085;\n/** How far the corner has to travel (in print heights) before letting go pulls the print right off. */\nconst LET_GO = 0.8;\n\nconst VERT = `attribute vec2 a; void main(){ gl_Position = vec4(a, 0.0, 1.0); }`;\n\nconst FRAG = `precision highp float;\nuniform vec2 u_res;\nuniform vec4 u_rect;\nuniform sampler2D u_a;\nuniform sampler2D u_b;\nuniform vec2 u_img;\nuniform vec2 u_c;\nuniform vec2 u_d;\nuniform float u_r;\nuniform float u_fade;\nuniform float u_top;\nfloat A;\nfloat PX;\nconst float PI = 3.14159265;\n\n// Each print has a white border, as a lab print does. BORDER is its width as a share of the print height.\nconst float BORDER = 0.026;\nvec2 fit(vec2 q, float img){\n  vec2 size = vec2(A, 1.0) - 2.0 * BORDER;\n  vec2 uv = (q - BORDER) / size;\n  float inner = size.x / size.y;\n  if (img > inner) uv.x = (uv.x - 0.5) * inner / img + 0.5;\n  else uv.y = (uv.y - 0.5) * img / inner + 0.5;\n  return clamp(uv, 0.0, 1.0);\n}\nvec3 bordered(vec3 c, vec2 q){\n  vec2 e = min(q, vec2(A, 1.0) - q);\n  return mix(vec3(0.958, 0.952, 0.936), c, smoothstep(BORDER - PX, BORDER + PX, min(e.x, e.y)));\n}\nvec3 topPrint(vec2 q){ return bordered(mix(texture2D(u_a, fit(q, u_img.x)).rgb, texture2D(u_b, fit(q, u_img.y)).rgb, u_top), q); }\nvec3 lowPrint(vec2 q){ return bordered(mix(texture2D(u_b, fit(q, u_img.y)).rgb, texture2D(u_a, fit(q, u_img.x)).rgb, u_top), q); }\n// 1 inside the sheet, 0 outside, with an edge w wide.\nfloat cov(vec2 q, float w){\n  vec2 e = min(q, vec2(A, 1.0) - q);\n  return clamp(min(e.x, e.y) / w + 0.5, 0.0, 1.0);\n}\nfloat hash(vec2 p){ return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }\n// How much of the lifted paper (the roll and the flap that has come over) sits above q.\nfloat lifted(vec2 q, float w){\n  float t = dot(q - u_c, u_d);\n  float ridge = 1.0 - smoothstep(u_r - w * 0.5, u_r + w * 0.5, t);\n  if (t < 0.0) return cov(q + u_d * (PI * u_r - 2.0 * t), w);\n  float th = asin(clamp(t / u_r, 0.0, 1.0));\n  return cov(q + u_d * (u_r * (PI - th) - min(t, u_r)), w) * ridge;\n}\n\nvoid main(){\n  vec2 px = vec2(gl_FragCoord.x, u_res.y - gl_FragCoord.y);\n  PX = 1.0 / u_rect.w;\n  A = u_rect.z / u_rect.w;\n  vec2 q = (px - u_rect.xy) * PX;\n  float t = dot(q - u_c, u_d);\n  vec2 fall = normalize(vec2(0.50, 0.86));\n  float lift = 2.0 * u_r;\n\n  // Shadow of the lifted paper: a tight dark one where it leaves the table and a wide soft one further out.\n  float near = lifted(q - fall * lift * 0.30, 0.018 + lift * 0.10);\n  float far = lifted(q - fall * lift * 0.95, 0.03 + lift * 0.30);\n  float dark = clamp(near * 0.42 + far * 0.34, 0.0, 1.0) * u_fade;\n\n  vec3 col = vec3(0.0);\n  float al = dark * 0.36 * cov(q, 0.30);\n\n  // The print underneath.\n  float lowIn = cov(q, PX);\n  vec3 low = lowPrint(q);\n  low *= 1.0 - dark;\n  col = mix(col, low, lowIn);\n  al = mix(al, 1.0, lowIn);\n\n  // The top print, picture side. Flat where it still lies down, then climbing the roll.\n  float frontIn;\n  vec3 front;\n  if (t < 0.0) {\n    frontIn = cov(q, PX);\n    front = topPrint(q) * (1.0 - dark * 0.85);\n    // The gloss coat picks up a breath of light just before the paper leaves the table.\n    front += vec3(1.0) * 0.07 * exp(t / (u_r * 0.9));\n  } else {\n    float th = asin(clamp(t / u_r, 0.0, 1.0));\n    vec2 qo = q + u_d * (u_r * th - t);\n    frontIn = cov(qo, PX) * (1.0 - smoothstep(u_r - PX, u_r + PX, t));\n    float up = sin(th);\n    front = topPrint(qo) * mix(1.0, 0.36, pow(up, 1.6));\n    front += vec3(1.0, 0.98, 0.95) * 0.34 * exp(-pow((th - 0.50) / 0.20, 2.0));\n  }\n  col = mix(col, front, frontIn * u_fade);\n  al = mix(al, 1.0, frontIn * u_fade);\n\n  // The back of the top print: over the roll, then lying flat, lit from above.\n  float backIn;\n  float lit;\n  vec2 qb;\n  float turn;\n  if (t < 0.0) {\n    qb = q + u_d * (PI * u_r - 2.0 * t);\n    backIn = cov(qb, PX);\n    lit = 0.985 - 0.07 * smoothstep(0.0, 0.7, -t);\n    turn = 0.0;\n  } else {\n    float th = asin(clamp(t / u_r, 0.0, 1.0));\n    qb = q + u_d * (u_r * (PI - th) - t);\n    backIn = cov(qb, PX) * (1.0 - smoothstep(u_r - PX, u_r + PX, t));\n    float k = cos(th);\n    lit = 0.50 + 0.50 * pow(k, 0.8) + 0.05 * exp(-pow((k - 0.78) / 0.16, 2.0));\n    turn = 1.0 - k;\n  }\n  vec3 paper = vec3(0.972, 0.962, 0.940) * lit;\n  // Colour thrown up onto the underside by the print below it.\n  vec2 under = q + u_d * u_r;\n  vec3 bounce = (lowPrint(under + vec2(0.07, 0.0)) + lowPrint(under - vec2(0.07, 0.0)) + lowPrint(under + vec2(0.0, 0.07)) + lowPrint(under - vec2(0.0, 0.07))) * 0.25;\n  paper = mix(paper, paper * (0.45 + bounce * 1.1), 0.36 * turn * lowIn);\n  // Paper tooth, and the maker's ruled marks that photo paper carries on its back.\n  paper *= 1.0 - 0.030 * hash(floor(qb * vec2(1100.0, 420.0)));\n  float rule = step(0.90, fract((qb.x + qb.y) * 9.0)) * step(0.45, fract((qb.y - qb.x) * 4.5));\n  paper *= 1.0 - 0.045 * rule;\n  col = mix(col, paper, backIn * u_fade);\n  al = mix(al, 1.0, backIn * u_fade);\n\n  gl_FragColor = vec4(col, al);\n}`;\n\nexport default function GlossyPhotoPrintThatPeelsOffASecondPhotographWithALitCurlingUnderside() {\n  const reduce = useReducedMotion();\n  const stage = useRef<HTMLElement>(null);\n  const print = useRef<HTMLButtonElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const first = useRef<HTMLImageElement>(null);\n  const second = useRef<HTMLImageElement>(null);\n  /** Where a person is holding the corner (in print heights from its top left), when they last moved, and a request to pull it off. */\n  const hand = useRef({ x: 0, y: 0, at: -1e6, pull: false });\n  const [live, setLive] = useState(false);\n  const [top, setTop] = useState(0);\n\n  useEffect(() => {\n    const host = stage.current;\n    const el = print.current;\n    const cv = canvas.current;\n    const one = first.current;\n    const two = second.current;\n    if (reduce || !host || !el || !cv || !one || !two) return;\n    const gl = cv.getContext(\"webgl\", { antialias: false, alpha: true, premultipliedAlpha: true });\n    if (!gl) return;\n\n    const compile = (type: number, source: string) => {\n      const shader = gl.createShader(type)!;\n      gl.shaderSource(shader, source);\n      gl.compileShader(shader);\n      return shader;\n    };\n    const program = gl.createProgram()!;\n    gl.attachShader(program, compile(gl.VERTEX_SHADER, VERT));\n    gl.attachShader(program, compile(gl.FRAGMENT_SHADER, FRAG));\n    gl.linkProgram(program);\n    if (!gl.getProgramParameter(program, gl.LINK_STATUS)) return;\n    gl.useProgram(program);\n    gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());\n    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);\n    const at = gl.getAttribLocation(program, \"a\");\n    gl.enableVertexAttribArray(at);\n    gl.vertexAttribPointer(at, 2, gl.FLOAT, false, 0, 0);\n    const u = (name: string) => gl.getUniformLocation(program, name);\n    gl.uniform1i(u(\"u_a\"), 0);\n    gl.uniform1i(u(\"u_b\"), 1);\n    const uni = { res: u(\"u_res\"), rect: u(\"u_rect\"), img: u(\"u_img\"), c: u(\"u_c\"), d: u(\"u_d\"), r: u(\"u_r\"), fade: u(\"u_fade\"), top: u(\"u_top\") };\n\n    let ready = false;\n    let visible = true;\n    let raf = 0;\n    let last = 0;\n    let ratio = 1;\n    let aspect = 0.8;\n    let onTop = 0;\n    let flying = false;\n    let idle = 0;\n    const started = performance.now();\n    // The corner starts already lifted so the first frame shows what the piece is.\n    const pos = { x: 0.8 - 0.36, y: 1 - 0.3 };\n    const vel = { x: 0, y: 0 };\n\n    const upload = () => {\n      if (ready || !one.complete || !two.complete || !one.naturalWidth || !two.naturalWidth) return;\n      try {\n        [one, two].forEach((img, unit) => {\n          gl.activeTexture(gl.TEXTURE0 + unit);\n          gl.bindTexture(gl.TEXTURE_2D, gl.createTexture());\n          gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n          gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n          gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);\n          gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n          gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, img);\n        });\n        gl.uniform2f(uni.img, one.naturalWidth / one.naturalHeight, two.naturalWidth / two.naturalHeight);\n        ready = true;\n        setLive(true);\n      } catch {\n        // A picture could not be read into the shader: the still version underneath stays on show.\n        ready = false;\n      }\n    };\n\n    const resize = () => {\n      ratio = Math.min(window.devicePixelRatio || 1, 2);\n      cv.width = Math.max(1, Math.round(host.clientWidth * ratio));\n      cv.height = Math.max(1, Math.round(host.clientHeight * ratio));\n      gl.viewport(0, 0, cv.width, cv.height);\n      const outer = host.getBoundingClientRect();\n      const inner = el.getBoundingClientRect();\n      aspect = inner.width / inner.height;\n      gl.uniform2f(uni.res, cv.width, cv.height);\n      gl.uniform4f(uni.rect, (inner.left - outer.left) * ratio, (inner.top - outer.top) * ratio, inner.width * ratio, inner.height * ratio);\n    };\n\n    const loop = (stamp: number) => {\n      if (ready) {\n        const t = (stamp - started) / 1000;\n        const dt = Math.min(last ? (stamp - last) / 1000 : 0.016, 0.033);\n        last = stamp;\n        const kx = aspect;\n        const ky = 1;\n        const held = stamp - hand.current.at < 2200;\n        if (hand.current.pull) {\n          hand.current.pull = false;\n          flying = true;\n        }\n        let tx: number;\n        let ty: number;\n        let stiff = 46;\n        if (flying) {\n          // Pulled clean off: the corner heads up and away past the far side of the print.\n          tx = kx - 3.4;\n          ty = ky - 1.5;\n          stiff = 9;\n        } else if (held) {\n          tx = hand.current.x;\n          ty = hand.current.y;\n          idle = 0;\n        } else {\n          // Nobody is holding it: the corner lifts and settles by itself, and now and then the print comes away.\n          idle += dt;\n          const reach = 0.46 + 0.2 * Math.sin(t * 0.85 - 0.6);\n          const lean = -2.5 + 0.2 * Math.sin(t * 0.53);\n          tx = kx + Math.cos(lean) * reach;\n          ty = ky + Math.sin(lean) * reach;\n          if (idle > 6.4) flying = true;\n        }\n        // The paper does not stretch: the corner cannot go further from the two pinned corners than the edges are long.\n        if (!flying) {\n          const pin = (cx: number, cy: number, max: number) => {\n            const dx = tx - cx;\n            const dy = ty - cy;\n            const far = Math.hypot(dx, dy);\n            if (far > max) {\n              tx = cx + (dx / far) * max;\n              ty = cy + (dy / far) * max;\n            }\n          };\n          pin(kx, 0, 1);\n          pin(0, ky, kx);\n        }\n        const damp = 2 * Math.sqrt(stiff) * (flying ? 1 : 0.72);\n        vel.x += ((tx - pos.x) * stiff - vel.x * damp) * dt;\n        vel.y += ((ty - pos.y) * stiff - vel.y * damp) * dt;\n        pos.x += vel.x * dt;\n        pos.y += vel.y * dt;\n\n        let dx = kx - pos.x;\n        let dy = ky - pos.y;\n        let gone = Math.hypot(dx, dy);\n        if (flying && gone > 2.9) {\n          // The top print has left. The one underneath is now on top, with the other slid in below it.\n          onTop = 1 - onTop;\n          setTop(onTop);\n          flying = false;\n          idle = 0;\n          pos.x = kx - 0.02;\n          pos.y = ky - 0.015;\n          vel.x = 0;\n          vel.y = 0;\n          dx = 0.02;\n          dy = 0.015;\n          gone = 0.025;\n        }\n        if (gone < 0.004) {\n          dx = 0.0032;\n          dy = 0.0024;\n          gone = 0.004;\n        }\n        const r = Math.min(CURL, (gone / Math.PI) * 0.8);\n        const along = (gone + Math.PI * r) / 2;\n        gl.uniform2f(uni.d, dx / gone, dy / gone);\n        gl.uniform2f(uni.c, kx - (dx / gone) * along, ky - (dy / gone) * along);\n        gl.uniform1f(uni.r, r);\n        gl.uniform1f(uni.fade, flying ? 1 - Math.min(1, Math.max(0, (gone - 2.0) / 0.8)) : 1);\n        gl.uniform1f(uni.top, onTop);\n        gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);\n      }\n      raf = visible && !document.hidden ? requestAnimationFrame(loop) : 0;\n    };\n    const wake = () => {\n      if (!raf && visible && !document.hidden) {\n        last = 0;\n        raf = requestAnimationFrame(loop);\n      }\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(host);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      wake();\n    });\n    io.observe(host);\n    document.addEventListener(\"visibilitychange\", wake);\n    one.addEventListener(\"load\", upload);\n    two.addEventListener(\"load\", upload);\n    upload();\n    resize();\n    wake();\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", wake);\n      one.removeEventListener(\"load\", upload);\n      two.removeEventListener(\"load\", upload);\n    };\n  }, [reduce]);\n\n  function hold(e: ReactPointerEvent<HTMLButtonElement>) {\n    const r = e.currentTarget.getBoundingClientRect();\n    hand.current = { ...hand.current, x: (e.clientX - r.left) / r.height, y: (e.clientY - r.top) / r.height, at: performance.now() };\n  }\n  /** Letting go far enough from the corner, or pressing the button from the keyboard, takes the print off. */\n  function release(e: ReactPointerEvent<HTMLButtonElement>) {\n    const r = e.currentTarget.getBoundingClientRect();\n    const far = Math.hypot(r.width / r.height - hand.current.x, 1 - hand.current.y);\n    if (far > LET_GO) hand.current = { ...hand.current, pull: true, at: -1e6 };\n    void e;\n  }\n  function takeOff() {\n    if (live) hand.current = { ...hand.current, pull: true, at: -1e6 };\n    else setTop((n) => 1 - n);\n  }\n\n  const upper = prints[top];\n  const lower = prints[1 - top];\n\n  return (\n    <section ref={stage} className=\"relative isolate flex min-h-[780px] w-full flex-col items-center justify-center overflow-hidden bg-[#e7e2d8] px-4 py-16\">\n      <style>{`\n        .gpp-cut { clip-path: polygon(0 0, 100% 0, 100% 58%, 46% 100%, 0 100%); }\n        .gpp-flap { clip-path: polygon(100% 0, 0 100%, 21% 9%); background: linear-gradient(128deg, #b9b3a6 0%, #f7f4ec 34%, #fffdf8 62%, #dcd6c9 100%); }\n      `}</style>\n      <div aria-hidden=\"true\" className=\"absolute inset-0 -z-10\" style={{ background: \"radial-gradient(70% 60% at 42% 34%, #f6f2ea 0%, #e7e2d8 55%, #d6d0c4 100%)\" }} />\n\n      <button\n        ref={print}\n        type=\"button\"\n        aria-label={`Peel off the ${upper.name} print to show ${lower.name} underneath. Move the pointer over the print to lift its corner.`}\n        onClick={takeOff}\n        onPointerMove={hold}\n        onPointerDown={hold}\n        onPointerUp={release}\n        className=\"relative block aspect-[4/5] w-[min(78vw,400px)] cursor-grab touch-pan-y bg-[#cfc9bc] shadow-[0_1px_1px_rgba(60,48,30,0.25),0_18px_26px_-14px_rgba(60,48,30,0.45),0_50px_70px_-36px_rgba(60,48,30,0.5)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-stone-900 focus-visible:ring-offset-8 focus-visible:ring-offset-[#e7e2d8] active:cursor-grabbing\"\n      >\n        {/* The still version: the lower print with the top one cut back at its corner. Shown with reduced motion or without WebGL. */}\n        <img ref={first} src={prints[0].src} alt={prints[0].alt} crossOrigin=\"anonymous\" width={900} height={1125} style={{ backgroundColor: prints[0].tone }} className={`absolute inset-0 box-border h-full w-full border-[10px] border-[#f5f3ee] object-cover ${top === 0 ? \"gpp-cut z-10\" : \"\"}`} />\n        <img ref={second} src={prints[1].src} alt={prints[1].alt} crossOrigin=\"anonymous\" width={900} height={1125} style={{ backgroundColor: prints[1].tone }} className={`absolute inset-0 box-border h-full w-full border-[10px] border-[#f5f3ee] object-cover ${top === 1 ? \"gpp-cut z-10\" : \"\"}`} />\n        <span aria-hidden=\"true\" className=\"gpp-flap absolute bottom-0 right-0 z-10 block h-[42%] w-[54%] drop-shadow-[6px_10px_10px_rgba(40,30,15,0.35)]\" />\n      </button>\n      <canvas ref={canvas} aria-hidden=\"true\" className={`pointer-events-none absolute inset-0 z-20 h-full w-full transition-opacity duration-300 ${live ? \"opacity-100\" : \"opacity-0\"}`} />\n\n      <div className=\"relative z-30 mt-9 flex w-[min(78vw,400px)] items-baseline justify-between gap-4 text-[13px]\">\n        <p className=\"text-stone-600\">\n          <span className=\"font-semibold text-stone-900\">{upper.name}</span>\n          <span className=\"mx-2 text-stone-500\">on</span>\n          {lower.name}\n        </p>\n        <p className=\"text-stone-600\">Peel the corner</p>\n      </div>\n    </section>\n  );\n}\n"}]}
