{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"screwgate-carabiner-lock-switch-that-clips-a-rope-snaps-its-gate-shut-and-spins-the-sleeve-up","type":"registry:component","title":"Screwgate Carabiner Lock Switch That Clips a Rope, Snaps Its Gate Shut and Spins the Sleeve Up","description":"Lock switch on a canvas: a rope clips through the gate of a carabiner, the gate snaps shut and the sleeve screws up to lock. Free React and HTML to copy.","dependencies":[],"files":[{"path":"components/we-inc/screwgate-carabiner-lock-switch-that-clips-a-rope-snaps-its-gate-shut-and-spins-the-sleeve-up.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Everything a visitor is likely to change sits here.\nconst LABEL = \"Belay check\"; // what locking the carabiner confirms\nconst START_LOCKED = true; // the state on load\nconst BODY = [\"#0b3557\", \"#1c79b0\", \"#7fd0f5\"]; // anodised colour: shade, face, highlight\nconst ROPE = [\"#b8430c\", \"#f2691c\", \"#ffd45a\", \"#1d2430\"]; // rope: shade, sheath, bright fleck, dark fleck\nconst WARN = \"#dc2233\"; // the band that shows while the sleeve is undone\nconst TRAVEL = 1.5; // seconds for the whole clip-and-lock move\nconst DEMO = true; // unlock and lock by itself until a visitor does\n\nconst SIZE = 1.12; // largest scale the drawing is shown at\nconst TILT = 0.2; // how far the carabiner is turned on the table, in radians\nconst BW = 340, BH = 490; // design pixels of the layer the carabiner is composed on\nconst NOSE = { x: -70, y: -112 }, HINGE = { x: -56, y: 122 };\nconst GATE = Math.hypot(NOSE.x - HINGE.x, NOSE.y - HINGE.y), REST = Math.atan2(NOSE.x - HINGE.x, -(NOSE.y - HINGE.y));\nconst TAU = Math.PI * 2;\nconst seg = (p: number, a: number, b: number) => Math.min(1, Math.max(0, (p - a) / (b - a)));\nconst ease = (x: number) => x * x * (3 - 2 * x);\nconst mix = (a: number, b: number, t: number) => a + (b - a) * t;\nconst tone = (a: string, b: string, t: number) => {\n  const n = (h: string, i: number) => parseInt(h.slice(1 + i * 2, 3 + i * 2), 16);\n  return `rgb(${[0, 1, 2].map((i) => Math.round(mix(n(a, i), n(b, i), t))).join(\",\")})`;\n};\n\nexport default function ScrewgateCarabinerSwitch() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [locked, setLocked] = useState(START_LOCKED);\n  const want = useRef(START_LOCKED);\n  const api = useRef<{ go: () => void; hands: () => void } | null>(null);\n\n  useEffect(() => {\n    const box = stage.current, cv = canvas.current;\n    if (!box || !cv) 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, ox = 0, oy = 0; // canvas pixels per design pixel, and where the carabiner's centre lies\n    const S = { p: want.current ? 1 : 0, gate: 0, gateV: 0, sheen: 0.3, sheenTo: 0.3, auto: DEMO && !reduce, born: performance.now() };\n    const body = document.createElement(\"canvas\"), obj = document.createElement(\"canvas\");\n    const place = (c: CanvasRenderingContext2D, x: number, y: number) => { c.setTransform(k, 0, 0, k, x, y); c.rotate(TILT); };\n    const onLayer = (c: CanvasRenderingContext2D) => place(c, (BW / 2) * k, (BH / 2) * k);\n\n    const frame = (c: CanvasRenderingContext2D) => {\n      c.beginPath(); c.moveTo(NOSE.x, NOSE.y);\n      c.bezierCurveTo(-80, -170, -22, -188, 30, -179); c.bezierCurveTo(72, -171, 93, -140, 92, -100);\n      c.lineTo(80, 96); c.bezierCurveTo(77, 150, 40, 177, 0, 172); c.bezierCurveTo(-30, 169, -52, 150, HINGE.x, HINGE.y);\n    };\n    // The forged body never moves, so it is painted once: a tube shaded in ten passes, then grain and markings.\n    const paintBody = () => {\n      body.width = obj.width = Math.ceil(BW * k); body.height = obj.height = Math.ceil(BH * k);\n      const b = body.getContext(\"2d\");\n      if (!b) return;\n      onLayer(b); b.lineCap = \"round\"; b.lineJoin = \"round\";\n      frame(b); b.lineWidth = 25; b.strokeStyle = \"#062238\"; b.stroke();\n      for (let i = 0; i < 10; i++) {\n        const t = i / 9;\n        b.save(); b.translate(-t * 3.4, -t * 3.8); frame(b);\n        b.lineWidth = 23.5 * (1 - t * 0.86); b.strokeStyle = t < 0.5 ? tone(BODY[0], BODY[1], t * 2) : tone(BODY[1], BODY[2], (t - 0.5) * 2); b.stroke(); b.restore();\n      }\n      b.save(); b.translate(-5.2, -5.8); frame(b); b.lineWidth = 1.5; b.strokeStyle = \"rgba(255,255,255,0.7)\"; b.stroke(); b.restore();\n      b.save(); b.translate(7.6, 8); frame(b); b.lineWidth = 2; b.strokeStyle = \"rgba(160,220,255,0.22)\"; b.stroke(); b.restore();\n      // The weight-saving groove forged into the spine, with its markings etched inside.\n      b.beginPath(); b.moveTo(90.5, -92); b.lineTo(81.5, 86); b.lineWidth = 9; b.strokeStyle = tone(BODY[0], BODY[1], 0.25); b.stroke();\n      b.beginPath(); b.moveTo(87.6, -91); b.lineTo(78.6, 86); b.lineWidth = 1.6; b.strokeStyle = \"rgba(2,18,32,0.6)\"; b.stroke();\n      b.beginPath(); b.moveTo(94, -91); b.lineTo(85, 86); b.lineWidth = 1.2; b.strokeStyle = \"rgba(190,235,255,0.6)\"; b.stroke();\n      b.save(); b.translate(86, -3); b.rotate(Math.PI / 2 + Math.atan2(9, 178));\n      b.font = `600 6.4px \"Helvetica Neue\", Helvetica, Arial, sans-serif`; b.textAlign = \"center\"; b.textBaseline = \"middle\";\n      b.fillStyle = \"rgba(225,244,255,0.86)\"; b.fillText(\"MAJOR 24 kN     MINOR 8 kN     GATE OPEN 7 kN\", 0, 0.4);\n      b.restore();\n      // Bead-blast grain, kept inside the metal.\n      b.globalCompositeOperation = \"source-atop\";\n      let seed = 5;\n      const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n      for (let i = 0; i < 5200; i++) {\n        b.fillStyle = rand() > 0.5 ? \"rgba(255,255,255,0.09)\" : \"rgba(0,20,40,0.12)\";\n        b.fillRect(-110 + rand() * 220, -200 + rand() * 390, 0.8, 0.8);\n      }\n      b.globalCompositeOperation = \"source-over\";\n    };\n\n    const cylinder = (c: CanvasRenderingContext2D, w: number, cols: [number, string][]) => {\n      const g = c.createLinearGradient(-w / 2, 0, w / 2, 0);\n      for (const [p, col] of cols) g.addColorStop(p, col);\n      return g;\n    };\n    const drawGate = (c: CanvasRenderingContext2D, open: number, sleeve: number) => {\n      c.save(); c.translate(HINGE.x, HINGE.y); c.rotate(REST + open);\n      const L = GATE, G = 9.5;\n      // The bare aluminium gate.\n      c.beginPath(); c.moveTo(-G, 0); c.lineTo(-G, -L + 9); c.quadraticCurveTo(-G, -L + 3, -3, -L + 3); c.lineTo(3, -L + 3); c.quadraticCurveTo(G, -L + 3, G, -L + 9); c.lineTo(G, 0); c.arc(0, 0, G, 0, Math.PI); c.closePath();\n      c.fillStyle = cylinder(c, G * 2, [[0, \"#6f757d\"], [0.16, \"#c9ced4\"], [0.36, \"#ffffff\"], [0.55, \"#c2c7cd\"], [0.82, \"#7b8088\"], [1, \"#4a4e55\"]]); c.fill();\n      c.lineWidth = 0.6; c.strokeStyle = \"rgba(20,24,30,0.55)\"; c.stroke();\n      // The warning band near the nose and the thread lower down.\n      c.fillStyle = WARN; c.fillRect(-G, -L + 9, G * 2, 17);\n      const band = c.createLinearGradient(-G, 0, G, 0);\n      band.addColorStop(0, \"rgba(0,0,0,0.4)\"); band.addColorStop(0.36, \"rgba(255,255,255,0.42)\"); band.addColorStop(1, \"rgba(0,0,0,0.45)\");\n      c.fillStyle = band; c.fillRect(-G, -L + 9, G * 2, 17);\n      for (let y = -L + 56; y < -L + 100; y += 2.3) { c.beginPath(); c.moveTo(-G, y + 0.9); c.lineTo(G, y - 0.9); c.lineWidth = 0.9; c.strokeStyle = \"rgba(30,34,42,0.6)\"; c.stroke(); c.beginPath(); c.moveTo(-G, y + 1.9); c.lineTo(G, y + 0.1); c.lineWidth = 0.5; c.strokeStyle = \"rgba(255,255,255,0.55)\"; c.stroke(); }\n      // Hinge rivet.\n      c.beginPath(); c.arc(0, 0, 5.2, 0, TAU); c.fillStyle = cylinder(c, 10.4, [[0, \"#5c6068\"], [0.35, \"#f4f5f7\"], [1, \"#565a61\"]]); c.fill(); c.lineWidth = 0.7; c.strokeStyle = \"rgba(0,0,0,0.6)\"; c.stroke();\n      c.beginPath(); c.arc(-1, -1, 1.8, 0, TAU); c.fillStyle = \"rgba(255,255,255,0.8)\"; c.fill();\n      // The sleeve: screwed up over the nose when locked, wound down when open.\n      const top = -L - 10 + (1 - sleeve) * 37, len = 68, w = 31;\n      c.save(); c.translate(0, top);\n      c.shadowColor = \"rgba(0,10,20,0.45)\"; c.shadowBlur = 4 * k; c.shadowOffsetX = 2 * k; c.shadowOffsetY = 2.5 * k;\n      c.beginPath(); c.moveTo(-w / 2 + 3, 0); c.lineTo(w / 2 - 3, 0); c.quadraticCurveTo(w / 2, 0, w / 2, 3.5); c.lineTo(w / 2, len - 3.5); c.quadraticCurveTo(w / 2, len, w / 2 - 3, len); c.lineTo(-w / 2 + 3, len); c.quadraticCurveTo(-w / 2, len, -w / 2, len - 3.5); c.lineTo(-w / 2, 3.5); c.quadraticCurveTo(-w / 2, 0, -w / 2 + 3, 0); c.closePath();\n      c.fillStyle = cylinder(c, w, [[0, BODY[0]], [0.12, BODY[1]], [0.32, BODY[2]], [0.52, BODY[1]], [0.86, BODY[0]], [1, \"#041a2b\"]]); c.fill();\n      c.shadowColor = \"transparent\"; c.lineWidth = 0.7; c.strokeStyle = \"rgba(2,16,28,0.7)\"; c.stroke();\n      c.clip();\n      // Grip ribs travel across the sleeve as it turns.\n      const turn = sleeve * TAU * 1.75;\n      for (let i = 0; i < 26; i++) {\n        const a = ((((i / 26) * TAU + turn) % TAU) + TAU) % TAU;\n        if (a > Math.PI) continue;\n        const x = -Math.cos(a) * (w / 2), face = Math.sin(a);\n        c.beginPath(); c.moveTo(x, 13); c.lineTo(x, len - 13); c.lineWidth = 0.5 + face * 1.3; c.strokeStyle = `rgba(2,18,32,${0.5 + 0.2 * (1 - face)})`; c.stroke();\n        c.beginPath(); c.moveTo(x - 1.2 * face, 13); c.lineTo(x - 1.2 * face, len - 13); c.lineWidth = 0.6; c.strokeStyle = `rgba(200,240,255,${0.5 * face})`; c.stroke();\n      }\n      for (const y of [8.5, len - 8.5]) { c.fillStyle = \"rgba(2,16,28,0.55)\"; c.fillRect(-w / 2, y - 0.6, w, 1.2); c.fillStyle = \"rgba(200,240,255,0.4)\"; c.fillRect(-w / 2, y + 0.6, w, 0.7); }\n      const lipTop = c.createLinearGradient(0, 0, 0, 5); lipTop.addColorStop(0, \"rgba(255,255,255,0.55)\"); lipTop.addColorStop(1, \"rgba(255,255,255,0)\");\n      c.fillStyle = lipTop; c.fillRect(-w / 2, 0, w, 5);\n      c.restore();\n      c.restore();\n    };\n\n    // The rope is a curve walked at even steps, so each stitch of the braid is the same length.\n    type Stitch = { x: number; y: number; nx: number; ny: number };\n    const ropePoints = (clip: number, now: number) => {\n      const breathe = reduce ? 0 : Math.sin(now / 1700) * 2.2;\n      const p0 = { x: -345, y: 640 }, p3 = { x: mix(-140, 96, clip), y: -650 }, mid = mix(-146, 12, clip) + breathe;\n      const cx = (8 * mid - p0.x - p3.x) / 6, p1 = { x: cx, y: 215 }, p2 = { x: cx, y: -300 };\n      const at = (t: number) => { const u = 1 - t; return { x: u * u * u * p0.x + 3 * u * u * t * p1.x + 3 * u * t * t * p2.x + t * t * t * p3.x, y: u * u * u * p0.y + 3 * u * u * t * p1.y + 3 * u * t * t * p2.y + t * t * t * p3.y }; };\n      const pts: Stitch[] = [];\n      let prev = at(0), carry = 0, half = 0;\n      for (let i = 1; i <= 160; i++) {\n        const next = at(i / 160), dx = next.x - prev.x, dy = next.y - prev.y, d = Math.hypot(dx, dy) || 1;\n        for (let s = carry; s < d; s += 3.6) pts.push({ x: prev.x + (dx * s) / d, y: prev.y + (dy * s) / d, nx: -dy / d, ny: dx / d });\n        carry = (carry - d) % 3.6; if (carry < 0) carry += 3.6;\n        if (i === 80) half = pts.length;\n        prev = next;\n      }\n      return { pts, half };\n    };\n    const drawRope = (pts: Stitch[], from: number, to: number, lift: number) => {\n      const line = (off: number) => { ctx.beginPath(); for (let i = from; i <= to; i++) { const q = pts[i]; ctx.lineTo(q.x + q.nx * off, q.y + q.ny * off); } };\n      ctx.lineJoin = \"round\"; ctx.lineCap = \"butt\";\n      ctx.save(); ctx.shadowColor = \"rgba(38,42,54,0.4)\"; ctx.shadowBlur = (9 + lift * 10) * k; ctx.shadowOffsetX = (6 + lift * 8) * k; ctx.shadowOffsetY = (9 + lift * 12) * k;\n      line(0); ctx.lineWidth = 22; ctx.strokeStyle = ROPE[0]; ctx.stroke(); ctx.restore();\n      line(-0.6); ctx.lineWidth = 19.5; ctx.strokeStyle = ROPE[1]; ctx.stroke();\n      // The braid: every stitch is a pair of short strands leaning into the middle, like a herringbone.\n      ctx.lineCap = \"round\"; ctx.lineWidth = 2.5;\n      const stitch = (side: number, pick: (i: number) => string | null) => {\n        let ink = \"\";\n        for (let i = from; i <= to; i++) {\n          const col = pick(i);\n          if (!col) continue;\n          if (col !== ink) { if (ink) ctx.stroke(); ctx.beginPath(); ctx.strokeStyle = col; ink = col; }\n          const q = pts[i], tx = q.ny, ty = -q.nx;\n          ctx.moveTo(q.x + q.nx * side * 0.6 + tx * 2.6, q.y + q.ny * side * 0.6 + ty * 2.6);\n          ctx.lineTo(q.x + q.nx * side * 8.6 - tx * 2.6, q.y + q.ny * side * 8.6 - ty * 2.6);\n        }\n        if (ink) ctx.stroke();\n      };\n      for (const side of [1, -1]) {\n        stitch(side, (i) => (i % 2 ? \"rgba(120,36,0,0.42)\" : null));\n        stitch(side, (i) => ((i + (side > 0 ? 0 : 6)) % 12 < 2 ? ROPE[2] : null));\n        stitch(side, (i) => ((i + (side > 0 ? 6 : 0)) % 24 === 4 ? ROPE[3] : null));\n      }\n      ctx.lineCap = \"butt\";\n      line(9); ctx.lineWidth = 4.5; ctx.strokeStyle = \"rgba(70,18,0,0.38)\"; ctx.stroke();\n      line(5.5); ctx.lineWidth = 5; ctx.strokeStyle = \"rgba(70,18,0,0.14)\"; ctx.stroke();\n      line(-5.4); ctx.lineWidth = 4.4; ctx.strokeStyle = \"rgba(255,240,214,0.26)\"; ctx.stroke();\n      line(-9.6); ctx.lineWidth = 1.6; ctx.strokeStyle = \"rgba(90,26,0,0.3)\"; ctx.stroke();\n    };\n\n    const draw = (now: number) => {\n      const clip = ease(seg(S.p, 0, 0.56)), sleeve = ease(seg(S.p, 0.62, 1));\n      const lift = Math.sin(seg(S.p, 0.04, 0.56) * Math.PI);\n      // Carabiner and gate are composed off screen so one soft sheen and one shadow cover both.\n      const o = obj.getContext(\"2d\");\n      if (!o) return;\n      o.setTransform(1, 0, 0, 1, 0, 0); o.globalCompositeOperation = \"source-over\"; o.clearRect(0, 0, obj.width, obj.height);\n      o.drawImage(body, 0, 0);\n      onLayer(o); drawGate(o, S.gate, sleeve);\n      o.globalCompositeOperation = \"source-atop\";\n      const sx = mix(-150, 170, S.sheen), sh = o.createLinearGradient(sx - 60, -120, sx + 60, 40);\n      sh.addColorStop(0, \"rgba(255,255,255,0)\"); sh.addColorStop(0.5, \"rgba(255,255,255,0.3)\"); sh.addColorStop(1, \"rgba(255,255,255,0)\");\n      o.fillStyle = sh; o.fillRect(-260, -260, 520, 520);\n\n      ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0, 0, cv.width, cv.height);\n      place(ctx, ox, oy);\n      const { pts, half } = ropePoints(clip, now);\n      drawRope(pts, 0, half, lift * 0.4);\n      ctx.save(); ctx.setTransform(1, 0, 0, 1, 0, 0);\n      const lx = Math.round(ox - (BW / 2) * k), ly = Math.round(oy - (BH / 2) * k);\n      ctx.shadowColor = \"rgba(38,42,54,0.3)\"; ctx.shadowBlur = 20 * k; ctx.shadowOffsetX = 11 * k; ctx.shadowOffsetY = 16 * k; ctx.drawImage(obj, lx, ly);\n      ctx.shadowColor = \"rgba(30,34,44,0.5)\"; ctx.shadowBlur = 3.5 * k; ctx.shadowOffsetX = 2 * k; ctx.shadowOffsetY = 3 * k; ctx.drawImage(obj, lx, ly);\n      ctx.restore(); place(ctx, ox, oy);\n      drawRope(pts, half - 1, pts.length - 1, lift);\n      drawRope(pts, 32, 64, lift);\n    };\n\n    let raf = 0, last = performance.now(), seen = true;\n    const tick = (now: number) => {\n      raf = 0;\n      const dt = Math.min(0.033, (now - last) / 1000); last = now;\n      if (S.auto) {\n        const d = ((now - S.born) / 1000) % 9.5, should = d < 4 || d > 7.2;\n        if (should !== want.current) api.current?.hands();\n        S.sheenTo = 0.5 + Math.sin(now / 2100) * 0.42;\n      }\n      const goal = want.current ? 1 : 0;\n      S.p = goal > S.p ? Math.min(goal, S.p + dt / TRAVEL) : Math.max(goal, S.p - dt / TRAVEL);\n      // The rope shoulders the gate aside on its way through; a spring slams it back.\n      const c = seg(S.p, 0, 0.56), push = ease(seg(c, 0.2, 0.44)) * (1 - seg(c, 0.66, 0.7)) * 0.4;\n      for (let i = 0; i < 3; i++) { const h = dt / 3; S.gateV += ((push - S.gate) * 900 - S.gateV * (push > S.gate ? 60 : 13)) * h; S.gate += S.gateV * h; if (S.gate < 0) { S.gate = 0; S.gateV *= -0.38; } }\n      S.sheen += (S.sheenTo - S.sheen) * Math.min(1, dt * 4);\n      draw(now);\n      if (seen && !document.hidden) raf = requestAnimationFrame(tick);\n    };\n    const wake = () => { if (!reduce && !raf && seen && !document.hidden) { last = performance.now(); raf = requestAnimationFrame(tick); } };\n    const still = () => { S.p = want.current ? 1 : 0; S.gate = 0; draw(performance.now()); };\n    api.current = {\n      hands: () => { want.current = !want.current; setLocked(want.current); if (reduce) still(); else wake(); },\n      go: () => { S.auto = false; api.current?.hands(); },\n    };\n\n    const onMove = (e: PointerEvent) => { const r = cv.getBoundingClientRect(); S.sheenTo = Math.min(1, Math.max(0, (e.clientX - r.left) / r.width)); };\n    const size = () => {\n      const w = cv.clientWidth, h = cv.clientHeight;\n      if (!w || !h) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1), fit = Math.min(SIZE, w / 400, h / 640);\n      k = fit * dpr; ox = (w / 2 + 12 * fit) * dpr; oy = h * 0.47 * dpr;\n      cv.width = Math.round(w * dpr); cv.height = Math.round(h * dpr);\n      paintBody();\n      if (reduce) still(); else { draw(performance.now()); wake(); }\n    };\n    const ro = new ResizeObserver(size); ro.observe(cv);\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    box.addEventListener(\"pointermove\", onMove);\n    size();\n    return () => {\n      cancelAnimationFrame(raf); ro.disconnect(); io.disconnect();\n      document.removeEventListener(\"visibilitychange\", onVis); box.removeEventListener(\"pointermove\", onMove);\n      api.current = null;\n    };\n  }, []);\n\n  return (\n    <div ref={stage} className=\"relative min-h-[760px] w-full overflow-hidden bg-[#e3e4e1] bg-[radial-gradient(75%_60%_at_42%_38%,#f6f6f3_0%,#e6e7e4_55%,#cfd1ce_100%)]\">\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={locked}\n        aria-label={`${LABEL}: carabiner locked`}\n        onClick={() => api.current?.go()}\n        className=\"absolute left-1/2 top-[47%] h-[66%] w-[min(84%,400px)] -translate-x-1/2 -translate-y-1/2 cursor-pointer rounded-[48px] outline-none focus-visible:ring-2 focus-visible:ring-sky-700\"\n      />\n      <p aria-live=\"polite\" className=\"absolute inset-x-0 bottom-7 mx-auto w-fit max-w-[92%] rounded-full bg-[#e3e4e1]/85 px-4 py-1.5 text-center font-mono text-[11px] uppercase leading-5 tracking-[0.18em] text-slate-600\">\n        <span className=\"font-semibold text-slate-900\">{LABEL}</span>\n        <span className=\"mx-2 text-slate-400\">/</span>\n        {locked ? \"Rope clipped, gate locked\" : \"Open. Press to clip in and lock\"}\n      </p>\n    </div>\n  );\n}\n"}]}
