{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"enamel-lever-billing-switch-in-webgl-on-sunlit-terrazzo-that-throws-over-centre-between-monthly-and-yearly","type":"registry:component","title":"Enamel Lever Billing Switch in WebGL on Sunlit Terrazzo That Throws Over Centre Between Monthly and Yearly","description":"Free React billing toggle: a red enamel lever on a cream plate, ray-marched in WebGL on sunlit terrazzo, that snaps over centre between monthly and yearly.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/enamel-lever-billing-switch-in-webgl-on-sunlit-terrazzo-that-throws-over-centre-between-monthly-and-yearly.tsx","type":"registry:component","content":"import { useEffect, useRef, useState, type KeyboardEvent, type PointerEvent as RPointerEvent } from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"framer-motion\";\n\n// Example plan. These prices are placeholders for the demo: put your own in.\nconst plan = {\n  name: \"Studio plan\",\n  monthly: { price: \"$18\", per: \"per month\", note: \"Billed each month. Stop whenever you like.\" },\n  yearly: { price: \"$180\", per: \"per year\", note: \"Billed once a year. Twelve months for the price of ten.\" },\n};\nconst LABELS = [\"MONTHLY\", \"YEARLY\"] as const;\nconst FONT = '\"Inter Tight\", \"Inter\", ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", sans-serif';\nconst SERIF = '\"Fraunces\", \"Iowan Old Style\", \"Palatino Linotype\", Palatino, Georgia, serif';\nconst FLOOR = \"#ece6dc\"; // terrazzo binder as a screen colour, for the frame and the still picture\nconst STOP = 0.42; // lever travel either side of upright, in radians\n\nconst VERT = `attribute vec2 a;void main(){gl_Position=vec4(a,0.,1.);}`;\nconst FRAG = `precision highp float;\nuniform vec2 R;uniform float A;uniform float S;uniform vec2 J;uniform vec2 LJ;uniform float W;uniform vec2 C;uniform vec2 O;uniform float F;uniform sampler2D T;\nmat2 rot(float a){float c=cos(a),s=sin(a);return mat2(c,-s,s,c);}\nfloat box(vec3 p,vec3 b,float r){vec3 q=abs(p)-b+r;return length(max(q,0.))+min(max(q.x,max(q.y,q.z)),0.)-r;}\nfloat smin(float a,float b,float k){float h=clamp(.5+.5*(b-a)/k,0.,1.);return mix(b,a,h)-k*h*(1.-h);}\nvec2 map(vec3 p){\n  vec2 r=vec2(p.y,0.);\n  vec3 q=p;q.y+=S;\n  float base=box(q-vec3(0.,.2,0.),vec3(1.5,.2,.62),.17);\n  float slot=box(q-vec3(0.,.42,-.12),vec3(.66,.2,.13),.1);\n  base=max(base,-slot);\n  if(base<r.x)r=vec2(base,1.);\n  vec3 s=q;s.x=abs(s.x);\n  float scr=length(s-vec3(1.25,.33,-.12))-.09;\n  if(scr<r.x)r=vec2(scr,3.);\n  vec3 l=q-vec3(0.,.26,-.12);\n  float bar=max(length(l.xy)-.15,abs(l.z)-.17);\n  if(bar<r.x)r=vec2(bar,3.);\n  l.xy=rot(-A)*l.xy;\n  float stem=box(l-vec3(0.,.5,0.),vec3(.092,.42,.092),.07);\n  float hd=box(l-vec3(0.,1.02,0.),vec3(.25,.19,.31),.17);\n  float lev=smin(stem,hd,.12);\n  if(lev<r.x)r=vec2(lev,2.);\n  return r;\n}\nvec3 nrm(vec3 p){vec2 e=vec2(.0012,-.0012);return normalize(e.xyy*map(p+e.xyy).x+e.yyx*map(p+e.yyx).x+e.yxy*map(p+e.yxy).x+e.xxx*map(p+e.xxx).x);}\nvec2 march(vec3 ro,vec3 rd,float tmax,int n){float t=.02;for(int i=0;i<110;i++){if(i>=n)break;vec2 h=map(ro+rd*t);if(h.x<.0006*t)return vec2(t,h.y);t+=h.x;if(t>tmax)break;}return vec2(-1.);}\nfloat shadow(vec3 ro,vec3 rd){float res=1.,t=.015;for(int i=0;i<40;i++){float h=map(ro+rd*t).x;res=min(res,16.*h/t);t+=clamp(h,.012,.2);if(res<.002||t>5.)break;}return clamp(res,0.,1.);}\nfloat ao(vec3 p,vec3 n){float o=0.,s=1.;for(int i=1;i<6;i++){float h=.03*float(i)*float(i)*.5+.01;o+=(h-map(p+n*h).x)*s;s*=.72;}return clamp(1.-1.6*o,0.,1.);}\nvec3 h3(vec2 p){vec3 q=vec3(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3)),dot(p,vec2(419.2,371.9)));return fract(sin(q)*43758.5453);}\nfloat vn(vec2 p){vec2 i=floor(p),f=fract(p);f=f*f*(3.-2.*f);return mix(mix(h3(i).x,h3(i+vec2(1,0)).x,f.x),mix(h3(i+vec2(0,1)).x,h3(i+vec2(1,1)).x,f.x),f.y);}\nvec3 pal(float k){\n  if(k<.16)return vec3(.62,.29,.19);\n  if(k<.27)return vec3(.55,.6,.5);\n  if(k<.36)return vec3(.33,.34,.36);\n  if(k<.46)return vec3(.82,.63,.36);\n  if(k<.56)return vec3(.84,.64,.58);\n  if(k<.7)return vec3(.78,.76,.72);\n  return vec3(.99,.975,.95);\n}\nvec3 chips(vec2 x,float sc,float sz,inout float m){\n  vec2 n=floor(x*sc),f=fract(x*sc);float md=9.;vec3 id=vec3(0.);\n  for(int j=-1;j<=1;j++)for(int i=-1;i<=1;i++){vec2 g=vec2(i,j);vec3 o=h3(n+g);vec2 r=g+.15+o.xy*.7-f;r=rot(o.z*6.283)*r;\n    float d=max(abs(r.x)*1.15+abs(r.y)*.35,abs(r.y)*.95+abs(r.x)*.25)/(sz*(.55+.7*o.y));if(d<md){md=d;id=o;}}\n  float e=.5+.18*(vn(x*sc*5.)-.5);\n  m=1.-smoothstep(e-.04,e+.04,md);\n  return pal(fract(id.z*7.13));\n}\nvec3 terr(vec2 p,out float ch){\n  vec3 col=vec3(.96,.925,.87)*(.97+.06*vn(p*14.));\n  float m;vec3 c=chips(p,2.6,.36,m);col=mix(col,c,m);ch=m;\n  c=chips(p+17.3,6.,.32,m);col=mix(col,c,m*.95);ch=max(ch,m);\n  c=chips(p-9.1,15.,.3,m);col=mix(col,c*.95,m*.9);\n  return col;\n}\nvec3 LD=normalize(vec3(-.62,.7,-.36));\nvec3 env(vec3 d){\n  vec3 sky=mix(vec3(1.,.97,.92),vec3(.62,.75,.95),clamp(d.y*1.4,0.,1.));\n  vec3 c=sky*1.15;\n  vec3 w=d-LD*dot(d,LD);float a=dot(d,LD);\n  vec2 wq=vec2(dot(w,normalize(vec3(.5,0.,-.86))),w.y);\n  float win=step(.8,a)*(1.-smoothstep(.22,.26,abs(wq.x)))*(1.-smoothstep(.3,.34,abs(wq.y+.02)));\n  win*=1.-(1.-smoothstep(.006,.014,abs(wq.x)))*.9;\n  c+=win*vec3(6.,5.6,5.)*1.4;\n  c=mix(c,vec3(.32,.30,.28),smoothstep(.1,.7,d.x)*smoothstep(.0,.4,d.y)*.55);\n  if(d.y<0.)c=mix(vec3(.8,.76,.7),vec3(.55,.5,.45),clamp(-d.y*2.,0.,1.));\n  return c;\n}\nfloat gobo(vec3 p){\n  vec3 w=p-LD*(p.y/LD.y);\n  vec2 g=rot(.18)*w.xz;\n  float pen=.05+.05*p.y;\n  float open=smoothstep(-pen,pen,g.x+2.15)*smoothstep(-pen,pen,3.8-g.x)*smoothstep(-pen,pen,g.y+2.9)*smoothstep(-pen,pen,2.6-g.y);\n  float bar=smoothstep(.07-pen,.07+pen,abs(g.x-2.05))*smoothstep(.06-pen,.06+pen,abs(g.y-.9));\n  float leaf=smoothstep(.45,.7,vn(g*1.3+3.)*.6+vn(g*3.7)*.4);\n  return open*bar*mix(1.,.45,leaf*smoothstep(.5,2.5,g.x));\n}\nvec3 shade(vec3 p,vec3 rd,float m,vec3 n,bool deep){\n  float sh=shadow(p+n*.004,LD)*gobo(p);\n  float oc=ao(p,n);\n  float dif=clamp(dot(n,LD),0.,1.);\n  vec3 sun=vec3(1.,.9,.74)*3.5;\n  vec3 amb=mix(vec3(.78,.68,.58),vec3(.86,.84,.82),n.y*.5+.5)*.72;\n  vec3 hlf=normalize(LD-rd);\n  float fr=pow(1.-clamp(dot(n,-rd),0.,1.),5.);\n  float ink=0.;vec3 alb;float gloss;float f0;vec3 refl=env(reflect(rd,n));\n  if(m<.5){\n    float ch;alb=terr(p.xz*1.25,ch);\n    gloss=.35+.4*ch;f0=.03;\n    vec3 col=alb*(amb*oc+sun*dif*sh);\n    col+=refl*(f0+.6*fr)*gloss*oc*.6;\n    col+=sun*pow(clamp(dot(n,hlf),0.,1.),220.)*sh*gloss*1.2;\n    return col;\n  }\n  if(m<1.5){\n    alb=vec3(.84,.78,.66);\n    vec3 sp=p;sp.y+=S;\n    if(sp.y>.365&&n.y>.85){vec2 uv=vec2((sp.x+1.5)/3.,(sp.z+.62)/1.24);vec4 tx=texture2D(T,uv);ink=tx.a;alb*=mix(.74,1.06,smoothstep(-.6,.6,sp.z))*mix(.94,1.,smoothstep(-1.5,1.5,-sp.x));}\n    if(sp.y<.33&&abs(sp.x)<.8&&abs(sp.z+.12)<.3)alb*=.3;\n  } else if(m<2.5){alb=vec3(.62,.06,.028);}\n  else {\n    vec3 br=vec3(.98,.76,.42);\n    vec3 col=br*refl*(.55+.45*fr)*oc+br*sun*pow(clamp(dot(n,hlf),0.,1.),60.)*sh*.9+br*amb*.12*oc;\n    return col;\n  }\n  float wr=clamp((dot(n,LD)+.22)/1.22,0.,1.);\n  vec3 sss=m>1.5?vec3(.9,.25,.12):vec3(.95,.85,.7);\n  vec3 col=alb*(amb*oc+sun*wr*sh*.92)+sss*alb*.08*(1.-dif)*oc;\n  float F0=.045,fre=F0+(1.-F0)*fr;\n  vec3 rc=refl;float hk=0.;\n  if(deep){vec3 rr=reflect(rd,n);vec2 h=march(p+n*.01,rr,4.,48);if(h.x>0.){vec3 q=p+n*.01+rr*h.x;vec3 qn=nrm(q);\n      vec3 qa=h.y<.5?vec3(.9,.87,.82):h.y<1.5?vec3(.94,.9,.83):h.y<2.5?vec3(.8,.2,.12):vec3(.9,.7,.4);\n      rc=qa*(amb+sun*clamp(dot(qn,LD),0.,1.)*.8);hk=1.;}}\n  float rk=m<1.5?fre*1.05+.07*step(.85,n.y):fre*1.05;\n  col+=rc*rk*oc;\n  col+=sun*pow(clamp(dot(n,hlf),0.,1.),900.)*sh*7.;\n  col=mix(col,rc*.85,hk*.5*step(.85,n.y)*(m<1.5?1.:0.));\n  if(m<1.5){col+=sun*pow(clamp(dot(n,hlf),0.,1.),40.)*sh*.12;\n    col+=vec3(1.,.96,.88)*smoothstep(.25,.85,n.z)*smoothstep(.15,.6,n.y)*3.2*(.45+.55*sh)*pow(clamp(1.-abs(n.x),0.,1.),2.);\n    if(n.y>.85){vec3 sp2=p;sp2.y+=S;col*=mix(.62,1.,smoothstep(-.62,.5,sp2.z))*mix(.86,1.,smoothstep(1.6,-1.2,sp2.x));}\n    col=mix(col,vec3(.03,.055,.17)*(amb*oc*.8+sun*wr*sh*.22)+rc*fre*.5,ink);}\n  return col;\n}\nvec3 aces(vec3 x){return clamp((x*(2.51*x+.03))/(x*(2.43*x+.59)+.14),0.,1.);}\nvoid main(){\n  vec2 fc=gl_FragCoord.xy+J;\n  vec2 uv=(fc-.5*R)/R.y-O;\n  vec3 ro=vec3(C.x*.9,3.25+C.y*.35,5.1);vec3 ta=vec3(0.,.45,0.);\n  vec3 fw=normalize(ta-ro),rt=normalize(cross(fw,vec3(0,1,0))),up=cross(rt,fw);\n  vec3 rd=normalize(uv.x*rt+uv.y*up+F*fw);\n  float fd=length(ta-ro);vec3 fp=ro+rd*fd/dot(rd,fw);\n  ro+=(rt*LJ.x+up*LJ.y);rd=normalize(fp-ro);\n  vec3 col;\n  float tg=-ro.y/rd.y;\n  vec2 h=march(ro,rd,min(tg>0.?tg+.01:30.,30.),110);\n  if(h.x<0.&&tg>0.){vec3 p=ro+rd*tg;col=shade(p,rd,0.,vec3(0,1,0),false);float fog=1.-exp(-max(tg-6.,0.)*.08);col=mix(col,vec3(.93,.9,.85)*1.3,fog);}\n  else if(h.x>0.){vec3 p=ro+rd*h.x;vec3 n=h.y<.5?vec3(0,1,0):nrm(p);col=shade(p,rd,h.y,n,h.y>.5&&h.y<2.5);}\n  else col=env(rd);\n  col=aces(col*.74);\n  col=pow(col,vec3(1./2.2));\n  vec2 v=gl_FragCoord.xy/R-.5;col*=1.-.18*dot(v,v);\n  col+=(h3(gl_FragCoord.xy+W*13.).x-.5)*.018;\n  gl_FragColor=vec4(col,1./max(W,1.));\n}`;\n\ntype Cam = { cx: number; cy: number; ox: number; oy: number; f: number };\n// Same camera as the shader, so the real switch button sits exactly on the drawn one.\nfunction project(w: number, h: number, cam: Cam, p: [number, number, number]) {\n  const ro = [cam.cx * 0.9, 3.25 + cam.cy * 0.35, 5.1];\n  const ta = [0, 0.45, 0];\n  const fw = norm([ta[0] - ro[0], ta[1] - ro[1], ta[2] - ro[2]]);\n  const rt = norm([-fw[2], 0, fw[0]]);\n  const up = [rt[1] * fw[2] - rt[2] * fw[1], rt[2] * fw[0] - rt[0] * fw[2], rt[0] * fw[1] - rt[1] * fw[0]];\n  const d = [p[0] - ro[0], p[1] - ro[1], p[2] - ro[2]];\n  const z = dot(d, fw);\n  const ux = (dot(d, rt) / z) * cam.f + cam.ox;\n  const uy = (dot(d, up) / z) * cam.f + cam.oy;\n  return [w / 2 + ux * h, h / 2 - uy * h];\n}\nfunction dot(a: number[], b: number[]) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; }\nfunction norm(a: number[]) { const l = Math.hypot(a[0], a[1], a[2]) || 1; return [a[0] / l, a[1] / l, a[2] / l]; }\nfunction camFor(w: number, h: number): Cam {\n  const wide = w >= 768;\n  const asp = w / Math.max(1, h);\n  return { cx: 0, cy: 0, ox: wide ? 0.33 : 0, oy: wide ? -0.05 : -0.06, f: wide ? 2.0 : 2.0 * Math.min(1, asp / 1.38) };\n}\n\n// The printing on the enamel plate, drawn once per change into a small texture.\nfunction paintPlate(cv: HTMLCanvasElement, sel: number) {\n  const g = cv.getContext(\"2d\");\n  if (!g) return;\n  const W = cv.width, H = cv.height;\n  g.clearRect(0, 0, W, H);\n  g.textAlign = \"center\";\n  g.textBaseline = \"middle\";\n  const y = H * 0.75;\n  LABELS.forEach((t, i) => {\n    const x = W * (i === 0 ? 0.3 : 0.7);\n    g.globalAlpha = i === sel ? 1 : 0.6;\n    g.font = `800 58px ${FONT}`;\n    if (\"letterSpacing\" in g) (g as unknown as { letterSpacing: string }).letterSpacing = \"7px\";\n    g.fillStyle = \"#fff\";\n    g.fillText(t, x, y);\n  });\n  g.globalAlpha = 0.7;\n  g.fillStyle = \"#fff\";\n  for (let k = -6; k <= 6; k++) {\n    const x = W / 2 + k * 24;\n    g.fillRect(x - 2, H * 0.53, 4, k % 3 === 0 ? 26 : 14);\n  }\n  g.globalAlpha = 1;\n}\n\nfunction Still({ box, yearly }: { box: { l: number; t: number; w: number; h: number }; yearly: boolean }) {\n  const dots = Array.from({ length: 140 }, (_, i) => {\n    const r = Math.sin(i * 91.7) * 43758.5453, s = Math.sin(i * 12.3) * 9631.7, u = Math.sin(i * 47.1) * 3711.3;\n    const fr = (v: number) => v - Math.floor(v);\n    return { x: fr(r) * 100, y: fr(s) * 100, z: 4 + fr(u) * 14, c: [\"#a84d33\", \"#80947a\", \"#4d545e\", \"#d6a04c\", \"#d99a8a\", \"#f7f3ec\"][i % 6], a: fr(r + s) * 180 };\n  });\n  return (\n    <div aria-hidden=\"true\" className=\"absolute inset-0 overflow-hidden\" style={{ background: FLOOR }}>\n      {dots.map((d, i) => (\n        <span key={i} className=\"absolute\" style={{ left: `${d.x}%`, top: `${d.y}%`, width: d.z, height: d.z * 0.7, background: d.c, borderRadius: \"38% 52% 30% 60%\", transform: `rotate(${d.a}deg)`, opacity: 0.9 }} />\n      ))}\n      <div className=\"absolute\" style={{ left: box.l + box.w * 0.06, top: box.t + box.h * 0.5, width: box.w * 0.96, height: box.h * 0.46, borderRadius: 40, background: \"rgba(70,52,36,0.35)\", filter: \"blur(18px)\" }} />\n      <div className=\"absolute\" style={{ left: box.l, top: box.t + box.h * 0.38, width: box.w, height: box.h * 0.5, borderRadius: 34, background: \"linear-gradient(180deg,#fbf6ec 0%,#efe7d6 55%,#d9cdb6 100%)\", boxShadow: \"inset 0 2px 0 #fff, inset 0 -10px 18px rgba(120,96,60,0.25)\" }}>\n        <div className=\"absolute left-1/2 top-[26%] h-[20%] w-[44%] -translate-x-1/2 rounded-full\" style={{ background: \"#2a2018\", boxShadow: \"inset 0 6px 10px #000\" }} />\n        <div className=\"absolute inset-x-0 bottom-[16%] flex justify-around text-[13px] font-extrabold tracking-[0.3em] text-[#17213f]\" style={{ fontFamily: FONT }}>\n          <span style={{ opacity: yearly ? 0.6 : 1 }}>MONTHLY</span><span style={{ opacity: yearly ? 1 : 0.6 }}>YEARLY</span>\n        </div>\n      </div>\n      <div className=\"absolute\" style={{ left: box.l + box.w / 2 - box.w * 0.09, top: box.t + box.h * 0.066, width: box.w * 0.18, height: box.h * 0.52, transformOrigin: \"50% 95%\", transform: `rotate(${yearly ? 22 : -22}deg)` }}>\n        <div className=\"mx-auto h-[62%] w-[34%] rounded-full\" style={{ marginTop: \"38%\", background: \"linear-gradient(90deg,#8f1f12,#d2462c 45%,#9a2615)\" }} />\n        <div className=\"absolute inset-x-0 top-0 h-[44%] rounded-[40%]\" style={{ background: \"radial-gradient(circle at 35% 30%,#ff9a7a 0%,#d4412a 32%,#8e1c10 100%)\", boxShadow: \"0 10px 18px rgba(80,20,10,0.35)\" }} />\n      </div>\n    </div>\n  );\n}\n\nexport default function EnamelLeverBillingSwitch() {\n  const reduceMotion = useReducedMotion();\n  const [yearly, setYearly] = useState(true);\n  const [shown, setShown] = useState<\"wait\" | \"gl\" | \"still\">(\"wait\");\n  const [box, setBox] = useState({ l: 0, t: 0, w: 0, h: 0 });\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const api = useRef<{ set: (y: boolean, user: boolean) => void; drag: (dx: number | null) => boolean; touch: () => void } | null>(null);\n  const yearlyRef = useRef(true);\n  const dragRef = useRef<{ x: number; moved: boolean } | null>(null);\n\n  const choose = (y: boolean, user = true) => {\n    yearlyRef.current = y;\n    setYearly(y);\n    api.current?.set(y, user);\n  };\n\n  useEffect(() => {\n    const st = stage.current, cv = canvas.current;\n    if (!st || !cv) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const s = {\n      a: STOP, v: 0, side: 1, load: 0, shake: 0, sv: 0, touched: false, dragging: false,\n      raf: 0, last: 0, t0: 0, demoAt: 0, alive: true, visible: true, count: 0, fresh: true,\n      px: 0, py: 0, sx: 0, sy: 0, fpsN: 0, fpsT: 0, slow: 0,\n    };\n    let cam = camFor(st.clientWidth, st.clientHeight);\n    const layout = () => {\n      const w = st.clientWidth, h = st.clientHeight;\n      cam = camFor(w, h);\n      const pts: [number, number, number][] = [[-1.6, 0, -0.7], [1.6, 0, -0.7], [-1.6, 0, 0.7], [1.6, 0, 0.7], [0, 1.45, -0.1]];\n      const xy = pts.map((p) => project(w, h, cam, p));\n      const xs = xy.map((p) => p[0]), ys = xy.map((p) => p[1]);\n      const l = Math.min(...xs), t = Math.min(...ys);\n      setBox({ l, t, w: Math.max(...xs) - l, h: Math.max(...ys) - t });\n    };\n    layout();\n\n    const opts = { antialias: false, alpha: false, preserveDrawingBuffer: true, powerPreference: \"high-performance\" as const };\n    const gl = (cv.getContext(\"webgl\", opts) ?? cv.getContext(\"experimental-webgl\", opts)) as WebGLRenderingContext | null;\n    if (!gl) { setShown(\"still\"); const ro0 = new ResizeObserver(layout); ro0.observe(st); return () => ro0.disconnect(); }\n    const sh = (type: number, src: string) => { const o = gl.createShader(type)!; gl.shaderSource(o, src); gl.compileShader(o); return gl.getShaderParameter(o, gl.COMPILE_STATUS) ? o : null; };\n    const vs = sh(gl.VERTEX_SHADER, VERT), fs = sh(gl.FRAGMENT_SHADER, FRAG);\n    const pr = gl.createProgram()!;\n    if (!vs || !fs) { setShown(\"still\"); return; }\n    gl.attachShader(pr, vs); gl.attachShader(pr, fs); gl.linkProgram(pr);\n    if (!gl.getProgramParameter(pr, gl.LINK_STATUS)) { setShown(\"still\"); return; }\n    gl.useProgram(pr);\n    const buf = gl.createBuffer();\n    gl.bindBuffer(gl.ARRAY_BUFFER, buf);\n    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);\n    const loc = gl.getAttribLocation(pr, \"a\");\n    gl.enableVertexAttribArray(loc); gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);\n    const U = (n: string) => gl.getUniformLocation(pr, n);\n    const u = { R: U(\"R\"), A: U(\"A\"), S: U(\"S\"), J: U(\"J\"), LJ: U(\"LJ\"), W: U(\"W\"), C: U(\"C\"), O: U(\"O\"), F: U(\"F\"), T: U(\"T\") };\n\n    const tc = document.createElement(\"canvas\");\n    tc.width = 1024; tc.height = 424;\n    const tex = gl.createTexture();\n    const upload = (sel: number) => {\n      paintPlate(tc, sel);\n      gl.bindTexture(gl.TEXTURE_2D, tex);\n      gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);\n      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, tc);\n      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);\n      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n    };\n    upload(1);\n    gl.uniform1i(u.T, 0);\n    gl.enable(gl.BLEND);\n    gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);\n\n    let scale = 1;\n    const size = () => {\n      const w = st.clientWidth, h = st.clientHeight;\n      const dpr = Math.min(1.5, window.devicePixelRatio || 1) * scale;\n      const k = Math.min(1, Math.sqrt(1.2e6 / (w * h * dpr * dpr)));\n      cv.width = Math.max(2, Math.round(w * dpr * k)); cv.height = Math.max(2, Math.round(h * dpr * k));\n      gl.viewport(0, 0, cv.width, cv.height);\n      s.fresh = true;\n    };\n\n    const draw = (n: number) => {\n      const ang = n * 2.39996, rad = Math.sqrt(n % 24) / 4.9;\n      gl.uniform2f(u.R, cv.width, cv.height);\n      gl.uniform1f(u.A, s.a);\n      gl.uniform1f(u.S, s.shake);\n      gl.uniform2f(u.J, n > 1 ? Math.cos(ang * 3.1) * 0.5 : 0, n > 1 ? Math.sin(ang * 2.3) * 0.5 : 0);\n      gl.uniform2f(u.LJ, n > 1 ? Math.cos(ang) * rad * 0.05 : 0, n > 1 ? Math.sin(ang) * rad * 0.05 : 0);\n      gl.uniform1f(u.W, n);\n      gl.uniform2f(u.C, s.sx, s.sy);\n      gl.uniform2f(u.O, cam.ox, cam.oy);\n      gl.uniform1f(u.F, cam.f);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n    };\n\n    // Over-centre snap: a slow creep while the spring loads, then it lets go and slams into the stop.\n    const step = (dt: number) => {\n      if (s.dragging) return true;\n      let moving = false;\n      const T = s.side, target = T * STOP;\n      if (s.load > 0) {\n        s.load -= dt;\n        s.v += (-T * 2.2 - s.v) * Math.min(1, dt * 18);\n        moving = true;\n      } else if (Math.abs(s.a - target) > 1e-4 || Math.abs(s.v) > 1e-3) {\n        s.v += T * 46 * dt;\n        s.v *= Math.exp(-dt * 1.6);\n        moving = true;\n      }\n      s.a += s.v * dt;\n      if ((s.a - target) * T > 0) {\n        const hit = Math.abs(s.v);\n        s.a = target;\n        s.v = -s.v * 0.3;\n        if (hit > 0.8) s.sv += Math.min(hit, 9) * 0.012;\n        if (Math.abs(s.v) < 0.35) s.v = 0;\n      }\n      s.sv += (-s.shake * 900 - s.sv * 26) * dt;\n      s.shake += s.sv * dt;\n      if (Math.abs(s.shake) > 1e-5 || Math.abs(s.sv) > 1e-4) moving = true; else { s.shake = 0; s.sv = 0; }\n      return moving;\n    };\n\n    const frame = (now: number) => {\n      s.raf = 0;\n      if (!s.alive || !s.visible) return;\n      const dtRaw = s.last ? (now - s.last) / 1000 : 1 / 60;\n      const dt = Math.min(dtRaw, 1 / 30);\n      s.last = now;\n      if (!s.t0) { s.t0 = now; s.demoAt = now + 3400; }\n      if (!reduce && !s.touched && now > s.demoAt) {\n        const y = !yearlyRef.current;\n        yearlyRef.current = y; setYearly(y); set(y, false);\n        s.demoAt = now + (y ? 3600 : 2600);\n      }\n      let moving = reduce ? false : step(dt);\n      if (!reduce) {\n        const k = 1 - Math.exp(-dt * 3);\n        const dx = s.px - s.sx, dy = s.py - s.sy;\n        s.sx += dx * k; s.sy += dy * k;\n        if (Math.abs(dx) + Math.abs(dy) > 0.002) moving = true;\n      }\n      if (s.fresh) { draw(1); s.fresh = false; s.count = 1; }\n      else if (moving) { draw(1); s.count = 1; }\n      else if (s.count < 24) { s.count++; draw(s.count); }\n      if (shownRef !== \"gl\") { shownRef = \"gl\"; setShown(\"gl\"); }\n      if (moving && dtRaw > 0.028) s.slow++; else s.slow = Math.max(0, s.slow - 2);\n      if (s.slow > 30 && scale > 0.45) { scale *= 0.8; s.slow = 0; size(); }\n      if (moving) { s.fpsN++; s.fpsT += dtRaw; }\n      st.dataset.fps = s.fpsT > 0 ? (s.fpsN / s.fpsT).toFixed(1) : \"\";\n      if (moving || s.count < 24 || (!reduce && !s.touched)) s.raf = requestAnimationFrame(frame);\n    };\n    let shownRef = \"wait\";\n    const wake = () => { if (!s.raf && s.visible && s.alive) { s.last = 0; s.raf = requestAnimationFrame(frame); } };\n\n    const set = (y: boolean, user: boolean) => {\n      if (user) s.touched = true;\n      upload(y ? 1 : 0);\n      const T = y ? 1 : -1;\n      if (reduce) { s.side = T; s.a = T * STOP; s.v = 0; s.fresh = true; wake(); return; }\n      if (s.side !== T) { s.side = T; s.load = s.dragging ? 0 : 0.11; s.v = 0; }\n      s.dragging = false;\n      s.fresh = true;\n      wake();\n    };\n    const touch = () => { s.touched = true; };\n    const drag = (dx: number | null) => {\n      if (reduce) return false;\n      s.touched = true;\n      if (dx === null) return false;\n      s.dragging = true;\n      const base = s.side * STOP;\n      s.a = Math.max(-STOP, Math.min(STOP, base + dx / 140));\n      s.v = 0; s.fresh = true; wake();\n      return Math.sign(s.a) !== s.side && Math.abs(s.a) > 0.04;\n    };\n    api.current = { set, drag, touch };\n\n    size();\n    const ro = new ResizeObserver(() => { size(); layout(); wake(); });\n    ro.observe(st);\n    const io = new IntersectionObserver(([e]) => { s.visible = e.isIntersecting && !document.hidden; if (s.visible) wake(); }, { threshold: 0.05 });\n    io.observe(st);\n    const vis = () => { s.visible = !document.hidden; if (s.visible) wake(); };\n    document.addEventListener(\"visibilitychange\", vis);\n    const move = (e: PointerEvent) => {\n      if (reduce || e.pointerType !== \"mouse\") return;\n      const r = st.getBoundingClientRect();\n      s.px = ((e.clientX - r.left) / r.width) * 2 - 1; s.py = ((e.clientY - r.top) / r.height) * 2 - 1;\n      wake();\n    };\n    const leave = () => { s.px = 0; s.py = 0; wake(); };\n    st.addEventListener(\"pointermove\", move);\n    st.addEventListener(\"pointerleave\", leave);\n    const lost = (e: Event) => { e.preventDefault(); setShown(\"still\"); };\n    cv.addEventListener(\"webglcontextlost\", lost);\n    wake();\n    return () => {\n      s.alive = false;\n      cancelAnimationFrame(s.raf); ro.disconnect(); io.disconnect();\n      document.removeEventListener(\"visibilitychange\", vis);\n      st.removeEventListener(\"pointermove\", move); st.removeEventListener(\"pointerleave\", leave);\n      cv.removeEventListener(\"webglcontextlost\", lost);\n      api.current = null;\n    };\n  }, []);\n\n  const onKey = (e: KeyboardEvent) => {\n    if (e.key === \"ArrowRight\") { e.preventDefault(); if (!yearlyRef.current) choose(true); }\n    else if (e.key === \"ArrowLeft\") { e.preventDefault(); if (yearlyRef.current) choose(false); }\n  };\n  const down = (e: RPointerEvent<HTMLButtonElement>) => {\n    dragRef.current = { x: e.clientX, moved: false };\n    e.currentTarget.setPointerCapture(e.pointerId);\n  };\n  const moveP = (e: RPointerEvent<HTMLButtonElement>) => {\n    const d = dragRef.current;\n    if (!d) return;\n    const dx = e.clientX - d.x;\n    if (Math.abs(dx) > 6) d.moved = true;\n    if (d.moved) api.current?.drag(dx);\n  };\n  const up = (e: RPointerEvent<HTMLButtonElement>) => {\n    const d = dragRef.current;\n    dragRef.current = null;\n    if (!d || !d.moved) return;\n    e.preventDefault();\n    const dx = e.clientX - d.x;\n    const flip = (yearlyRef.current && dx < -24) || (!yearlyRef.current && dx > 24);\n    choose(flip ? !yearlyRef.current : yearlyRef.current);\n  };\n  const click = () => {\n    if (dragRef.current?.moved) return;\n    choose(!yearlyRef.current);\n  };\n  const p = yearly ? plan.yearly : plan.monthly;\n\n  return (\n    <section className=\"relative isolate overflow-hidden text-[#17213f]\" style={{ fontFamily: FONT, background: FLOOR }}>\n      <div ref={stage} className=\"relative h-[380px] w-full md:h-[640px]\">\n        {shown !== \"gl\" && <Still box={box} yearly={yearly} />}\n        <canvas ref={canvas} aria-hidden=\"true\" className={`absolute inset-0 h-full w-full transition-opacity duration-500 motion-reduce:transition-none ${shown === \"gl\" ? \"opacity-100\" : \"opacity-0\"}`} />\n        <p className=\"pointer-events-none absolute left-5 top-5 text-[11px] font-bold uppercase tracking-[0.32em] text-[#17213f] md:left-12 md:top-10\">Billing</p>\n        <button\n          type=\"button\"\n          role=\"switch\"\n          aria-checked={yearly}\n          aria-label=\"Bill yearly\"\n          onKeyDown={onKey}\n          onFocus={() => api.current?.touch()}\n          onClick={click}\n          onPointerDown={down}\n          onPointerMove={moveP}\n          onPointerUp={up}\n          onPointerCancel={() => { dragRef.current = null; choose(yearlyRef.current); }}\n          className=\"absolute cursor-grab touch-none rounded-[28px] outline-none active:cursor-grabbing focus-visible:ring-2 focus-visible:ring-[#17213f] focus-visible:ring-offset-4 focus-visible:ring-offset-transparent\"\n          style={{ left: box.l, top: box.t, width: box.w, height: box.h }}\n        />\n      </div>\n      <div className=\"relative px-5 pb-10 pt-1 md:pointer-events-none md:absolute md:bottom-0 md:left-0 md:top-0 md:flex md:w-[44%] md:flex-col md:justify-center md:px-12 md:pb-0\">\n        <h2 className=\"max-w-[12ch] text-[34px] leading-[0.98] tracking-[-0.03em] text-[#17213f] md:text-[54px]\" style={{ fontFamily: SERIF }}>Pay by the month, or once a year.</h2>\n        <div className=\"mt-6 flex items-end gap-3 md:mt-10\" aria-live=\"polite\">\n          <div className=\"relative h-[64px] overflow-hidden md:h-[104px]\">\n            <AnimatePresence mode=\"popLayout\" initial={false}>\n              <motion.span\n                key={p.price}\n                initial={reduceMotion ? false : { y: \"100%\", opacity: 0 }}\n                animate={{ y: \"0%\", opacity: 1 }}\n                exit={reduceMotion ? { opacity: 0 } : { y: \"-100%\", opacity: 0 }}\n                transition={{ type: \"spring\", stiffness: 420, damping: 30 }}\n                className=\"block text-[60px] font-semibold leading-none tracking-[-0.05em] tabular-nums md:text-[100px]\"\n              >\n                {p.price}\n              </motion.span>\n            </AnimatePresence>\n          </div>\n          <div className=\"pb-2 md:pb-4\">\n            <p className=\"text-[14px] font-semibold text-[#17213f] md:text-[15px]\">{plan.name}, {p.per}</p>\n            <p className=\"text-[11px] font-bold uppercase tracking-[0.22em] text-[#5a4a3a]\">Example prices</p>\n          </div>\n        </div>\n        <p className=\"mt-3 max-w-[30ch] text-[15px] leading-[1.55] text-[#3b3328]\">{p.note}</p>\n        <p className=\"mt-6 hidden text-[11px] font-bold uppercase tracking-[0.26em] text-[#5a4a3a] md:block\">Throw the lever, or press left and right</p>\n      </div>\n    </section>\n  );\n}\n"}]}
