{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"photo-gallery-on-a-cotton-paper-sheet-where-each-portrait-blooms-in-like-ink-through-wet-fibres","type":"registry:block","title":"Photo Gallery on a Cotton Paper Sheet Where Each Portrait Blooms In Like Ink Through Wet Fibres","description":"Photo slider in WebGL: each portrait soaks across a sheet of cotton paper like ink through wet fibres, then its colours develop. Free React and HTML code.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/photo-gallery-on-a-cotton-paper-sheet-where-each-portrait-blooms-in-like-ink-through-wet-fibres.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// One entry per plate: the picture, its title, and the colour of ink it soaks in with.\nconst plates = [\n  {\n    title: \"Carmine\",\n    ink: \"#3b0d16\",\n    src: \"https://images.pexels.com/photos/32174976/pexels-photo-32174976.jpeg?auto=compress&cs=tinysrgb&w=900\",\n    alt: \"A woman with a full round afro, both hands raised to her hair, in a black top against a bright red wall\",\n  },\n  {\n    title: \"Verdigris\",\n    ink: \"#0b2c36\",\n    src: \"https://images.pexels.com/photos/19342936/pexels-photo-19342936.jpeg?auto=compress&cs=tinysrgb&w=900\",\n    alt: \"A woman with long straight hair in a dark open jacket, lit in teal with a warm edge of light, looking down into the camera\",\n  },\n  {\n    title: \"Rose madder\",\n    ink: \"#1c2038\",\n    src: \"https://images.pexels.com/photos/15227249/pexels-photo-15227249.jpeg?auto=compress&cs=tinysrgb&w=900\",\n    alt: \"A woman with dark hair tied back, a navy blazer pulled around bare shoulders, against a soft peach wall\",\n  },\n  {\n    title: \"Saffron\",\n    ink: \"#47200a\",\n    src: \"https://images.pexels.com/photos/38164214/pexels-photo-38164214.jpeg?auto=compress&cs=tinysrgb&w=900\",\n    alt: \"A woman with a shaved head in a white sleeveless top and dark trousers, leaning one elbow on a tall white plinth against an orange backdrop\",\n  },\n];\n\n/** Seconds one bloom takes, and seconds a finished plate rests before the next one starts. */\nconst BLOOM = 2.6;\nconst REST = 2.4;\n/** Where on the sheet each bloom starts (0 to 1 each way). They are used in turn. */\nconst SEEDS = [\n  [0.34, 0.3],\n  [0.7, 0.62],\n  [0.3, 0.74],\n  [0.68, 0.24],\n];\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_from;\nuniform sampler2D u_to;\nuniform vec2 u_img;\nuniform float u_blank;\nuniform float u_p;\nuniform vec2 u_seed;\nuniform vec3 u_ink;\nuniform float u_shift;\n\nfloat hash(vec2 p){ return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }\nfloat noise(vec2 p){\n  vec2 i = floor(p), f = fract(p);\n  f = f * f * (3.0 - 2.0 * f);\n  return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);\n}\nfloat fbm(vec2 p){\n  float v = 0.0, a = 0.5;\n  for (int i = 0; i < 5; i++){ v += a * noise(p); p = p * 2.03 + 17.1; a *= 0.5; }\n  return v;\n}\nvec2 cover(vec2 uv, float img){\n  float A = u_res.x / u_res.y;\n  vec2 t = uv;\n  if (img > A) t.x = (uv.x - 0.5) * A / img + 0.5;\n  else t.y = (uv.y - 0.5) * img / A + 0.5;\n  return clamp(t, 0.0, 1.0);\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  vec2 p = vec2(uv.x * A, uv.y);\n\n  // The sheet: long fibres one way, a finer tooth across them.\n  float fibre = fbm(p * vec2(34.0, 150.0)) * 0.6 + fbm(p * 210.0) * 0.4;\n  vec3 paper = vec3(0.972, 0.958, 0.925) * (0.95 + 0.09 * fibre);\n\n  // How long the wet front takes to reach this point. Big soft lobes, then fine feathering along the fibres.\n  vec2 q = p * 2.1 + u_shift;\n  vec2 warp = vec2(fbm(q), fbm(q + 7.3)) - 0.5;\n  float d = length(p - vec2(u_seed.x * A, u_seed.y) + warp * 0.34);\n  float reach = d * 0.86 + (fbm(p * 5.0 + warp * 2.4 + u_shift * 1.7) - 0.5) * 0.46 + (fibre - 0.5) * 0.13;\n  float s = (u_p * 2.35 - 0.24) - reach;\n\n  float soaked = smoothstep(0.0, 0.03, s);\n  float settled = smoothstep(0.14, 0.58, s);\n  // Pigment gathers where the water stops: a darker line riding the front.\n  float tide = smoothstep(0.0, 0.016, s) * (1.0 - smoothstep(0.016, 0.085, s));\n  float damp = smoothstep(-0.11, 0.0, s) * (1.0 - soaked);\n\n  // The paper swells a little where it is wettest, which shifts the picture under it.\n  vec2 swell = warp * 0.022 * (1.0 - settled) * soaked;\n  vec3 next = texture2D(u_to, cover(uv + swell, u_img.y)).rgb;\n  vec3 prev = mix(texture2D(u_from, cover(uv, u_img.x)).rgb, paper, u_blank);\n  prev = mix(prev, prev * vec3(0.86, 0.85, 0.84), damp);\n\n  // First the picture arrives in one ink only, then its own colours come up behind the front.\n  float tone = dot(next, vec3(0.299, 0.587, 0.114));\n  float load = clamp(1.22 - tone * 1.08, 0.0, 1.0);\n  load = load * (0.82 + 0.36 * fbm(p * 9.0 + u_shift));\n  vec3 wash = mix(paper, u_ink, clamp(load, 0.0, 1.0));\n  wash = mix(wash, u_ink, tide * 0.8);\n\n  vec3 col = mix(prev, wash, soaked);\n  col = mix(col, next, settled);\n  // Everything is printed on the same sheet, so the tooth shows through the finished plate too.\n  col *= 0.955 + 0.075 * fibre;\n  gl_FragColor = vec4(col, 1.0);\n}`;\n\nconst rgb = (hex: string) => [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255);\n\nexport default function PhotoGalleryOnACottonPaperSheetWhereEachPortraitBloomsInLikeInkThroughWetFibres() {\n  const reduce = useReducedMotion();\n  const frame = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const images = useRef<(HTMLImageElement | null)[]>([]);\n  const want = useRef(0);\n  const playing = useRef(true);\n  const held = useRef(0);\n  const [index, setIndex] = useState(0);\n  const [play, setPlay] = useState(true);\n  const [live, setLive] = useState(false);\n\n  useEffect(() => {\n    want.current = index;\n  }, [index]);\n  useEffect(() => {\n    playing.current = play;\n  }, [play]);\n\n  useEffect(() => {\n    const el = frame.current;\n    const cv = canvas.current;\n    if (reduce || !el || !cv) 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_from\"), 0);\n    gl.uniform1i(u(\"u_to\"), 1);\n    const uni = { res: u(\"u_res\"), img: u(\"u_img\"), blank: u(\"u_blank\"), p: u(\"u_p\"), seed: u(\"u_seed\"), ink: u(\"u_ink\"), shift: u(\"u_shift\") };\n\n    const textures: (WebGLTexture | null)[] = plates.map(() => null);\n    const shapes = plates.map(() => 0.8);\n    const cleanups: (() => void)[] = [];\n    let broken = false;\n    plates.forEach((_, i) => {\n      const img = images.current[i];\n      if (!img) return;\n      const upload = () => {\n        if (textures[i] || broken || !img.naturalWidth) return;\n        try {\n          const tex = gl.createTexture();\n          gl.activeTexture(gl.TEXTURE0);\n          gl.bindTexture(gl.TEXTURE_2D, tex);\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          textures[i] = tex;\n          shapes[i] = img.naturalWidth / img.naturalHeight;\n          wake();\n        } catch {\n          // A picture could not be read into the shader: the plain pictures underneath stay on show.\n          broken = true;\n          setLive(false);\n        }\n      };\n      if (img.complete) upload();\n      img.addEventListener(\"load\", upload);\n      cleanups.push(() => img.removeEventListener(\"load\", upload));\n    });\n\n    let shown = -1; // the plate that is soaking in, or has finished\n    let under = -1; // what it is soaking over: -1 is the bare sheet\n    let begun = 0;\n    let count = 0;\n    let visible = true;\n    let raf = 0;\n    const started = performance.now();\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      const t = (stamp - started) / 1000;\n      if (!broken) {\n        let p = shown < 0 ? 1 : Math.min(1, (t - begun) / BLOOM);\n        // The next plate can start once the last has settled and its own picture has arrived.\n        if (p >= 1 && want.current !== shown && textures[want.current]) {\n          under = shown;\n          shown = want.current;\n          begun = t;\n          count += 1;\n          p = 0;\n          if (count === 1) setLive(true);\n        } else if (p >= 1 && shown >= 0 && playing.current && stamp > held.current && t - begun > BLOOM + (count === 1 ? REST * 0.3 : REST)) {\n          const following = (shown + 1) % plates.length;\n          if (textures[following]) setIndex(following);\n        }\n        if (shown >= 0) {\n          const seed = SEEDS[(count - 1) % SEEDS.length];\n          const ink = rgb(plates[shown].ink);\n          gl.activeTexture(gl.TEXTURE0);\n          gl.bindTexture(gl.TEXTURE_2D, textures[under < 0 ? shown : under]);\n          gl.activeTexture(gl.TEXTURE1);\n          gl.bindTexture(gl.TEXTURE_2D, textures[shown]);\n          gl.uniform2f(uni.res, cv.width, cv.height);\n          gl.uniform2f(uni.img, shapes[under < 0 ? shown : under], shapes[shown]);\n          gl.uniform1f(uni.blank, under < 0 ? 1 : 0);\n          // Quick to spread at first, slow to finish, the way a wet front runs out of water.\n          gl.uniform1f(uni.p, 1 - Math.pow(1 - p, 1.35));\n          gl.uniform2f(uni.seed, seed[0], seed[1]);\n          gl.uniform3f(uni.ink, ink[0], ink[1], ink[2]);\n          gl.uniform1f(uni.shift, count * 3.71);\n          gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);\n        }\n      }\n      raf = visible && !document.hidden && !broken ? requestAnimationFrame(loop) : 0;\n    };\n    function wake() {\n      if (!raf && visible && !document.hidden && !broken) raf = requestAnimationFrame(loop);\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    resize();\n    wake();\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", wake);\n      cleanups.forEach((off) => off());\n    };\n  }, [reduce]);\n\n  function choose(i: number) {\n    held.current = performance.now() + 7000;\n    setIndex(i);\n  }\n  const plate = plates[index];\n\n  return (\n    <section className=\"relative isolate flex min-h-[820px] w-full flex-col items-center justify-center overflow-hidden bg-[#e9e2d3] px-4 py-12\">\n      <div aria-hidden=\"true\" className=\"absolute inset-0 -z-10\" style={{ background: \"radial-gradient(80% 65% at 50% 30%, #f3eee2 0%, #e9e2d3 60%, #dbd2bf 100%)\" }} />\n\n      {/* The sheet: heavy cotton paper with the plate printed on it and its title set beneath. */}\n      <figure\n        className=\"relative w-[min(86vw,430px)] rounded-[3px] bg-[#f8f4ea] p-[18px] pb-0 sm:p-[22px] sm:pb-0\"\n        style={{ boxShadow: \"0 1px 0 #ffffff inset, 0 0 0 1px rgba(120,100,70,0.10), 0 2px 3px rgba(80,60,30,0.12), 0 50px 60px -34px rgba(80,60,30,0.55), 0 18px 24px -16px rgba(80,60,30,0.30)\" }}\n      >\n        <div ref={frame} className=\"relative aspect-[4/5] overflow-hidden bg-[#f1ecdf]\">\n          {plates.map((item, i) => (\n            <img\n              key={item.src}\n              src={item.src}\n              alt={item.alt}\n              ref={(node) => {\n                images.current[i] = node;\n              }}\n              aria-hidden={i === index ? undefined : true}\n              crossOrigin=\"anonymous\"\n              width={900}\n              height={1125}\n              className={`absolute inset-0 h-full w-full object-cover ${i === index ? \"opacity-100\" : \"opacity-0\"}`}\n            />\n          ))}\n          <canvas ref={canvas} aria-hidden=\"true\" className={`absolute inset-0 h-full w-full ${live ? \"opacity-100\" : \"opacity-0\"}`} />\n          {/* The plate mark: the press leaves the picture sitting a hair below the surface of the sheet. */}\n          <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 shadow-[inset_0_1px_2px_rgba(60,45,20,0.35),inset_0_0_0_1px_rgba(60,45,20,0.08)]\" />\n        </div>\n        <figcaption className=\"flex h-[58px] items-center justify-between text-[13px] text-stone-700\">\n          <span aria-live=\"polite\" className=\"font-serif text-[17px] italic text-stone-900\">\n            {plate.title}\n          </span>\n          <span className=\"tabular-nums tracking-wide\">\n            Plate {index + 1} of {plates.length}\n          </span>\n        </figcaption>\n      </figure>\n\n      {/* One pot of ink per plate, and a way to stop the sheet changing by itself. */}\n      <div className=\"mt-7 flex items-center gap-1\">\n        {plates.map((item, i) => (\n          <button\n            key={item.title}\n            type=\"button\"\n            onClick={() => choose(i)}\n            aria-label={`Show ${item.title}, plate ${i + 1}`}\n            aria-current={i === index ? \"true\" : undefined}\n            className=\"group grid h-11 w-11 place-items-center rounded-full focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-stone-900\"\n          >\n            <span\n              className={`block rounded-full shadow-[inset_0_2px_3px_rgba(255,255,255,0.35),inset_0_-3px_5px_rgba(0,0,0,0.45),0_3px_6px_-2px_rgba(60,45,20,0.6)] transition-all duration-300 motion-reduce:transition-none ${i === index ? \"h-7 w-7 ring-2 ring-stone-900 ring-offset-[3px] ring-offset-[#e9e2d3]\" : \"h-5 w-5 group-hover:h-6 group-hover:w-6\"}`}\n              style={{ backgroundColor: item.ink }}\n            />\n          </button>\n        ))}\n        <span aria-hidden=\"true\" className=\"mx-2 h-5 w-px bg-stone-400\" />\n        <button\n          type=\"button\"\n          onClick={() => setPlay((v) => !v)}\n          aria-pressed={!play}\n          aria-label=\"Pause the slideshow\"\n          className=\"grid h-11 w-11 place-items-center rounded-full text-stone-800 transition-colors hover:bg-stone-900/10 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-stone-900 motion-reduce:hidden\"\n        >\n          <svg aria-hidden=\"true\" viewBox=\"0 0 20 20\" className=\"h-4 w-4\" fill=\"currentColor\">\n            {play ? <path d=\"M5 3.5h3.4v13H5zM11.6 3.5H15v13h-3.4z\" /> : <path d=\"M6 3.2 16.5 10 6 16.8z\" />}\n          </svg>\n        </button>\n      </div>\n    </section>\n  );\n}\n"}]}
