{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"concrete-wall-texture","type":"registry:component","title":"Concrete Wall Texture","description":"A seamless procedural concrete surface drawn once to canvas and blended over anything.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/concrete-wall-texture.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, type ReactNode } from \"react\";\n\nfunction mulberry32(a: number) {\n  return function () {\n    a |= 0;\n    a = (a + 0x6d2b79f5) | 0;\n    let t = Math.imul(a ^ (a >>> 15), 1 | a);\n    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;\n    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n  };\n}\n\n/**\n * Value noise on a WRAPPING 64x64 lattice with a smoothstep interpolant.\n * Wrapping is what keeps the field seamless at any canvas size.\n */\nfunction makeNoise(seed: number) {\n  const rnd = mulberry32(seed);\n  const G = 64;\n  const grid = new Float32Array(G * G);\n  for (let i = 0; i < grid.length; i += 1) grid[i] = rnd();\n  const at = (x: number, y: number) => grid[(((y % G) + G) % G) * G + (((x % G) + G) % G)];\n  return (x: number, y: number) => {\n    const xi = Math.floor(x);\n    const yi = Math.floor(y);\n    const xf = x - xi;\n    const yf = y - yi;\n    const u = xf * xf * (3 - 2 * xf);\n    const v = yf * yf * (3 - 2 * yf);\n    const a = at(xi, yi);\n    const b = at(xi + 1, yi);\n    const c = at(xi, yi + 1);\n    const d = at(xi + 1, yi + 1);\n    return (a + (b - a) * u) * (1 - v) + (c + (d - c) * u) * v;\n  };\n}\n\nexport function ConcreteWallTexture({\n  children,\n  seed = 4817,\n  scale = 1,\n  opacity = 0.62,\n  blend = \"overlay\",\n  tone = \"neutral\",\n}: {\n  children?: ReactNode;\n  seed?: number;\n  scale?: number;\n  opacity?: number;\n  blend?: \"overlay\" | \"soft-light\" | \"multiply\" | \"screen\";\n  tone?: \"cold\" | \"neutral\" | \"warm\";\n}) {\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    if (!canvas) return;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n\n    let w = 0;\n    let h = 0;\n\n    const tint =\n      tone === \"cold\" ? [0.96, 0.99, 1.05] : tone === \"warm\" ? [1.05, 1.0, 0.93] : [1, 1, 1];\n\n    const paint = () => {\n      if (w <= 0 || h <= 0) return;\n      const rnd = mulberry32(seed ^ 0x9e37);\n      const noise = makeNoise(seed);\n\n      // --- base mottling, HALF RESOLUTION ---------------------------------\n      // Plaster has no high-frequency detail worth four times the pixels. The\n      // aggregate specks, which do need hard edges, are drawn at full\n      // resolution further down.\n      const nw = Math.max(1, Math.ceil(w / 2));\n      const nh = Math.max(1, Math.ceil(h / 2));\n      const off = document.createElement(\"canvas\");\n      off.width = nw;\n      off.height = nh;\n      const octx = off.getContext(\"2d\");\n      if (!octx) return;\n\n      const img = octx.createImageData(nw, nh);\n      const d = img.data;\n      const f = 0.05 / Math.max(0.2, scale);\n      for (let y = 0; y < nh; y += 1) {\n        for (let x = 0; x < nw; x += 1) {\n          const v =\n            noise(x * f, y * f) * 0.56 +\n            noise(x * f * 2.3, y * f * 2.3) * 0.29 +\n            noise(x * f * 5.7, y * f * 5.7) * 0.15;\n          // PULLED TOWARDS MID GREY. The texture is a modulation, not an\n          // image: a full-range field pushed through overlay crushes whatever\n          // is underneath it into black and white.\n          const g = 92 + Math.pow(v, 1.12) * 104;\n          const i = (y * nw + x) * 4;\n          d[i] = Math.min(255, g * tint[0]);\n          d[i + 1] = Math.min(255, g * tint[1]);\n          d[i + 2] = Math.min(255, g * tint[2]);\n          d[i + 3] = 255;\n        }\n      }\n      octx.putImageData(img, 0, 0);\n      ctx.clearRect(0, 0, w, h);\n      ctx.drawImage(off, 0, 0, w, h);\n\n      // --- trowel streaks ---------------------------------------------------\n      for (let i = 0; i < 30; i += 1) {\n        const y0 = rnd() * h;\n        const x0 = -w * 0.15 + rnd() * w;\n        const len = w * (0.25 + rnd() * 0.8);\n        const light = rnd() < 0.5;\n        ctx.beginPath();\n        ctx.moveTo(x0, y0);\n        ctx.quadraticCurveTo(\n          x0 + len * 0.5,\n          y0 + (rnd() - 0.5) * h * 0.14,\n          x0 + len,\n          y0 + (rnd() - 0.5) * h * 0.07,\n        );\n        ctx.lineWidth = 1 + rnd() * 7;\n        ctx.strokeStyle =\n          (light ? \"rgba(255,255,255,\" : \"rgba(0,0,0,\") + (0.02 + rnd() * 0.05) + \")\";\n        ctx.stroke();\n      }\n\n      // --- staining ----------------------------------------------------------\n      for (let i = 0; i < 6; i += 1) {\n        const cx = rnd() * w;\n        const cy = rnd() * h;\n        const r = Math.max(w, h) * (0.14 + rnd() * 0.3);\n        const g = ctx.createRadialGradient(cx, cy, 0, cx, cy, r);\n        g.addColorStop(0, rnd() < 0.62 ? \"rgba(0,0,0,0.13)\" : \"rgba(255,255,255,0.09)\");\n        g.addColorStop(1, \"rgba(0,0,0,0)\");\n        ctx.fillStyle = g;\n        ctx.fillRect(0, 0, w, h);\n      }\n\n      // --- aggregate, FULL resolution -----------------------------------------\n      const specks = Math.round((w * h) / 620);\n      for (let i = 0; i < specks; i += 1) {\n        const x = rnd() * w;\n        const y = rnd() * h;\n        const big = rnd() < 0.12;\n        ctx.fillStyle =\n          rnd() < 0.55\n            ? \"rgba(0,0,0,\" + (0.06 + rnd() * 0.16) + \")\"\n            : \"rgba(255,255,255,\" + (0.05 + rnd() * 0.13) + \")\";\n        ctx.fillRect(x, y, big ? 2 : 1, big ? 2 : 1);\n      }\n\n      // --- edge fall-off --------------------------------------------------------\n      const vg = ctx.createRadialGradient(\n        w / 2,\n        h / 2,\n        Math.min(w, h) * 0.25,\n        w / 2,\n        h / 2,\n        Math.max(w, h) * 0.78,\n      );\n      vg.addColorStop(0, \"rgba(0,0,0,0)\");\n      vg.addColorStop(1, \"rgba(0,0,0,0.26)\");\n      ctx.fillStyle = vg;\n      ctx.fillRect(0, 0, w, h);\n    };\n\n    const observer = new ResizeObserver((entries) => {\n      const entry = entries[0];\n      if (!entry) return;\n      const nw = Math.max(0, Math.round(entry.contentRect.width));\n      const nh = Math.max(0, Math.round(entry.contentRect.height));\n      // ResizeObserver fires on every pixel of a drag. Repainting a whole\n      // noise field per pixel is the difference between instant and unusable.\n      if (nw === w && nh === h) return;\n      w = nw;\n      h = nh;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      canvas.width = Math.max(1, Math.round(w * dpr));\n      canvas.height = Math.max(1, Math.round(h * dpr));\n      // Assigning width or height RESETS the transform as well as clearing the\n      // bitmap, so the DPR scale is reapplied here rather than once on mount.\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      paint();\n    });\n    observer.observe(canvas);\n    return () => observer.disconnect();\n  }, [seed, scale, tone]);\n\n  return (\n    <div\n      className=\"relative h-full w-full overflow-hidden\"\n      // mix-blend-mode composites against EVERYTHING below it in the stacking\n      // context. Without isolation the texture reaches past this box and tints\n      // the page behind it.\n      style={{ isolation: \"isolate\" }}\n    >\n      {children}\n      <canvas\n        ref={canvasRef}\n        aria-hidden\n        className=\"pointer-events-none absolute inset-0 h-full w-full\"\n        style={{ mixBlendMode: blend, opacity }}\n      />\n    </div>\n  );\n}\n","type":"registry:component","target":"components/viberdy/concrete-wall-texture.tsx"}],"meta":{"tier":"free","tags":["texture","concrete","canvas","noise","surface"],"href":"https://viberdy.dev/library/concrete-wall-texture","aiPrompt":"Build a procedural concrete/plaster surface in React + Tailwind named ConcreteWallTexture — a canvas overlay that sits over children and gives them a poured-concrete surface.\n\nWHY CANVAS AND NOT AN IMAGE: a convincing concrete texture as a photograph is several hundred kilobytes and tiles visibly. Generated, it is a few milliseconds of work, seamless, resolution-independent and reseedable, so no two sections of a page share a surface.\n\nBUILD IT IN FIVE LAYERS, in this order:\n1. Base mottling — value noise on a WRAPPING lattice (so it is seamless) at three octaves, roughly 0.56 / 0.29 / 0.15 weights.\n2. Trowel streaks — long, shallow quadratic curves at 2-7% alpha, mixed light and dark.\n3. Staining — half a dozen large radial gradients at very low alpha.\n4. Aggregate — single-pixel specks, about one per 600 pixels of area, mixed light and dark.\n5. Edge fall-off — one broad radial gradient darkening the corners.\n\nFOUR THINGS THAT MATTER MORE THAN THE LAYERS:\n\n- PULL THE NOISE TOWARDS MID GREY (roughly 92-196 rather than 0-255). The texture is a MODULATION, not an image. A full-range field pushed through `overlay` crushes whatever is underneath into black and white, which is why most procedural textures look like dirt rather than surface.\n- `mix-blend-mode` NEEDS AN ISOLATED PARENT. It composites against everything below it in the stacking context, so without `isolation: isolate` on the wrapper the texture reaches past its own container and tints the whole page.\n- RENDER THE NOISE AT HALF RESOLUTION into an offscreen canvas and scale it up. Plaster has no high-frequency detail worth four times the pixels. Draw the aggregate specks at FULL resolution on top, because those do need hard edges.\n- ASSIGNING `canvas.width` RESETS THE TRANSFORM as well as clearing the bitmap. Reapply the device-pixel-ratio scale inside the resize handler, never once on mount, and cap DPR at 2 so a 3x phone does not allocate a nine-times buffer.\n\nTrack size with a ResizeObserver, start width and height at 0, and paint only when both are above zero. Bail out of the handler when the rounded size has not actually changed — ResizeObserver fires on every pixel of a drag, and repainting a full noise field per pixel is the difference between instant and unusable.\n\nDO NOT ANIMATE IT. A wall does not move. There is no requestAnimationFrame anywhere in this component, which is what makes it free after the first frame.\n\nProps: children, seed, scale, opacity, blend (overlay | soft-light | multiply | screen), tone (cold | neutral | warm).\n\nOutput a single self-contained \"use client\" component.","license":"https://viberdy.dev/license"}}