{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"ray-traced-glass-ball-cursor-on-a-wire-that-turns-a-poster-portrait-upside-down-and-burns-the-lamp-to-a-point-when-knocked-to-the-wall","type":"registry:block","title":"Ray-Traced Glass Ball Cursor on a Wire That Turns a Poster Portrait Upside Down and Burns the Lamp to a Point When Knocked to the Wall","description":"Free React cursor effect: a solid glass ball on a steel wire follows the pointer over a poster, flips the portrait inside it and focuses the lamp on the wall.","dependencies":[],"files":[{"path":"components/we-inc/ray-traced-glass-ball-cursor-on-a-wire-that-turns-a-poster-portrait-upside-down-and-burns-the-lamp-to-a-point-when-knocked-to-the-wall.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Everything a visitor is likely to change sits here.\nconst MASTHEAD = \"Verso\";\nconst FOLIO = [\"No. 08\", \"The upside-down issue\"];\nconst CAPTION = \"Carry the glass across her. Press, and it swings at the wall.\";\nconst PHOTO = \"https://images.pexels.com/photos/10198110/pexels-photo-10198110.jpeg?auto=compress&cs=tinysrgb&w=1600\";\nconst PHOTO_ALT = \"Portrait of a young woman in an oversized white shirt framing her chin with both hands against a deep green backdrop\";\nconst FACE = { u: 0.51, v: 0.36 }; // where her face is in the photograph (0 to 1 across, 0 to 1 down)\nconst BALL = 0.62; // radius of the glass; the frame is 6 tall\nconst GAP = 4.4; // how far the glass hangs off the wall, in ball radii; under 1.5 the picture inside stands upright\nconst IOR = 1.43; // how strongly the glass bends light\nconst SPREAD = 0.075; // how far red and violet separate; 0 is colourless glass\nconst TINT = [0.05, 0.012, 0.03]; // what the glass absorbs per unit of thickness (red, green, blue)\nconst LAMP: [number, number, number] = [-0.68, 0.47, 0.56]; // where the lamp stands: left, up, towards the camera\nconst VIEW_FROM = 10; // degrees the camera stands to the right of square-on\nconst REST = { x: 0, y: 0.3 }; // where the glass waits, measured from her face (the frame is 6 tall)\nconst ROAM = { x: 3, y: 1.05, seconds: 19 }; // the figure of eight it wanders when nobody is steering\nconst KNOCK_EVERY = 8200; // milliseconds between the knocks it gives itself\nconst GROTESK = '\"Inter Tight\", \"Inter\", ui-sans-serif, system-ui, -apple-system, \"Helvetica Neue\", Arial, sans-serif';\n\n// Pass one: a disc of lamp rays is pushed through the ball and drawn where it lands on the wall. Where the disc bunches up the wall gets brighter.\nconst CAUSTIC_VS = `#version 300 es\nprecision highp float;\nlayout(location=0) in vec2 aG;\nuniform vec3 uC, uLight, uE1, uE2; uniform float uR, uIor; uniform vec2 uCausO, uCausS;\nout vec2 vOld; out float vGain; out float vOk;\nvoid main(){\n  float r=aG.x*.9995, a=aG.y*6.2831853;\n  vec3 n=r*cos(a)*uE1+r*sin(a)*uE2+sqrt(1.-r*r)*uLight;\n  vec3 p=uC+uR*n, rd=-uLight;\n  vec3 land=p+rd*(p.z/uLight.z);\n  vOld=land.xy; vOk=0.; vGain=0.;\n  float f=.043+.957*pow(1.-dot(uLight,n),5.);\n  vec3 d=refract(rd,n,1./uIor);\n  vec3 p2=p+d*(-2.*dot(d,n)*uR), n2=(p2-uC)/uR;\n  vec3 o=refract(d,-n2,uIor);\n  if(dot(o,o)>0.&&o.z<-.04){\n    float f2=.043+.957*pow(1.-max(dot(o,n2),0.),5.);\n    land=p2+o*(-p2.z/o.z); vGain=(1.-f)*(1.-f2); vOk=1.;\n  }\n  gl_Position=vec4((land.xy-uCausO)/uCausS*2.-1.,0.,1.);\n}`;\nconst CAUSTIC_FS = `#version 300 es\nprecision highp float;\nin vec2 vOld; in float vGain; in float vOk;\nuniform float uCell, uScale, uCap; uniform vec3 uHue;\nout vec4 o;\nvoid main(){\n  if(vOk<.999) discard;\n  vec2 dx=dFdx(vOld), dy=dFdy(vOld);\n  float bunch=abs(dx.x*dy.y-dx.y*dy.x)/uCell;\n  o=vec4(uHue*(min(bunch,uCap)*vGain*uScale),1.);\n}`;\n\n// The camera's last touches, shared by both ways of finishing a frame: darker corners, a film curve and grain.\nconst FILM = `\nvec3 film(vec3 col, vec2 fc, vec2 res, float time){\n  vec2 v=fc/res-.5;\n  col*=(1.-dot(v,v)*.9)*1.18;\n  col=clamp((col*(2.51*col+.03))/(col*(2.43*col+.59)+.14),0.,1.);\n  col=pow(col,vec3(1./2.2));\n  vec2 g=fract(fc*vec2(.1234,.4562)+fract(time)*7.13); g+=dot(g,g+45.32);\n  return col+(fract(g.x*g.y)-.5)*.022;\n}`;\nconst SCENE_VS = `#version 300 es\nvoid main(){ vec2 p=vec2(gl_VertexID==1?3.:-1., gl_VertexID==2?3.:-1.); gl_Position=vec4(p,0.,1.); }`;\n// Pass two: the picture. One ray per pixel on the wall, four on the glass, each split into six colours inside it.\nconst SCENE_FS = `#version 300 es\nprecision highp float;\nuniform vec2 uRes; uniform float uTime, uTan, uFocus, uAper, uCausScale, uIor, uSpread, uR; uniform int uAA;\nuniform vec3 uCam, uF, uRt, uU, uLight, uTint, uC, uTop;\nuniform vec4 uPrint, uBand; uniform vec2 uCausO, uCausS;\nuniform sampler2D uTex, uCaus; uniform bool uDirect;\nout vec4 frag;\n${FILM}\nconst float LAMP=.021, WIRE=.0085;\n// Colour of one slice of the spectrum, 0 at red to 1 at violet, scaled so the whole run adds up to white.\nvec3 hue(float t){ return vec3((max(0.,1.-abs(t-.08)/.42)+.35*max(0.,1.-abs(t-1.)/.2))/.317, max(0.,1.-abs(t-.5)/.36)/.36, max(0.,1.-abs(t-.85)/.38)/.31); }\nfloat hash(vec2 p){ p=fract(p*vec2(123.34,456.21)); p+=dot(p,p+45.32); return fract(p.x*p.y); }\nfloat vnoise(vec2 p){ vec2 i=floor(p), f=fract(p); f=f*f*(3.-2.*f); return mix(mix(hash(i),hash(i+vec2(1,0)),f.x),mix(hash(i+vec2(0,1)),hash(i+vec2(1,1)),f.x),f.y); }\nfloat card(vec2 p, vec2 c, vec2 h, float s){ vec2 q=abs(p-c)-h; float d=length(max(q,0.))+min(max(q.x,q.y),0.); return 1.-smoothstep(-s,s,d); }\n// The room behind the camera: a tall window where the lamp stands (its glazing bars show in the glass), a cool strip to the right,\n// a warm floor and a dim ceiling. s spreads them for rough surfaces.\nvec3 room(vec3 d, float s){\n  vec3 c=mix(vec3(.16,.13,.1),vec3(.07,.075,.085),smoothstep(-.6,.5,d.y));\n  if(d.z>.02){\n    vec2 p=d.xy/d.z, w=uLight.xy/uLight.z, h=vec2(.34,.52);\n    float pane=card(p,w,h,.012+s);\n    vec2 g=(p-w)/h;\n    float bars=smoothstep(.0,.05+s*2.,abs(abs(g.x)-.34))*smoothstep(.0,.035+s*2.,abs(g.y))*smoothstep(.0,.035+s*2.,abs(abs(g.y)-.5));\n    c+=vec3(.86,.93,1.)*7.*pane*mix(1.,bars,1./(1.+s*30.));\n    float sr=.03+s*1.5; c+=vec3(1.,.93,.8)*(.07/(sr*sr))*smoothstep(sr,sr*.5,length(p-w-vec2(.0,.13)));\n  }\n  if(d.x>.02){ vec2 p=d.zy/d.x; c+=vec3(.62,.78,1.)*2.6*card(p,vec2(.5,.1),vec2(.1,1.1),.05+s); }\n  if(d.x<-.02){ vec2 p=d.zy/-d.x; c+=vec3(1.,.8,.62)*.7*card(p,vec2(-.2,-.3),vec2(.5,.5),.4+s); }\n  return c;\n}\n// How much of the lamp a point on the wall can see past the ball, its brass cap and its wire.\nfloat clearOf(vec3 P){\n  vec3 pc=uC-P; float a=dot(pc,uLight), m=length(pc-uLight*a)-uR, w=max(a,0.)*LAMP+.003;\n  float ball=smoothstep(-w,w,m);\n  vec3 up=normalize(uTop-uC), A=uC+up*uR, B=uTop;\n  vec2 sa=A.xy-uLight.xy*(A.z/uLight.z), sb=B.xy-uLight.xy*(B.z/uLight.z), ab=sb-sa, ap=P.xy-sa;\n  float h=clamp(dot(ap,ab)/dot(ab,ab),0.,1.), dist=length(ap-ab*h);\n  float pw=(A.z/uLight.z)*LAMP+.002;\n  float wire=1.-min(1.,WIRE/pw)*smoothstep(pw+WIRE,max(pw-WIRE,0.),dist)*.9;\n  float capd=length(ap-ab*clamp(dot(ap,ab)/dot(ab,ab),0.,.13/length(ab)));\n  float cap=smoothstep(.07-pw,.07+pw,capd);\n  return ball*wire*cap;\n}\nvec3 poolOfLight(vec3 P, float lod){\n  float clear=clearOf(P);\n  vec2 uv=(P.xy-uCausO)/uCausS;\n  vec2 e=smoothstep(vec2(0.),vec2(.06),uv)*smoothstep(vec2(0.),vec2(.06),1.-uv);\n  float k=e.x*e.y; if(k<=0.) return vec3(clear);\n  vec2 px=exp2(lod)/vec2(textureSize(uCaus,0));\n  vec3 c=textureLod(uCaus,uv,lod).rgb*.4+(textureLod(uCaus,uv+px*vec2(.7,.3),lod).rgb+textureLod(uCaus,uv+px*vec2(-.3,.7),lod).rgb+textureLod(uCaus,uv+px*vec2(-.7,-.3),lod).rgb+textureLod(uCaus,uv+px*vec2(.3,-.7),lod).rgb)*.15;\n  c*=uCausScale*k;\n  return vec3(clear)+90.*(1.-exp(-c/90.));\n}\n// The poster, pasted to the wall. gx and gy say how much wall one pixel covers; blur is the lens being focused on the glass, not the wall.\nvec3 wall(vec3 P, vec3 V, vec2 gx, vec2 gy, float blur, float fine){\n  vec2 hs=uPrint.zw, q=P.xy-uPrint.xy;\n  vec2 uv=vec2(q.x/(2.*hs.x)+.5,.5-q.y/(2.*hs.y));\n  vec2 ux=vec2(gx.x,-gx.y)/(2.*hs), uy=vec2(gy.x,-gy.y)/(2.*hs);\n  vec3 alb;\n  if(blur>0.){\n    vec2 o=vec2(blur,-blur)/(2.*hs); ux+=vec2(o.x*.45,0.); uy+=vec2(0.,o.y*.45);\n    alb=textureGrad(uTex,uv,ux,uy).rgb*.16;\n    for(int i=0;i<6;i++){ float a=float(i)*1.0472+.4; alb+=textureGrad(uTex,uv+o*vec2(cos(a),sin(a))*.72,ux,uy).rgb*.14; }\n  } else alb=textureGrad(uTex,uv,ux,uy).rgb;\n  alb=pow(alb,vec3(2.2));\n  // Past the edge of the poster the wall is bare plaster: the camera never sees it, the glass does.\n  vec2 pe=abs(q)-hs; float edge=.5*(length(gx)+length(gy))+blur+.004;\n  alb=mix(alb,vec3(.6,.57,.52),smoothstep(-edge,edge,max(pe.x,pe.y)));\n  // Paste wrinkles and the lap where two sheets meet: slopes in the paper that the raking lamp picks out.\n  vec3 n=vec3(0.,0.,1.);\n  if(fine>0.){\n    vec2 w=P.xy*vec2(.55,1.7); float e=.06;\n    float h0=vnoise(w)+.35*vnoise(w*3.1+7.), hx=vnoise(w+vec2(e,0.))+.35*vnoise((w+vec2(e,0.))*3.1+7.), hy=vnoise(w+vec2(0.,e))+.35*vnoise((w+vec2(0.,e))*3.1+7.);\n    n=normalize(vec3(-(hx-h0)/e*.03*fine,-(hy-h0)/e*.03*fine,1.));\n    alb*=1.+.05*(vnoise(P.xy*150.)-.5)*fine;\n  }\n  float diff=max(dot(n,uLight),0.)/uLight.z;\n  vec3 pool=poolOfLight(P,1.+log2(1.+(.5*(length(gx)+length(gy))+blur)*90.));\n  vec2 k=P.xy-vec2(-1.3,1.);\n  // The sun comes in through a side window, so it crosses the wall as one slanted band with shade above and below it.\n  float across=dot(P.xy-uBand.xy,normalize(vec2(-uLight.y,uLight.x)))*sign(-uLight.x);\n  float band=smoothstep(-uBand.z-.3,-uBand.z+.3,across)*smoothstep(uBand.w+.3,uBand.w-.3,across);\n  float spot=(.4+.6*exp(-dot(k,k)*.03))*(.1+.9*band);\n  vec3 col=alb*(vec3(.24,.26,.3)+vec3(1.25,1.15,1.0)*pool*spot*diff);\n  vec3 r=reflect(V,n);\n  float fr=.04+.96*pow(1.-max(dot(-V,n),0.),5.);\n  col+=room(r,.16)*fr*.3*min(pool.g+.3,1.);\n  return col;\n}\n// Finite cylinder between pa and pb: distance along the ray and the surface normal, or -1.\nvec4 cyl(vec3 ro, vec3 rd, vec3 pa, vec3 pb, float ra){\n  vec3 ba=pb-pa, oc=ro-pa; float baba=dot(ba,ba), bard=dot(ba,rd), baoc=dot(ba,oc);\n  float k2=baba-bard*bard, k1=baba*dot(oc,rd)-baoc*bard, k0=baba*dot(oc,oc)-baoc*baoc-ra*ra*baba;\n  float h=k1*k1-k2*k0; if(h<0.) return vec4(-1.);\n  h=sqrt(h); float t=(-k1-h)/k2, y=baoc+t*bard;\n  if(y>0.&&y<baba) return vec4(t,(oc+t*rd-ba*y/baba)/ra);\n  t=((y<0.?0.:baba)-baoc)/bard;\n  if(abs(k1+k2*t)<h) return vec4(t,ba*sign(y)/sqrt(baba));\n  return vec4(-1.);\n}\nvec3 ray(vec2 fc){ vec2 s=(fc*2.-uRes)/uRes.y; return normalize(uF+uRt*s.x*uTan+uU*s.y*uTan); }\n// Where the middle of the spectrum lands on the wall after going through the ball; z is 0 if it never gets there.\nvec3 landing(vec3 rd){\n  vec3 oc=uCam-uC; float b=dot(oc,rd), h=b*b-dot(oc,oc)+uR*uR;\n  if(h<=0.) return vec3(0.);\n  vec3 p=uCam+rd*(-b-sqrt(h)), n=(p-uC)/uR;\n  vec3 d=refract(rd,n,1./uIor); vec3 p2=p+d*(-2.*dot(d,n)*uR);\n  vec3 o=refract(d,(uC-p2)/uR,uIor);\n  if(o.z>-.02) return vec3(0.);\n  return vec3((p2+o*(-p2.z/o.z)).xy,1.);\n}\nvec3 beyond(vec3 p, vec3 d, vec2 gx, vec2 gy){\n  vec3 far=room(d,.01);\n  if(d.z>-.02) return far;\n  vec3 P=p+d*(-p.z/d.z);\n  return mix(wall(P,d,gx,gy,0.,0.),far,smoothstep(14.,40.,length(P.xy)));\n}\nvec3 glass(vec3 p, vec3 rd, vec2 fc, float jit){\n  vec3 n=(p-uC)/uR, up=normalize(uTop-uC);\n  float c=max(dot(-rd,n),0.), F=.043+.957*pow(1.-c,5.);\n  vec3 refl=beyond(p,reflect(rd,n),vec2(.3,0.),vec2(0.,.3));\n  // How much wall one pixel covers once the ball has bent the view, from where the neighbouring pixels land.\n  vec3 l0=landing(rd), lx=landing(ray(fc+vec2(1.,0.))), ly=landing(ray(fc+vec2(0.,1.)));\n  vec2 gx=l0.z*lx.z>0. ? lx.xy-l0.xy : vec2(.01,0.), gy=l0.z*ly.z>0. ? ly.xy-l0.xy : vec2(0.,.01);\n  float gl=max(length(gx),length(gy)); if(gl>1.2){ gx*=1.2/gl; gy*=1.2/gl; }\n  vec3 tr=vec3(0.);\n  for(int i=0;i<6;i++){\n    float w=(float(i)+jit)/6., ior=uIor+uSpread*(w-.5);\n    vec3 d=refract(rd,n,1./ior); float len=-2.*dot(d,n)*uR;\n    vec3 p2=p+d*len, n2=(p2-uC)/uR;\n    vec3 o=refract(d,-n2,ior);\n    float f2=.043+.957*pow(1.-max(dot(o,n2),0.),5.);\n    vec3 col=beyond(p2,o,gx,gy)*(1.-f2);\n    // The foot of the brass cap, seen from inside the glass.\n    col=mix(col,vec3(.09,.06,.03),smoothstep(.9925,.9945,dot(n2,up)));\n    if(i==2){\n      // A second, fainter picture of the room: light that bounced once off the inside of the far wall of the ball.\n      vec3 dr=reflect(d,-n2); vec3 p3=p2+dr*(-2.*dot(dr,n2)*uR), n3=(p3-uC)/uR;\n      vec3 o3=refract(dr,-n3,ior);\n      tr+=beyond(p3,o3,vec2(.4,0.),vec2(0.,.4))*f2*(1.-f2)*exp(-uTint*len*2.);\n    }\n    tr+=col*exp(-uTint*len)*hue(w)/6.;\n  }\n  return tr*(1.-F)+refl*F;\n}\nvec3 brass(vec3 n, vec3 rd, float along){\n  vec3 r=reflect(rd,n);\n  vec3 c=room(r,.05)*vec3(1.,.74,.38)+vec3(.2,.13,.05)*(.25+.75*max(dot(n,uLight),0.));\n  return c*(.75+.25*smoothstep(0.,.3,abs(fract(along*46.)-.5)));\n}\nvec3 shade(vec2 fc, float jit){\n  vec3 rd=ray(fc);\n  float tw=-uCam.z/rd.z;\n  vec3 oc=uCam-uC; float b=dot(oc,rd), h=b*b-dot(oc,oc)+uR*uR;\n  float ts=h>0. ? -b-sqrt(h) : 1e9;\n  vec3 up=normalize(uTop-uC);\n  vec4 c1=cyl(uCam,rd,uC+up*(uR-.012),uC+up*(uR+.075),.068), c2=cyl(uCam,rd,uC+up*(uR+.07),uC+up*(uR+.15),.026);\n  vec3 col; float tn;\n  if(c1.x>0.&&c1.x<ts){ tn=c1.x; col=brass(c1.yzw,rd,dot(uCam+rd*tn-uC,up)); }\n  else if(c2.x>0.&&c2.x<ts){ tn=c2.x; col=brass(c2.yzw,rd,0.); }\n  else if(ts<tw){ tn=ts; col=glass(uCam+rd*ts,rd,fc,jit); }\n  else {\n    tn=tw; vec3 P=uCam+rd*tw;\n    vec3 rx=ray(fc+vec2(1.,0.)), ry=ray(fc+vec2(0.,1.));\n    vec2 gx=(uCam+rx*(-uCam.z/rx.z)).xy-P.xy, gy=(uCam+ry*(-uCam.z/ry.z)).xy-P.xy;\n    col=wall(P,rd,gx,gy,uAper*abs(tw-uFocus)/uFocus,1.);\n  }\n  // The wire: a hair of steel, drawn by how much of the pixel it covers so it never breaks into steps.\n  vec3 A=uC+up*(uR+.14), ax=normalize(uTop-A), w0=uCam-A;\n  float bb=dot(rd,ax), dd=dot(rd,w0), ee=dot(ax,w0), den=1.-bb*bb;\n  float tr=(bb*ee-dd)/den, ta=(ee-bb*dd)/den;\n  if(ta>0.&&tr>0.&&tr<tn){\n    vec3 side=normalize(cross(ax,rd));\n    float x=dot(uCam+rd*tr-(A+ax*ta),side), pf=tr*uTan*2./uRes.y;\n    float cov=clamp((WIRE-abs(x))/pf+.5,0.,1.)*min(1.,2.*WIRE/pf+.25);\n    float u=clamp(x/WIRE,-1.,1.);\n    vec3 n=normalize(side*u*.8-rd*sqrt(1.-u*u*.64));\n    float lit=.5+.5*dot(n,uLight);\n    vec3 steel=vec3(.07,.07,.075)+vec3(.95,.9,.8)*lit*lit*lit*.9;\n    col=mix(col,steel,cov);\n  }\n  return col;\n}\nconst vec2 JIT[4]=vec2[4](vec2(.125,.375),vec2(-.375,.125),vec2(.375,-.125),vec2(-.125,-.375));\nvoid main(){\n  vec3 rd=ray(gl_FragCoord.xy);\n  vec3 oc=uCam-(uC+normalize(uTop-uC)*.07); float b=dot(oc,rd), rr=uR+.11+2.*length(oc)*uTan/uRes.y;\n  int n=b*b-dot(oc,oc)+rr*rr>0. ? uAA : 1;\n  vec3 col=vec3(0.);\n  // Each sample starts its six colours at a different point in the spectrum, so sharp edges fan out smoothly instead of in six steps.\n  float dith=fract(52.9829189*fract(dot(gl_FragCoord.xy,vec2(.06711056,.00583715))));\n  for(int s=0;s<4;s++){ if(s>=n) break; col+=shade(gl_FragCoord.xy+(n>1?JIT[s]:vec2(0.)),fract(dith+float(s)*.25+.125)); }\n  col/=float(n);\n  // With a float buffer the frame is kept unfinished and its too-bright part is noted, so the lens glow can be added before the film curve.\n  if(uDirect) frag=vec4(film(col,gl_FragCoord.xy,uRes,uTime),1.);\n  else { col=min(col,vec3(40.)); frag=vec4(col,max(dot(col,vec3(.3,.55,.15))-1.6,0.)); }\n}`;\n\n// Pass three: lens glow. Smaller copies of the frame spread whatever was brighter than white into a soft halo.\nconst LENS_FS = `#version 300 es\nprecision highp float;\nuniform sampler2D uScene; uniform vec2 uRes; uniform float uTime;\nout vec4 frag;\n${FILM}\nvec3 halo(vec2 uv, float lod){\n  vec2 px=exp2(lod)/uRes*.75; vec4 c=vec4(0.);\n  c+=textureLod(uScene,uv+px*vec2(1.,.4),lod); c+=textureLod(uScene,uv+px*vec2(-.4,1.),lod);\n  c+=textureLod(uScene,uv+px*vec2(-1.,-.4),lod); c+=textureLod(uScene,uv+px*vec2(.4,-1.),lod);\n  c*=.25;\n  return c.rgb*c.a/(dot(c.rgb,vec3(.3,.55,.15))+.02);\n}\nvoid main(){\n  vec2 uv=gl_FragCoord.xy/uRes;\n  vec3 col=textureLod(uScene,uv,0.).rgb;\n  float k=log2(uRes.y/700.);\n  col+=halo(uv,1.5+k)*.2+halo(uv,3.+k)*.18+halo(uv,4.5+k)*.14;\n  frag=vec4(film(col,gl_FragCoord.xy,uRes,uTime),1.);\n}`;\n\ntype V3 = [number, number, number];\nconst sub = (a: V3, b: V3): V3 => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];\nconst cross = (a: V3, b: V3): V3 => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];\nconst unit = (a: V3): V3 => { const l = Math.hypot(a[0], a[1], a[2]) || 1; return [a[0] / l, a[1] / l, a[2] / l]; };\nconst clamp = (v: number, a: number, b: number) => Math.min(b, Math.max(a, v));\n\nexport default function RayTracedGlassBallCursorOnAWire() {\n  const root = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const photo = useRef<HTMLImageElement>(null);\n  const knock = useRef<HTMLButtonElement>(null);\n  const api = useRef<{ pause: (on: boolean) => void } | null>(null);\n  const [live, setLive] = useState(false);\n  const [moving, setMoving] = useState(false);\n  const [paused, setPaused] = useState(false);\n  const [knocks, setKnocks] = useState(0);\n\n  useEffect(() => {\n    const el = root.current, cv = canvas.current, img = photo.current, btn = knock.current;\n    if (!el || !cv || !img || !btn) return;\n    const gl = cv.getContext(\"webgl2\", { antialias: false, alpha: false, powerPreference: \"high-performance\" });\n    if (!gl) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n\n    const compile = (type: number, src: string) => { const s = gl.createShader(type)!; gl.shaderSource(s, src); gl.compileShader(s); return s; };\n    const link = (vs: string, fs: string) => {\n      const p = gl.createProgram()!;\n      gl.attachShader(p, compile(gl.VERTEX_SHADER, vs)); gl.attachShader(p, compile(gl.FRAGMENT_SHADER, fs)); gl.linkProgram(p);\n      if (!gl.getProgramParameter(p, gl.LINK_STATUS)) return null;\n      const at = new Map<string, WebGLUniformLocation | null>();\n      return { p, u: (n: string) => { if (!at.has(n)) at.set(n, gl.getUniformLocation(p, n)); return at.get(n)!; } };\n    };\n    const caus = link(CAUSTIC_VS, CAUSTIC_FS), scene = link(SCENE_VS, SCENE_FS), lens = link(SCENE_VS, LENS_FS);\n    if (!caus || !scene || !lens) return;\n\n    const LIGHT = unit(LAMP);\n    const TAN = Math.tan((11 * Math.PI) / 180), DIST = 3 / TAN; // the frame is 6 tall where it meets the wall\n    const YTOP = 9.5; // the rail the wire runs up to, far out of frame\n    let R = BALL, D0 = BALL * GAP;\n\n    // The poster: the photograph with its masthead, painted once to a canvas and used as the wall's surface.\n    const TW = 2048, TH = 1365;\n    const sheet = document.createElement(\"canvas\"); sheet.width = TW; sheet.height = TH;\n    // seen is the part of the poster the camera frames, in poster pixels: the type is laid out inside it.\n    const paintPoster = (withPhoto: boolean, seen: number[]) => {\n      const x = sheet.getContext(\"2d\")! as CanvasRenderingContext2D & { letterSpacing: string };\n      const [x0, y0, x1, y1] = seen, w = x1 - x0, h = y1 - y0, wide = w > h;\n      x.globalAlpha = 1; x.textAlign = \"left\";\n      const g = x.createRadialGradient(TW * 0.5, TH * 0.4, 60, TW * 0.5, TH * 0.4, TW * 0.7);\n      g.addColorStop(0, \"#2b5a52\"); g.addColorStop(1, \"#0f2a29\");\n      x.fillStyle = g; x.fillRect(0, 0, TW, TH);\n      const word = document.createElement(\"canvas\"); word.width = TW; word.height = TH;\n      const wx = word.getContext(\"2d\")! as CanvasRenderingContext2D & { letterSpacing: string };\n      // The masthead runs from the left edge of the frame until it tucks in behind her head.\n      const left = x0 + w * (wide ? 0.04 : 0.06), right = wide ? TW * (FACE.u - 0.062) : x1 - w * 0.07;\n      wx.font = `800 100px ${GROTESK}`; wx.letterSpacing = \"-5px\";\n      const size = Math.min(((right - left) / wx.measureText(MASTHEAD).width) * 100, h * (wide ? 0.36 : 0.15));\n      wx.font = `800 ${size}px ${GROTESK}`; wx.letterSpacing = `${(-size * 0.055).toFixed(1)}px`;\n      wx.fillStyle = \"#efe9da\"; wx.textBaseline = \"alphabetic\";\n      const base = y0 + h * (wide ? 0.075 : 0.05) + size * 0.73;\n      wx.fillText(MASTHEAD, left - size * 0.045, base);\n      if (withPhoto) {\n        const sc = Math.max(TW / img.naturalWidth, TH / img.naturalHeight), dw = img.naturalWidth * sc, dh = img.naturalHeight * sc;\n        x.drawImage(img, (TW - dw) / 2, (TH - dh) / 2, dw, dh);\n        // A quarter-size copy is keyed on the green backdrop, so the masthead prints on the backdrop and passes behind her.\n        const kw = TW >> 2, kh = TH >> 2;\n        const key = document.createElement(\"canvas\"); key.width = kw; key.height = kh;\n        const kx = key.getContext(\"2d\", { willReadFrequently: true })!;\n        kx.filter = \"blur(1.2px)\"; kx.drawImage(sheet, 0, 0, kw, kh); kx.filter = \"none\";\n        const px = kx.getImageData(0, 0, kw, kh), d = px.data;\n        const sm = (a: number, b: number, v: number) => { const t = clamp((v - a) / (b - a), 0, 1); return t * t * (3 - 2 * t); };\n        for (let i = 0; i < d.length; i += 4) d[i + 3] = 255 * sm(1.12, 1.42, (d[i + 1] + d[i + 2] + 8) / (2 * d[i] + 8));\n        kx.putImageData(px, 0, 0);\n        wx.globalCompositeOperation = \"destination-in\"; wx.imageSmoothingQuality = \"high\";\n        wx.drawImage(key, 0, 0, TW, TH);\n      }\n      x.drawImage(word, 0, 0);\n      // Folio lines, set small against the masthead.\n      const small = Math.max(15, h * 0.0215);\n      x.fillStyle = \"#efe9da\"; x.textBaseline = \"alphabetic\"; x.letterSpacing = `${(small * 0.24).toFixed(1)}px`; x.font = `600 ${small}px ${GROTESK}`;\n      if (wide) {\n        x.textAlign = \"right\";\n        x.fillText(FOLIO[0].toUpperCase(), x1 - w * 0.045, y0 + h * 0.105);\n        x.globalAlpha = 0.72; x.fillText(FOLIO[1].toUpperCase(), x1 - w * 0.045, y0 + h * 0.105 + small * 1.75); x.globalAlpha = 1;\n        x.fillRect(x1 - w * 0.045 - small * 2.6, y0 + h * 0.105 - small * 2.1, small * 2.4, Math.max(2, small * 0.12));\n        x.textAlign = \"left\";\n      }\n    };\n    const tex = (unit_: number, src: TexImageSource) => {\n      const t = gl.createTexture();\n      gl.activeTexture(gl.TEXTURE0 + unit_); gl.bindTexture(gl.TEXTURE_2D, t);\n      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, src);\n      gl.generateMipmap(gl.TEXTURE_2D);\n      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);\n      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n      const an = gl.getExtension(\"EXT_texture_filter_anisotropic\");\n      if (an) gl.texParameterf(gl.TEXTURE_2D, an.TEXTURE_MAX_ANISOTROPY_EXT, 4);\n      return t;\n    };\n    let posterTex: WebGLTexture | null = null, seen = [0, 0, TW, TH], painted = \"\";\n    const loadPoster = () => {\n      if (posterTex) gl.deleteTexture(posterTex);\n      // A photo host that forbids pixel reads throws here; the poster then keeps its plain green ground.\n      try { paintPoster(img.complete && img.naturalWidth > 0, seen); posterTex = tex(0, sheet); }\n      catch { paintPoster(false, seen); posterTex = tex(0, sheet); }\n    };\n\n    // The light map the first pass draws into. Half-float where the browser allows it, so a focused point keeps its headroom.\n    const MW = 640, MH = 640;\n    const float = !!gl.getExtension(\"EXT_color_buffer_float\") || !!gl.getExtension(\"EXT_color_buffer_half_float\");\n    const mapScale = float ? 1 : 0.1;\n    const map = gl.createTexture();\n    gl.activeTexture(gl.TEXTURE2); gl.bindTexture(gl.TEXTURE_2D, map);\n    if (float) gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, MW, MH, 0, gl.RGBA, gl.HALF_FLOAT, null);\n    else gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, MW, MH, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n    const fbo = gl.createFramebuffer();\n    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);\n    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, map, 0);\n    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE) { gl.bindFramebuffer(gl.FRAMEBUFFER, null); return; }\n    gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n    // The unfinished frame, kept in a float buffer for the lens glow. Without float buffers the scene pass finishes the picture itself.\n    const shot = float ? gl.createTexture() : null, shotFbo = float ? gl.createFramebuffer() : null;\n    let shotW = 0, shotH = 0;\n    const fitShot = () => {\n      if (!shot || (shotW === W && shotH === Hh)) return;\n      shotW = W; shotH = Hh;\n      gl.activeTexture(gl.TEXTURE3); gl.bindTexture(gl.TEXTURE_2D, shot);\n      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, W, Hh, 0, gl.RGBA, gl.HALF_FLOAT, null);\n      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);\n      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n      gl.bindFramebuffer(gl.FRAMEBUFFER, shotFbo);\n      gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, shot, 0);\n    };\n\n    // The disc of lamp rays: rings from the middle of the ball out to its rim.\n    const GR = 96, GA = 160;\n    const pts = new Float32Array((GR + 1) * (GA + 1) * 2), idx = new Uint32Array(GR * GA * 6);\n    for (let j = 0, k = 0; j <= GA; j++) for (let i = 0; i <= GR; i++) { pts[k++] = Math.sin(((i / GR) * Math.PI) / 2); pts[k++] = j / GA; }\n    for (let j = 0, k = 0; j < GA; j++) for (let i = 0; i < GR; i++) { const a = j * (GR + 1) + i, b = a + GR + 1; idx[k++] = a; idx[k++] = a + 1; idx[k++] = b; idx[k++] = a + 1; idx[k++] = b + 1; idx[k++] = b; }\n    const vao = gl.createVertexArray(); gl.bindVertexArray(vao);\n    gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer()); gl.bufferData(gl.ARRAY_BUFFER, pts, gl.STATIC_DRAW);\n    gl.enableVertexAttribArray(0); gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl.createBuffer()); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, idx, gl.STATIC_DRAW);\n    const plain = gl.createVertexArray();\n\n    // The same spectrum the scene shader uses, cut into six slices that add up to white.\n    const tri = (t: number, c: number, w: number) => Math.max(0, 1 - Math.abs(t - c) / w);\n    const BANDS = Array.from({ length: 6 }, (_, i) => { const t = (i + 0.5) / 6; return [t, [(tri(t, 0.08, 0.42) + 0.35 * tri(t, 1, 0.2)) / 0.317 / 6, tri(t, 0.5, 0.36) / 0.36 / 6, tri(t, 0.85, 0.38) / 0.31 / 6]] as [number, number[]]; });\n    const E1 = unit(cross([0, 1, 0], LIGHT)), E2 = cross(LIGHT, E1);\n\n    // tx, ty: where the glass is being sent. ax, ay: the carriage on the rail above it. sx, sz: how far it has swung sideways and in depth.\n    const s = { tx: 0, ty: 0, ax: 0, ay: 0, vax: 0, vay: 0, sx: 0, vsx: 0, sz: 0, vsz: 0, held: false, cx: 0, cy: 0 };\n    let W = 0, Hh = 0, asp = 1.8, quality = 1, aa = 4, raf = 0, last = 0, start = 0, shown = true, stopped = false;\n    let touched = -1e9, nextKnock = 4300, knockUp = 0, slow = 0, roamAt = 0;\n    let cam: V3 = [0, 0, DIST], F: V3 = [0, 0, -1], Rv: V3 = [1, 0, 0], U: V3 = [0, 1, 0];\n    let print = [0, 0, 6, 4];\n\n    const aimCamera = () => {\n      const yaw = ((asp > 1 ? VIEW_FROM : VIEW_FROM * 0.6) * Math.PI) / 180 + s.cx * 0.03, pitch = -0.035 + s.cy * 0.02;\n      cam = [DIST * Math.sin(yaw) * Math.cos(pitch), DIST * Math.sin(pitch), DIST * Math.cos(yaw) * Math.cos(pitch)];\n      F = unit(sub([0, 0, 0], cam)); Rv = unit(cross(F, [0, 1, 0])); U = cross(Rv, F);\n    };\n    // Where a point of the frame (-1 to 1 across and up) meets the plane at depth z.\n    const meet = (nx: number, ny: number, z: number) => {\n      const d: V3 = [F[0] + Rv[0] * nx * asp * TAN + U[0] * ny * TAN, F[1] + Rv[1] * nx * asp * TAN + U[1] * ny * TAN, F[2] + Rv[2] * nx * asp * TAN + U[2] * ny * TAN];\n      const t = (z - cam[2]) / d[2];\n      return [cam[0] + d[0] * t, cam[1] + d[1] * t];\n    };\n    // Size the poster so it covers everything the camera can see, with her face held near the middle of the frame.\n    const hang = () => {\n      const cx = s.cx, cy = s.cy; s.cx = 0; s.cy = 0; aimCamera();\n      const c = [meet(-1, -1, 0), meet(1, -1, 0), meet(-1, 1, 0), meet(1, 1, 0)];\n      const bx0 = Math.min(...c.map((p) => p[0])) - 0.5, bx1 = Math.max(...c.map((p) => p[0])) + 0.5, by0 = Math.min(...c.map((p) => p[1])) - 0.3, by1 = Math.max(...c.map((p) => p[1])) + 0.3;\n      const pw = Math.max(bx1 - bx0, (by1 - by0) * (TW / TH)), ph = (pw * TH) / TW;\n      const px = clamp((asp > 1 ? -0.1 : 0.05) - (FACE.u - 0.5) * pw, bx1 - pw / 2, bx0 + pw / 2);\n      const py = clamp((asp > 1 ? 0.5 : 0.1) + (FACE.v - 0.5) * ph, by1 - ph / 2, by0 + ph / 2);\n      print = [px, py, pw / 2, ph / 2];\n      const toU = (wx: number) => ((wx - (px - pw / 2)) / pw) * TW, toV = (wy: number) => ((py + ph / 2 - wy) / ph) * TH;\n      seen = [toU(Math.max(c[0][0], c[2][0])), toV(Math.min(c[2][1], c[3][1])), toU(Math.min(c[1][0], c[3][0])), toV(Math.max(c[0][1], c[1][1]))];\n      s.cx = cx; s.cy = cy;\n      const sig = seen.map((v) => Math.round(v / 8)).join();\n      if (sig !== painted) { painted = sig; loadPoster(); }\n    };\n\n    const size = () => {\n      const b = el.getBoundingClientRect();\n      const k = Math.min(Math.max(window.devicePixelRatio || 1, 0.5), 1.5) * quality;\n      const w = Math.max(2, Math.round(b.width * k)), h = Math.max(2, Math.round(b.height * k));\n      if (w === W && h === Hh) return;\n      W = w; Hh = h; cv.width = W; cv.height = Hh;\n      const was = asp; asp = W / Hh;\n      R = asp > 1 ? BALL : BALL * 0.86; D0 = R * GAP;\n      if (Math.abs(was - asp) > 0.01 || !painted) hang();\n    };\n    // The glass waits on the line between the camera and her face, so the face is what it turns over.\n    const rest = () => {\n      const fx = print[0] + (FACE.u - 0.5) * 2 * print[2] + REST.x, fy = print[1] - (FACE.v - 0.5) * 2 * print[3] + REST.y, k = D0 / cam[2];\n      return [fx + (cam[0] - fx) * k, fy + (cam[1] - fy) * k];\n    };\n\n    const draw = (time: number) => {\n      aimCamera();\n      const len = YTOP - s.ay;\n      const C: V3 = [s.ax + s.sx, s.ay + (s.sx * s.sx + s.sz * s.sz) / (2 * len), D0 + s.sz];\n      const top: V3 = [s.ax, YTOP, D0];\n      // The lamp's pool moves and grows with the distance to the wall, so the map is fitted round it every frame.\n      const along = C[2] / LIGHT[2], cw = [C[0] - LIGHT[0] * along, C[1] - LIGHT[1] * along];\n      const half = (R * Math.max(1.3, 0.4 + 1.1 * Math.abs(along / R - 1.45)) * 1.2) / LIGHT[2];\n      const cs = [half * 2, half * 2], co = [cw[0] - half, cw[1] - half];\n\n      // Light map: six slices of the spectrum, each bent a little differently and added together.\n      gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); gl.viewport(0, 0, MW, MH);\n      gl.clearColor(0, 0, 0, 1); gl.clear(gl.COLOR_BUFFER_BIT);\n      gl.useProgram(caus.p); gl.bindVertexArray(vao);\n      gl.enable(gl.BLEND); gl.blendFunc(gl.ONE, gl.ONE);\n      gl.uniform3fv(caus.u(\"uC\"), C); gl.uniform1f(caus.u(\"uR\"), R); gl.uniform3fv(caus.u(\"uLight\"), LIGHT);\n      gl.uniform3fv(caus.u(\"uE1\"), E1); gl.uniform3fv(caus.u(\"uE2\"), E2);\n      gl.uniform2fv(caus.u(\"uCausO\"), co); gl.uniform2fv(caus.u(\"uCausS\"), cs);\n      gl.uniform1f(caus.u(\"uCell\"), (cs[0] / MW) * (cs[1] / MH)); gl.uniform1f(caus.u(\"uScale\"), mapScale); gl.uniform1f(caus.u(\"uCap\"), float ? 400 : 9);\n      for (const [w, hue] of BANDS) {\n        gl.uniform1f(caus.u(\"uIor\"), IOR + SPREAD * (w - 0.5)); gl.uniform3fv(caus.u(\"uHue\"), hue);\n        gl.drawElements(gl.TRIANGLES, idx.length, gl.UNSIGNED_INT, 0);\n      }\n      gl.disable(gl.BLEND);\n      gl.activeTexture(gl.TEXTURE2); gl.bindTexture(gl.TEXTURE_2D, map); gl.generateMipmap(gl.TEXTURE_2D);\n\n      fitShot();\n      gl.bindFramebuffer(gl.FRAMEBUFFER, shotFbo); gl.viewport(0, 0, W, Hh);\n      gl.useProgram(scene.p); gl.bindVertexArray(plain); gl.uniform1i(scene.u(\"uDirect\"), shot ? 0 : 1);\n      gl.uniform2f(scene.u(\"uRes\"), W, Hh); gl.uniform1f(scene.u(\"uTime\"), time); gl.uniform1f(scene.u(\"uTan\"), TAN);\n      gl.uniform1f(scene.u(\"uFocus\"), DIST - D0); gl.uniform1f(scene.u(\"uAper\"), 0.08); gl.uniform1f(scene.u(\"uCausScale\"), 1 / mapScale);\n      gl.uniform1f(scene.u(\"uIor\"), IOR); gl.uniform1f(scene.u(\"uSpread\"), SPREAD); gl.uniform1f(scene.u(\"uR\"), R); gl.uniform1i(scene.u(\"uAA\"), aa);\n      gl.uniform3fv(scene.u(\"uCam\"), cam); gl.uniform3fv(scene.u(\"uF\"), F); gl.uniform3fv(scene.u(\"uRt\"), Rv); gl.uniform3fv(scene.u(\"uU\"), U);\n      gl.uniform3fv(scene.u(\"uLight\"), LIGHT); gl.uniform3fv(scene.u(\"uTint\"), TINT);\n      gl.uniform3fv(scene.u(\"uC\"), C); gl.uniform3fv(scene.u(\"uTop\"), top);\n      gl.uniform4fv(scene.u(\"uPrint\"), print); gl.uniform4f(scene.u(\"uBand\"), 0, 0.2, asp > 1 ? 3.3 : 2.6, asp > 1 ? 3.1 : 2.6); gl.uniform2fv(scene.u(\"uCausO\"), co); gl.uniform2fv(scene.u(\"uCausS\"), cs);\n      gl.uniform1i(scene.u(\"uTex\"), 0); gl.uniform1i(scene.u(\"uCaus\"), 2);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n      if (shot) {\n        gl.activeTexture(gl.TEXTURE3); gl.bindTexture(gl.TEXTURE_2D, shot); gl.generateMipmap(gl.TEXTURE_2D);\n        gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n        gl.useProgram(lens.p); gl.uniform1i(lens.u(\"uScene\"), 3); gl.uniform2f(lens.u(\"uRes\"), W, Hh); gl.uniform1f(lens.u(\"uTime\"), time);\n        gl.drawArrays(gl.TRIANGLES, 0, 3);\n      }\n    };\n\n    const step = (dt: number, t: number) => {\n      // The carriage and hoist run after the target; the glass hangs below and is dragged along late, so it lags, then swings past.\n      const kx = 46 * (s.tx - s.ax) - 11.5 * s.vax, ky = 60 * (s.ty - s.ay) - 13 * s.vay;\n      s.vax += kx * dt; s.ax += s.vax * dt; s.vay += ky * dt; s.ay += s.vay * dt;\n      s.vsx += (-17 * s.sx - 0.42 * kx - 1.8 * s.vsx) * dt; s.sx += s.vsx * dt;\n      const idle = t - touched > 2600;\n      // A finger on it is one steady push at the wall; let go and it swings back out past where it hung.\n      const push = s.held ? -64 : idle ? 1.3 * Math.sin(t / 1450) : 0;\n      s.vsz += (-15 * s.sz - 1.15 * s.vsz + push) * dt; s.sz += s.vsz * dt;\n      const stop = R + 0.012 - D0;\n      if (s.sz < stop) { s.sz = stop; s.vsz = s.vsz < -0.5 ? -s.vsz * 0.4 : 0; }\n      const ease = Math.min(1, dt * 2.5), [hx, hy] = rest();\n      s.cx += ((idle ? 0.45 * Math.sin(t / 5300) : clamp((s.tx - hx) * 0.3, -1, 1)) - s.cx) * ease;\n      s.cy += ((idle ? 0.4 * Math.sin(t / 6700) : clamp((s.ty - hy) * 0.3, -1, 1)) - s.cy) * ease;\n    };\n    // Left alone, it waits where it looks best, then wanders a slow figure of eight across her and knocks now and then.\n    const roam = (t: number, dt: number) => {\n      const wait = clamp((t - Math.max(touched + 2600, 3000)) / 2500, 0, 1);\n      roamAt += dt * wait * wait * (3 - 2 * wait);\n      const a = (roamAt / ROAM.seconds) * Math.PI * 2;\n      const [rx, ry] = rest(), wx = asp > 1 ? ROAM.x : ROAM.x * 0.36;\n      s.tx = rx - wx * Math.sin(a); s.ty = ry - ROAM.y * (0.75 * Math.sin(2 * a) + 0.35 * (1 - Math.cos(a)));\n    };\n\n    const frame = (now: number) => {\n      raf = 0;\n      if (!start) { start = now; last = now; }\n      const raw = now - last, dt = Math.min(0.032, raw / 1000); last = now;\n      const t = now - start;\n      if (t - touched > 2600) {\n        roam(t, dt);\n        if (t > nextKnock && !s.held) { s.held = true; knockUp = t + 950; nextKnock = t + KNOCK_EVERY; }\n        if (knockUp && t > knockUp) { knockUp = 0; s.held = false; }\n      }\n      // Four small steps keep the knock against the wall stable at low frame rates.\n      for (let i = 0; i < 4; i++) step(dt / 4, t);\n      size(); draw(t / 1000);\n      // If frames run long, first drop the extra samples on the glass, then the resolution.\n      if (t > 1500) { slow = raw > 24 ? slow + 1 : Math.max(0, slow - 0.25); if (slow > 24) { slow = 0; if (aa > 2) aa = 2; else if (quality > 0.6) quality *= 0.85; else aa = 1; } }\n      if (shown && !stopped && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    // One finished picture, for reduced motion and for the first paint: the glass goes straight to where it was sent.\n    const still = () => { size(); s.ax = s.tx; s.ay = s.ty; s.vax = s.vay = s.sx = s.vsx = s.vsz = 0; s.sz = s.held ? R + 0.012 - D0 : 0; draw(0); };\n    const wake = () => { if (reduce) { still(); return; } if (!raf && !stopped) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n\n    size();\n    [s.tx, s.ty] = rest(); s.ax = s.tx; s.ay = s.ty;\n    still(); setLive(true); setMoving(!reduce); wake();\n    const onPhoto = () => { loadPoster(); if (reduce || stopped) still(); };\n    if (!(img.complete && img.naturalWidth > 0)) img.addEventListener(\"load\", onPhoto);\n\n    const mark = () => { touched = start ? performance.now() - start : 0; knockUp = 0; };\n    const send = (e: PointerEvent) => {\n      const b = el.getBoundingClientRect();\n      const nx = clamp(((e.clientX - b.left) / b.width) * 2 - 1, -0.93, 0.93), ny = clamp(1 - ((e.clientY - b.top) / b.height) * 2, -0.8, 0.8);\n      [s.tx, s.ty] = meet(nx, ny, D0);\n      mark(); if (reduce || stopped) still();\n    };\n    const press = () => { mark(); s.held = true; if (reduce || stopped) still(); };\n    const release = () => { if (!s.held) return; mark(); s.held = false; setKnocks((n) => n + 1); if (reduce || stopped) still(); };\n    const onMove = (e: PointerEvent) => { if (e.pointerType === \"mouse\" || e.buttons) send(e); };\n    const onDown = (e: PointerEvent) => { if ((e.target as HTMLElement).closest(\"button\")) return; send(e); press(); };\n    const onKeyDown = (e: KeyboardEvent) => {\n      const arrows: Record<string, number[]> = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, 1], ArrowDown: [0, -1] };\n      if (arrows[e.key]) {\n        e.preventDefault();\n        const [lx, ly] = meet(-0.93, -0.8, D0), [hx, hy] = meet(0.93, 0.8, D0);\n        s.tx = clamp(s.tx + arrows[e.key][0] * 0.45, Math.min(lx, hx), Math.max(lx, hx)); s.ty = clamp(s.ty + arrows[e.key][1] * 0.45, Math.min(ly, hy), Math.max(ly, hy));\n        mark(); if (reduce || stopped) still();\n      } else if ((e.key === \" \" || e.key === \"Enter\") && !e.repeat) press();\n    };\n    const onKeyUp = (e: KeyboardEvent) => { if (e.key === \" \" || e.key === \"Enter\") release(); };\n    const onLost = (e: Event) => { e.preventDefault(); cancelAnimationFrame(raf); raf = -1; setLive(false); setMoving(false); };\n    el.addEventListener(\"pointermove\", onMove); el.addEventListener(\"pointerdown\", onDown);\n    window.addEventListener(\"pointerup\", release); window.addEventListener(\"pointercancel\", release);\n    btn.addEventListener(\"keydown\", onKeyDown); btn.addEventListener(\"keyup\", onKeyUp); btn.addEventListener(\"blur\", release);\n    const btnDown = () => press();\n    btn.addEventListener(\"pointerdown\", btnDown);\n    cv.addEventListener(\"webglcontextlost\", onLost);\n    api.current = { pause: (on) => { stopped = on; if (on) { cancelAnimationFrame(raf); raf = 0; } else wake(); } };\n    const ro = new ResizeObserver(() => { if (reduce || !raf) still(); });\n    ro.observe(el);\n    const io = new IntersectionObserver(([entry]) => { shown = entry.isIntersecting; if (shown && raf !== -1) wake(); });\n    io.observe(el);\n    const onVis = () => { if (!document.hidden && raf !== -1) wake(); };\n    document.addEventListener(\"visibilitychange\", onVis);\n\n    return () => {\n      cancelAnimationFrame(raf); api.current = null;\n      img.removeEventListener(\"load\", onPhoto);\n      el.removeEventListener(\"pointermove\", onMove); el.removeEventListener(\"pointerdown\", onDown);\n      window.removeEventListener(\"pointerup\", release); window.removeEventListener(\"pointercancel\", release);\n      btn.removeEventListener(\"keydown\", onKeyDown); btn.removeEventListener(\"keyup\", onKeyUp); btn.removeEventListener(\"blur\", release);\n      btn.removeEventListener(\"pointerdown\", btnDown);\n      cv.removeEventListener(\"webglcontextlost\", onLost);\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext(); // hand the textures and buffers back at once\n      ro.disconnect(); io.disconnect();\n      document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  const pill = \"pointer-events-auto flex h-10 items-center gap-2 rounded-full border border-white/25 bg-black/35 px-4 text-[11px] font-semibold uppercase tracking-[0.2em] text-white backdrop-blur-md transition-colors hover:bg-black/55 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-offset-2 focus-visible:ring-offset-black/60\";\n  return (\n    <div ref={root} className=\"relative h-[620px] w-full cursor-none touch-pan-y select-none overflow-hidden bg-[#10201f] sm:h-[700px]\" style={{ fontFamily: GROTESK }}>\n      <style>{`@keyframes glass-ball-cursor-on-a-wire-sway { 0%, 100% { transform: rotate(-1.6deg); } 50% { transform: rotate(1.6deg); } }`}</style>\n      {/* Shown until the traced picture is ready, and kept when the browser cannot trace it: the same poster and glass, built from plain layers. */}\n      <div aria-hidden={live} className={`absolute inset-0 ${live ? \"opacity-0\" : \"opacity-100\"}`}>\n        <img ref={photo} src={PHOTO} alt={PHOTO_ALT} crossOrigin=\"anonymous\" width={1600} height={1068} className=\"absolute inset-0 h-full w-full bg-[#1d3f3b] object-cover object-[51%_30%]\" />\n        <p className=\"absolute left-[5%] top-[3%] text-[27vw] font-extrabold leading-none tracking-[-0.055em] text-[#efe9da] opacity-90 sm:left-[4%] sm:top-[6%] sm:text-[15.5vw]\">{MASTHEAD}</p>\n        <div className=\"absolute inset-0 bg-[radial-gradient(ellipse_at_38%_36%,transparent_30%,rgba(4,12,12,0.6)_100%)]\" />\n        <div className=\"absolute left-[62%] top-[36%] h-0 w-0 origin-[0_-700px] motion-safe:animate-[glass-ball-cursor-on-a-wire-sway_5s_ease-in-out_infinite] sm:left-[52%] sm:top-[33%]\">\n          <span className=\"absolute -top-[700px] left-0 h-[620px] w-px bg-gradient-to-b from-white/20 to-white/70\" />\n          <span className=\"absolute left-[80px] top-[110px] h-[170px] w-[170px] -translate-x-1/2 -translate-y-1/2 rounded-full bg-[radial-gradient(circle,rgba(255,244,214,0.5)_0%,rgba(255,244,214,0.28)_45%,rgba(0,0,0,0.3)_62%,transparent_72%)] blur-[3px]\" />\n          <span className=\"absolute left-0 top-0 h-[170px] w-[170px] -translate-x-1/2 -translate-y-1/2 overflow-hidden rounded-full shadow-[inset_0_0_0_1px_rgba(255,255,255,0.35),inset_0_0_26px_6px_rgba(0,0,0,0.55),0_30px_50px_rgba(0,0,0,0.35)]\">\n            <span className=\"absolute left-1/2 top-1/2 h-[700px] w-[1050px] -translate-x-[46%] -translate-y-[62%] rotate-180 bg-cover bg-center\" style={{ backgroundImage: `url(${PHOTO})` }} />\n            <span className=\"absolute left-[18%] top-[12%] h-[26%] w-[20%] -rotate-12 rounded-[40%] bg-gradient-to-b from-white/90 to-white/30 blur-[1px]\" />\n            <span className=\"absolute inset-0 rounded-full shadow-[inset_-10px_-14px_24px_rgba(255,255,255,0.28)]\" />\n          </span>\n          <span className=\"absolute -top-[97px] left-0 h-[14px] w-[16px] -translate-x-1/2 rounded-[3px] bg-gradient-to-r from-[#5b3d12] via-[#f1c877] to-[#6b4a17]\" />\n        </div>\n      </div>\n\n      <canvas ref={canvas} role=\"img\" aria-label={`${PHOTO_ALT}, pasted up as a poster with the masthead ${MASTHEAD}. A clear glass ball hangs in front of it on a wire and shows the picture behind it upside down.`} className={`absolute inset-0 h-full w-full ${live ? \"opacity-100\" : \"opacity-0\"}`} />\n\n      <h2 className=\"sr-only\">{MASTHEAD}. {FOLIO.join(\". \")}.</h2>\n      <div className=\"pointer-events-none absolute inset-x-0 bottom-0 flex items-end justify-between gap-4 bg-gradient-to-t from-black/55 to-transparent px-5 pb-5 pt-16 sm:px-8 sm:pb-7\">\n        <p className=\"max-w-[15rem] text-[11px] font-medium uppercase leading-[1.65] tracking-[0.2em] text-white/85 sm:max-w-none\">{CAPTION}</p>\n        <div className=\"flex shrink-0 gap-2\">\n          <button ref={knock} type=\"button\" aria-label=\"Knock the glass against the wall. Arrow keys carry it.\" aria-describedby=\"glass-ball-cursor-keys\" className={`${pill} w-10 cursor-pointer justify-center px-0 sm:w-auto sm:px-4`}>\n            <svg viewBox=\"0 0 20 20\" aria-hidden=\"true\" className=\"h-4 w-4\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\"><circle cx=\"8\" cy=\"10\" r=\"4.6\" /><path d=\"M16.5 4v12M14 7.5v5\" /></svg>\n            <span className=\"hidden sm:inline\">Knock</span>\n          </button>\n          {moving && (\n            <button type=\"button\" aria-pressed={paused} onClick={() => { api.current?.pause(!paused); setPaused(!paused); }} aria-label={paused ? \"Resume the glass\" : \"Pause the glass\"} className={`${pill} w-10 cursor-pointer justify-center px-0 sm:w-auto sm:px-4`}>\n              <svg viewBox=\"0 0 20 20\" aria-hidden=\"true\" className=\"h-4 w-4\" fill=\"currentColor\">{paused ? <path d=\"M6.5 4.5v11l9-5.5z\" /> : <path d=\"M6 4.5h2.6v11H6zM11.4 4.5H14v11h-2.6z\" />}</svg>\n              <span className=\"hidden sm:inline\">{paused ? \"Play\" : \"Pause\"}</span>\n            </button>\n          )}\n        </div>\n      </div>\n      <p id=\"glass-ball-cursor-keys\" className=\"sr-only\">With this button focused, the arrow keys carry the glass and Space or Enter knocks it against the wall.</p>\n      <p className=\"sr-only\" aria-live=\"polite\">{knocks > 0 ? `The glass has knocked the wall ${knocks} ${knocks === 1 ? \"time\" : \"times\"}.` : \"\"}</p>\n    </div>\n  );\n}\n"}]}
