{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"photograph-in-an-enamel-darkroom-tray-under-water-that-ripples-where-the-pointer-drags","type":"registry:block","title":"Photograph in an Enamel Darkroom Tray Under Water That Ripples Where the Pointer Drags","description":"Water ripple effect: a print lies in an enamel darkroom tray and the pointer drags waves across it, bending the photo beneath. Free React and HTML to copy.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/photograph-in-an-enamel-darkroom-tray-under-water-that-ripples-where-the-pointer-drags.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// The print lying in the tray. Swap the address and the alt text together.\nconst PHOTO = \"https://images.pexels.com/photos/35024256/pexels-photo-35024256.jpeg?auto=compress&cs=tinysrgb&w=900\";\nconst PHOTO_ALT = \"A person in a pale yellow suit and a wide flat-brimmed black hat, standing against a deep red wall\";\nconst tray = { label: \"Tray 3\", bath: \"Wash, 20 °C\", action: \"Let a drop fall\" };\n\n// Water surface grid. More cells give finer rings and cost more per frame.\nconst GW = 168;\nconst GH = 210;\n/** How much of its height a wave keeps each step: closer to 1 means the water rings for longer. */\nconst CALM = 0.983;\n/** Tilt of the print in the tray, in radians. Keep the still print's rotate() in step with it. */\nconst TILT = -0.061;\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 sampler2D u_photo;\nuniform sampler2D u_wave;\nuniform float u_img;\nuniform float u_tilt;\n\nfloat box(vec2 p, vec2 b, float r){\n  vec2 d = abs(p) - b + r;\n  return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0) - r;\n}\nfloat hash(vec2 p){ return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }\n\n// What lies on the bottom of the tray at a point: the enamel floor, the paper border or the picture.\nvec3 bed(vec2 uv){\n  float A = u_res.x / u_res.y;\n  vec2 q = (uv - 0.5) * vec2(A, 1.0);\n  float c = cos(-u_tilt), s = sin(-u_tilt);\n  vec2 l = vec2(c * q.x - s * q.y, s * q.x + c * q.y);\n  vec2 half_ = vec2(0.36 * A, 0.45 * A);\n  float edge = 0.03 * A;\n\n  vec3 enamel = vec3(0.965, 0.962, 0.945);\n  float wall = box(q, vec2(0.5 * A, 0.5), 0.07);\n  enamel *= 0.90 + 0.10 * smoothstep(0.0, -0.10, wall);\n  enamel -= (hash(floor(uv * u_res * 0.5)) - 0.5) * 0.012;\n  float shade = box(l - vec2(0.010, 0.016), half_, 0.004);\n  enamel *= 1.0 - 0.42 * (1.0 - smoothstep(-0.012, 0.040, shade));\n\n  vec2 inner = half_ - edge;\n  vec2 t = l / inner * 0.5 + 0.5;\n  float ia = inner.x / inner.y;\n  if (u_img > ia) t.x = (t.x - 0.5) * ia / u_img + 0.5;\n  else t.y = (t.y - 0.5) * u_img / ia + 0.5;\n  vec3 photo = texture2D(u_photo, clamp(t, 0.0, 1.0)).rgb;\n  // A wet print is a touch deeper and richer than a dry one.\n  photo = pow(photo, vec3(1.08));\n  float px = 1.5 / u_res.y;\n  vec3 paper = vec3(0.965, 0.96, 0.94) - (hash(floor(uv * u_res * 0.7)) - 0.5) * 0.015;\n  vec3 print = mix(photo, paper, smoothstep(-px, px, box(l, inner, 0.0)));\n  return mix(print, enamel, smoothstep(-px, px, box(l, half_, 0.004)));\n}\n\nvoid main(){\n  vec2 uv = vec2(gl_FragCoord.x / u_res.x, 1.0 - gl_FragCoord.y / u_res.y);\n  float A = u_res.x / u_res.y;\n  vec4 w = texture2D(u_wave, uv);\n  vec2 slope = (w.xy - 0.50196) * 2.0;\n  float bend = (w.z - 0.50196) * 2.0;\n\n  // Looking down through the surface: each colour is bent a little differently.\n  vec3 col;\n  col.r = bed(uv + slope * 0.040).r;\n  col.g = bed(uv + slope * 0.043).g;\n  col.b = bed(uv + slope * 0.046).b;\n  col *= vec3(0.955, 0.982, 1.0);\n  // Crests focus the light on the bottom and troughs spread it.\n  col *= 1.0 + pow(clamp(bend, 0.0, 1.0), 1.4) * 0.95 - clamp(-bend, 0.0, 1.0) * 0.22;\n\n  // The window above the bench, mirrored in the surface and torn apart by every wave.\n  vec2 r = (uv - vec2(0.26, 0.15)) * vec2(A, 1.0) + slope * 0.55;\n  r = vec2(r.x + r.y * 0.30, r.y);\n  float pane = 1.0 - smoothstep(-0.03, 0.05, box(r, vec2(0.17, 0.085), 0.03));\n  col = mix(col, vec3(1.0, 0.995, 0.98), pane * 0.36);\n\n  // Sharp glints where the surface tips toward the light.\n  vec3 n = normalize(vec3(-slope * 3.2, 1.0));\n  float glint = pow(max(dot(n, normalize(vec3(-0.34, -0.42, 0.84))), 0.0), 60.0);\n  col += glint * 0.32;\n\n  gl_FragColor = vec4(col, 1.0);\n}`;\n\nexport default function PhotographInAnEnamelDarkroomTrayUnderWaterThatRipplesWhereThePointerDrags() {\n  const reduce = useReducedMotion();\n  const well = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const image = useRef<HTMLImageElement>(null);\n  /** Pushes the water down at a point of the tray (0 to 1 each way). Set once the shader is running. */\n  const poke = useRef<((x: number, y: number, radius: number, amount: number) => void) | null>(null);\n  const hand = useRef({ x: 0, y: 0, down: false, at: 0 });\n  const [wet, setWet] = useState(false);\n  const [drops, setDrops] = useState(0);\n\n  useEffect(() => {\n    const el = well.current;\n    const cv = canvas.current;\n    const img = image.current;\n    if (reduce || !el || !cv || !img) return;\n    const gl = cv.getContext(\"webgl\", { antialias: false, alpha: false });\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_photo\"), 0);\n    gl.uniform1i(u(\"u_wave\"), 1);\n    gl.uniform1f(u(\"u_tilt\"), TILT);\n    const uRes = u(\"u_res\");\n\n    const texture = (unit: number) => {\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    };\n\n    // The water: two height fields, this step and the one before, on a small grid.\n    let now = new Float32Array(GW * GH);\n    let before = new Float32Array(GW * GH);\n    const pixels = new Uint8Array(GW * GH * 4);\n    texture(1);\n    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, GW, GH, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels);\n\n    const push = (x: number, y: number, radius: number, amount: number) => {\n      const cx = x * GW;\n      const cy = y * GH;\n      const reach = Math.ceil(radius * 2.4);\n      for (let j = Math.max(1, Math.floor(cy - reach)); j <= Math.min(GH - 2, Math.ceil(cy + reach)); j++) {\n        for (let i = Math.max(1, Math.floor(cx - reach)); i <= Math.min(GW - 2, Math.ceil(cx + reach)); i++) {\n          const d = ((i - cx) * (i - cx) + (j - cy) * (j - cy)) / (radius * radius);\n          now[j * GW + i] += amount * Math.exp(-d);\n        }\n      }\n    };\n    const step = () => {\n      for (let j = 0; j < GH; j++) {\n        const row = j * GW;\n        const up = j > 0 ? row - GW : row;\n        const down = j < GH - 1 ? row + GW : row;\n        for (let i = 0; i < GW; i++) {\n          const left = i > 0 ? i - 1 : i;\n          const right = i < GW - 1 ? i + 1 : i;\n          // Each cell heads for the average of its neighbours and overshoots. The walls of the tray send waves back.\n          before[row + i] = ((now[row + left] + now[row + right] + now[up + i] + now[down + i]) * 0.5 - before[row + i]) * CALM;\n        }\n      }\n      const swap = now;\n      now = before;\n      before = swap;\n    };\n    const pack = (v: number) => (v > 127 ? 255 : v < -127 ? 1 : 128 + v);\n    const encode = () => {\n      for (let j = 0; j < GH; j++) {\n        const row = j * GW;\n        const up = j > 0 ? row - GW : row;\n        const down = j < GH - 1 ? row + GW : row;\n        for (let i = 0; i < GW; i++) {\n          const left = now[row + (i > 0 ? i - 1 : i)];\n          const right = now[row + (i < GW - 1 ? i + 1 : i)];\n          const o = (row + i) * 4;\n          pixels[o] = pack((left - right) * 330);\n          pixels[o + 1] = pack((now[up + i] - now[down + i]) * 330);\n          pixels[o + 2] = pack((now[row + i] * 4 - left - right - now[up + i] - now[down + i]) * 700);\n          pixels[o + 3] = 255;\n        }\n      }\n    };\n\n    let ready = false;\n    let visible = true;\n    let raf = 0;\n    let last = 0;\n    let owed = 0;\n    let nextDrip = 1.9;\n    const started = performance.now();\n\n    const upload = () => {\n      try {\n        texture(0);\n        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, img);\n        gl.uniform1f(u(\"u_img\"), img.naturalWidth / img.naturalHeight);\n        ready = true;\n        setWet(true);\n        poke.current = (x, y, radius, amount) => {\n          push(x, y, radius, amount);\n          wake();\n        };\n        // One drop straight away, so the water is already moving when the tray first shows.\n        push(0.64, 0.34, 3.2, -1.0);\n      } catch {\n        // The picture could not be read into the shader: the still print underneath stays on show.\n        ready = false;\n      }\n    };\n\n    const resize = () => {\n      const ratio = Math.min(window.devicePixelRatio || 1, 2);\n      cv.width = Math.max(1, Math.round(el.clientWidth * ratio));\n      cv.height = Math.max(1, Math.round(el.clientHeight * ratio));\n      gl.viewport(0, 0, cv.width, cv.height);\n    };\n\n    const loop = (stamp: number) => {\n      if (ready) {\n        const t = (stamp - started) / 1000;\n        owed = Math.min(owed + (last ? (stamp - last) / 1000 : 0.016), 0.05);\n        last = stamp;\n        // Left alone, something stirs the water: a slow unseen fingertip and the odd falling drop.\n        if (stamp - hand.current.at > 2600) {\n          push(0.5 + 0.31 * Math.sin(t * 0.83), 0.52 + 0.33 * Math.sin(t * 0.61 + 1.3), 2.4, -0.016);\n          if (t > nextDrip) {\n            nextDrip = t + 1.9 + Math.random() * 1.6;\n            push(0.14 + Math.random() * 0.72, 0.14 + Math.random() * 0.72, 2.8, -0.6 - Math.random() * 0.4);\n          }\n        }\n        while (owed >= 1 / 60) {\n          step();\n          owed -= 1 / 60;\n        }\n        encode();\n        gl.activeTexture(gl.TEXTURE1);\n        gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, GW, GH, gl.RGBA, gl.UNSIGNED_BYTE, pixels);\n        gl.uniform2f(uRes, cv.width, cv.height);\n        gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);\n      }\n      raf = visible && !document.hidden ? requestAnimationFrame(loop) : 0;\n    };\n    function 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(el);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      wake();\n    });\n    io.observe(el);\n    document.addEventListener(\"visibilitychange\", wake);\n    if (img.complete && img.naturalWidth) upload();\n    else img.addEventListener(\"load\", upload);\n    resize();\n    wake();\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", wake);\n      img.removeEventListener(\"load\", upload);\n      poke.current = null;\n    };\n  }, [reduce]);\n\n  const place = (e: ReactPointerEvent<HTMLDivElement>) => {\n    const r = e.currentTarget.getBoundingClientRect();\n    return { x: (e.clientX - r.left) / r.width, y: (e.clientY - r.top) / r.height };\n  };\n  /** A fingertip pressed into the water. */\n  function onDown(e: ReactPointerEvent<HTMLDivElement>) {\n    const p = place(e);\n    hand.current = { ...p, down: true, at: performance.now() };\n    poke.current?.(p.x, p.y, 3, -0.9);\n  }\n  /** Dragging through the water leaves a wake: the faster the drag, the deeper the furrow. */\n  function onMove(e: ReactPointerEvent<HTMLDivElement>) {\n    const p = place(e);\n    const h = hand.current;\n    const fresh = performance.now() - h.at < 300;\n    const from = fresh ? h : p;\n    const dx = p.x - from.x;\n    const dy = p.y - from.y;\n    const travel = Math.hypot(dx * GW, dy * GH);\n    const steps = Math.max(1, Math.min(14, Math.round(travel / 2)));\n    const depth = Math.min(0.2, 0.02 + travel * 0.016) / Math.sqrt(steps);\n    for (let k = 1; k <= steps; k++) poke.current?.(from.x + (dx * k) / steps, from.y + (dy * k) / steps, 2.4, -depth);\n    hand.current = { ...p, down: h.down, at: performance.now() };\n  }\n  function drip() {\n    setDrops((n) => n + 1);\n    hand.current.at = performance.now() - 1800;\n    poke.current?.(0.2 + Math.random() * 0.6, 0.2 + Math.random() * 0.6, 3.2, -1.1);\n  }\n\n  return (\n    <section className=\"relative isolate flex min-h-[760px] w-full flex-col items-center justify-center overflow-hidden bg-[#d9d5cd] px-4 py-14\">\n      <div aria-hidden=\"true\" className=\"absolute inset-0 -z-10\" style={{ background: \"radial-gradient(90% 70% at 32% 18%, #ece9e3 0%, #d9d5cd 55%, #c6c1b7 100%)\" }} />\n\n      {/* The tray: white enamel, a dark rolled rim, and a well with the water in it. */}\n      <div\n        className=\"relative w-[min(82vw,410px)] rounded-[44px] p-[22px]\"\n        style={{\n          background: \"linear-gradient(150deg, #ffffff 0%, #f4f2ee 38%, #e3e0d9 100%)\",\n          boxShadow:\n            \"0 0 0 3px #1c2740, 0 0 0 4px rgba(255,255,255,0.35), 0 70px 80px -40px rgba(58,44,28,0.55), 0 26px 34px -18px rgba(58,44,28,0.35), inset 0 2px 1px #ffffff, inset 0 -5px 8px rgba(70,60,45,0.14)\",\n        }}\n      >\n        <div\n          ref={well}\n          onPointerDown={onDown}\n          onPointerMove={onMove}\n          onPointerUp={() => (hand.current.down = false)}\n          className=\"relative aspect-[4/5] touch-pan-y overflow-hidden rounded-[24px] bg-[#e9e7e0]\"\n        >\n          {/* The still print. It is what shows with reduced motion or without WebGL. */}\n          <div className=\"absolute left-1/2 top-1/2 aspect-[4/5] w-[72%] -translate-x-1/2 -translate-y-1/2 rotate-[-3.5deg] bg-[#f6f5f0] p-[3%] shadow-[3px_6px_14px_rgba(60,50,35,0.35)]\">\n            <img ref={image} src={PHOTO} alt={PHOTO_ALT} crossOrigin=\"anonymous\" width={900} height={1125} className=\"h-full w-full object-cover\" />\n          </div>\n          <canvas ref={canvas} aria-hidden=\"true\" className={`absolute inset-0 h-full w-full transition-opacity duration-500 ${wet ? \"opacity-100\" : \"opacity-0\"}`} />\n          {/* The wall of the well throws a shadow onto the water, and the water climbs it in a thin bright line. */}\n          <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 rounded-[24px] shadow-[inset_0_14px_20px_-6px_rgba(45,38,26,0.5),inset_10px_0_16px_-10px_rgba(45,38,26,0.3),inset_0_-2px_2px_rgba(255,255,255,0.75),inset_0_0_0_1px_rgba(255,255,255,0.35)]\" />\n        </div>\n      </div>\n\n      <div className=\"mt-9 flex w-[min(82vw,410px)] items-center justify-between gap-4 px-1\">\n        <p className=\"whitespace-nowrap text-[13px] leading-tight text-stone-700\">\n          <span className=\"font-semibold text-stone-900\">{tray.label}</span>\n          <span className=\"mx-2 text-stone-500\">/</span>\n          {tray.bath}\n        </p>\n        <button\n          type=\"button\"\n          onClick={drip}\n          className=\"inline-flex h-10 shrink-0 items-center gap-2 rounded-full bg-[#1c2740] px-4 text-[13px] font-medium text-white shadow-[0_6px_14px_-6px_rgba(28,39,64,0.7)] transition-transform duration-150 hover:-translate-y-0.5 active:translate-y-0 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#1c2740] focus-visible:ring-offset-2 focus-visible:ring-offset-[#d9d5cd] motion-reduce:transition-none\"\n        >\n          <svg aria-hidden=\"true\" viewBox=\"0 0 20 20\" className=\"h-4 w-4\" fill=\"currentColor\">\n            <path d=\"M10 2.2c2.6 3.3 5 6.2 5 9.3a5 5 0 0 1-10 0c0-3.1 2.4-6 5-9.3Z\" />\n          </svg>\n          {tray.action}\n        </button>\n        <span className=\"sr-only\" aria-live=\"polite\">\n          {drops > 0 ? `${drops} ${drops === 1 ? \"drop has\" : \"drops have\"} fallen into the tray.` : \"\"}\n        </span>\n      </div>\n    </section>\n  );\n}\n"}]}
