{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"oxblood-silk-satin-webgl-background-with-folds-lit-by-a-lamp-that-follows-the-pointer","type":"registry:block","title":"Oxblood Silk Satin WebGL Background with Folds Lit by a Lamp That Follows the Pointer","description":"Silk background in raw WebGL: a shader drapes oxblood satin into slow folds and lights it with a lamp the pointer carries. Free React and HTML to copy.","dependencies":[],"files":[{"path":"components/we-inc/oxblood-silk-satin-webgl-background-with-folds-lit-by-a-lamp-that-follows-the-pointer.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Everything about the cloth. Colours are red, green, blue from 0 to 1.\nconst silk = {\n  speed: 1, // how fast the folds travel; 0 holds the cloth still\n  folds: 2.5, // how many folds cross the view; higher gathers the cloth tighter\n  depth: 0.42, // how deep the folds are; 0.2 is a pressed sheet, 0.6 a heavy drape\n  gloss: 1, // strength of the satin highlight; 0 gives matt cotton\n  lampHeight: 1.35, // how far the lamp hangs above the cloth; lower gives longer shadows\n  quality: 0.85, // share of the device pixels that are drawn\n  maxPixels: 1100000, // the canvas never draws more pixels than this\n  deep: [0.085, 0.006, 0.022], // the cloth in the bottom of a fold\n  body: [0.52, 0.035, 0.085], // the cloth facing the lamp\n  sheen: [1.0, 0.5, 0.46], // the colour the weave turns where it catches the light\n};\n\nconst VERTEX = `\nattribute vec2 position;\nvoid main() { gl_Position = vec4(position, 0.0, 1.0); }`;\n\nconst FRAGMENT = `\nprecision highp float;\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec2 uLamp;\nuniform float uFolds;\nuniform float uDepth;\nuniform float uGloss;\nuniform float uLampHeight;\nuniform vec3 uDeep;\nuniform vec3 uBody;\nuniform vec3 uSheen;\n\nfloat hash(vec2 p) {\n  p = fract(p * vec2(123.34, 456.21));\n  p += dot(p, p + 45.32);\n  return fract(p.x * p.y);\n}\nfloat noise(vec2 p) {\n  vec2 i = floor(p);\n  vec2 f = fract(p);\n  f = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\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}\n\n// The height of the cloth. Long folds run up one diagonal; a slow warp makes them pinch,\n// fork and wander the way a length of satin does when it is dropped on a table.\nfloat cloth(vec2 p) {\n  float t = uTime * 0.05;\n  vec2 q = vec2(p.x * 0.82 + p.y * 0.57, -p.x * 0.57 + p.y * 0.82);\n  vec2 w = vec2(noise(q * 0.42 + vec2(0.0, t)), noise(q * 0.42 + vec2(7.3, -t * 0.8))) - 0.5;\n  q += w * 1.5;\n  float a = q.x * uFolds + 1.25 * sin(q.y * 0.62 + t * 1.6);\n  float h = 0.56 * sin(a);\n  h += 0.25 * sin(a * 2.03 + 1.7 + 1.1 * sin(q.y * 1.15 - t * 2.3));\n  h += 0.11 * sin(a * 4.1 + q.y * 0.9 + 5.0 * w.x);\n  h += 0.04 * sin(a * 8.3 + q.y * 2.3 + 9.0 * w.y);\n  h += 0.32 * (noise(q * vec2(0.9, 0.5) + vec2(t, 3.0)) - 0.5);\n  // Satin folds are round on top and pinched underneath.\n  return h - 0.34 * h * h;\n}\n\nvoid main() {\n  float unit = min(uRes.x, uRes.y);\n  vec2 p = (gl_FragCoord.xy - 0.5 * uRes) / unit * 4.6;\n\n  float e = 0.014;\n  float h = cloth(p);\n  vec2 slope = vec2(cloth(p + vec2(e, 0.0)) - h, cloth(p + vec2(0.0, e)) - h) / e;\n  vec3 n = normalize(vec3(-slope * uDepth, 1.0));\n\n  vec3 here = vec3(p, h * uDepth);\n  vec3 lamp = vec3(uLamp * 2.4, uLampHeight);\n  vec3 toLamp = lamp - here;\n  float reach = length(toLamp);\n  vec3 L = toLamp / reach;\n  vec3 H = normalize(L + vec3(0.0, 0.0, 1.0));\n\n  // Walk toward the lamp: any fold that rises above the ray puts this point in soft shadow.\n  float shade = 1.0;\n  float step = 0.07;\n  float along = 0.0;\n  for (int i = 0; i < 9; i++) {\n    along += step;\n    step *= 1.32;\n    vec3 s = here + L * along;\n    float gap = s.z - cloth(s.xy) * uDepth;\n    shade = min(shade, 0.5 + 8.0 * gap / along);\n  }\n  shade = smoothstep(0.0, 1.0, clamp(shade, 0.0, 1.0));\n\n  float fall = 1.0 / (1.0 + 0.075 * reach * reach);\n  float lit = max(dot(n, L), 0.0);\n  float hollow = smoothstep(-0.95, 0.45, h);\n\n  vec3 base = mix(uDeep, uBody, hollow);\n  vec3 col = base * (0.13 + 1.55 * lit * shade * fall);\n  col += uDeep * 0.5 * (1.0 - n.z);\n\n  // Satin: the floats of the weave run one way, so the highlight smears across the folds\n  // into long bands, with a tighter, whiter core where the cloth faces the lamp exactly.\n  vec3 thread = normalize(vec3(-0.57, 0.82, 0.0));\n  vec3 T = normalize(thread - n * dot(n, thread));\n  float tdh = dot(T, H);\n  float band = pow(max(1.0 - tdh * tdh, 0.0), 90.0);\n  float nh = max(dot(n, H), 0.0);\n  float wide = band * pow(nh, 5.0);\n  float core = pow(nh, 150.0);\n  float glint = (wide * 0.8 + core * 0.7) * shade * fall * uGloss * smoothstep(0.0, 0.25, lit);\n  col += mix(uBody * 2.4, uSheen, 0.72) * glint * 1.5;\n\n  // Shot silk: seen edge on, the cloth goes pale and pink.\n  float edge = pow(1.0 - n.z, 2.0);\n  col += uSheen * uBody * edge * 1.1 * (0.2 + 0.8 * shade) * fall;\n\n  // The weave itself, a hair wide, and a little grain.\n  float weft = sin((gl_FragCoord.x * 0.82 + gl_FragCoord.y * 0.57) * 2.1);\n  col *= 1.0 + 0.035 * weft * (0.4 + glint * 2.0);\n  col += (hash(gl_FragCoord.xy + fract(uTime) * 17.0) - 0.5) * 0.014;\n\n  vec2 c = gl_FragCoord.xy / uRes - 0.5;\n  col *= 1.0 - 0.55 * dot(c, c);\n  col = mix(col, 1.0 - exp(-col * 1.5), 0.6);\n  gl_FragColor = vec4(pow(max(col, 0.0), vec3(0.92)), 1.0);\n}`;\n\nconst mix = (a: number, b: number, t: number) => a + (b - a) * t;\n\n/** One fragment shader fills the block. `onLive` reports when a frame has been painted. */\nfunction SilkCanvas({ onLive }: { onLive: (live: boolean) => void }) {\n  const ref = useRef<HTMLCanvasElement>(null);\n\n  useEffect(() => {\n    const canvas = ref.current;\n    const gl = canvas?.getContext(\"webgl\", { antialias: false, alpha: false });\n    if (!canvas || !gl) return; // no WebGL: the CSS cloth underneath stays\n\n    const compile = (type: number, source: string) => {\n      const s = gl.createShader(type)!;\n      gl.shaderSource(s, source);\n      gl.compileShader(s);\n      return s;\n    };\n    const program = gl.createProgram()!;\n    gl.attachShader(program, compile(gl.VERTEX_SHADER, VERTEX));\n    gl.attachShader(program, compile(gl.FRAGMENT_SHADER, FRAGMENT));\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, 3, -1, -1, 3]), gl.STATIC_DRAW);\n    const position = gl.getAttribLocation(program, \"position\");\n    gl.enableVertexAttribArray(position);\n    gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0);\n    const at = (name: string) => gl.getUniformLocation(program, name);\n    const u = { res: at(\"uRes\"), time: at(\"uTime\"), lamp: at(\"uLamp\") };\n    gl.uniform1f(at(\"uFolds\"), silk.folds);\n    gl.uniform1f(at(\"uDepth\"), silk.depth);\n    gl.uniform1f(at(\"uGloss\"), silk.gloss);\n    gl.uniform1f(at(\"uLampHeight\"), silk.lampHeight);\n    gl.uniform3fv(at(\"uDeep\"), silk.deep);\n    gl.uniform3fv(at(\"uBody\"), silk.body);\n    gl.uniform3fv(at(\"uSheen\"), silk.sheen);\n\n    const still = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const lamp = { x: -0.3, y: 0.25, tx: -0.3, ty: 0.25, touched: -1e9 };\n    let raf = 0;\n    let visible = true;\n    let time = 20;\n    let before = 0;\n\n    const draw = () => {\n      gl.uniform1f(u.time, time);\n      gl.uniform2f(u.lamp, lamp.x, lamp.y);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n    };\n    const resize = () => {\n      const w = canvas.clientWidth;\n      const h = canvas.clientHeight || 1;\n      const scale = Math.min(Math.min(Math.max(window.devicePixelRatio || 1, 1), 2) * silk.quality, Math.sqrt(silk.maxPixels / Math.max(1, w * h)));\n      canvas.width = Math.max(1, Math.round(w * scale));\n      canvas.height = Math.max(1, Math.round(h * scale));\n      gl.viewport(0, 0, canvas.width, canvas.height);\n      gl.uniform2f(u.res, canvas.width, canvas.height);\n      draw();\n      onLive(true);\n    };\n    const loop = (now: number) => {\n      raf = requestAnimationFrame(loop);\n      const dt = Math.min((now - (before || now)) / 1000, 0.05);\n      before = now;\n      time += dt * silk.speed;\n      // Left alone for a few seconds, the lamp goes back to a slow figure of eight.\n      if (now - lamp.touched > 2600) {\n        const a = now / 1000;\n        lamp.tx = 0.62 * Math.sin(a * 0.42);\n        lamp.ty = 0.3 * Math.sin(a * 0.84 + 0.6);\n      }\n      const k = 1 - Math.exp(-dt * 3.4);\n      lamp.x = mix(lamp.x, lamp.tx, k);\n      lamp.y = mix(lamp.y, lamp.ty, k);\n      draw();\n    };\n    const start = () => {\n      cancelAnimationFrame(raf);\n      before = 0;\n      if (still) draw();\n      else if (visible && !document.hidden) raf = requestAnimationFrame(loop);\n    };\n    const onPointer = (e: PointerEvent) => {\n      const box = canvas.getBoundingClientRect();\n      const x = ((e.clientX - box.left) / box.width) * 2 - 1;\n      const y = 1 - ((e.clientY - box.top) / box.height) * 2;\n      if (Math.abs(x) > 1 || Math.abs(y) > 1) return;\n      const wide = Math.max(1, box.width / box.height);\n      const tall = Math.max(1, box.height / box.width);\n      lamp.tx = x * wide;\n      lamp.ty = y * tall;\n      lamp.touched = performance.now();\n    };\n    const onLost = (e: Event) => {\n      e.preventDefault();\n      cancelAnimationFrame(raf);\n      onLive(false);\n    };\n\n    const sizeWatcher = new ResizeObserver(resize);\n    sizeWatcher.observe(canvas);\n    const viewWatcher = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      start();\n    });\n    viewWatcher.observe(canvas);\n    document.addEventListener(\"visibilitychange\", start);\n    if (!still) window.addEventListener(\"pointermove\", onPointer, { passive: true });\n    canvas.addEventListener(\"webglcontextlost\", onLost);\n    resize();\n    start();\n\n    return () => {\n      cancelAnimationFrame(raf);\n      sizeWatcher.disconnect();\n      viewWatcher.disconnect();\n      document.removeEventListener(\"visibilitychange\", start);\n      window.removeEventListener(\"pointermove\", onPointer);\n      canvas.removeEventListener(\"webglcontextlost\", onLost);\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n    };\n  }, [onLive]);\n\n  return <canvas ref={ref} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />;\n}\n\n/** The cloth without a shader: the same folds as stacked CSS gradients. Shown first, and kept when WebGL is missing. */\nfunction StillSilk() {\n  return (\n    <div aria-hidden=\"true\" className=\"absolute inset-0 overflow-hidden bg-[#3d0611]\">\n      <div className=\"absolute -inset-[60%] rotate-[-35deg] bg-[linear-gradient(90deg,#1c0308_0%,#5e0a17_7%,#b0283a_12%,#f0a39a_14.5%,#a51f33_17%,#4a0812_24%,#1c0308_30%,#3d0611_36%,#8e1527_43%,#e58f8a_46%,#8e1527_49%,#2a040b_58%,#16020a_63%,#6d0d1c_70%,#c94a55_75%,#f4b1a6_77%,#9a1a2d_80%,#3d0611_88%,#1c0308_100%)]\" />\n      <div className=\"absolute -inset-[60%] rotate-[-28deg] bg-[linear-gradient(90deg,rgba(20,2,8,0)_0%,rgba(20,2,8,0.75)_22%,rgba(20,2,8,0)_40%,rgba(20,2,8,0.6)_61%,rgba(20,2,8,0)_78%,rgba(20,2,8,0.7)_100%)] blur-2xl\" />\n      <div className=\"oxblood-silk-satin-webgl-background-with-folds-lit-by-a-lamp-that-follows-the-pointer-lamp absolute -inset-[15%] bg-[radial-gradient(50%_45%_at_40%_42%,rgba(255,190,180,0.2)_0%,rgba(20,2,8,0)_60%,rgba(20,2,8,0.6)_100%)]\" />\n    </div>\n  );\n}\n\nexport default function OxbloodSilkSatinWebglBackgroundWithFoldsLitByALampThatFollowsThePointer() {\n  const [live, setLive] = useState(false);\n\n  return (\n    <section className=\"relative isolate h-[620px] overflow-hidden bg-[#3d0611] sm:h-[760px]\">\n      <style>{`\n        @keyframes oxblood-silk-satin-webgl-background-with-folds-lit-by-a-lamp-that-follows-the-pointer-lamp { 0%, 100% { transform: translate3d(-6%, 0, 0); } 50% { transform: translate3d(8%, -3%, 0); } }\n        .oxblood-silk-satin-webgl-background-with-folds-lit-by-a-lamp-that-follows-the-pointer-lamp { animation: oxblood-silk-satin-webgl-background-with-folds-lit-by-a-lamp-that-follows-the-pointer-lamp 8s ease-in-out infinite; }\n        @media (prefers-reduced-motion: reduce) { .oxblood-silk-satin-webgl-background-with-folds-lit-by-a-lamp-that-follows-the-pointer-lamp { animation: none; } }\n      `}</style>\n      <StillSilk />\n      <div className={`pointer-events-none absolute inset-0 transition-opacity duration-700 motion-reduce:transition-none ${live ? \"opacity-100\" : \"opacity-0\"}`}>\n        <SilkCanvas onLive={setLive} />\n      </div>\n      <p className=\"absolute bottom-5 left-5 rounded-full bg-[#16020a]/80 px-3.5 py-2 font-mono text-[11px] uppercase tracking-[0.2em] text-rose-50 sm:bottom-7 sm:left-7\">\n        Silk drape <span className=\"text-rose-200\">/ oxblood satin, one lamp</span>\n      </p>\n    </section>\n  );\n}\n"}]}
