{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"hotel-keycard-door-lock-switch-with-a-card-that-dips-into-the-reader-a-lamp-that-turns-green-and-a-lever-that-drops","type":"registry:component","title":"Hotel Keycard Door Lock Switch with a Card That Dips into the Reader, a Lamp That Turns Green and a Lever That Drops","description":"Lock switch drawn as a hotel door lock: a key card dips into the reader, the lamp turns green and the chrome lever drops. Free React and HTML code to copy.","dependencies":[],"files":[{"path":"components/we-inc/hotel-keycard-door-lock-switch-with-a-card-that-dips-into-the-reader-a-lamp-that-turns-green-and-a-lever-that-drops.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\nconst ROOM = \"412\"; // printed on the card and read out in the label\nconst HOTEL = \"MERIDIAN\"; // a made-up house name on the card\nconst RED = [255, 64, 48], AMBER = [255, 176, 32], GREEN = [56, 230, 132]; // the lamp: locked, reading, open\nconst CARD = { paper: \"#f2e8d5\", sun: \"#dc5a32\", sea: \"#1f3a5c\" };\n\nconst W = 560, H = 730;\nconst PX = 196, PY = 250, PW = 168, PH = 450; // the steel plate\nconst SLOT = 269; // the card vanishes below this line\nconst PIVOT = { x: 280, y: 522 };\n\nexport default function HotelKeycardDoorLockSwitch() {\n  const wrap = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [on, setOn] = useState(true);\n  const onRef = useRef(true);\n  const api = useRef<{ set: (v: boolean, fromUser: boolean) => void } | null>(null);\n\n  useEffect(() => {\n    const cv = canvas.current, box = wrap.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    let K = 1;\n    const s = { card: 1, cardV: 0, lever: 1, leverV: 0, lamp: GREEN.slice(), stage: 2, since: 0 };\n\n    // Fine vertical brushing for the plate, made once.\n    const brush = document.createElement(\"canvas\"); brush.width = PW * 2; brush.height = 64;\n    const bx = brush.getContext(\"2d\");\n    if (bx) { let seed = 11; const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n      for (let x = 0; x < brush.width; x++) { const v = rand(); bx.fillStyle = v > 0.5 ? `rgba(255,255,255,${(v - 0.5) * 0.34})` : `rgba(0,0,0,${(0.5 - v) * 0.22})`; bx.fillRect(x, 0, 1, 64); } }\n\n    const rr = (x: number, y: number, w: number, h: number, r: number) => {\n      ctx.beginPath(); ctx.moveTo(x + r, y); ctx.lineTo(x + w - r, y); ctx.quadraticCurveTo(x + w, y, x + w, y + r);\n      ctx.lineTo(x + w, y + h - r); ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h); ctx.lineTo(x + r, y + h);\n      ctx.quadraticCurveTo(x, y + h, x, y + h - r); ctx.lineTo(x, y + r); ctx.quadraticCurveTo(x, y, x + r, y); ctx.closePath();\n    };\n    const lin = (x0: number, y0: number, x1: number, y1: number, stops: [number, string][]) => { const g = ctx.createLinearGradient(x0, y0, x1, y1); for (const [p, c] of stops) g.addColorStop(p, c); return g; };\n    const shadow = (shape: () => void, blur: number, dx: number, dy: number, alpha: number) => {\n      ctx.save(); ctx.shadowColor = `rgba(2,10,8,${alpha})`; ctx.shadowBlur = blur * K; ctx.shadowOffsetX = (dx + 3000) * K; ctx.shadowOffsetY = dy * K;\n      ctx.translate(-3000, 0); ctx.fillStyle = \"#000\"; shape(); ctx.restore();\n    };\n    const CHROME: [number, string][] = [[0, \"#fbfcfc\"], [0.16, \"#d9dce0\"], [0.4, \"#9ba0a7\"], [0.5, \"#5f646b\"], [0.56, \"#8d9299\"], [0.74, \"#d2d5d9\"], [0.9, \"#f1f2f4\"], [1, \"#868b92\"]];\n    const arm = () => { ctx.beginPath(); ctx.moveTo(0, -18); ctx.lineTo(178, -13); ctx.arc(178, 0, 13, -Math.PI / 2, Math.PI / 2); ctx.lineTo(0, 18); ctx.closePath(); };\n    const hasConic = typeof ctx.createConicGradient === \"function\";\n\n    const draw = (time: number) => {\n      ctx.setTransform(K, 0, 0, K, 0, 0); ctx.clearRect(0, 0, W, H); ctx.lineJoin = \"round\";\n      const c = Math.max(-0.08, Math.min(1.06, s.card)), hover = 1 - Math.max(0, Math.min(1, c));\n      const bob = reduce ? 0 : Math.sin(time / 700) * 4 * hover;\n      const lamp = `rgb(${s.lamp[0] | 0},${s.lamp[1] | 0},${s.lamp[2] | 0})`;\n      const glow = 0.82 + (reduce ? 0 : 0.18 * Math.sin(time / 520));\n      const ang = (Math.max(-0.1, s.lever) * 25 * Math.PI) / 180;\n\n      // The plate: shadow on the door, brushed face, bevel.\n      shadow(() => { rr(PX, PY, PW, PH, 30); ctx.fill(); }, 34, 12, 30, 0.62);\n      shadow(() => { rr(PX, PY, PW, PH, 30); ctx.fill(); }, 8, 3, 7, 0.55);\n      rr(PX, PY, PW, PH, 30); ctx.fillStyle = lin(PX, 0, PX + PW, 0, [[0, \"#7f848a\"], [0.05, \"#e4e6e9\"], [0.28, \"#c3c7cc\"], [0.5, \"#f0f1f3\"], [0.76, \"#b4b8be\"], [0.95, \"#dcdfe2\"], [1, \"#74797f\"]]); ctx.fill();\n      ctx.save(); rr(PX, PY, PW, PH, 30); ctx.clip();\n      for (let y = PY; y < PY + PH; y += 64) ctx.drawImage(brush, PX, y, PW, 64);\n      ctx.fillStyle = lin(0, PY, 0, PY + PH, [[0, \"rgba(255,255,255,0.3)\"], [0.25, \"rgba(255,255,255,0)\"], [0.7, \"rgba(0,0,0,0)\"], [1, \"rgba(0,0,0,0.26)\"]]); ctx.fillRect(PX, PY, PW, PH);\n      ctx.restore();\n      ctx.lineWidth = 1.2; ctx.strokeStyle = \"rgba(20,24,28,0.7)\"; rr(PX + 0.6, PY + 0.6, PW - 1.2, PH - 1.2, 30); ctx.stroke();\n      ctx.lineWidth = 1.6; ctx.strokeStyle = lin(0, PY, 0, PY + PH, [[0, \"rgba(255,255,255,0.95)\"], [0.5, \"rgba(255,255,255,0.3)\"], [1, \"rgba(0,0,0,0.35)\"]]); rr(PX + 3, PY + 3, PW - 6, PH - 6, 27); ctx.stroke();\n\n      // The reader: a gloss black block. Its top face holds the slot.\n      const BX = PX + 12, BY = 262, BW = PW - 24, BH = 160;\n      shadow(() => { rr(BX, BY, BW, BH, 15); ctx.fill(); }, 9, 3, 8, 0.6);\n      rr(BX, BY, BW, BH, 15); ctx.fillStyle = lin(0, BY, 0, BY + BH, [[0, \"#3a3e45\"], [0.08, \"#1c1e22\"], [0.6, \"#0c0d0f\"], [1, \"#030304\"]]); ctx.fill();\n      ctx.save(); rr(BX, BY, BW, BH, 15); ctx.clip();\n      ctx.fillStyle = lin(0, BY, 0, BY + 15, [[0, \"#6a6f78\"], [1, \"#2b2e34\"]]); ctx.fillRect(BX, BY, BW, 15);\n      ctx.beginPath(); ctx.moveTo(BX, BY + 15); ctx.lineTo(BX + 84, BY + 15); ctx.lineTo(BX + 26, BY + BH); ctx.lineTo(BX, BY + BH); ctx.closePath(); ctx.fillStyle = \"rgba(255,255,255,0.055)\"; ctx.fill();\n      const spill = ctx.createRadialGradient(280, 374, 4, 280, 374, 74); spill.addColorStop(0, `rgba(${s.lamp[0] | 0},${s.lamp[1] | 0},${s.lamp[2] | 0},${0.34 * glow})`); spill.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx.fillStyle = spill; ctx.fillRect(BX, BY, BW, BH);\n      ctx.restore();\n      ctx.fillStyle = \"#000\"; rr(218, SLOT - 4, 124, 8, 4); ctx.fill();\n      ctx.fillStyle = \"rgba(255,255,255,0.35)\"; ctx.fillRect(222, SLOT + 4, 116, 0.9);\n      ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(255,255,255,0.16)\"; rr(BX + 0.5, BY + 0.5, BW - 1, BH - 1, 15); ctx.stroke();\n      // engraved pictogram: a card and the way it goes\n      ctx.strokeStyle = \"rgba(255,255,255,0.3)\"; ctx.lineWidth = 1.3; rr(268, 303, 24, 34, 3.5); ctx.stroke();\n      ctx.beginPath(); ctx.moveTo(280, 310); ctx.lineTo(280, 328); ctx.moveTo(274.5, 322.5); ctx.lineTo(280, 328); ctx.lineTo(285.5, 322.5); ctx.stroke();\n      // the lamp behind a domed lens\n      ctx.beginPath(); ctx.arc(280, 374, 15, 0, Math.PI * 2); ctx.fillStyle = lin(0, 359, 0, 389, [[0, \"#0a0a0b\"], [1, \"#4a4e55\"]]); ctx.fill();\n      ctx.save(); ctx.shadowColor = lamp; ctx.shadowBlur = 26 * glow * K; ctx.beginPath(); ctx.arc(280, 374, 10.5, 0, Math.PI * 2); ctx.fillStyle = lamp; ctx.fill(); ctx.restore();\n      const dome = ctx.createRadialGradient(277, 370, 1, 280, 374, 10.5); dome.addColorStop(0, \"rgba(255,255,255,0.75)\"); dome.addColorStop(0.45, \"rgba(255,255,255,0.12)\"); dome.addColorStop(1, \"rgba(0,0,0,0.38)\");\n      ctx.beginPath(); ctx.arc(280, 374, 10.5, 0, Math.PI * 2); ctx.fillStyle = dome; ctx.fill();\n      ctx.beginPath(); ctx.ellipse(276.4, 369.6, 3.4, 2, -0.6, 0, Math.PI * 2); ctx.fillStyle = \"rgba(255,255,255,0.8)\"; ctx.fill();\n      // lamp colour caught on the plate under the reader\n      ctx.save(); rr(PX, PY, PW, PH, 30); ctx.clip(); const wash = ctx.createRadialGradient(280, BY + BH, 2, 280, BY + BH, 70); wash.addColorStop(0, `rgba(${s.lamp[0] | 0},${s.lamp[1] | 0},${s.lamp[2] | 0},${0.2 * glow})`); wash.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx.fillStyle = wash; ctx.fillRect(PX, BY + BH, PW, 70); ctx.restore();\n\n      // The key card, cut off where it enters the slot.\n      const cw = 112, ch = 176;\n      const cx = 280 + 26 * hover, bottom = SLOT + 48 - (48 + 34) * hover + bob, rot = (-7 * Math.PI) / 180 * hover;\n      ctx.save(); ctx.beginPath(); ctx.rect(0, 0, W, SLOT); ctx.clip();\n      ctx.translate(cx, bottom); ctx.rotate(rot);\n      shadow(() => { rr(-cw / 2, -ch, cw, ch, 9); ctx.fill(); }, 8 + 14 * hover, 5 + 9 * hover, 6 + 20 * hover, 0.5 - 0.14 * hover);\n      rr(-cw / 2 + 1.2, -ch + 1.6, cw, ch, 9); ctx.fillStyle = \"#b9ad95\"; ctx.fill();\n      rr(-cw / 2, -ch, cw, ch, 9); ctx.fillStyle = CARD.paper; ctx.fill();\n      ctx.save(); rr(-cw / 2, -ch, cw, ch, 9); ctx.clip();\n      ctx.beginPath(); ctx.arc(0, -ch + 86, 34, Math.PI, 0); ctx.fillStyle = CARD.sun; ctx.fill();\n      for (let j = 0; j < 5; j++) { ctx.fillStyle = CARD.sea; ctx.globalAlpha = 1 - j * 0.17; ctx.fillRect(-34 - j * 0.5 + j * 5, -ch + 90 + j * 7.5, 68 - j * 10, 3.2); }\n      ctx.globalAlpha = 1; ctx.textAlign = \"center\"; ctx.textBaseline = \"middle\";\n      ctx.fillStyle = CARD.sea; ctx.font = \"700 10px ui-sans-serif, 'Helvetica Neue', Arial, sans-serif\"; ctx.fillText(HOTEL.split(\"\").join(\" \"), 0, -ch + 26);\n      ctx.font = \"600 7px ui-monospace, SFMono-Regular, Menlo, monospace\"; ctx.fillStyle = \"rgba(31,58,92,0.75)\"; ctx.fillText(`ROOM ${ROOM}`, 0, -ch + 142);\n      ctx.beginPath(); ctx.moveTo(-6, -22); ctx.lineTo(0, -15); ctx.lineTo(6, -22); ctx.lineWidth = 1.6; ctx.strokeStyle = CARD.sun; ctx.stroke();\n      ctx.fillStyle = lin(-cw / 2, -ch, cw / 2, 0, [[0, \"rgba(255,255,255,0.5)\"], [0.35, \"rgba(255,255,255,0)\"], [0.62, \"rgba(255,255,255,0)\"], [0.7, \"rgba(255,255,255,0.22)\"], [0.78, \"rgba(255,255,255,0)\"], [1, \"rgba(60,40,10,0.12)\"]]); ctx.fillRect(-cw / 2, -ch, cw, ch);\n      ctx.restore();\n      ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(255,255,255,0.8)\"; rr(-cw / 2 + 0.5, -ch + 0.5, cw - 1, ch - 1, 9); ctx.stroke();\n      ctx.restore();\n\n      // Key override below the handle.\n      ctx.beginPath(); ctx.arc(280, 646, 17, 0, Math.PI * 2); ctx.fillStyle = lin(0, 629, 0, 663, [[0, \"#5d6168\"], [1, \"#f4f5f6\"]]); ctx.fill();\n      ctx.beginPath(); ctx.arc(280, 646, 14, 0, Math.PI * 2); ctx.fillStyle = lin(0, 632, 0, 660, CHROME); ctx.fill();\n      ctx.fillStyle = \"#0b0c0d\"; rr(278, 637, 4, 18, 2); ctx.fill(); ctx.beginPath(); ctx.arc(280, 640.5, 3.6, 0, Math.PI * 2); ctx.fill();\n\n      // The lever: rose, arm with its shadow, hub.\n      shadow(() => { ctx.beginPath(); ctx.arc(PIVOT.x, PIVOT.y, 46, 0, Math.PI * 2); ctx.fill(); }, 10, 4, 9, 0.5);\n      ctx.beginPath(); ctx.arc(PIVOT.x, PIVOT.y, 46, 0, Math.PI * 2);\n      if (hasConic) { const g = ctx.createConicGradient(-2.1, PIVOT.x, PIVOT.y); const st: [number, number][] = [[0, 250], [0.09, 196], [0.2, 128], [0.3, 176], [0.42, 232], [0.5, 150], [0.58, 206], [0.7, 122], [0.82, 168], [0.93, 226], [1, 250]]; for (const [q, v] of st) g.addColorStop(q, `rgb(${v - 6},${v},${v + 5})`); ctx.fillStyle = g; }\n      else ctx.fillStyle = lin(0, PIVOT.y - 46, 0, PIVOT.y + 46, CHROME);\n      ctx.fill(); ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(20,24,28,0.6)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(PIVOT.x, PIVOT.y, 43, 0, Math.PI * 2); ctx.lineWidth = 1.2; ctx.strokeStyle = \"rgba(255,255,255,0.7)\"; ctx.stroke();\n      ctx.save(); ctx.translate(PIVOT.x, PIVOT.y); ctx.rotate(ang);\n      shadow(() => { arm(); ctx.fill(); }, 15, 8 * Math.cos(ang) + 22 * Math.sin(ang), 22 * Math.cos(ang) - 8 * Math.sin(ang), 0.5);\n      shadow(() => { arm(); ctx.fill(); }, 4, 1, 4, 0.45);\n      arm(); ctx.fillStyle = lin(0, -18, 0, 18, CHROME); ctx.fill();\n      ctx.lineWidth = 0.9; ctx.strokeStyle = \"rgba(20,24,28,0.55)\"; ctx.stroke();\n      ctx.beginPath(); ctx.moveTo(24, -15.6); ctx.lineTo(174, -11.4); ctx.lineWidth = 1.1; ctx.strokeStyle = \"rgba(255,255,255,0.85)\"; ctx.stroke();\n      ctx.restore();\n      ctx.beginPath(); ctx.arc(PIVOT.x, PIVOT.y, 25, 0, Math.PI * 2); ctx.fillStyle = lin(0, PIVOT.y - 25, 0, PIVOT.y + 25, [[0, \"#ffffff\"], [0.45, \"#b3b8be\"], [0.55, \"#7c8188\"], [1, \"#d9dcdf\"]]); ctx.fill();\n      ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(20,24,28,0.5)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(PIVOT.x, PIVOT.y, 19, 0, Math.PI * 2); ctx.fillStyle = lin(0, PIVOT.y - 19, 0, PIVOT.y + 19, [[0, \"#8d9299\"], [1, \"#eceef0\"]]); ctx.fill();\n    };\n\n    let raf = 0, last = 0, t0 = 0, seen = true, touched = -1e9, demoAt = 3500;\n    const snap = () => { const v = onRef.current; s.card = v ? 1 : 0; s.lever = v ? 1 : 0; s.lamp = (v ? GREEN : RED).slice(); s.stage = v ? 2 : 0; draw(0); };\n    const set = (v: boolean, fromUser: boolean) => {\n      if (fromUser) touched = performance.now();\n      onRef.current = v; setOn(v);\n      if (reduce) snap(); else wake();\n    };\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, want = onRef.current;\n      s.cardV += (170 * ((want ? 1 : 0) - s.card) - 17 * s.cardV) * dt; s.card += s.cardV * dt;\n      // The reader thinks for a moment in amber before it lets you in.\n      if (!want) s.stage = 0;\n      else if (s.stage === 0 && s.card > 0.9) { s.stage = 1; s.since = t; }\n      else if (s.stage === 1 && t - s.since > 300) s.stage = 2;\n      const tone = s.stage === 2 ? GREEN : s.stage === 1 ? (Math.floor((t - s.since) / 75) % 2 ? [70, 46, 8] : AMBER) : RED;\n      s.lamp = s.lamp.map((v, i) => v + (tone[i] - v) * Math.min(1, dt * 22));\n      s.leverV += (230 * ((s.stage === 2 ? 1 : 0) - s.lever) - 13 * s.leverV) * dt; s.lever += s.leverV * dt;\n      if (now - touched > 8000 && t > demoAt) { set(!want, false); demoAt = t + (want ? 2200 : 3400); }\n      draw(t);\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    const size = () => {\n      const w = box.clientWidth, dpr = Math.min(2, window.devicePixelRatio || 1);\n      cv.width = Math.round(w * dpr); cv.height = Math.round((w * H) / W * dpr); K = cv.width / W; draw(0);\n    };\n    const ro = new ResizeObserver(size); ro.observe(box); size();\n    api.current = { set };\n    wake();\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 = () => { const v = !onRef.current; onRef.current = v; setOn(v); api.current?.set(v, true); };\n\n  return (\n    <div className=\"flex min-h-[720px] w-full flex-col items-center justify-center bg-[#1c3a34] bg-[linear-gradient(101deg,rgba(255,255,255,0)_22%,rgba(255,255,255,0.06)_31%,rgba(255,255,255,0)_40%),radial-gradient(90%_70%_at_40%_30%,#2a5249_0%,#1c3a34_55%,#11241f_100%)] px-4 py-8 sm:min-h-[800px]\">\n      <div ref={wrap} onClick={toggle} className=\"relative w-full max-w-[520px] cursor-pointer\" style={{ aspectRatio: `${W} / ${H}` }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n        <button\n          type=\"button\"\n          role=\"switch\"\n          aria-checked={on}\n          aria-label={`Room ${ROOM} door unlocked`}\n          onClick={(e) => { e.stopPropagation(); toggle(); }}\n          style={{ left: `${(PX / W) * 100}%`, top: `${(PY / H) * 100}%`, width: `${(PW / W) * 100}%`, height: `${(PH / H) * 100}%`, borderRadius: \"18% / 6.7%\" }}\n          className=\"absolute cursor-pointer bg-transparent outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-offset-4 focus-visible:ring-offset-[#1c3a34]\"\n        />\n      </div>\n      <p aria-live=\"polite\" className=\"mt-1 text-center font-mono text-[11px] uppercase leading-5 tracking-[0.16em] text-emerald-100/70\">\n        <span className=\"font-semibold text-white\">Room {ROOM}</span>\n        <span className=\"mx-2 text-emerald-100/40\">/</span>\n        {on ? \"Unlocked, handle free\" : \"Locked, card out\"}\n      </p>\n    </div>\n  );\n}\n"}]}
