{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"pepperoni-pizza-share-picker-on-an-aluminium-pan-that-cuts-the-photograph-into-equal-slices-and-pulls-one-out","type":"registry:block","title":"Pepperoni Pizza Share Picker on an Aluminium Pan That Cuts the Photograph into Equal Slices and Pulls One out","description":"Quantity picker where one overhead pizza photograph on an aluminium pan is cut into equal slices as the head count changes. Free React and HTML to copy.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/pepperoni-pizza-share-picker-on-an-aluminium-pan-that-cuts-the-photograph-into-equal-slices-and-pulls-one-out.tsx","type":"registry:component","content":"import { useEffect, useMemo, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n/** The pie on the pan. `cx`, `cy` and `r` say where the pizza sits inside its photograph, as fractions of the picture's width. */\nconst pizza = {\n  name: \"Pepperoni, 14 inch\",\n  price: 19.5,\n  currency: \"£\",\n  min: 2,\n  max: 8,\n  start: 6,\n  photo: {\n    src: \"https://images.pexels.com/photos/29829308/pexels-photo-29829308.jpeg?auto=compress&cs=tinysrgb&w=1000\",\n    alt: \"Whole pepperoni pizza with a charred crust seen from directly above\",\n    tone: \"#c4572b\",\n    cx: 0.5035,\n    cy: 0.5215,\n    r: 0.444,\n  },\n};\n\n// Design pixels. The scene is a square that is scaled down as one object on narrow screens.\nconst SCENE = 676;\nconst PAN = 614;\nconst D = 468; // pizza diameter\nconst GAP = 7; // how far every slice sits from the centre once cut\nconst PULL = 58; // extra travel of the slice that is yours\nconst NUDGE = 13; // lift under the pointer\nconst STIFF = 190;\nconst DAMP = 15;\nconst HOLD_MS = 2600;\nconst DEMO = [8, 4, 3, 5, 6];\n\nconst clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));\nconst rad = (d: number) => (d * Math.PI) / 180;\n\nfunction wedge(a0: number, a1: number) {\n  const pts = [\"50% 50%\"];\n  const steps = Math.max(2, Math.ceil((a1 - a0) / 4));\n  for (let s = 0; s <= steps; s += 1) {\n    const a = rad(a0 + ((a1 - a0) * s) / steps);\n    pts.push(`${(50 + 50 * Math.cos(a)).toFixed(3)}% ${(50 + 50 * Math.sin(a)).toFixed(3)}%`);\n  }\n  return `polygon(${pts.join(\",\")})`;\n}\n\nconst WELL = \"conic-gradient(from 200deg, #d9dadb 0deg, #9da0a3 34deg, #cfd0d1 70deg, #7e8285 112deg, #bfc1c2 150deg, #8f9396 190deg, #dcdddd 230deg, #868a8d 268deg, #c6c8c9 306deg, #9a9ea1 336deg, #d9dadb 360deg)\";\nconst BRUSHED = \"conic-gradient(from 218deg, #f6f6f4 0deg, #b9bbbd 28deg, #e6e7e7 58deg, #8d9093 96deg, #d5d6d6 132deg, #a2a5a8 170deg, #efefee 204deg, #969a9d 244deg, #dcdddd 286deg, #a9acae 322deg, #f6f6f4 360deg)\";\n\nexport default function PizzaSharePicker() {\n  const reduce = !!useReducedMotion();\n  const wrapRef = useRef<HTMLDivElement>(null);\n  const sceneRef = useRef<HTMLDivElement>(null);\n  const sliceRefs = useRef<(HTMLDivElement | null)[]>([]);\n  const [n, setN] = useState(pizza.start);\n  const [mineAt, setMineAt] = useState(18); // compass angle (0 is right, 90 is down) of the slice that is pulled out\n  const [focus, setFocus] = useState(-1);\n  const live = useRef({ n: pizza.start, mineAt: 18, hover: -1, closed: false, openAt: 0, touched: false, pos: Array(pizza.max).fill(GAP), vel: Array(pizza.max).fill(0) });\n\n  const span = 360 / n;\n  const mine = Math.floor((((mineAt + 90) % 360) + 360) % 360 / span) % n;\n  live.current.n = n;\n  live.current.mineAt = mineAt;\n\n  const bg = useMemo(() => {\n    const size = D / (2 * pizza.photo.r);\n    return { backgroundImage: `url(${pizza.photo.src})`, backgroundSize: `${size.toFixed(1)}px auto`, backgroundPosition: `${(D / 2 - pizza.photo.cx * size).toFixed(1)}px ${(D / 2 - pizza.photo.cy * size).toFixed(1)}px`, backgroundColor: pizza.photo.tone, filter: \"saturate(1.18) contrast(1.06)\" };\n  }, []);\n\n  // close the pie, cut it again, then let the slices spring apart one after another\n  const timer = useRef(0);\n  const cut = (to: number) => {\n    const next = clamp(to, pizza.min, pizza.max);\n    if (next === live.current.n) return;\n    if (reduce) {\n      setN(next);\n      return;\n    }\n    live.current.closed = true;\n    window.clearTimeout(timer.current);\n    timer.current = window.setTimeout(() => {\n      setN(next);\n      live.current.closed = false;\n      live.current.openAt = performance.now();\n    }, 300);\n  };\n\n  useEffect(() => {\n    const wrap = wrapRef.current;\n    const scene = sceneRef.current;\n    if (!wrap || !scene) return;\n    const measure = () => {\n      const sc = clamp(wrap.clientWidth / (SCENE - 30), 0.46, 1);\n      wrap.style.height = `${Math.round(SCENE * sc)}px`;\n      scene.style.transform = `translateX(-50%) scale(${sc.toFixed(4)})`;\n    };\n    measure();\n    const ro = new ResizeObserver(measure);\n    ro.observe(wrap);\n    return () => ro.disconnect();\n  }, []);\n\n  useEffect(() => {\n    const s = live.current;\n    let raf = 0;\n    let last = performance.now();\n    let seen = true;\n    const io = new IntersectionObserver(([e]) => (seen = e.isIntersecting));\n    if (wrapRef.current) io.observe(wrapRef.current);\n    const frame = (now: number) => {\n      raf = requestAnimationFrame(frame);\n      if (!seen || document.hidden) {\n        last = now;\n        return;\n      }\n      const dt = Math.min(0.032, (now - last) / 1000);\n      last = now;\n      const sp = 360 / s.n;\n      const my = Math.floor((((s.mineAt + 90) % 360) + 360) % 360 / sp) % s.n;\n      for (let i = 0; i < pizza.max; i += 1) {\n        const el = sliceRefs.current[i];\n        if (!el || i >= s.n) continue;\n        const waiting = now - s.openAt < i * 42;\n        const target = s.closed || waiting ? 0 : GAP + (i === my ? PULL : 0) + (i === s.hover ? NUDGE : 0);\n        if (reduce) {\n          s.pos[i] = target;\n          s.vel[i] = 0;\n        } else {\n          s.vel[i] += (-STIFF * (s.pos[i] - target) - DAMP * s.vel[i]) * dt;\n          s.pos[i] += s.vel[i] * dt;\n        }\n        const mid = rad(-90 + (i + 0.5) * sp);\n        const p = s.pos[i];\n        const lift = clamp((p - GAP) / PULL, 0, 1.2);\n        el.style.transform = `translate(${(Math.cos(mid) * p).toFixed(2)}px, ${(Math.sin(mid) * p).toFixed(2)}px) rotate(${(lift * 3.2).toFixed(2)}deg)`;\n        el.style.filter = `drop-shadow(${(2 + lift * 7).toFixed(1)}px ${(3.5 + lift * 11).toFixed(1)}px ${(2.5 + lift * 9).toFixed(1)}px rgba(24,12,4,${(0.7 - lift * 0.25).toFixed(2)}))`;\n      }\n    };\n    raf = requestAnimationFrame(frame);\n    return () => {\n      cancelAnimationFrame(raf);\n      io.disconnect();\n    };\n  }, [reduce]);\n\n  // the demo: start from the finished picture, then try other head counts until someone touches it\n  useEffect(() => {\n    if (reduce) return;\n    let k = 0;\n    let loop = 0;\n    // the first hold is longer, so the opening picture is the finished one\n    const first = window.setTimeout(() => {\n      loop = window.setInterval(() => {\n        if (live.current.touched || document.hidden) return;\n        cut(DEMO[k % DEMO.length]);\n        k += 1;\n      }, HOLD_MS);\n    }, 2400);\n    return () => {\n      window.clearTimeout(first);\n      window.clearInterval(loop);\n      window.clearTimeout(timer.current);\n    };\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [reduce]);\n\n  const each = (pizza.price / n).toFixed(2);\n  const step = (by: number) => {\n    live.current.touched = true;\n    cut(live.current.n + by);\n  };\n  const round = \"grid h-11 w-11 place-items-center rounded-full bg-[#1d1a16] text-[#f4eee2] transition-transform duration-150 hover:scale-105 active:scale-95 disabled:opacity-30 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#b3311b] focus-visible:ring-offset-2 focus-visible:ring-offset-[#ebe4d5]\";\n\n  return (\n    <section className=\"relative overflow-hidden bg-[#ebe4d5]\">\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\" style={{ background: \"radial-gradient(70% 62% at 42% 36%, #f7f2e7 0%, #ebe4d5 55%, #d9d0bd 100%)\" }} />\n      <div ref={wrapRef} className=\"relative mx-auto w-full\" style={{ height: SCENE }}>\n        <div ref={sceneRef} className=\"absolute left-1/2 top-0 select-none\" style={{ width: SCENE, height: SCENE, transformOrigin: \"50% 0\", transform: \"translateX(-50%)\" }}>\n          {/* the pan: its shadow on the table, the rolled rim, the spun well */}\n          <div aria-hidden=\"true\" className=\"absolute rounded-full\" style={{ left: (SCENE - PAN) / 2 + 14, top: (SCENE - PAN) / 2 + 22, width: PAN, height: PAN, background: \"rgba(60,40,15,0.5)\", filter: \"blur(26px)\" }} />\n          <div aria-hidden=\"true\" className=\"absolute rounded-full\" style={{ left: (SCENE - PAN) / 2 + 3, top: (SCENE - PAN) / 2 + 5, width: PAN, height: PAN, background: \"rgba(40,26,10,0.6)\", filter: \"blur(4px)\" }} />\n          <div aria-hidden=\"true\" className=\"absolute rounded-full\" style={{ left: (SCENE - PAN) / 2, top: (SCENE - PAN) / 2, width: PAN, height: PAN, background: BRUSHED, boxShadow: \"inset 0 0 0 1px rgba(255,255,255,0.55), inset 2px 3px 3px rgba(255,255,255,0.85), inset -2px -3px 4px rgba(0,0,0,0.38)\" }}>\n            <div className=\"absolute inset-[15px] rounded-full\" style={{ background: WELL, boxShadow: \"inset 5px 8px 12px rgba(0,0,0,0.5), inset 1px 2px 2px rgba(0,0,0,0.55), inset -3px -4px 4px rgba(255,255,255,0.75), 0 0 0 1px rgba(0,0,0,0.22), 0 0 0 2.5px rgba(255,255,255,0.35)\" }}>\n              <div className=\"absolute inset-0 rounded-full\" style={{ background: \"repeating-radial-gradient(circle at 50% 50%, rgba(255,255,255,0.09) 0 0.6px, rgba(0,0,0,0.05) 0.6px 1.7px, rgba(255,255,255,0) 1.7px 3.1px)\" }} />\n              <div className=\"absolute inset-0 rounded-full\" style={{ background: \"radial-gradient(circle at 50% 50%, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 60%, rgba(0,0,0,0.14) 100%)\" }} />\n              {/* knife scores left by other pies */}\n              {[[22, 38, 190, 24], [48, 64, 240, -41], [30, 71, 150, 8], [55, 30, 210, 63], [18, 52, 260, -12]].map(([x, y, w, r], i) => (\n                <span key={`k${i}`} className=\"absolute h-px\" style={{ left: `${x}%`, top: `${y}%`, width: w, transform: `rotate(${r}deg)`, background: \"linear-gradient(90deg, rgba(255,255,255,0), rgba(255,255,255,0.55) 30%, rgba(0,0,0,0.2) 70%, rgba(0,0,0,0))\" }} />\n              ))}\n              {/* oil and a few crumbs left where the pie was cut */}\n              <div className=\"absolute rounded-full\" style={{ inset: 58, background: \"radial-gradient(circle at 46% 44%, rgba(214,120,20,0.1) 0%, rgba(200,104,24,0.07) 58%, rgba(190,110,40,0) 72%), radial-gradient(circle at 62% 60%, rgba(120,60,10,0.1) 0%, rgba(120,60,10,0) 40%)\" }} />\n              {[\n                [44, 31, 5], [57, 68, 4], [29, 55, 3], [66, 40, 4], [49, 52, 5], [38, 72, 3], [71, 60, 3], [52, 24, 3],\n              ].map(([x, y, w], i) => (\n                <span key={i} className=\"absolute rounded-full\" style={{ left: `${x}%`, top: `${y}%`, width: w, height: w - 1, background: \"#a8641f\", boxShadow: \"0.5px 1px 0.5px rgba(0,0,0,0.45)\" }} />\n              ))}\n            </div>\n          </div>\n\n          {/* the pizza: one photograph, cut into wedges */}\n          <div role=\"group\" aria-label={`${pizza.name}, cut into ${n} slices`} className=\"absolute\" style={{ left: (SCENE - D) / 2, top: (SCENE - D) / 2, width: D, height: D }}>\n            <img src={pizza.photo.src} alt={pizza.photo.alt} className=\"sr-only\" />\n            {Array.from({ length: pizza.max }, (_, i) => {\n              const on = i < n;\n              const a0 = -90 + i * span;\n              return (\n                <div\n                  key={i}\n                  ref={(el) => {\n                    sliceRefs.current[i] = el;\n                  }}\n                  className=\"absolute inset-0\"\n                  style={{ display: on ? \"block\" : \"none\", zIndex: i === mine ? 3 : i === focus ? 2 : 1, willChange: \"transform\", pointerEvents: \"none\" }}\n                >\n                  <button\n                    type=\"button\"\n                    aria-pressed={i === mine}\n                    aria-label={`Slice ${i + 1} of ${n}${i === mine ? \", pulled out\" : \", pull this one out\"}`}\n                    onClick={() => {\n                      live.current.touched = true;\n                      setMineAt(a0 + span / 2);\n                    }}\n                    onPointerEnter={() => (live.current.hover = i)}\n                    onPointerLeave={() => (live.current.hover = -1)}\n                    onFocus={() => {\n                      live.current.hover = i;\n                      setFocus(i);\n                    }}\n                    onBlur={() => {\n                      live.current.hover = -1;\n                      setFocus(-1);\n                    }}\n                    className=\"absolute inset-0 block cursor-pointer outline-none\"\n                    style={{ ...bg, pointerEvents: \"auto\", clipPath: on ? wedge(a0 + 0.12, a0 + span - 0.12) : undefined }}\n                  >\n                    {/* light catching the cheese, and the focus mark drawn on the slice itself */}\n                    <span className=\"absolute inset-0\" style={{ background: \"radial-gradient(circle at 50% 50%, rgba(30,10,0,0) 78%, rgba(30,10,0,0.22) 100%)\" }} />\n                    {i === focus && <span className=\"absolute inset-0\" style={{ boxShadow: \"inset 0 0 0 5px #ffffff, inset 0 0 0 9px #1d4ed8\", borderRadius: \"50%\" }} />}\n                  </button>\n                </div>\n              );\n            })}\n          </div>\n        </div>\n      </div>\n\n      <div className=\"relative mx-auto flex max-w-md flex-col items-center gap-3 px-4 pb-8 pt-1 sm:pb-9\">\n        <div className=\"flex items-center gap-4\">\n          <button type=\"button\" onClick={() => step(-1)} disabled={n <= pizza.min} aria-label=\"One person fewer\" className={round}>\n            <svg width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" aria-hidden=\"true\"><path d=\"M3 8h10\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" /></svg>\n          </button>\n          <p className=\"min-w-[10.5rem] text-center text-[15px] font-medium tracking-tight text-[#2b2620]\" aria-live=\"polite\">\n            Shared between <span className=\"inline-block w-[1.1ch] text-center font-semibold tabular-nums text-[#b3311b]\">{n}</span>\n            <span className=\"block font-mono text-[11px] uppercase tracking-[0.18em] text-[#5d5446]\">\n              {pizza.currency}\n              {each} a slice\n            </span>\n          </p>\n          <button type=\"button\" onClick={() => step(1)} disabled={n >= pizza.max} aria-label=\"One person more\" className={round}>\n            <svg width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" aria-hidden=\"true\"><path d=\"M3 8h10M8 3v10\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" /></svg>\n          </button>\n        </div>\n        <p className=\"font-mono text-[11px] uppercase tracking-[0.18em] text-[#5d5446]\">\n          {pizza.name} · {pizza.currency}\n          {pizza.price.toFixed(2)}\n        </p>\n      </div>\n    </section>\n  );\n}\n"}]}
