{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"snow-globe-you-shake-with-drifting-flakes-over-a-tiny-cabin-on-a-turned-walnut-base","type":"registry:component","title":"Snow Globe You Shake with Drifting Flakes over a Tiny Cabin on a Turned Walnut Base","description":"Canvas snow globe: grab and shake it, and the water lags behind the glass, whirls the flakes up and lets them sink over a cabin. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/snow-globe-you-shake-with-drifting-flakes-over-a-tiny-cabin-on-a-turned-walnut-base.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n/** How many flakes float in the water. 260 reads as a proper flurry; halve it for a calmer globe. */\nconst FLAKES = 260;\n/** Fall speed of a settled flurry in pixels a second. Lower is thicker liquid. */\nconst SETTLE = 16;\n/** The engraved brass plate on the base. */\nconst PLATE = \"FIRST SNOW\";\n/** Lamp colour in the cabin window, as red, green, blue. */\nconst LAMP = \"255,186,92\";\n\nconst R = 188; // glass radius in drawing units\nconst CY = 262; // centre of the glass\nconst FOOT = 590; // where the base meets the table\n\n// The turned base, top to bottom: [height, half width]. Edit the pairs to cut a different profile.\nconst PROFILE: [number, number][] = [[404, 124], [426, 131], [432, 119], [447, 143], [462, 126], [478, 151], [538, 166], [548, 176], [560, 169], [573, 183], [FOOT, 185]];\n\nconst ground = (x: number) => CY + R * 0.5 - 30 * Math.exp(-(((x + 34) / 92) ** 2)) - 12 * Math.exp(-(((x - 96) / 52) ** 2));\n\nexport default function SnowGlobeYouShake() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const hit = useRef<HTMLDivElement>(null);\n  const api = useRef<{ shake: () => void } | null>(null);\n  const [shakes, setShakes] = useState(0);\n\n  useEffect(() => {\n    const cv = canvas.current, pad = hit.current;\n    if (!cv || !pad) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let dpr = 1, scale = 1, ox = 0, oy = 0, box = cv.getBoundingClientRect();\n    let baseLayer: HTMLCanvasElement | null = null, sceneLayer: HTMLCanvasElement | null = null;\n\n    let seed = 11;\n    const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n\n    type Flake = { x: number; y: number; vx: number; vy: number; z: number; ph: number; rest: boolean };\n    const scatter = (f: Flake) => {\n      for (let k = 0; k < 40; k++) {\n        const a = rand() * Math.PI * 2, d = Math.sqrt(rand()) * (R - 12);\n        f.x = Math.cos(a) * d; f.y = CY + Math.sin(a) * d;\n        if (f.y < ground(f.x) - 3) break;\n      }\n      f.vx = 0; f.vy = 0; f.rest = false;\n    };\n    const flakes: Flake[] = Array.from({ length: FLAKES }, () => {\n      const f: Flake = { x: 0, y: 0, vx: 0, vy: 0, z: rand(), ph: rand() * 6.28, rest: false };\n      scatter(f);\n      return f;\n    });\n\n    // The whole object: where it is, how fast it moves, and how stirred up the water is.\n    const s = { x: 0, y: 0, vx: 0, vy: 0, tx: 0, ty: 0, drag: false, ux: 0, uy: 0, energy: reduce ? 0 : 210, light: 0, lightTo: 0, shakeAt: -1 };\n\n    const bake = (x0: number, y0: number, w: number, h: number, paint: (c: CanvasRenderingContext2D) => void) => {\n      const off = document.createElement(\"canvas\");\n      const k = dpr * scale;\n      off.width = Math.max(1, Math.ceil(w * k)); off.height = Math.max(1, Math.ceil(h * k));\n      const c = off.getContext(\"2d\");\n      if (c) { c.setTransform(k, 0, 0, k, -x0 * k, -y0 * k); paint(c); }\n      return off;\n    };\n\n    const outline = (c: CanvasRenderingContext2D) => {\n      const p = PROFILE;\n      c.beginPath(); c.moveTo(-p[0][1], p[0][0]);\n      for (let i = 1; i < p.length; i++) { const my = (p[i - 1][0] + p[i][0]) / 2; c.bezierCurveTo(-p[i - 1][1], my, -p[i][1], my, -p[i][1], p[i][0]); }\n      c.ellipse(0, FOOT, p[p.length - 1][1], 13, 0, Math.PI, 0, true);\n      for (let i = p.length - 1; i > 0; i--) { const my = (p[i - 1][0] + p[i][0]) / 2; c.bezierCurveTo(p[i][1], my, p[i - 1][1], my, p[i - 1][1], p[i - 1][0]); }\n      c.closePath();\n    };\n\n    const paintBase = (c: CanvasRenderingContext2D) => {\n      const W = 185;\n      c.save(); outline(c); c.clip();\n      // A cylinder of oiled walnut: dark at both edges, one broad highlight left of centre.\n      const g = c.createLinearGradient(-W, 0, W, 0);\n      const wood: [number, string][] = [[0, \"#150b06\"], [0.07, \"#3a2013\"], [0.2, \"#7a4a2a\"], [0.31, \"#a8703f\"], [0.4, \"#8a5630\"], [0.6, \"#56311b\"], [0.82, \"#2b170d\"], [0.94, \"#1a0e08\"], [1, \"#3a2416\"]];\n      for (const [p, col] of wood) g.addColorStop(p, col);\n      c.fillStyle = g; c.fillRect(-W - 4, 396, W * 2 + 8, 220);\n      // Grain follows the turning, so it wraps the base as shallow arcs.\n      for (let i = 0; i < 150; i++) {\n        const y = 398 + rand() * 204, v = rand();\n        c.beginPath(); c.ellipse(rand() * 30 - 15, y - 10, W + 30, 9 + rand() * 6, 0, 0.12 + rand() * 0.5, Math.PI - 0.12 - rand() * 0.5);\n        c.lineWidth = 0.5 + rand() * 1.3;\n        c.strokeStyle = v > 0.45 ? `rgba(18,8,3,${0.1 + rand() * 0.24})` : `rgba(255,196,140,${0.03 + rand() * 0.07})`;\n        c.stroke();\n      }\n      // Every bead and cove catches light on its upper slope and falls into shade underneath.\n      for (let i = 1; i < PROFILE.length; i++) {\n        const [y0, w0] = PROFILE[i - 1], [y1, w1] = PROFILE[i];\n        const v = c.createLinearGradient(0, y0, 0, y1);\n        if (w1 > w0 + 1) { v.addColorStop(0, \"rgba(255,214,170,0.02)\"); v.addColorStop(0.55, \"rgba(255,214,170,0.2)\"); v.addColorStop(1, \"rgba(255,214,170,0.04)\"); }\n        else if (w1 < w0 - 1) { v.addColorStop(0, \"rgba(0,0,0,0.12)\"); v.addColorStop(1, \"rgba(0,0,0,0.6)\"); }\n        else { v.addColorStop(0, \"rgba(0,0,0,0)\"); v.addColorStop(1, \"rgba(0,0,0,0)\"); }\n        c.fillStyle = v; c.fillRect(-W - 4, y0, W * 2 + 8, y1 - y0);\n        c.beginPath(); c.ellipse(0, y1 - 9, w1 + 1, 9, 0, 0.05, Math.PI - 0.05);\n        c.lineWidth = 1; c.strokeStyle = w1 < w0 ? \"rgba(0,0,0,0.5)\" : \"rgba(255,225,190,0.16)\"; c.stroke();\n      }\n      // The long reflection of the lamp down the lacquer.\n      const streak = c.createLinearGradient(-112, 0, -52, 0);\n      streak.addColorStop(0, \"rgba(255,236,214,0)\"); streak.addColorStop(0.5, \"rgba(255,236,214,0.2)\"); streak.addColorStop(1, \"rgba(255,236,214,0)\");\n      c.fillStyle = streak; c.fillRect(-112, 396, 60, 220);\n      const thin = c.createLinearGradient(-92, 0, -80, 0);\n      thin.addColorStop(0, \"rgba(255,244,228,0)\"); thin.addColorStop(0.5, \"rgba(255,244,228,0.34)\"); thin.addColorStop(1, \"rgba(255,244,228,0)\");\n      c.fillStyle = thin; c.fillRect(-92, 478, 12, 62);\n      const back = c.createLinearGradient(150, 0, 186, 0);\n      back.addColorStop(0, \"rgba(120,160,230,0)\"); back.addColorStop(1, \"rgba(120,160,230,0.22)\");\n      c.fillStyle = back; c.fillRect(150, 400, 40, 210);\n      const foot = c.createLinearGradient(0, 566, 0, 606);\n      foot.addColorStop(0, \"rgba(0,0,0,0)\"); foot.addColorStop(1, \"rgba(0,0,0,0.55)\");\n      c.fillStyle = foot; c.fillRect(-W - 4, 566, W * 2 + 8, 40);\n      c.restore();\n\n      // Brass plate, pinned with two round-head nails.\n      const px = -66, py = 494, pw = 132, ph = 30;\n      c.save();\n      c.shadowColor = \"rgba(0,0,0,0.6)\"; c.shadowBlur = 5; c.shadowOffsetY = 2 * dpr * scale;\n      const brass = c.createLinearGradient(px, py, px + pw, py + ph);\n      brass.addColorStop(0, \"#f3dc9a\"); brass.addColorStop(0.3, \"#c79a3e\"); brass.addColorStop(0.55, \"#e8cb7c\"); brass.addColorStop(1, \"#7d5a1a\");\n      c.beginPath(); c.roundRect(px, py, pw, ph, 4); c.fillStyle = brass; c.fill();\n      c.restore();\n      c.lineWidth = 0.8; c.strokeStyle = \"rgba(255,244,200,0.7)\"; c.beginPath(); c.roundRect(px + 0.6, py + 0.6, pw - 1.2, ph - 1.2, 3.5); c.stroke();\n      c.strokeStyle = \"rgba(60,38,6,0.5)\"; c.beginPath(); c.roundRect(px + 4, py + 4, pw - 8, ph - 8, 2); c.stroke();\n      c.font = \"600 10.5px Georgia, 'Times New Roman', serif\"; c.textAlign = \"center\"; c.textBaseline = \"middle\";\n      const spaced = PLATE.split(\"\").join(\"\\u200A\\u200A\");\n      c.fillStyle = \"rgba(255,246,210,0.7)\"; c.fillText(spaced, 0, py + ph / 2 + 1.4);\n      c.fillStyle = \"#4a3208\"; c.fillText(spaced, 0, py + ph / 2 + 0.6);\n      for (const nx of [px + 9, px + pw - 9]) {\n        c.beginPath(); c.arc(nx, py + ph / 2, 2.2, 0, Math.PI * 2); c.fillStyle = \"#8a6520\"; c.fill();\n        c.beginPath(); c.arc(nx - 0.6, py + ph / 2 - 0.7, 0.9, 0, Math.PI * 2); c.fillStyle = \"rgba(255,250,220,0.9)\"; c.fill();\n      }\n    };\n\n    // The brass collar the glass is bedded in. `front` is the half that overlaps the sphere.\n    const collar = (c: CanvasRenderingContext2D, front: boolean) => {\n      const y = 404, w = 124, h = 15;\n      const g = c.createLinearGradient(-w, 0, w, 0);\n      const stops: [number, string][] = [[0, \"#3d2a0a\"], [0.1, \"#8f6b22\"], [0.26, \"#f1d68c\"], [0.33, \"#fff6cf\"], [0.42, \"#c9a045\"], [0.66, \"#7a5918\"], [0.9, \"#4a340d\"], [1, \"#b08a38\"]];\n      for (const [p, col] of stops) g.addColorStop(p, col);\n      c.beginPath();\n      if (front) { c.ellipse(0, y, w, 17, 0, 0, Math.PI); c.lineTo(-w - 2, y + h); c.ellipse(0, y + h, w + 2, 18, 0, Math.PI, 0, true); c.closePath(); }\n      else c.ellipse(0, y, w, 17, 0, 0, Math.PI * 2);\n      c.fillStyle = front ? g : \"#2a1c07\"; c.fill();\n      if (front) {\n        c.beginPath(); c.ellipse(0, y, w, 17, 0, 0.04, Math.PI - 0.04); c.lineWidth = 1.4; c.strokeStyle = \"rgba(255,248,214,0.75)\"; c.stroke();\n        c.beginPath(); c.ellipse(0, y + h, w + 2, 18, 0, 0.04, Math.PI - 0.04); c.lineWidth = 1.2; c.strokeStyle = \"rgba(30,18,2,0.7)\"; c.stroke();\n      }\n    };\n\n    const pine = (c: CanvasRenderingContext2D, x: number, by: number, h: number, dim: number) => {\n      c.fillStyle = \"#2a1a10\"; c.fillRect(x - h * 0.035, by - h * 0.12, h * 0.07, h * 0.14);\n      const tiers = 5;\n      for (let i = 0; i < tiers; i++) {\n        const k = i / tiers, top = by - h + k * h * 0.74, bot = by - h * 0.08 - (tiers - 1 - i) * h * 0.13, half = h * (0.11 + k * 0.26);\n        const g = c.createLinearGradient(x - half, 0, x + half, 0);\n        g.addColorStop(0, `rgba(${62 * dim | 0},${122 * dim | 0},${92 * dim | 0},1)`); g.addColorStop(0.45, `rgba(${28 * dim | 0},${78 * dim | 0},${60 * dim | 0},1)`); g.addColorStop(1, `rgba(${8 * dim | 0},${34 * dim | 0},${30 * dim | 0},1)`);\n        c.beginPath(); c.moveTo(x, top);\n        c.quadraticCurveTo(x + half * 0.5, top + (bot - top) * 0.7, x + half, bot);\n        c.quadraticCurveTo(x + half * 0.5, bot + h * 0.035, x, bot - h * 0.012);\n        c.quadraticCurveTo(x - half * 0.5, bot + h * 0.035, x - half, bot);\n        c.quadraticCurveTo(x - half * 0.5, top + (bot - top) * 0.7, x, top); c.closePath();\n        c.fillStyle = g; c.fill();\n        // Snow lying on the lit side of each bough.\n        c.beginPath(); c.moveTo(x, top - 1);\n        c.quadraticCurveTo(x - half * 0.5, top + (bot - top) * 0.66, x - half * 0.96, bot - 1);\n        c.quadraticCurveTo(x - half * 0.5, bot - h * 0.02, x - half * 0.18, top + (bot - top) * 0.62);\n        c.quadraticCurveTo(x + half * 0.08, top + (bot - top) * 0.5, x + half * 0.3, top + (bot - top) * 0.56);\n        c.quadraticCurveTo(x + half * 0.16, top + (bot - top) * 0.28, x, top - 1); c.closePath();\n        c.fillStyle = `rgb(${250 * (0.55 + dim * 0.45) | 0},${252 * (0.6 + dim * 0.4) | 0},${255 * (0.72 + dim * 0.28) | 0})`; c.fill();\n      }\n    };\n\n    const paintScene = (c: CanvasRenderingContext2D) => {\n      c.save(); c.beginPath(); c.arc(0, CY, R - 1, 0, Math.PI * 2); c.clip();\n      pine(c, -134, ground(-134) + 4, 78, 0.62); pine(c, 58, ground(58) + 2, 96, 0.62); pine(c, 146, ground(146) + 3, 60, 0.6); pine(c, -96, ground(-96) + 3, 58, 0.55);\n      // The drift.\n      c.beginPath(); c.moveTo(-R, CY + R);\n      for (let x = -R; x <= R; x += 6) c.lineTo(x, ground(x));\n      c.lineTo(R, CY + R); c.closePath();\n      const snow = c.createLinearGradient(0, CY + R * 0.3, 0, CY + R * 0.86);\n      snow.addColorStop(0, \"#fbfdff\"); snow.addColorStop(0.3, \"#dce8f7\"); snow.addColorStop(0.7, \"#9db6d8\"); snow.addColorStop(1, \"#5e7ba6\");\n      c.fillStyle = snow; c.fill();\n      const side = c.createLinearGradient(-R, 0, R, 0);\n      side.addColorStop(0, \"rgba(255,255,255,0.25)\"); side.addColorStop(0.4, \"rgba(255,255,255,0)\"); side.addColorStop(1, \"rgba(20,40,90,0.42)\");\n      c.fillStyle = side; c.fill();\n\n      // The cabin: a gable end facing us and one wall running back to the right.\n      const bx = -74, by = ground(-40) + 5, fw = 58, fh = 34, sw = 46, rise = 8, peak = 30;\n      c.save(); c.translate(-40, by); c.scale(1.34, 1.34); c.translate(40, -by);\n      c.beginPath(); c.ellipse(bx + 60, by + 3, 76, 8, 0, 0, Math.PI * 2); c.fillStyle = \"rgba(40,62,110,0.35)\"; c.fill();\n      const wallF = c.createLinearGradient(bx, 0, bx + fw, 0); wallF.addColorStop(0, \"#8a5a36\"); wallF.addColorStop(1, \"#5f3a22\");\n      c.beginPath(); c.moveTo(bx, by); c.lineTo(bx, by - fh); c.lineTo(bx + fw / 2, by - fh - peak); c.lineTo(bx + fw, by - fh); c.lineTo(bx + fw, by); c.closePath(); c.fillStyle = wallF; c.fill();\n      c.beginPath(); c.moveTo(bx + fw, by); c.lineTo(bx + fw, by - fh); c.lineTo(bx + fw + sw, by - fh - rise); c.lineTo(bx + fw + sw, by - rise); c.closePath(); c.fillStyle = \"#3d2414\"; c.fill();\n      c.lineWidth = 0.7; c.strokeStyle = \"rgba(20,10,4,0.55)\";\n      for (let i = 1; i < 6; i++) {\n        const y = by - (fh / 6) * i;\n        c.beginPath(); c.moveTo(bx, y); c.lineTo(bx + fw, y); c.lineTo(bx + fw + sw, y - rise); c.stroke();\n      }\n      // Door and lit window.\n      c.fillStyle = \"#2c1a0f\"; c.fillRect(bx + 9, by - 22, 13, 22);\n      c.fillStyle = \"rgba(255,225,170,0.9)\"; c.fillRect(bx + 18.4, by - 12, 1.6, 1.6);\n      const wx = bx + 31, wy = by - 25, ws = 15;\n      c.fillStyle = `rgb(${LAMP})`; c.fillRect(wx, wy, ws, ws);\n      const pane = c.createRadialGradient(wx + ws / 2, wy + ws / 2, 1, wx + ws / 2, wy + ws / 2, ws * 0.8);\n      pane.addColorStop(0, \"rgba(255,250,225,0.95)\"); pane.addColorStop(1, \"rgba(255,250,225,0)\");\n      c.fillStyle = pane; c.fillRect(wx, wy, ws, ws);\n      c.strokeStyle = \"#2c1a0f\"; c.lineWidth = 1.5; c.strokeRect(wx, wy, ws, ws);\n      c.beginPath(); c.moveTo(wx + ws / 2, wy); c.lineTo(wx + ws / 2, wy + ws); c.moveTo(wx, wy + ws / 2); c.lineTo(wx + ws, wy + ws / 2); c.lineWidth = 1.1; c.stroke();\n      c.fillStyle = `rgba(${LAMP},0.85)`; c.fillRect(bx + fw + 16, by - 25, 9, 12);\n      c.strokeRect(bx + fw + 16, by - 25, 9, 12);\n      // Chimney, then the roof under a thick slab of snow.\n      c.fillStyle = \"#4a2c1a\"; c.fillRect(bx + fw + 20, by - fh - peak - 6, 10, 20);\n      c.fillStyle = \"#f6faff\"; c.beginPath(); c.roundRect(bx + fw + 18, by - fh - peak - 10, 14, 6, 3); c.fill();\n      const ex = 7;\n      c.beginPath(); c.moveTo(bx + fw / 2, by - fh - peak - 2); c.lineTo(bx + fw / 2 + sw, by - fh - peak - rise - 2); c.lineTo(bx + fw + sw + ex, by - fh - rise + 5); c.lineTo(bx + fw + ex, by - fh + 6); c.closePath();\n      const roofS = c.createLinearGradient(0, by - fh - peak, 0, by - fh + 6); roofS.addColorStop(0, \"#d3e2f5\"); roofS.addColorStop(1, \"#8faed6\");\n      c.fillStyle = roofS; c.fill();\n      c.beginPath(); c.moveTo(bx - ex, by - fh + 6); c.lineTo(bx + fw / 2, by - fh - peak - 3);\n      c.lineTo(bx + fw + ex, by - fh + 6); c.quadraticCurveTo(bx + fw + ex - 2, by - fh + 11, bx + fw + ex - 7, by - fh + 9);\n      c.lineTo(bx + fw / 2, by - fh - peak + 9); c.lineTo(bx - ex + 7, by - fh + 9); c.quadraticCurveTo(bx - ex + 1, by - fh + 11, bx - ex, by - fh + 6); c.closePath();\n      c.fillStyle = \"#ffffff\"; c.fill();\n      c.lineWidth = 1; c.strokeStyle = \"rgba(120,150,200,0.55)\"; c.stroke();\n      // Lamplight spilling onto the drift.\n      c.globalCompositeOperation = \"lighter\";\n      const pool = c.createRadialGradient(wx + ws / 2, by + 6, 2, wx + ws / 2, by + 6, 64);\n      pool.addColorStop(0, `rgba(${LAMP},0.5)`); pool.addColorStop(1, `rgba(${LAMP},0)`);\n      c.save(); c.translate(0, by + 6); c.scale(1, 0.34); c.translate(0, -(by + 6)); c.fillStyle = pool; c.fillRect(wx - 70, by - 60, 150, 132); c.restore();\n      const halo = c.createRadialGradient(wx + ws / 2, wy + ws / 2, 2, wx + ws / 2, wy + ws / 2, 40);\n      halo.addColorStop(0, `rgba(${LAMP},0.42)`); halo.addColorStop(1, `rgba(${LAMP},0)`);\n      c.fillStyle = halo; c.fillRect(wx - 40, wy - 40, 100, 100);\n      c.globalCompositeOperation = \"source-over\";\n      c.restore();\n      pine(c, 104, ground(104) + 12, 132, 1); pine(c, -140, ground(-140) + 16, 56, 0.95);\n      c.restore();\n    };\n\n    const drawFlakes = (front: boolean) => {\n      ctx.fillStyle = \"#ffffff\";\n      for (const f of flakes) {\n        if (f.rest || f.z > 0.55 !== front) continue;\n        ctx.globalAlpha = 0.42 + f.z * 0.58;\n        ctx.beginPath(); ctx.arc(f.x, f.y, 0.7 + f.z * f.z * 2.5, 0, Math.PI * 2); ctx.fill();\n      }\n      ctx.globalAlpha = 1;\n    };\n\n    const draw = () => {\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.clearRect(0, 0, cv.width, cv.height);\n      const k = dpr * scale;\n      ctx.setTransform(k, 0, 0, k, ox * dpr, oy * dpr);\n\n      // Shadow and a breath of blue light on the table.\n      const sh = ctx.createRadialGradient(s.x * 0.5, FOOT + 6, 20, s.x * 0.5, FOOT + 6, 290);\n      sh.addColorStop(0, \"rgba(0,0,0,0.78)\"); sh.addColorStop(0.5, \"rgba(0,0,0,0.34)\"); sh.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx.save(); ctx.translate(0, FOOT + 6); ctx.scale(1, 0.13); ctx.translate(0, -(FOOT + 6)); ctx.fillStyle = sh; ctx.fillRect(-320, FOOT - 300, 640, 620); ctx.restore();\n      const spill = ctx.createRadialGradient(70 + s.x * 0.5, FOOT + 14, 4, 70 + s.x * 0.5, FOOT + 14, 190);\n      spill.addColorStop(0, \"rgba(150,190,255,0.2)\"); spill.addColorStop(1, \"rgba(150,190,255,0)\");\n      ctx.save(); ctx.translate(0, FOOT + 14); ctx.scale(1, 0.12); ctx.translate(0, -(FOOT + 14)); ctx.fillStyle = spill; ctx.fillRect(-140, FOOT - 190, 420, 410); ctx.restore();\n\n      // Everything else leans and travels with the hand.\n      const tilt = Math.max(-0.16, Math.min(0.16, s.x * 0.0016 + s.vx * 0.00006));\n      ctx.translate(s.x, s.y); ctx.translate(0, FOOT); ctx.rotate(tilt); ctx.translate(0, -FOOT);\n\n      collar(ctx, false);\n      if (baseLayer) ctx.drawImage(baseLayer, -200, 390, 400, 226);\n\n      // Water: almost clear in the middle, thicker and bluer where you look through more glass.\n      const body = ctx.createRadialGradient(-20, CY - 30, R * 0.1, 0, CY, R);\n      body.addColorStop(0, \"rgba(120,160,230,0.07)\"); body.addColorStop(0.62, \"rgba(96,138,214,0.1)\"); body.addColorStop(0.9, \"rgba(140,184,255,0.2)\"); body.addColorStop(1, \"rgba(200,226,255,0.4)\");\n      // Nothing of the sphere shows below the lip of the collar.\n      ctx.save(); ctx.beginPath(); ctx.rect(-R - 4, CY - R - 4, R * 2 + 8, 404 - (CY - R - 4)); ctx.ellipse(0, 404, 124, 17, 0, 0, Math.PI); ctx.clip();\n      ctx.beginPath(); ctx.arc(0, CY, R, 0, Math.PI * 2); ctx.fillStyle = \"rgba(9,15,30,0.55)\"; ctx.fill(); ctx.fillStyle = body; ctx.fill();\n\n      ctx.save(); ctx.beginPath(); ctx.arc(0, CY, R - 1.5, 0, Math.PI * 2); ctx.clip();\n      drawFlakes(false);\n      if (sceneLayer) ctx.drawImage(sceneLayer, -R, CY - R, R * 2, R * 2);\n      drawFlakes(true);\n      // The wall of the glass: a dark band just inside the rim, then the rim itself.\n      const wall = ctx.createRadialGradient(0, CY, R * 0.8, 0, CY, R);\n      wall.addColorStop(0, \"rgba(4,10,26,0)\"); wall.addColorStop(0.8, \"rgba(4,10,26,0.34)\"); wall.addColorStop(0.95, \"rgba(4,10,26,0.16)\"); wall.addColorStop(1, \"rgba(210,230,255,0.5)\");\n      ctx.fillStyle = wall; ctx.fillRect(-R, CY - R, R * 2, R * 2);\n      // Where the sphere sinks into the collar.\n      const seat = ctx.createLinearGradient(0, CY + R * 0.62, 0, CY + R * 0.82);\n      seat.addColorStop(0, \"rgba(6,10,22,0)\"); seat.addColorStop(1, \"rgba(6,10,22,0.55)\");\n      ctx.fillStyle = seat; ctx.fillRect(-R, CY + R * 0.6, R * 2, R * 0.4);\n\n      // Reflections stay with the lamp, not with the globe, so they are drawn un-tilted.\n      const la = -2.3 + s.light - tilt;\n      ctx.save(); ctx.translate(0, CY); ctx.rotate(la + Math.PI / 2);\n      const box1 = ctx.createLinearGradient(0, -R * 0.93, 0, -R * 0.56);\n      box1.addColorStop(0, \"rgba(255,255,255,0.7)\"); box1.addColorStop(0.5, \"rgba(255,255,255,0.2)\"); box1.addColorStop(1, \"rgba(255,255,255,0)\");\n      // A four-pane window, bent round the glass: two bands, each split by the glazing bar.\n      for (const [r0, r1] of [[0.925, 0.77], [0.745, 0.56]]) for (const [a0, a1] of [[-0.44, -0.07], [-0.03, 0.3]]) {\n        const pinch = (0.925 - r1) * 0.22;\n        ctx.beginPath(); ctx.arc(0, 0, R * r0, -Math.PI / 2 + a0, -Math.PI / 2 + a1); ctx.arc(0, 0, R * r1, -Math.PI / 2 + a1 - pinch * Math.sign(a1), -Math.PI / 2 + a0 + pinch, true); ctx.closePath();\n        ctx.fillStyle = box1; ctx.fill();\n      }\n      ctx.rotate(Math.PI - 0.25);\n      const box2 = ctx.createLinearGradient(0, -R * 0.96, 0, -R * 0.8);\n      box2.addColorStop(0, \"rgba(190,214,255,0.5)\"); box2.addColorStop(1, \"rgba(190,214,255,0)\");\n      ctx.beginPath(); ctx.arc(0, 0, R * 0.965, -Math.PI / 2 - 0.62, -Math.PI / 2 + 0.62); ctx.arc(0, 0, R * 0.82, -Math.PI / 2 + 0.5, -Math.PI / 2 - 0.5, true); ctx.closePath();\n      ctx.fillStyle = box2; ctx.fill();\n      ctx.restore();\n      ctx.restore();\n\n      const rim = ctx.createLinearGradient(-R, CY - R, R, CY + R);\n      rim.addColorStop(0, \"rgba(255,255,255,0.95)\"); rim.addColorStop(0.4, \"rgba(220,235,255,0.35)\"); rim.addColorStop(0.75, \"rgba(160,190,240,0.2)\"); rim.addColorStop(1, \"rgba(220,235,255,0.6)\");\n      ctx.beginPath(); ctx.arc(0, CY, R - 0.6, 0, Math.PI * 2); ctx.lineWidth = 1.6; ctx.strokeStyle = rim; ctx.stroke();\n      // One hard glint from the lamp itself.\n      const hx = Math.cos(la - 0.5) * R * 0.8, hy = CY + Math.sin(la - 0.5) * R * 0.8;\n      const hot = ctx.createRadialGradient(hx, hy, 0, hx, hy, 13);\n      hot.addColorStop(0, \"rgba(255,255,255,1)\"); hot.addColorStop(0.25, \"rgba(255,255,255,0.75)\"); hot.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = hot; ctx.fillRect(hx - 14, hy - 14, 28, 28);\n      ctx.restore();\n      collar(ctx, true);\n    };\n\n    const fit = () => {\n      box = cv.getBoundingClientRect();\n      dpr = Math.min(2, window.devicePixelRatio || 1);\n      cv.width = Math.round(box.width * dpr); cv.height = Math.round(box.height * dpr);\n      const bottom = box.width < 640 ? 96 : 70; // room for the caption row\n      scale = Math.min(box.width / 500, (box.height - bottom) / 640);\n      ox = box.width / 2; oy = Math.max(0, (box.height - bottom - 640 * scale) / 2 + 12 * scale);\n      const w = 300 * scale, h = 560 * scale;\n      pad.style.left = `${ox - w / 2}px`; pad.style.top = `${oy + 60 * scale}px`; pad.style.width = `${w}px`; pad.style.height = `${h}px`;\n      seed = 5;\n      baseLayer = bake(-200, 390, 400, 226, paintBase);\n      sceneLayer = bake(-R, CY - R, R * 2, R * 2, paintScene);\n    };\n\n    let raf = 0, last = 0, t = 0, seen = true, touched = -1e9, nextDemo = 5.2;\n    const step = (dt: number) => {\n      t += dt;\n      // A programmed shake is a short decaying wobble of the target the globe springs toward.\n      if (s.shakeAt >= 0) {\n        const q = (t - s.shakeAt) / 0.75;\n        if (q >= 1) { s.shakeAt = -1; s.tx = 0; s.ty = 0; } else { s.tx = 62 * Math.sin(q * 21) * (1 - q); s.ty = 14 * Math.sin(q * 15 + 1) * (1 - q); }\n      }\n      const kq = s.drag || s.shakeAt >= 0 ? 520 : 170, cq = s.drag || s.shakeAt >= 0 ? 30 : 9;\n      const pvx = s.vx, pvy = s.vy;\n      s.vx += (kq * (s.tx - s.x) - cq * s.vx) * dt; s.vy += (kq * (s.ty - s.y) - cq * s.vy) * dt;\n      s.x += s.vx * dt; s.y += s.vy * dt;\n      // The water lags the glass: every change of speed stirs it and shoves it the other way.\n      const dvx = s.vx - pvx, dvy = s.vy - pvy;\n      s.energy = Math.min(330, s.energy + Math.hypot(dvx, dvy) * 0.5);\n      s.ux -= dvx * 0.55; s.uy -= dvy * 0.55;\n      const fade = Math.exp(-dt * 3.2); s.ux *= fade; s.uy *= fade;\n      s.energy *= Math.exp(-dt * 0.42);\n      s.light += (s.lightTo - s.light) * Math.min(1, dt * 5);\n      const E = s.energy, lim = R - 8, follow = Math.min(1, dt * 5);\n      for (const f of flakes) {\n        if (f.rest) {\n          if (E > 55 && Math.random() < dt * (E / 60)) { f.rest = false; f.vx = (Math.random() - 0.5) * E; f.vy = -E * (0.3 + Math.random() * 0.7); }\n          continue;\n        }\n        // Two layers of eddies taken from a stream function, so the flow never bunches the flakes together.\n        const a1 = f.x * 0.019 + t * 0.9, b1 = f.y * 0.017 - t * 0.7, a2 = f.x * 0.043 - t * 1.4 + 2, b2 = f.y * 0.039 + t * 1.1;\n        const gx = Math.sin(a1) * Math.cos(b1) + 0.5 * Math.sin(a2) * Math.cos(b2) + 0.35 * Math.sin(t * 2.3 + f.ph * 3);\n        const gy = -Math.cos(a1) * Math.sin(b1) * 1.1 - 0.55 * Math.cos(a2) * Math.sin(b2) + 0.35 * Math.cos(t * 1.9 + f.ph * 5);\n        const wantX = s.ux + E * gx + Math.sin(t * 0.8 + f.ph) * 5;\n        const wantY = s.uy + E * (gy - 0.05) + SETTLE * (0.55 + f.z);\n        // Water turns back at the glass, so flakes near the wall are carried inward again.\n        const edge = Math.hypot(f.x, f.y - CY) / lim;\n        const turn = edge > 0.8 ? ((edge - 0.8) / 0.2) * E * 0.9 : 0;\n        f.vx += (wantX - (f.x / (edge * lim || 1)) * turn - f.vx) * follow; f.vy += (wantY - ((f.y - CY) / (edge * lim || 1)) * turn - f.vy) * follow;\n        f.x += f.vx * dt; f.y += f.vy * dt;\n        const dx = f.x, dy = f.y - CY, d = Math.hypot(dx, dy);\n        if (d > lim) { const nx = dx / d, ny = dy / d, vn = f.vx * nx + f.vy * ny; f.x = nx * lim; f.y = CY + ny * lim; if (vn > 0) { f.vx -= vn * nx * 1.3; f.vy -= vn * ny * 1.3; } }\n        const g = ground(f.x) - 1.5;\n        if (f.y > g) { f.y = g; if (E < 42) f.rest = true; else { f.vy = -E * (0.25 + Math.random() * 0.6); f.vx += (Math.random() - 0.5) * E; } }\n      }\n    };\n\n    const frame = (now: number) => {\n      raf = 0;\n      if (!last) last = now;\n      let dt = Math.min(0.05, (now - last) / 1000); last = now;\n      if (!s.drag && s.shakeAt < 0 && t > nextDemo && now - touched > 6000) { s.shakeAt = t; nextDemo = t + 8.5; }\n      while (dt > 0) { const h = Math.min(dt, 1 / 120); step(h); dt -= h; }\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = 0; raf = requestAnimationFrame(frame); } };\n\n    api.current = {\n      shake: () => {\n        touched = performance.now();\n        if (reduce) { for (const f of flakes) scatter(f); draw(); return; }\n        s.shakeAt = t; wake();\n      },\n    };\n\n    const local = (e: PointerEvent) => ({ x: (e.clientX - box.left - ox) / scale, y: (e.clientY - box.top - oy) / scale });\n    let grab = { x: 0, y: 0 };\n    const onDown = (e: PointerEvent) => {\n      if (reduce) return;\n      box = cv.getBoundingClientRect();\n      const p = local(e); grab = { x: p.x - s.x, y: p.y - s.y };\n      s.drag = true; s.shakeAt = -1; touched = performance.now();\n      pad.setPointerCapture(e.pointerId); e.preventDefault(); wake();\n    };\n    const onMove = (e: PointerEvent) => {\n      const p = local(e);\n      s.lightTo = Math.max(-0.35, Math.min(0.35, p.x * 0.0011));\n      if (!s.drag) { if (reduce) { s.light = s.lightTo; draw(); } return; }\n      touched = performance.now();\n      let tx = p.x - grab.x, ty = p.y - grab.y;\n      const far = Math.hypot(tx, ty * 1.6);\n      if (far > 96) { tx *= 96 / far; ty *= 96 / far; }\n      s.tx = tx; s.ty = ty;\n    };\n    const onUp = () => { if (!s.drag) return; s.drag = false; s.tx = 0; s.ty = 0; touched = performance.now(); };\n    pad.addEventListener(\"pointerdown\", onDown);\n    pad.addEventListener(\"pointermove\", onMove);\n    pad.addEventListener(\"pointerup\", onUp);\n    pad.addEventListener(\"pointercancel\", onUp);\n\n    fit();\n    if (reduce) draw(); else wake();\n    const ro = new ResizeObserver(() => { fit(); if (reduce || !raf) draw(); });\n    ro.observe(cv);\n    const io = new IntersectionObserver(([entry]) => { seen = entry.isIntersecting; if (seen) wake(); });\n    io.observe(cv);\n    const onVis = () => { if (!document.hidden) wake(); };\n    document.addEventListener(\"visibilitychange\", onVis);\n    return () => {\n      cancelAnimationFrame(raf); api.current = null;\n      pad.removeEventListener(\"pointerdown\", onDown); pad.removeEventListener(\"pointermove\", onMove);\n      pad.removeEventListener(\"pointerup\", onUp); pad.removeEventListener(\"pointercancel\", onUp);\n      ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  return (\n    <div className=\"relative flex h-[600px] w-full flex-col overflow-hidden bg-[#0a101d] bg-[radial-gradient(110%_78%_at_50%_34%,#1d2c49_0%,#111a2d_46%,#0a101d_78%)] sm:h-[720px]\">\n      <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n      <div ref={hit} aria-hidden=\"true\" className=\"absolute cursor-grab touch-none rounded-[40%] active:cursor-grabbing\" />\n      <div className=\"pointer-events-none relative mx-auto mt-auto flex w-full max-w-[880px] flex-col items-center gap-3 px-5 pb-6 text-[13px] sm:flex-row sm:justify-between sm:pb-8\">\n        <p className=\"text-center text-slate-300 sm:text-left\">\n          <span className=\"font-semibold text-white\">Snow globe</span>\n          <span className=\"mx-2 text-slate-500\">/</span>\n          pick it up and shake it\n        </p>\n        <button\n          type=\"button\"\n          onClick={() => { api.current?.shake(); setShakes((n) => n + 1); }}\n          className=\"pointer-events-auto min-h-[40px] shrink-0 rounded-full bg-white px-5 font-medium text-[#0a101d] shadow-[0_12px_24px_-12px_rgba(140,180,255,0.7)] transition-transform hover:-translate-y-0.5 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-offset-2 focus-visible:ring-offset-[#0a101d] motion-reduce:transition-none\"\n        >\n          Shake it\n        </button>\n        <span aria-live=\"polite\" className=\"sr-only\">{shakes > 0 ? `Shaken ${shakes} ${shakes === 1 ? \"time\" : \"times\"}. The snow is falling.` : \"\"}</span>\n      </div>\n    </div>\n  );\n}\n"}]}
