{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"velvet-stage-curtain-dialog-that-draws-apart-on-a-pull-cord-with-pleats-that-gather-and-sway","type":"registry:block","title":"Velvet Stage Curtain Dialog That Draws Apart on a Pull Cord with Pleats That Gather and Sway","description":"Dialog hidden behind velvet stage curtains: pull the cord and the pleats gather to the wings to show a photo and its actions. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/velvet-stage-curtain-dialog-that-draws-apart-on-a-pull-cord-with-pleats-that-gather-and-sway.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// What the curtains open on. Change the words, the two actions and the photograph here.\nconst show = {\n  kicker: \"Tonight at eight\",\n  title: \"The Long Interval\",\n  line: \"A solo in three movements. Doors open at half past seven and the bar stays open after.\",\n  primary: \"Reserve a seat\",\n  secondary: \"Close the curtains\",\n  src: \"https://images.pexels.com/photos/33865974/pexels-photo-33865974.jpeg?auto=compress&cs=tinysrgb&w=1100\",\n  alt: \"Dancer in an orange leotard reaching one arm up under a spotlight on a dark stage\",\n};\nconst VELVET: [number, number, number] = [172, 20, 40]; // the lit colour of the cloth\nconst GOLD: [number, number, number] = [222, 170, 70]; // fringe, braid and cord\nconst FULLNESS = 1.36; // cloth width over closed width: more cloth means deeper pleats\nconst PLEAT = 46; // width of one pleat in pixels when the curtains are shut\nconst HOLD_MS = 6200; // how long the demo leaves the stage open before closing again\n\nexport default function VelvetStageCurtainDialog() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const cord = useRef<HTMLButtonElement>(null);\n  const first = useRef<HTMLButtonElement>(null);\n  const api = useRef<{ set: (open: boolean) => void } | null>(null);\n  const [open, setOpen] = useState(true);\n\n  useEffect(() => {\n    const cv = canvas.current, box = stage.current;\n    if (!cv || !box) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const dpr = Math.min(1.5, window.devicePixelRatio || 1);\n    const bufs = [document.createElement(\"canvas\"), document.createElement(\"canvas\")];\n    const top = document.createElement(\"canvas\");\n    let w = 0, h = 0, VH = 90, FLOOR = 34, hem = 0, shut = 0, away = 0, pleats = 14, cordX = 0;\n    const LX = 0.52, LZ = 0.854; // the light sits low and in front, toward the middle of the stage\n\n    // One vertical sliver of pleated velvet: how bright it is given where it sits on its fold.\n    const sliver = (x: number, span: number, n: number, depth: number, seed: number) => {\n      const t = x / span, kk = (n * Math.PI * 2) / span;\n      const ph = (t + 0.004 * Math.sin(t * 23 + seed)) * n * Math.PI * 2 + seed;\n      const z = Math.cos(ph) + 0.3 * Math.cos(2 * ph + 0.9 + seed);\n      const dz = (-Math.sin(ph) - 0.6 * Math.sin(2 * ph + 0.9 + seed)) * kk * depth;\n      const inv = 1 / Math.hypot(dz, 1), nx = -dz * inv, nz = inv;\n      const lam = Math.max(0, nx * LX + nz * LZ);\n      const occ = 0.22 + 0.78 * Math.pow((z + 1.3) / 2.6, 1.5);\n      const nap = Math.pow(1 - nz, 1.6); // velvet is brightest where you see it edge on\n      return { z, lit: occ * (0.07 + 0.93 * Math.pow(lam, 1.25)), rim: nap * (0.1 + 0.9 * lam) * occ };\n    };\n    const paint = (lit: number, rim: number, c: [number, number, number]) =>\n      `rgb(${Math.min(255, c[0] * lit + 255 * rim * 0.62) | 0},${Math.min(255, c[1] * lit + 120 * rim * 0.5) | 0},${Math.min(255, c[2] * lit + 110 * rim * 0.5) | 0})`;\n\n    const s = { top: 0, tv: 0, target: 0, bot: [0, 0], bv: [0, 0], drop: 0, dv: 0, swing: 0, sv: 0 };\n    let lastSpan = -1;\n\n    const weave = (which: number, span: number) => {\n      const b = bufs[which].getContext(\"2d\");\n      if (!b) return;\n      const cols = Math.max(2, Math.round(span * dpr)), H = bufs[which].height;\n      const pitch = span / pleats, arc = (shut * FULLNESS) / pleats;\n      const depth = Math.sqrt(Math.max(1, arc * arc - pitch * pitch)) / 4.4;\n      const fr = 13 * dpr;\n      b.clearRect(0, H - 40 * dpr, bufs[which].width, 40 * dpr);\n      for (let i = 0; i < cols; i++) {\n        const v = sliver(i / dpr, span, pleats, depth, which ? 2.1 : 0.4);\n        const end = i > cols - 4 * dpr ? 0.45 : 1;\n        const hb = H - (8 - Math.min(5.5, pitch * 0.2) * (v.z / 1.3)) * dpr;\n        b.fillStyle = paint(v.lit * end, v.rim * end, VELVET); b.fillRect(i, 0, 1, hb - fr);\n        b.fillStyle = \"rgba(20,4,6,0.9)\"; b.fillRect(i, hb - fr - 2.5 * dpr, 1, 2.5 * dpr);\n        const g = (0.2 + 1.05 * v.lit + v.rim * 0.5) * end * (i % 3 === 0 ? 0.72 : 1);\n        b.fillStyle = `rgb(${Math.min(255, GOLD[0] * g) | 0},${Math.min(255, GOLD[1] * g) | 0},${Math.min(255, GOLD[2] * g) | 0})`; b.fillRect(i, hb - fr, 1, fr);\n        b.fillStyle = `rgba(255,236,170,${Math.min(0.9, v.lit * 0.9)})`; b.fillRect(i, hb - fr - 5.5 * dpr, 1, 2 * dpr);\n      }\n    };\n\n    // The pelmet across the top is painted once: shallow box pleats, a scalloped edge and fringe.\n    const pelmet = () => {\n      top.width = Math.ceil(w * dpr); top.height = Math.ceil((VH + 4) * dpr);\n      const b = top.getContext(\"2d\");\n      if (!b) return;\n      const swags = Math.max(2, Math.round(w / 190)), n = Math.round(w / 31);\n      for (let i = 0; i < top.width; i++) {\n        const x = i / dpr, v = sliver(x, w, n, 5.2, 1.1), f = ((x / w) * swags) % 1;\n        const hb = (VH - 22 + 22 * Math.pow(Math.sin(Math.PI * f), 0.8) + 2 * (v.z / 1.3)) * dpr, fr = 11 * dpr;\n        const fall = 0.5 + 0.5 * Math.min(1, hb / (VH * dpr));\n        b.fillStyle = paint(v.lit * 0.82 * fall, v.rim * 0.7, VELVET); b.fillRect(i, 0, 1, hb - fr);\n        b.fillStyle = \"rgba(20,4,6,0.9)\"; b.fillRect(i, hb - fr - 2.5 * dpr, 1, 2.5 * dpr);\n        const g = (0.25 + 1.0 * v.lit + v.rim * 0.4) * (i % 3 === 0 ? 0.7 : 1);\n        b.fillStyle = `rgb(${Math.min(255, GOLD[0] * g) | 0},${Math.min(255, GOLD[1] * g) | 0},${Math.min(255, GOLD[2] * g) | 0})`; b.fillRect(i, hb - fr, 1, fr);\n        b.fillStyle = `rgba(255,236,170,${Math.min(0.9, v.lit)})`; b.fillRect(i, hb - fr - 6 * dpr, 1, 2.2 * dpr);\n      }\n      const dark = b.createLinearGradient(0, 0, 0, VH * dpr);\n      dark.addColorStop(0, \"rgba(0,0,0,0.72)\"); dark.addColorStop(0.5, \"rgba(0,0,0,0.12)\"); dark.addColorStop(1, \"rgba(0,0,0,0)\");\n      b.globalCompositeOperation = \"source-atop\"; b.fillStyle = dark; b.fillRect(0, 0, top.width, top.height); b.globalCompositeOperation = \"source-over\";\n    };\n\n    const size = () => {\n      w = box.clientWidth; h = box.clientHeight;\n      cv.width = Math.round(w * dpr); cv.height = Math.round(h * dpr);\n      VH = Math.max(70, Math.min(104, h * 0.135)); FLOOR = w < 640 ? 26 : 36; hem = h - FLOOR + 3;\n      shut = w / 2 + 22; away = Math.max(64, w * 0.185); pleats = Math.max(5, Math.round(shut / PLEAT));\n      cordX = w * 0.938;\n      for (const b of bufs) { b.width = Math.ceil((shut + 4) * dpr); b.height = Math.ceil(hem * dpr); }\n      pelmet(); lastSpan = -1;\n    };\n\n    const draw = () => {\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.clearRect(0, 0, w, h);\n      const span = s.top + 10;\n      if (Math.abs(span - lastSpan) > 0.04) { weave(0, span); weave(1, span); lastSpan = span; }\n      const cols = Math.max(2, Math.round(span * dpr)), band = 3;\n      const edge = (side: number, y: number) => { const q = y / hem; return s.top + (s.bot[side] - s.top) * q * q + Math.sin(q * Math.PI) * 5 + q * 7; };\n\n      // The apron of the stage with the glow of the footlights along its lip.\n      const fl = ctx.createLinearGradient(0, h - FLOOR, 0, h);\n      fl.addColorStop(0, \"#2a1509\"); fl.addColorStop(0.18, \"#160b06\"); fl.addColorStop(1, \"#070403\");\n      ctx.fillStyle = fl; ctx.fillRect(0, h - FLOOR, w, FLOOR);\n      ctx.fillStyle = \"rgba(255,196,128,0.5)\"; ctx.fillRect(0, h - FLOOR, w, 1.2);\n\n      for (const side of [0, 1]) {\n        ctx.save();\n        if (side) { ctx.translate(w, 0); ctx.scale(-1, 1); }\n        // Shadow thrown onto the stage behind the leading edge.\n        ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.85)\"; ctx.shadowBlur = 34 * dpr; ctx.shadowOffsetX = (side ? -16 : 16) * dpr;\n        ctx.beginPath(); ctx.moveTo(-20, -10); ctx.lineTo(edge(side, 0), -10);\n        for (let y = 0; y <= hem; y += 24) ctx.lineTo(edge(side, y), y);\n        ctx.lineTo(-20, hem); ctx.closePath(); ctx.fillStyle = \"#1a0406\"; ctx.fill(); ctx.restore();\n        for (let y = 0; y < hem; y += band) {\n          const e = edge(side, y + band / 2) + 10;\n          ctx.drawImage(bufs[side], 0, y * dpr, cols, band * dpr, -10, y, e, band + 0.6);\n        }\n        ctx.restore();\n      }\n\n      // Stage light on the cloth: dark up under the pelmet, warm where the footlights reach.\n      ctx.save(); ctx.globalCompositeOperation = \"source-atop\";\n      const up = ctx.createLinearGradient(0, 0, 0, hem);\n      up.addColorStop(0, \"rgba(0,0,0,0.62)\"); up.addColorStop(0.22, \"rgba(0,0,0,0.3)\"); up.addColorStop(0.55, \"rgba(0,0,0,0.04)\"); up.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx.fillStyle = up; ctx.fillRect(0, 0, w, hem - 14);\n      ctx.globalCompositeOperation = \"source-atop\";\n      const warm = ctx.createLinearGradient(0, hem, 0, hem * 0.45);\n      warm.addColorStop(0, \"rgba(255,168,96,0.17)\"); warm.addColorStop(0.35, \"rgba(255,150,90,0.07)\"); warm.addColorStop(1, \"rgba(255,150,90,0)\");\n      ctx.fillStyle = warm; ctx.fillRect(0, hem * 0.45, w, hem * 0.55 - 14);\n      ctx.restore();\n\n      // The pelmet, and its shadow on the curtains below.\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.8)\"; ctx.shadowBlur = 26 * dpr; ctx.shadowOffsetY = 12 * dpr;\n      ctx.drawImage(top, 0, 0, w, VH + 4); ctx.restore();\n\n      // The pull cord: a twisted rope and a tassel that hangs in front of the right-hand curtain.\n      const y0 = VH - 26, y1 = Math.min(h * 0.56, VH + 290) + s.drop;\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.7)\"; ctx.shadowBlur = 8 * dpr; ctx.shadowOffsetX = -6 * dpr; ctx.shadowOffsetY = 5 * dpr;\n      ctx.lineWidth = 6.2; ctx.strokeStyle = \"#7a5516\"; ctx.beginPath(); ctx.moveTo(cordX, y0); ctx.lineTo(cordX, y1); ctx.stroke(); ctx.restore();\n      ctx.lineWidth = 1.7; ctx.lineCap = \"round\";\n      for (let y = y0 + ((s.drop % 5) + 5) % 5; y < y1 - 2; y += 5) {\n        ctx.strokeStyle = \"#f6dc93\"; ctx.beginPath(); ctx.moveTo(cordX - 2.6, y + 2.8); ctx.lineTo(cordX + 2, y - 0.8); ctx.stroke();\n        ctx.strokeStyle = \"rgba(60,36,4,0.7)\"; ctx.beginPath(); ctx.moveTo(cordX - 2.3, y + 4.6); ctx.lineTo(cordX + 2.3, y + 0.8); ctx.stroke();\n      }\n      ctx.translate(cordX, y1); ctx.rotate(s.swing); ctx.scale(1.3, 1.3);\n      ctx.shadowColor = \"rgba(0,0,0,0.7)\"; ctx.shadowBlur = 10 * dpr; ctx.shadowOffsetX = -7 * dpr; ctx.shadowOffsetY = 6 * dpr;\n      const ball = ctx.createRadialGradient(-3, 3, 1, 0, 7, 10);\n      ball.addColorStop(0, \"#fff0b8\"); ball.addColorStop(0.5, \"#d9a544\"); ball.addColorStop(1, \"#6f4a10\");\n      ctx.fillStyle = ball; ctx.beginPath(); ctx.arc(0, 7, 8.6, 0, Math.PI * 2); ctx.fill();\n      const skirt = ctx.createLinearGradient(-10, 0, 10, 0);\n      skirt.addColorStop(0, \"#6f4a10\"); skirt.addColorStop(0.3, \"#e9bf62\"); skirt.addColorStop(0.5, \"#fff0b8\"); skirt.addColorStop(0.75, \"#c8943a\"); skirt.addColorStop(1, \"#5c3c0a\");\n      ctx.fillStyle = skirt; ctx.beginPath(); ctx.moveTo(-6, 15); ctx.lineTo(6, 15); ctx.lineTo(10.5, 56); ctx.quadraticCurveTo(0, 60, -10.5, 56); ctx.closePath(); ctx.fill();\n      ctx.shadowColor = \"transparent\";\n      ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(70,42,4,0.55)\";\n      for (let i = -3; i <= 3; i++) { ctx.beginPath(); ctx.moveTo(i * 1.7, 20); ctx.lineTo(i * 3.1, 57); ctx.stroke(); }\n      ctx.fillStyle = \"#8a2a1c\"; ctx.fillRect(-6.6, 14, 13.2, 4.6); ctx.fillStyle = \"rgba(255,220,200,0.35)\"; ctx.fillRect(-6.6, 14, 13.2, 1.2);\n    };\n\n    let raf = 0, last = 0, seen = true, t0 = 0, demo = true, demoAt = 380, isOpen = false;\n    const goal = () => (isOpen ? away : shut);\n    const frame = (now: number) => {\n      raf = 0;\n      if (!t0) { t0 = now; last = now; }\n      const dt = Math.min(0.033, (now - last) / 1000); last = now;\n      const t = now - t0;\n      if (demo && t > demoAt) { isOpen = !isOpen; s.target = goal(); s.dv += 520; demoAt = t + (isOpen ? HOLD_MS : 2300); setOpen(isOpen); }\n      s.tv += (15 * (s.target - s.top) - 7.4 * s.tv) * dt; s.top += s.tv * dt;\n      for (const i of [0, 1]) {\n        const draft = Math.sin(t / (1700 + i * 420) + i * 2) * 60; // a little air from the wings keeps the hems alive\n        s.bv[i] += ((20 - i * 3) * (s.top - s.bot[i]) - (2.5 + i * 0.5) * s.bv[i] + draft) * dt; s.bot[i] += s.bv[i] * dt;\n      }\n      s.dv += (-190 * s.drop - 9 * s.dv) * dt; s.drop += s.dv * dt;\n      s.sv += (-40 * s.swing - 1.6 * s.sv - s.dv * 0.004) * dt; s.swing += s.sv * dt;\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    const settle = () => { s.top = s.target; s.tv = 0; s.bot = [s.top, s.top]; s.bv = [0, 0]; s.drop = 0; s.dv = 0; s.swing = 0; s.sv = 0; draw(); };\n\n    size();\n    if (reduce) { isOpen = true; s.target = goal(); settle(); }\n    else { s.target = s.top = shut; s.bot = [shut, shut]; draw(); wake(); }\n    api.current = {\n      set: (o) => { demo = false; isOpen = o; s.target = goal(); s.dv += 560; s.sv += 0.5; if (reduce) settle(); else wake(); },\n    };\n\n    const ro = new ResizeObserver(() => { const was = s.target === shut; size(); s.target = was ? shut : away; if (reduce) settle(); else { s.top = s.target; s.bot = [s.top, s.top]; draw(); } });\n    ro.observe(box);\n    const io = new IntersectionObserver(([entry]) => { seen = entry.isIntersecting; if (seen) wake(); });\n    io.observe(cv);\n    const onVis = () => { if (!document.hidden) wake(); };\n    document.addEventListener(\"visibilitychange\", onVis);\n    return () => { cancelAnimationFrame(raf); api.current = null; ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis); };\n  }, []);\n\n  const toggle = (next: boolean) => {\n    setOpen(next); api.current?.set(next);\n    window.setTimeout(() => (next ? first.current : cord.current)?.focus(), next ? 500 : 0);\n  };\n\n  return (\n    <div\n      ref={stage}\n      onKeyDown={(e) => { if (e.key === \"Escape\" && open) toggle(false); }}\n      className=\"relative h-[640px] w-full overflow-hidden bg-[#070405] sm:h-[720px]\"\n    >\n      {/* The stage: a spotlight, the photograph and the dialog that the curtains uncover. */}\n      <div aria-hidden=\"true\" className=\"absolute inset-0 bg-[radial-gradient(40%_56%_at_50%_52%,#1c110d_0%,#0c0706_55%,#070405_100%)]\" />\n      <div className=\"absolute left-1/2 top-[7%] aspect-[2/3] h-[70%] -translate-x-1/2 lg:top-[4%] lg:h-[96%]\">\n        <img src={show.src} alt={show.alt} width={1100} height={1650} loading=\"eager\" className=\"h-full w-full object-cover [mask-image:radial-gradient(50%_52%_at_50%_50%,#000_52%,transparent_100%)]\" />\n      </div>\n      <div aria-hidden=\"true\" className=\"absolute left-1/2 top-0 h-[92%] w-[46%] -translate-x-1/2 bg-gradient-to-b from-amber-100/20 to-amber-100/0 blur-2xl [clip-path:polygon(44%_0,56%_0,86%_100%,14%_100%)]\" />\n      <div\n        role=\"dialog\" aria-modal=\"true\" aria-labelledby=\"velvet-curtain-title\"\n        className={`absolute inset-x-0 bottom-[7%] mx-auto flex w-[54%] max-w-[300px] flex-col items-center text-center transition-[visibility] lg:inset-x-auto lg:bottom-auto lg:left-[23%] lg:top-1/2 lg:mx-0 lg:w-[250px] lg:-translate-y-1/2 lg:items-start lg:text-left ${open ? \"visible\" : \"invisible delay-700\"} motion-reduce:delay-0`}\n      >\n        <p className=\"font-mono text-[10px] uppercase tracking-[0.28em] text-amber-200/90\">{show.kicker}</p>\n        <h2 id=\"velvet-curtain-title\" className=\"mt-2 font-serif text-[22px] italic leading-[1.05] text-stone-50 sm:text-[32px] lg:text-[42px]\">{show.title}</h2>\n        <p className=\"mt-3 hidden text-[13px] leading-5 text-stone-300 lg:block\">{show.line}</p>\n        <div className=\"mt-4 flex flex-wrap items-center justify-center gap-x-4 gap-y-1 lg:mt-6 lg:justify-start\">\n          <button ref={first} type=\"button\" className=\"h-10 rounded-full bg-amber-300 px-5 text-[13px] font-semibold text-stone-950 outline-none transition-colors hover:bg-amber-200 focus-visible:ring-2 focus-visible:ring-amber-200 focus-visible:ring-offset-2 focus-visible:ring-offset-[#070405] motion-reduce:transition-none\">{show.primary}</button>\n          <button type=\"button\" onClick={() => toggle(false)} className=\"h-10 rounded-full px-1 text-[13px] font-medium text-stone-200 underline decoration-stone-500 underline-offset-4 outline-none hover:text-white focus-visible:ring-2 focus-visible:ring-amber-200\">{show.secondary}</button>\n        </div>\n      </div>\n      <canvas ref={canvas} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 h-full w-full\" />\n      <button\n        ref={cord} type=\"button\" aria-expanded={open} aria-label={open ? \"Close the curtains\" : \"Open the curtains\"}\n        onClick={() => toggle(!open)}\n        className=\"absolute right-[6.2%] top-[10%] h-[58%] w-12 translate-x-1/2 cursor-pointer rounded-full outline-none focus-visible:ring-2 focus-visible:ring-amber-200\"\n      />\n    </div>\n  );\n}\n"}]}
