{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"posable-desk-lamp-on-a-jointed-arm-that-lights-a-still-life-and-throws-its-true-shadow-up-the-wall","type":"registry:block","title":"Posable Desk Lamp on a Jointed Arm That Lights a Still Life and Throws Its True Shadow up the Wall","description":"WebGL scene: an enamel desk lamp on a jointed arm follows the pointer and lights a still life with soft, true shadows. Copy the React code or HTML free.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/posable-desk-lamp-on-a-jointed-arm-that-lights-a-still-life-and-throws-its-true-shadow-up-the-wall.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport type { PointerEvent as ReactPointerEvent } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n/** Everything a visitor is likely to change. Colours are linear red, green, blue from 0 to 1. */\nconst lamp = {\n  /** The enamel on the shade and the foot. */\n  enamel: [0.78, 0.085, 0.03],\n  /** The painted wall behind the desk. */\n  wall: [0.085, 0.2, 0.185],\n  /** The colour of the bulb. Lower the last number for a warmer lamp. */\n  bulb: [1.0, 0.8, 0.56],\n  /** Lengths of the lower and upper arm, in scene units (the vase is 0.62 tall). */\n  lower: 0.92,\n  upper: 0.8,\n  /** How wide the shade lets the light spread. 0.56 is a broad task lamp, 0.8 a tight spot. */\n  spread: 0.56,\n  /** Where the head rests when nobody is steering it: across the desk, then height. */\n  rest: [-0.2, 1.36],\n  /** The share of full resolution drawn. Lower it if the scene runs hot on old phones. */\n  quality: 0.9,\n  maxPixels: 820000,\n};\n\n// The camera and the lamp's foot. The same numbers are used by the shader and by the pointer maths.\nconst CAM = [-0.08, 1.14, 4.6];\nconst LOOK = [-0.14, 0.84, 0.0];\nconst FOCAL = 3.4;\nconst PIVOT = [-1.05, 0.17, 0.25];\nconst SUBJECT = [0.5, 0.42, -0.26];\n\nconst VERTEX = `attribute vec2 position; void main(){ gl_Position = vec4(position, 0.0, 1.0); }`;\n\nconst FRAGMENT = `precision highp float;\nuniform vec2 uRes;\nuniform float uZoom;\nuniform vec3 uJ1;\nuniform vec3 uJ2;\nuniform vec3 uAim;\nuniform float uOn;\nuniform float uSpread;\nuniform vec3 uEnamel;\nuniform vec3 uWall;\nuniform vec3 uBulb;\n\nconst vec3 PIVOT = vec3(${PIVOT[0].toFixed(3)}, ${PIVOT[1].toFixed(3)}, ${PIVOT[2].toFixed(3)});\nconst vec3 VASE = vec3(0.46, 0.0, -0.28);\nconst float WALLZ = -0.86;\n\nfloat hash(vec2 p){ return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }\nfloat noise(vec2 p){\n  vec2 i = floor(p), f = fract(p);\n  vec2 u = f * f * (3.0 - 2.0 * f);\n  return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), u.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), u.x), u.y);\n}\nfloat fbm(vec2 p){ return noise(p) * 0.55 + noise(p * 2.1 + 7.0) * 0.3 + noise(p * 4.3 + 19.0) * 0.15; }\n\nfloat capsule(vec3 p, vec3 a, vec3 b, float r){\n  vec3 pa = p - a, ba = b - a;\n  return length(pa - ba * clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0)) - r;\n}\nfloat cylY(vec3 p, float r, float h){\n  vec2 d = abs(vec2(length(p.xz), p.y)) - vec2(r, h);\n  return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));\n}\nfloat cylZ(vec3 p, float r, float h){\n  vec2 d = abs(vec2(length(p.xy), p.z)) - vec2(r, h);\n  return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));\n}\nfloat cone(vec3 p, vec3 a, vec3 b, float ra, float rb){\n  float rba = rb - ra;\n  float baba = dot(b - a, b - a);\n  float papa = dot(p - a, p - a);\n  float paba = dot(p - a, b - a) / baba;\n  float x = sqrt(max(papa - paba * paba * baba, 0.0));\n  float cax = max(0.0, x - ((paba < 0.5) ? ra : rb));\n  float cay = abs(paba - 0.5) - 0.5;\n  float k = rba * rba + baba;\n  float f = clamp((rba * (x - ra) + paba * baba) / k, 0.0, 1.0);\n  float cbx = x - ra - f * rba;\n  float cby = paba - f;\n  float s = (cbx < 0.0 && cay < 0.0) ? -1.0 : 1.0;\n  return s * sqrt(min(cax * cax + cay * cay * baba, cbx * cbx + cby * cby * baba));\n}\n\nvec2 join(vec2 a, vec2 b){ return a.x < b.x ? a : b; }\n\nfloat vaseShape(vec3 q){\n  float t = clamp(q.y / 0.62, 0.0, 1.0);\n  float r = mix(0.082, 0.05, t) + 0.125 * pow(sin(3.14159 * min(t * 1.2, 1.0)), 1.3) + 0.03 * smoothstep(0.86, 1.0, t);\n  float side = (length(q.xz) - r) * 0.62;\n  return max(side, abs(q.y - 0.31) - 0.31) - 0.004;\n}\n\n// Three dried stems with round seed heads, the vase, two pieces of fruit.\nvec2 stillLife(vec3 p){\n  vec3 q = p - VASE;\n  vec2 d = vec2(vaseShape(q), 7.0);\n  vec3 neck = vec3(0.0, 0.6, 0.0);\n  vec3 f1 = vec3(-0.17, 1.1, 0.04), f2 = vec3(0.05, 1.3, -0.05), f3 = vec3(0.23, 0.98, 0.06);\n  float stems = min(capsule(q, neck, f1, 0.0065), min(capsule(q, neck, f2, 0.0065), capsule(q, neck, f3, 0.0065)));\n  d = join(d, vec2(stems, 8.0));\n  float heads = min(length(q - f1) - 0.052, min(length(q - f2) - 0.058, length(q - f3) - 0.047));\n  d = join(d, vec2(heads, 9.0));\n  d = join(d, vec2(length(q - vec3(0.35, 0.088, 0.2)) - 0.088, 10.0));\n  vec3 a = q - vec3(0.17, 0.066, 0.36);\n  d = join(d, vec2(length(a * vec3(1.0, 1.08, 1.0)) - 0.066, 11.0));\n  return d;\n}\n\n// The foot, the post and the two arms. These can stand between the bulb and the desk.\nvec2 frame(vec3 p){\n  vec3 foot = vec3(PIVOT.x, 0.0, PIVOT.z);\n  vec2 d = vec2(cylY(p - foot - vec3(0.0, 0.03, 0.0), 0.24, 0.014) - 0.016, 13.0);\n  d = join(d, vec2(capsule(p, foot + vec3(0.0, 0.05, 0.0), PIVOT, 0.026), 4.0));\n  vec3 off = vec3(0.0, 0.0, 0.034);\n  float rods = min(capsule(p, PIVOT + off, uJ1 + off, 0.0105), capsule(p, PIVOT - off, uJ1 - off, 0.0105));\n  rods = min(rods, min(capsule(p, uJ1 + off, uJ2 + off, 0.0105), capsule(p, uJ1 - off, uJ2 - off, 0.0105)));\n  d = join(d, vec2(rods, 5.0));\n  float knuckles = min(cylZ(p - PIVOT, 0.036, 0.052), min(cylZ(p - uJ1, 0.034, 0.052), cylZ(p - uJ2, 0.03, 0.05))) - 0.006;\n  d = join(d, vec2(knuckles, 4.0));\n  // The tension spring that runs beside the lower arm.\n  vec3 s0 = PIVOT + vec3(0.07, 0.0, 0.0);\n  vec3 s1 = mix(PIVOT, uJ1, 0.42);\n  d = join(d, vec2(capsule(p, s0, s1, 0.012), 12.0));\n  return d;\n}\n\nvec2 head(vec3 p){\n  vec3 a = uJ2 + uAim * 0.05;\n  vec3 b = uJ2 + uAim * 0.37;\n  float shell = abs(cone(p, a, b, 0.07, 0.215)) - 0.006;\n  shell = max(shell, dot(p - b, uAim) + 0.014);\n  vec2 d = vec2(shell, 3.0);\n  d = join(d, vec2(capsule(p, uJ2 - uAim * 0.03, uJ2 + uAim * 0.07, 0.058), 3.0));\n  d = join(d, vec2(length(p - (uJ2 + uAim * 0.2)) - 0.058, 6.0));\n  return d;\n}\n\nvec2 map(vec3 p){\n  vec2 d = vec2(p.y, 1.0);\n  d = join(d, vec2(p.z - WALLZ, 2.0));\n  d = join(d, stillLife(p));\n  d = join(d, frame(p));\n  d = join(d, head(p));\n  return d;\n}\nfloat blockers(vec3 p){ return min(stillLife(p).x, frame(p).x); }\n\nvec3 normalAt(vec3 p){\n  vec2 e = vec2(0.0012, -0.0012);\n  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);\n}\n\nfloat shadow(vec3 ro, vec3 rd, float far){\n  float res = 1.0;\n  float t = 0.015;\n  for (int i = 0; i < 56; i++) {\n    float h = blockers(ro + rd * t);\n    res = min(res, 24.0 * h / t);\n    t += clamp(h, 0.008, 0.1);\n    if (res < 0.004 || t > far) break;\n  }\n  res = clamp(res, 0.0, 1.0);\n  return res * res * (3.0 - 2.0 * res);\n}\n\nfloat occlusion(vec3 p, vec3 n){\n  float o = 0.0, w = 1.0;\n  for (int i = 1; i <= 4; i++) {\n    float h = 0.035 * float(i);\n    o += (h - map(p + n * h).x) * w;\n    w *= 0.62;\n  }\n  return clamp(1.0 - 2.4 * o, 0.0, 1.0);\n}\n\n// How much lamp light reaches a point, before anything gets in the way.\nfloat beam(vec3 p, vec3 light, out vec3 L, out float far){\n  L = light - p;\n  float d2 = dot(L, L);\n  far = sqrt(d2);\n  L /= far;\n  float inCone = smoothstep(uSpread - 0.2, uSpread + 0.16, dot(-L, uAim));\n  return uOn * 2.3 * inCone / (0.22 + d2);\n}\n\nvoid main(){\n  vec2 uv = (gl_FragCoord.xy * 2.0 - uRes) / uRes.y * uZoom;\n  vec3 ro = vec3(${CAM[0].toFixed(2)}, ${CAM[1].toFixed(2)}, ${CAM[2].toFixed(2)});\n  vec3 ta = vec3(${LOOK[0].toFixed(2)}, ${LOOK[1].toFixed(2)}, ${LOOK[2].toFixed(2)});\n  vec3 fw = normalize(ta - ro);\n  vec3 rt = normalize(cross(fw, vec3(0.0, 1.0, 0.0)));\n  vec3 up = cross(rt, fw);\n  vec3 rd = normalize(fw * ${FOCAL.toFixed(1)} + rt * uv.x + up * uv.y);\n\n  float t = 0.5;\n  vec2 hit = vec2(0.0);\n  for (int i = 0; i < 90; i++) {\n    hit = map(ro + rd * t);\n    if (hit.x < 0.0007 * t || t > 14.0) break;\n    t += hit.x * 0.92;\n  }\n  vec3 p = ro + rd * t;\n  vec3 n = normalAt(p);\n  float id = hit.y;\n  vec3 light = uJ2 + uAim * 0.2;\n\n  vec3 albedo = vec3(0.5);\n  float gloss = 0.0, shine = 20.0, metal = 0.0;\n  vec3 glow = vec3(0.0);\n  if (id < 1.5) {\n    // An oiled walnut top: long grain, darker streaks, a seam between boards.\n    float g = fbm(vec2(p.x * 1.3 + 3.0, p.z * 26.0)) * 0.7 + fbm(vec2(p.x * 7.0, p.z * 90.0)) * 0.3;\n    float ring = sin((p.z * 9.0 + fbm(vec2(p.x * 0.8, p.z * 3.0)) * 5.0) * 3.0) * 0.5 + 0.5;\n    albedo = mix(vec3(0.115, 0.052, 0.026), vec3(0.36, 0.185, 0.09), g * 0.75 + ring * 0.2);\n    float seam = smoothstep(0.012, 0.0, abs(fract(p.z * 1.9 + 0.31) - 0.5) * 0.52);\n    albedo *= 1.0 - seam * 0.45;\n    gloss = 0.22; shine = 46.0;\n  } else if (id < 2.5) {\n    float m = fbm(p.xy * 2.2) * 0.6 + fbm(p.xy * 11.0) * 0.4;\n    albedo = uWall * (0.86 + m * 0.3);\n    n = normalize(n + vec3(noise(p.xy * 70.0) - 0.5, noise(p.yx * 70.0 + 9.0) - 0.5, 0.0) * 0.05);\n  } else if (id < 3.5) {\n    albedo = uEnamel; gloss = 1.0; shine = 90.0;\n    // Inside, the shade is white enamel that glows with the bulb.\n    vec3 q = p - uJ2;\n    vec3 radial = q - uAim * dot(q, uAim);\n    if (dot(n, radial) < 0.0 && dot(q, uAim) > 0.06) {\n      albedo = vec3(0.86, 0.84, 0.8);\n      glow = uBulb * uOn * (0.5 + 0.9 * smoothstep(0.34, 0.1, dot(q, uAim)));\n      gloss = 0.1;\n    }\n  } else if (id < 4.5) {\n    albedo = vec3(0.86, 0.6, 0.27); metal = 1.0; gloss = 1.0; shine = 60.0;\n  } else if (id < 5.5) {\n    albedo = vec3(0.78, 0.78, 0.8); metal = 1.0; gloss = 1.0; shine = 120.0;\n  } else if (id < 6.5) {\n    albedo = vec3(0.8, 0.78, 0.74);\n    glow = uBulb * uOn * 3.2;\n  } else if (id < 7.5) {\n    // Unglazed stoneware with a few iron specks; the mouth is dark.\n    float speck = smoothstep(0.78, 0.86, noise(p.xy * 85.0 + p.z * 40.0));\n    albedo = vec3(0.82, 0.78, 0.7) * (0.93 + fbm(p.xy * 9.0) * 0.12) - speck * 0.22;\n    vec3 q = p - VASE;\n    if (q.y > 0.61 && length(q.xz) < 0.062) albedo = vec3(0.03);\n    gloss = 0.12; shine = 24.0;\n  } else if (id < 8.5) {\n    albedo = vec3(0.36, 0.31, 0.16);\n  } else if (id < 9.5) {\n    albedo = vec3(0.8, 0.5, 0.045);\n    n = normalize(n + (vec3(noise(p.xy * 110.0), noise(p.yz * 110.0), noise(p.zx * 110.0)) - 0.5) * 0.7);\n  } else if (id < 10.5) {\n    float bloom = fbm(p.xy * 14.0 + p.z * 9.0);\n    albedo = mix(vec3(0.12, 0.022, 0.1), vec3(0.2, 0.09, 0.26), bloom * 0.7);\n    gloss = 0.5; shine = 70.0;\n  } else if (id < 11.5) {\n    albedo = mix(vec3(0.9, 0.42, 0.07), vec3(0.86, 0.2, 0.06), smoothstep(0.3, 0.75, fbm(p.xy * 9.0 + 4.0)));\n    gloss = 0.16; shine = 18.0;\n  } else if (id > 12.5) {\n    albedo = uEnamel; gloss = 0.7; shine = 90.0;\n  } else {\n    albedo = vec3(0.72, 0.72, 0.74); metal = 1.0; gloss = 0.9; shine = 80.0;\n    albedo *= 0.55 + 0.45 * sin(dot(p - PIVOT, normalize(uJ1 - PIVOT)) * 520.0);\n  }\n\n  float ao = occlusion(p, n);\n  vec3 col = vec3(0.0);\n\n  // The lamp.\n  vec3 L; float far;\n  float I = beam(p, light, L, far);\n  bool ownHead = (id > 2.5 && id < 3.5) || (id > 5.5 && id < 6.5);\n  float sh = 1.0;\n  if (I > 0.002 && !ownHead) sh = shadow(p + n * 0.004, L, far - 0.12);\n  if (ownHead) I = 0.0;\n  float ndl = max(dot(n, L), 0.0);\n  vec3 hv = normalize(L - rd);\n  float spec = pow(max(dot(n, hv), 0.0), shine) * gloss;\n  vec3 direct = uBulb * I * sh;\n  col += albedo * direct * ndl * (1.0 - metal * 0.75);\n  col += mix(vec3(1.0), albedo, metal) * direct * spec * (0.6 + metal * 2.4) * step(0.0, dot(n, L));\n\n  // Warm light coming back up off the desk and off the lit wall.\n  vec3 Lb; float fb;\n  float pool = beam(vec3(p.x, 0.0, p.z), light, Lb, fb) * max(Lb.y, 0.0);\n  col += albedo * uBulb * vec3(0.55, 0.3, 0.16) * pool * 0.22 * (0.5 - 0.5 * n.y) * exp(-2.2 * p.y) * ao;\n  float back = beam(vec3(p.x, p.y, WALLZ), light, Lb, fb) * max(Lb.z * -1.0, 0.0);\n  col += albedo * uBulb * uWall * back * 0.5 * max(-n.z, 0.0) * ao;\n\n  // The rest of the room: a cool evening window somewhere behind the camera.\n  vec3 fill = normalize(vec3(0.5, 0.62, 0.78));\n  float fd = max(dot(n, fill), 0.0);\n  vec3 room = vec3(0.36, 0.45, 0.68) * 0.08 + uBulb * vec3(0.55, 0.42, 0.32) * 0.11 * uOn;\n  col += albedo * room * (0.3 + 0.7 * fd) * ao * (1.0 - metal * 0.6);\n  vec3 r = reflect(rd, n);\n  // Polished parts mirror a soft bright window and the dark of the room.\n  float win = smoothstep(0.72, 0.9, dot(r, fill)) * 1.6 + 0.25 * smoothstep(-0.2, 0.9, r.y);\n  float fres = pow(1.0 - max(dot(n, -rd), 0.0), 4.0);\n  col += mix(vec3(1.0), albedo, metal) * vec3(0.5, 0.6, 0.85) * win * (metal * 0.6 + gloss * (0.11 + fres * 0.45)) * ao;\n  col += mix(vec3(1.0), albedo, metal) * uBulb * metal * uOn * 0.1 * smoothstep(0.2, 1.0, dot(r, normalize(light - p)));\n\n  col += glow;\n\n  // The bulb seen through the air: a little bloom round the mouth of the shade.\n  vec3 toLight = light - ro;\n  float along = dot(toLight, rd);\n  float miss = length(toLight - rd * along);\n  float facing = smoothstep(-0.25, 0.6, dot(normalize(ro - light), uAim));\n  float clear = step(along, t + 0.25);\n  col += uBulb * uOn * (0.012 * exp(-miss * miss * 26.0) + 0.5 * facing * exp(-miss * miss * 240.0)) * clear;\n\n  // Film response, a soft corner fall-off and enough grain to stop banding in the dark wall.\n  col *= 1.25;\n  col = (col * (2.51 * col + 0.03)) / (col * (2.43 * col + 0.59) + 0.14);\n  col = pow(clamp(col, 0.0, 1.0), vec3(0.4545));\n  vec2 v = gl_FragCoord.xy / uRes - 0.5;\n  col *= 1.0 - dot(v, v) * 0.55;\n  col += (fract(52.9829189 * fract(dot(gl_FragCoord.xy, vec2(0.06711056, 0.00583715)))) - 0.5) * 0.012;\n  gl_FragColor = vec4(col, 1.0);\n}`;\n\ntype V3 = [number, number, number];\nconst sub = (a: number[], b: number[]): V3 => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];\nconst cross = (a: number[], b: number[]): 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: number[]): V3 => {\n  const l = Math.hypot(a[0], a[1], a[2]) || 1;\n  return [a[0] / l, a[1] / l, a[2] / l];\n};\nconst clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));\n\n/** The two-bone arm: where the elbow and the head end up for a wanted head position. */\nfunction pose(x: number, y: number) {\n  let dx = x - PIVOT[0];\n  let dy = y - PIVOT[1];\n  const reach = clamp(Math.hypot(dx, dy), Math.abs(lamp.lower - lamp.upper) + 0.22, lamp.lower + lamp.upper - 0.03);\n  const len = Math.hypot(dx, dy) || 1;\n  dx = (dx / len) * reach;\n  dy = (dy / len) * reach;\n  const bend = Math.acos(clamp((lamp.lower * lamp.lower + reach * reach - lamp.upper * lamp.upper) / (2 * lamp.lower * reach), -1, 1));\n  const a = Math.atan2(dy, dx) + bend;\n  const j1: V3 = [PIVOT[0] + Math.cos(a) * lamp.lower, PIVOT[1] + Math.sin(a) * lamp.lower, PIVOT[2]];\n  const j2: V3 = [PIVOT[0] + dx, PIVOT[1] + dy, PIVOT[2]];\n  return { j1, j2 };\n}\n\n/** Keeps a wanted head position somewhere the lamp can really go. */\nfunction tidy(x: number, y: number): [number, number] {\n  return [clamp(x, -1.0, 0.66), clamp(y, 0.74, 1.62)];\n}\n\nexport default function PosableDeskLamp() {\n  const reduce = useReducedMotion();\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [live, setLive] = useState(false);\n  const [on, setOn] = useState(true);\n  const [reachValue, setReachValue] = useState(50);\n  const [liftValue, setLiftValue] = useState(60);\n  const want = useRef({ x: lamp.rest[0], y: lamp.rest[1], touched: -1e9 });\n  const power = useRef(true);\n  power.current = on;\n\n  useEffect(() => {\n    const cv = canvas.current;\n    const el = stage.current;\n    const gl = cv?.getContext(\"webgl\", { antialias: false, alpha: false });\n    if (!cv || !el || !gl) return; // no WebGL: the painted still underneath stays\n\n    const compile = (type: number, source: string) => {\n      const s = gl.createShader(type)!;\n      gl.shaderSource(s, source);\n      gl.compileShader(s);\n      return s;\n    };\n    const program = gl.createProgram()!;\n    gl.attachShader(program, compile(gl.VERTEX_SHADER, VERTEX));\n    gl.attachShader(program, compile(gl.FRAGMENT_SHADER, FRAGMENT));\n    gl.linkProgram(program);\n    if (!gl.getProgramParameter(program, gl.LINK_STATUS)) return;\n    gl.useProgram(program);\n    gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());\n    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);\n    const position = gl.getAttribLocation(program, \"position\");\n    gl.enableVertexAttribArray(position);\n    gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0);\n    const at = (name: string) => gl.getUniformLocation(program, name);\n    const u = { res: at(\"uRes\"), zoom: at(\"uZoom\"), j1: at(\"uJ1\"), j2: at(\"uJ2\"), aim: at(\"uAim\"), on: at(\"uOn\") };\n    gl.uniform1f(at(\"uSpread\"), lamp.spread);\n    gl.uniform3fv(at(\"uEnamel\"), lamp.enamel);\n    gl.uniform3fv(at(\"uWall\"), lamp.wall);\n    gl.uniform3fv(at(\"uBulb\"), lamp.bulb);\n\n    // The head is on a spring, so it overshoots a little and settles, as a sprung arm does.\n    const headAt = { x: lamp.rest[0], y: lamp.rest[1], vx: 0, vy: 0 };\n    const aim = { v: unit(sub(SUBJECT, [lamp.rest[0], lamp.rest[1], PIVOT[2]])) };\n    let glowNow = power.current ? 1 : 0;\n    let raf = 0;\n    let visible = true;\n    let before = 0;\n    let clock = 0;\n\n    const draw = () => {\n      const { j1, j2 } = pose(headAt.x, headAt.y);\n      gl.uniform3fv(u.j1, j1);\n      gl.uniform3fv(u.j2, j2);\n      gl.uniform3fv(u.aim, aim.v);\n      gl.uniform1f(u.on, glowNow);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n    };\n    const resize = () => {\n      const w = cv.clientWidth || 1;\n      const h = cv.clientHeight || 1;\n      const scale = Math.min(Math.min(Math.max(window.devicePixelRatio || 1, 1), 2) * lamp.quality, Math.sqrt(lamp.maxPixels / (w * h)));\n      cv.width = Math.max(1, Math.round(w * scale));\n      cv.height = Math.max(1, Math.round(h * scale));\n      gl.viewport(0, 0, cv.width, cv.height);\n      gl.uniform2f(u.res, cv.width, cv.height);\n      gl.uniform1f(u.zoom, Math.max(0.82, 1.2 / (w / h)));\n      draw();\n      setLive(true);\n    };\n    const step = (dt: number, now: number) => {\n      clock += dt;\n      const w = want.current;\n      let tx = w.x;\n      let ty = w.y;\n      // Left alone, the lamp is slowly swung across the desk and back.\n      if (!reduce && now - w.touched > 2800) {\n        tx = lamp.rest[0] + 0.52 * Math.sin(clock * 0.42 - 0.5);\n        ty = lamp.rest[1] + 0.2 * Math.sin(clock * 0.31 + 1.2);\n        w.x = tx;\n        w.y = ty;\n      }\n      if (reduce) {\n        headAt.x = tx;\n        headAt.y = ty;\n      } else {\n        headAt.vx += ((tx - headAt.x) * 70 - headAt.vx * 9.5) * dt;\n        headAt.vy += ((ty - headAt.y) * 70 - headAt.vy * 9.5) * dt;\n        headAt.x += headAt.vx * dt;\n        headAt.y += headAt.vy * dt;\n      }\n      // The shade keeps looking at the still life, and nods a little with the arm's speed.\n      const { j2 } = pose(headAt.x, headAt.y);\n      const to = unit(sub([SUBJECT[0] + headAt.vx * 0.12, SUBJECT[1] + headAt.vy * 0.12, SUBJECT[2]], j2));\n      const k = reduce ? 1 : 1 - Math.exp(-dt * 9);\n      aim.v = unit([aim.v[0] + (to[0] - aim.v[0]) * k, aim.v[1] + (to[1] - aim.v[1]) * k, aim.v[2] + (to[2] - aim.v[2]) * k]);\n      // A filament does not switch instantly: quick to light, slower to die.\n      const goal = power.current ? 1 : 0;\n      glowNow += (goal - glowNow) * (reduce ? 1 : 1 - Math.exp(-dt * (goal ? 9 : 5)));\n    };\n    const loop = (now: number) => {\n      raf = requestAnimationFrame(loop);\n      const dt = Math.min((now - (before || now)) / 1000, 0.04);\n      before = now;\n      step(dt, now);\n      draw();\n    };\n    const wake = () => {\n      cancelAnimationFrame(raf);\n      raf = 0;\n      before = 0;\n      if (visible && !document.hidden) raf = requestAnimationFrame(loop);\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(el);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      wake();\n    });\n    io.observe(el);\n    document.addEventListener(\"visibilitychange\", wake);\n    resize();\n    wake();\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", wake);\n    };\n  }, [reduce]);\n\n  /** Turns a pointer position into a place on the upright plane the arm moves in. */\n  function steer(e: ReactPointerEvent<HTMLDivElement>) {\n    if (e.pointerType !== \"mouse\" && e.type === \"pointermove\" && e.buttons === 0) return;\n    const r = e.currentTarget.getBoundingClientRect();\n    const zoom = Math.max(0.82, 1.2 / (r.width / r.height));\n    const ux = (((e.clientX - r.left) / r.width) * 2 - 1) * (r.width / r.height) * zoom;\n    const uy = (1 - ((e.clientY - r.top) / r.height) * 2) * zoom;\n    const fw = unit(sub(LOOK, CAM));\n    const rt = unit(cross(fw, [0, 1, 0]));\n    const up = cross(rt, fw);\n    const rd = unit([fw[0] * FOCAL + rt[0] * ux + up[0] * uy, fw[1] * FOCAL + rt[1] * ux + up[1] * uy, fw[2] * FOCAL + rt[2] * ux + up[2] * uy]);\n    const t = (PIVOT[2] - CAM[2]) / rd[2];\n    const [x, y] = tidy(CAM[0] + rd[0] * t, CAM[1] + rd[1] * t);\n    want.current = { x, y, touched: performance.now() };\n    setReachValue(Math.round(((x + 1.0) / 1.66) * 100));\n    setLiftValue(Math.round(((y - 0.74) / 0.88) * 100));\n  }\n  function slide(reach: number, lift: number) {\n    setReachValue(reach);\n    setLiftValue(lift);\n    want.current = { x: -1.0 + (reach / 100) * 1.66, y: 0.74 + (lift / 100) * 0.88, touched: performance.now() + 6000 };\n  }\n\n  return (\n    <section className=\"relative isolate w-full overflow-hidden bg-[#0d1614]\">\n      <div\n        ref={stage}\n        onPointerMove={steer}\n        onPointerDown={steer}\n        className=\"relative h-[540px] w-full cursor-grab touch-pan-y select-none active:cursor-grabbing sm:h-[680px]\"\n      >\n        {/* The still version, shown until the first frame is drawn and wherever WebGL is missing. */}\n        <div aria-hidden=\"true\" className=\"absolute inset-0\" style={{ background: \"radial-gradient(46% 40% at 62% 60%, #6f7f60 0%, #2f4a41 34%, #15231f 62%, #0d1614 100%)\" }} />\n        <div aria-hidden=\"true\" className=\"absolute inset-x-0 bottom-0 h-[30%]\" style={{ background: \"linear-gradient(180deg, #3a1f12 0%, #150b06 100%)\" }} />\n        <canvas ref={canvas} aria-hidden=\"true\" className={`absolute inset-0 h-full w-full transition-opacity duration-500 ${live ? \"opacity-100\" : \"opacity-0\"}`} />\n\n        <label htmlFor=\"pdl-reach\" className=\"sr-only\">Lamp head, left to right</label>\n        <input id=\"pdl-reach\" type=\"range\" min={0} max={100} step={2} value={reachValue} onChange={(e) => slide(Number(e.target.value), liftValue)} className=\"peer/reach sr-only\" />\n        <label htmlFor=\"pdl-lift\" className=\"sr-only\">Lamp head, low to high</label>\n        <input id=\"pdl-lift\" type=\"range\" min={0} max={100} step={2} value={liftValue} onChange={(e) => slide(reachValue, Number(e.target.value))} className=\"peer/lift sr-only\" />\n        <span aria-hidden=\"true\" className=\"pointer-events-none absolute inset-3 rounded-2xl ring-2 ring-white/80 opacity-0 peer-focus-visible/reach:opacity-100 peer-focus-visible/lift:opacity-100\" />\n      </div>\n\n      <div className=\"pointer-events-none absolute inset-x-0 bottom-0 flex items-end justify-between gap-4 px-5 pb-5 sm:px-8 sm:pb-7\">\n        <p className=\"text-[13px] leading-5 text-[#c9d3cc]\">\n          <span className=\"font-semibold text-white\">Task lamp</span>\n          <span className=\"mx-2 text-[#7f8f87]\">/</span>\n          move its head\n        </p>\n        <button\n          type=\"button\"\n          role=\"switch\"\n          aria-checked={on}\n          onClick={() => setOn((v) => !v)}\n          className=\"pointer-events-auto inline-flex h-10 shrink-0 items-center gap-2.5 rounded-full bg-black/45 pl-3 pr-4 text-[13px] font-medium text-white ring-1 ring-white/15 backdrop-blur transition-colors hover:bg-black/60 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white\"\n        >\n          <span aria-hidden=\"true\" className={`h-2.5 w-2.5 rounded-full transition-all duration-300 ${on ? \"bg-[#ffcf8a] shadow-[0_0_10px_3px_rgba(255,190,110,0.7)]\" : \"bg-[#4a544f]\"}`} />\n          {on ? \"Lamp on\" : \"Lamp off\"}\n        </button>\n      </div>\n    </section>\n  );\n}\n"}]}
