{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"glazed-celadon-ceramic-button-with-carved-lettering-that-rocks-under-the-finger","type":"registry:component","title":"Glazed Celadon Ceramic Button with Carved Lettering That Rocks Under the Finger","description":"Button fired in celadon glaze on a canvas: carved letters where the green pools, a crackle net and a rock under the finger. Free React and HTML to copy.","dependencies":[],"files":[{"path":"components/we-inc/glazed-celadon-ceramic-button-with-carved-lettering-that-rocks-under-the-finger.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// What is carved into the clay, and the two lines of the caption underneath.\nconst LABEL = \"Reserve a seat\";\nconst IDLE_NOTE = \"Thursday wheel class. Six seats left.\";\nconst HELD_NOTE = \"Seat held for you. Five left.\";\n// Glaze where it runs thin, where it pools, and the bare stoneware under it (red, green, blue).\nconst THIN = [214, 230, 214];\nconst DEEP = [58, 122, 106];\nconst CLAY = [203, 184, 156];\nconst W = 340, H = 128; // size of the piece in CSS pixels\n\nconst hash = (x: number, y: number) => { const v = Math.sin(x * 127.1 + y * 311.7) * 43758.5453; return v - Math.floor(v); };\nconst noise = (x: number, y: number) => {\n  const ix = Math.floor(x), iy = Math.floor(y), fx = x - ix, fy = y - iy;\n  const ux = fx * fx * (3 - 2 * fx), uy = fy * fy * (3 - 2 * fy);\n  const a = hash(ix, iy), b = hash(ix + 1, iy), c = hash(ix, iy + 1), d = hash(ix + 1, iy + 1);\n  return a + (b - a) * ux + (c - a) * uy + (a - b - c + d) * ux * uy;\n};\nconst clamp = (v: number, lo = 0, hi = 1) => (v < lo ? lo : v > hi ? hi : v);\nconst boxBlur = (src: Float32Array, w: number, h: number, r: number) => {\n  const tmp = new Float32Array(src.length), out = new Float32Array(src.length), k = 1 / (2 * r + 1);\n  for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) { let s = 0; for (let i = -r; i <= r; i++) s += src[y * w + clamp(x + i, 0, w - 1)]; tmp[y * w + x] = s * k; }\n  for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) { let s = 0; for (let i = -r; i <= r; i++) s += tmp[clamp(y + i, 0, h - 1) * w + x]; out[y * w + x] = s * k; }\n  return out;\n};\n\n// Shapes the piece once: a height for every pixel, then slope, glaze colour and gloss worked out from it.\nfunction throwPiece(S: number) {\n  const w = Math.round(W * S), h = Math.round(H * S), n = w * h;\n  const hgt = new Float32Array(n), edge = new Float32Array(n);\n  const R = 46, hw = W / 2, hh = H / 2;\n\n  const tc = document.createElement(\"canvas\"); tc.width = w; tc.height = h;\n  const tx = tc.getContext(\"2d\");\n  let carve = new Float32Array(n);\n  if (tx) {\n    tx.font = `italic 600 ${41 * S}px \"Iowan Old Style\", \"Palatino Linotype\", Palatino, \"Book Antiqua\", Georgia, serif`;\n    tx.textAlign = \"center\"; tx.textBaseline = \"middle\"; tx.fillStyle = \"#000\";\n    tx.fillText(LABEL, w / 2, h / 2 + 1 * S, w - 84 * S);\n    const px = tx.getImageData(0, 0, w, h).data;\n    for (let i = 0; i < n; i++) carve[i] = px[i * 4 + 3] / 255;\n    carve = boxBlur(boxBlur(carve, w, h, Math.max(1, Math.round(0.45 * S))), w, h, 1);\n  }\n\n  for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {\n    const px = (x + 0.5) / S, py = (y + 0.5) / S, i = y * w + x;\n    const qx = Math.abs(px - hw) - (hw - R), qy = Math.abs(py - hh) - (hh - R);\n    const d = R - Math.hypot(Math.max(qx, 0), Math.max(qy, 0)) - Math.min(Math.max(qx, qy), 0);\n    edge[i] = d;\n    if (d <= -1) continue;\n    const t = clamp(d / 24) * 0.955 + 0.045;\n    const ux = (px - hw) / hw, uy = (py - hh) / hh;\n    let z = 15 * Math.sqrt(1 - (1 - t) * (1 - t)) + 5 * (1 - ux * ux * 0.6 - uy * uy * 0.6);\n    z -= 1.5 * Math.exp(-((d - 15.5) * (d - 15.5)) / 2.2); // the line the potter scored round the rim\n    z -= 2.3 * carve[i];\n    z += 0.42 * noise(px / 34, py / 34) + 0.05 * noise(px / 9 + 9, py / 9);\n    hgt[i] = z;\n  }\n\n  const sx = new Float32Array(n), sy = new Float32Array(n);\n  let lap = new Float32Array(n);\n  for (let y = 1; y < h - 1; y++) for (let x = 1; x < w - 1; x++) {\n    const i = y * w + x;\n    sx[i] = -(hgt[i + 1] - hgt[i - 1]) * 0.5 * S; sy[i] = -(hgt[i + w] - hgt[i - w]) * 0.5 * S;\n    lap[i] = (hgt[i + 1] + hgt[i - 1] + hgt[i + w] + hgt[i - w] - 4 * hgt[i]) * S * S;\n  }\n  lap = boxBlur(lap, w, h, Math.max(1, Math.round(0.9 * S)));\n\n  const alb = new Float32Array(n * 3), gloss = new Float32Array(n), mask = new Float32Array(n);\n  const cell = 10;\n  for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {\n    const i = y * w + x, d = edge[i];\n    mask[i] = clamp(d * S + 0.5);\n    if (mask[i] === 0) continue;\n    const px = (x + 0.5) / S, py = (y + 0.5) / S;\n    // Glaze runs off ridges and settles in hollows, so curvature decides how deep the green is.\n    const thick = clamp(0.5 + lap[i] * 0.8 + 0.2 * (noise(px / 44 + 3, py / 44) - 0.5) + 0.25 * clamp(1 - d / 9));\n    const k = thick * thick * (3 - 2 * thick), bare = clamp((0.17 - thick) / 0.17) * 0.5;\n    // Crazing: the fine crackle net, found as the borders between scattered cells.\n    const cx = Math.floor(px / cell), cy = Math.floor(py / cell);\n    let d1 = 1e9, d2 = 1e9;\n    for (let oy = -1; oy <= 1; oy++) for (let ox = -1; ox <= 1; ox++) {\n      const gx = cx + ox, gy = cy + oy;\n      const dd = Math.hypot((gx + hash(gx, gy)) * cell - px, (gy + hash(gy + 40, gx)) * cell - py);\n      if (dd < d1) { d2 = d1; d1 = dd; } else if (dd < d2) d2 = dd;\n    }\n    const patch = clamp((noise(px / 58 + 7, py / 58 + 2) - 0.36) * 3.2);\n    const craze = clamp(1 - (d2 - d1) / 0.62) * patch * 0.34;\n    const foot = clamp((1.5 - d) / 0.9);\n    for (let c = 0; c < 3; c++) {\n      let v = THIN[c] + (DEEP[c] - THIN[c]) * k;\n      v += ([226, 208, 176][c] - v) * bare;\n      v += ([96, 86, 62][c] - v) * craze;\n      v += (CLAY[c] * (0.9 + 0.2 * noise(px * 1.3, py * 1.3)) - v) * foot;\n      alb[i * 3 + c] = v;\n    }\n    gloss[i] = 1 - foot;\n  }\n  // Iron specks that burn through the glaze in the kiln.\n  let seed = 11;\n  const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n  for (let s = 0; s < 64; s++) {\n    const px = rand() * W, py = rand() * H, r = (0.35 + rand() * rand() * 1.5) * S, a = 0.35 + rand() * 0.5;\n    const x0 = Math.round(px * S), y0 = Math.round(py * S), rr = Math.ceil(r * 2.2);\n    for (let y = y0 - rr; y <= y0 + rr; y++) for (let x = x0 - rr; x <= x0 + rr; x++) {\n      if (x < 0 || y < 0 || x >= w || y >= h) continue;\n      const i = y * w + x, f = Math.exp(-((x - x0) * (x - x0) + (y - y0) * (y - y0)) / (r * r)) * a;\n      alb[i * 3] += (104 - alb[i * 3]) * f; alb[i * 3 + 1] += (74 - alb[i * 3 + 1]) * f; alb[i * 3 + 2] += (46 - alb[i * 3 + 2]) * f;\n    }\n  }\n  return { w, h, sx, sy, alb, gloss, mask };\n}\n\nexport default function GlazedCeramicButton() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const stage = useRef<HTMLDivElement>(null);\n  const body = useRef<HTMLDivElement>(null);\n  const far = useRef<HTMLDivElement>(null);\n  const near = useRef<HTMLDivElement>(null);\n  const button = useRef<HTMLButtonElement>(null);\n  const [held, setHeld] = useState(false);\n  const heldRef = useRef(false);\n  const api = useRef<{ tap: () => void; sync: () => void } | null>(null);\n\n  useEffect(() => {\n    const cv = canvas.current, st = stage.current, bd = body.current, btn = button.current;\n    if (!cv || !st || !bd || !btn) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const S = Math.min(2.6, (window.devicePixelRatio || 1) * 1.5); // drawn half as large again as its box for wide screens\n    const P = throwPiece(S);\n    cv.width = P.w; cv.height = P.h;\n    const img = ctx.createImageData(P.w, P.h), out = img.data, n = P.w * P.h;\n\n    // p is how far the piece is pushed down, tx and ty how far it has rocked, lx and ly where the window sits.\n    const s = { p: 0, pv: 0, tx: 0, tv: 0, ty: 0, uv: 0, down: false, ax: 0, ay: 0, lx: 0, ly: 0, lxTo: 0, lyTo: 0, kick: -1e9 };\n\n    const draw = () => {\n      const { sx, sy, alb, gloss, mask } = P;\n      const Lx = -0.42, Ly = -0.58, Lz = 0.7;\n      const wx = -0.26 + s.lx, wy = -0.74 + s.ly, band = -0.055 + s.ly * 0.6 - s.lx * 0.2;\n      for (let i = 0, j = 0, k = 0; i < n; i++, j += 4, k += 3) {\n        const m = mask[i];\n        if (m === 0) { out[j + 3] = 0; continue; }\n        const ax = sx[i] + s.tx, ay = sy[i] + s.ty;\n        const nz = 1 / Math.sqrt(ax * ax + ay * ay + 1), nx = ax * nz, ny = ay * nz;\n        const dif = nx * Lx + ny * Ly + nz * Lz;\n        const shade = 0.3 + 0.78 * (dif > 0 ? dif : 0);\n        // What the glaze mirrors: a long studio window with glazing bars, a fill lamp and the pale room.\n        const rx = 2 * nz * nx, ry = 2 * nz * ny;\n        let u = (rx - wx) * 1.45, v = (ry - wy) * 4.2; if (u < 0) u = -u; if (v < 0) v = -v;\n        let win = 0;\n        if (u < 1 && v < 1) { const eu = (1 - u) * 7, ev = (1 - v) * 7; win = (eu > 1 ? 1 : eu) * (ev > 1 ? 1 : ev); const bar = (u * 3.5) % 1; if (bar < 0.07 || bar > 0.93) win *= 0.35; win *= 1 - u * 0.45; }\n        const fx = rx - 0.62, fy = ry - 0.2, inv = 1 - nz, soft = 0.5 - ry * 2.6 - rx * 1.2;\n        // The edge of a bright ceiling panel, which falls across the flat of the face.\n        let sheen = (band - ry - 0.32 * rx) * 38; sheen = sheen < 0 ? 0 : sheen > 1 ? 1 : sheen; sheen *= sheen * (3 - 2 * sheen);\n        let fade = 1 + (ry + 0.3 * rx) * 2.6; fade = fade < 0 ? 0 : fade;\n        const spec = gloss[i] * (win * 0.9 + sheen * fade * 0.14 + 0.05 * (soft < 0 ? 0 : soft > 1 ? 1 : soft) + 0.2 * Math.exp(-(fx * fx + fy * fy) * 9) + inv * inv * (0.75 - 0.6 * ry) + 0.03 * (1 - ry));\n        const hi = 255 * spec;\n        out[j] = alb[k] * shade + hi; out[j + 1] = alb[k + 1] * shade + hi; out[j + 2] = alb[k + 2] * shade * 1.02 + hi * 0.98; out[j + 3] = m * 255;\n      }\n      ctx.putImageData(img, 0, 0);\n      bd.style.transform = `translate3d(0,${(s.p * 5).toFixed(2)}px,0) scale(${(1 - s.p * 0.014).toFixed(4)})`;\n      if (far.current) { far.current.style.opacity = (1 - s.p * 0.55).toFixed(3); far.current.style.transform = `translate3d(${(10 - s.p * 6).toFixed(1)}px,${(22 - s.p * 14).toFixed(1)}px,0) scale(${(1 - s.p * 0.03).toFixed(3)})`; }\n      if (near.current) near.current.style.opacity = (0.75 + s.p * 0.25).toFixed(3);\n    };\n\n    let raf = 0, last = 0, t0 = 0, seen = true, touched = -1e9, nextDemo = 1250, demoEnd = 0, demoSide = -1;\n    const rest = () => (heldRef.current ? 0.42 : 0);\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 (now - touched > 4500) {\n        // Nobody is here: the window drifts and an invisible finger rocks the piece now and then.\n        s.lxTo = 0.16 * Math.sin(t / 1900); s.lyTo = 0.07 * Math.cos(t / 2600);\n        if (t > nextDemo) { demoEnd = t + 620; nextDemo = t + 2900; demoSide = -demoSide; s.ax = demoSide * 0.62; s.ay = 0.25; }\n      }\n      const pressed = s.down || t < demoEnd || now - s.kick < 170;\n      const pTo = pressed ? 1 : rest(), txTo = pressed ? -s.ax * 0.06 : 0, tyTo = pressed ? -s.ay * 0.045 : 0;\n      // A stiff spring going down, a looser one coming back, so it lands with a knock and rises with a wobble.\n      const k = pressed ? 900 : 420, c = pressed ? 46 : 17;\n      s.pv += (k * (pTo - s.p) - c * s.pv) * dt; s.p += s.pv * dt;\n      s.tv += (380 * (txTo - s.tx) - 15 * s.tv) * dt; s.tx += s.tv * dt;\n      s.uv += (380 * (tyTo - s.ty) - 15 * s.uv) * dt; s.ty += s.uv * dt;\n      s.lx += (s.lxTo - s.lx) * Math.min(1, dt * 5); s.ly += (s.lyTo - s.ly) * Math.min(1, dt * 5);\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 still = () => { s.p = s.down ? 1 : rest(); s.tx = 0; s.ty = 0; s.lx = s.lxTo; s.ly = s.lyTo; draw(); };\n    api.current = { tap: () => { touched = performance.now(); s.kick = touched; if (!s.down) { s.ax = 0; s.ay = 0; } if (reduce) still(); else wake(); }, sync: () => { if (reduce) still(); else wake(); } };\n    if (reduce) still(); else wake();\n\n    const where = (e: PointerEvent) => { const b = btn.getBoundingClientRect(); return [clamp(((e.clientX - b.left) / b.width) * 2 - 1, -1.6, 1.6), clamp(((e.clientY - b.top) / b.height) * 2 - 1, -1.6, 1.6)]; };\n    const onMove = (e: PointerEvent) => {\n      touched = performance.now();\n      const [x, y] = where(e);\n      s.lxTo = x * 0.13; s.lyTo = y * 0.09;\n      if (s.down) { s.ax = clamp(x, -1, 1); s.ay = clamp(y, -1, 1); }\n      if (reduce) still();\n    };\n    const onDown = (e: PointerEvent) => { touched = performance.now(); const [x, y] = where(e); s.down = true; s.ax = clamp(x, -1, 1); s.ay = clamp(y, -1, 1); if (reduce) still(); else wake(); };\n    const onUp = () => { if (!s.down) return; s.down = false; touched = performance.now(); if (reduce) still(); };\n    st.addEventListener(\"pointermove\", onMove);\n    btn.addEventListener(\"pointerdown\", onDown);\n    window.addEventListener(\"pointerup\", onUp);\n    window.addEventListener(\"pointercancel\", onUp);\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 () => {\n      cancelAnimationFrame(raf); api.current = null;\n      st.removeEventListener(\"pointermove\", onMove); btn.removeEventListener(\"pointerdown\", onDown);\n      window.removeEventListener(\"pointerup\", onUp); window.removeEventListener(\"pointercancel\", onUp);\n      io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  const toggle = () => { heldRef.current = !heldRef.current; setHeld(heldRef.current); api.current?.tap(); };\n\n  return (\n    <div ref={stage} className=\"relative flex min-h-[640px] w-full flex-col items-center justify-center overflow-hidden bg-[#e6e0d6] px-4 py-16 sm:min-h-[800px]\">\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 bg-[radial-gradient(90%_70%_at_34%_22%,#f6f2eb_0%,#e6e0d6_55%,#d3ccc0_100%)]\" />\n      <svg aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 h-full w-full opacity-[0.22] mix-blend-multiply\">\n        <filter id=\"glazed-celadon-ceramic-button-grain\"><feTurbulence type=\"fractalNoise\" baseFrequency=\"0.9\" numOctaves=\"2\" seed=\"4\" /><feColorMatrix values=\"0 0 0 0 0.45  0 0 0 0 0.4  0 0 0 0 0.34  0 0 0 0.9 -0.2\" /></filter>\n        <rect width=\"100%\" height=\"100%\" filter=\"url(#glazed-celadon-ceramic-button-grain)\" />\n      </svg>\n      <div className=\"relative h-[128px] w-[340px] sm:my-10 sm:scale-150\">\n        <div ref={far} aria-hidden=\"true\" className=\"absolute inset-x-2 inset-y-1 rounded-[46px] bg-[#3d2e1e]/55 blur-[18px]\" style={{ transform: \"translate3d(10px,22px,0)\" }} />\n        <div ref={near} aria-hidden=\"true\" className=\"absolute inset-0 translate-x-[2px] translate-y-[5px] rounded-[46px] bg-[#3a2c1e]/70 blur-[5px]\" style={{ opacity: 0.75 }} />\n        <div ref={body} className=\"absolute inset-0 will-change-transform\">\n          <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n        </div>\n        <button\n          ref={button}\n          type=\"button\"\n          aria-pressed={held}\n          onClick={toggle}\n          className=\"absolute inset-0 cursor-pointer touch-manipulation select-none rounded-[46px] outline-none focus-visible:ring-2 focus-visible:ring-[#2f6b5c] focus-visible:ring-offset-4 focus-visible:ring-offset-[#e6e0d6]\"\n        >\n          <span className=\"sr-only\">{LABEL}</span>\n        </button>\n      </div>\n      <p aria-live=\"polite\" className=\"relative mt-12 min-h-[20px] text-center font-serif text-[15px] italic text-[#5b5245] sm:mt-20 sm:text-[17px]\">\n        {held ? HELD_NOTE : IDLE_NOTE}\n      </p>\n    </div>\n  );\n}\n"}]}
