{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"word-embroidered-in-satin-stitch-on-hooped-linen-by-a-travelling-needle","type":"registry:component","title":"Word Embroidered in Satin Stitch on Hooped Linen by a Travelling Needle","description":"Canvas text effect: a travelling needle sews a word into hooped linen in satin stitch, and the thread sheen follows the pointer. Free React and HTML code.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/word-embroidered-in-satin-stitch-on-hooped-linen-by-a-travelling-needle.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// What gets stitched, and the colour of the floss for each. Keep each one short enough for the hoop.\nconst pieces = [\n  { text: \"hello\", thread: \"#d4362c\" },\n  { text: \"take care\", thread: \"#1e4fb8\" },\n  { text: \"slowly\", thread: \"#e0a019\" },\n];\n\n/** The undyed linen. Every weave thread, fold and shadow is mixed from this one colour. */\nconst LINEN = \"#e6dbc4\";\n/** Angle of the stitches in radians. Zero lays them flat, a negative value tips them up to the right. */\nconst SLANT = -0.52;\n/** Seconds to stitch a whole word. */\nconst PACE = 5;\n\ntype Stitch = { x0: number; y0: number; x1: number; y1: number; tone: number; band: number };\n\nfunction parse(hex: string) {\n  const v = parseInt(hex.slice(1), 16);\n  return [(v >> 16) & 255, (v >> 8) & 255, v & 255];\n}\nfunction mix(hex: string, k: number, toward = 0) {\n  const [r, g, b] = parse(hex);\n  const f = (c: number) => Math.round(Math.min(255, Math.max(0, toward ? c + (255 - c) * k : c * k)));\n  return `rgb(${f(r)},${f(g)},${f(b)})`;\n}\nfunction rand(n: number) {\n  const s = Math.sin(n * 91.7 + 13.3) * 43758.5453;\n  return s - Math.floor(s);\n}\n\nexport default function WordEmbroideredInSatinStitchOnHoopedLinenByATravellingNeedle() {\n  const reduce = useReducedMotion();\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const lampAim = useRef({ x: 0.3, y: 0.2, at: -1e6 });\n  const skip = useRef(0);\n  const redraw = useRef<() => void>(() => {});\n  const [index, setIndex] = useState(0);\n\n  useEffect(() => {\n    const el = stage.current;\n    const cv = canvas.current;\n    if (!el || !cv) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const make = () => document.createElement(\"canvas\");\n    const base = make();\n    const shadows = make();\n    const threads = make();\n    let W = 0;\n    let H = 0;\n    let ratio = 1;\n    let cx = 0;\n    let cy = 0;\n    let rx = 0;\n    let ry = 0;\n    let stitches: Stitch[] = [];\n    let bandWidth = 12;\n    let drawn = 0;\n    let current = 0;\n    let phaseStart = performance.now();\n    let headStart = 0.56;\n    let raf = 0;\n    let visible = true;\n    let seenSkip = skip.current;\n    const lamp = { x: 0.3, y: 0.2 };\n    const needle = { x: 0, y: 0, lift: 0, ready: false };\n\n    const oval = (c: CanvasRenderingContext2D, a: number, b: number) => {\n      c.beginPath();\n      c.ellipse(cx, cy, a, b, 0, 0, Math.PI * 2);\n    };\n\n    const buildBase = () => {\n      base.width = cv.width;\n      base.height = cv.height;\n      const c = base.getContext(\"2d\")!;\n      c.setTransform(ratio, 0, 0, ratio, 0, 0);\n      // One small square of plain weave: each thread goes over one and under the next.\n      const tile = make();\n      const cell = 3;\n      tile.width = tile.height = 96 * ratio;\n      const t = tile.getContext(\"2d\")!;\n      t.scale(ratio, ratio);\n      t.fillStyle = mix(LINEN, 0.86);\n      t.fillRect(0, 0, 96, 96);\n      for (let j = 0; j < 32; j++) {\n        for (let i = 0; i < 32; i++) {\n          const over = (i + j) % 2 === 0;\n          const tone = 0.95 + rand(i * 17.3 + j * 5.9) * 0.07;\n          t.fillStyle = mix(LINEN, tone);\n          if (over) t.fillRect(i * cell - 0.6, j * cell + 0.35, cell + 1.2, cell - 0.7);\n          else t.fillRect(i * cell + 0.35, j * cell - 0.6, cell - 0.7, cell + 1.2);\n          t.fillStyle = \"rgba(255,255,255,0.16)\";\n          if (over) t.fillRect(i * cell, j * cell + 0.5, cell, 0.7);\n          else t.fillRect(i * cell + 0.5, j * cell, 0.7, cell);\n        }\n      }\n      c.fillStyle = c.createPattern(tile, \"repeat\")!;\n      c.save();\n      c.scale(1 / ratio, 1 / ratio);\n      c.fillRect(0, 0, cv.width, cv.height);\n      c.restore();\n      // Linen is uneven: thick places in single threads, and broad patches a shade off.\n      for (let i = 0; i < (W * H) / 2600; i++) {\n        const x = rand(i * 3.1) * W;\n        const y = rand(i * 7.7 + 1) * H;\n        const len = 6 + rand(i * 1.9) * rand(i * 4.3) * 46;\n        c.fillStyle = rand(i * 5.3) > 0.5 ? \"rgba(255,250,235,0.1)\" : \"rgba(90,66,30,0.05)\";\n        const across = rand(i * 9.1) > 0.5;\n        c.fillRect(Math.round(x / cell) * cell, Math.round(y / cell) * cell, across ? len : cell - 0.8, across ? cell - 0.8 : len);\n      }\n      for (let i = 0; i < 26; i++) {\n        const x = rand(i * 11.3) * W;\n        const y = rand(i * 4.9 + 2) * H;\n        const r = 120 + rand(i * 2.3) * 260;\n        const g = c.createRadialGradient(x, y, 0, x, y, r);\n        g.addColorStop(0, i % 2 ? \"rgba(255,248,230,0.09)\" : \"rgba(92,66,30,0.06)\");\n        g.addColorStop(1, \"rgba(0,0,0,0)\");\n        c.fillStyle = g;\n        c.fillRect(x - r, y - r, r * 2, r * 2);\n      }\n      // Loose cloth outside the hoop lies in soft folds.\n      c.save();\n      oval(c, rx + 24, ry + 24);\n      c.rect(0, 0, W, H);\n      c.clip(\"evenodd\");\n      c.fillStyle = \"rgba(70,48,20,0.07)\";\n      c.fillRect(0, 0, W, H);\n      for (let i = 0; i < 7; i++) {\n        const a = -0.9 + rand(i * 3.7) * 0.5 + (i % 2 ? 1.9 : 0);\n        const ox = rand(i * 8.1) * W;\n        const oy = rand(i * 2.9) * H;\n        const g = c.createLinearGradient(ox, oy, ox + Math.cos(a) * 280, oy + Math.sin(a) * 280);\n        g.addColorStop(0, \"rgba(0,0,0,0)\");\n        g.addColorStop(0.4, \"rgba(60,40,14,0.1)\");\n        g.addColorStop(0.6, \"rgba(255,250,236,0.12)\");\n        g.addColorStop(1, \"rgba(0,0,0,0)\");\n        c.fillStyle = g;\n        c.fillRect(0, 0, W, H);\n      }\n      c.restore();\n\n      // The hoop's shadow on the cloth.\n      c.save();\n      c.shadowColor = \"rgba(58,38,14,0.5)\";\n      c.shadowBlur = 22;\n      c.shadowOffsetX = 7 * ratio;\n      c.shadowOffsetY = 13 * ratio;\n      oval(c, rx + 13, ry + 13);\n      c.lineWidth = 24;\n      c.strokeStyle = \"#8f6a3c\";\n      c.stroke();\n      c.restore();\n      // Inside the ring the cloth is drum tight: lighter in the middle, shaded under the wood.\n      c.save();\n      oval(c, rx, ry);\n      c.clip();\n      const tight = c.createRadialGradient(cx - rx * 0.2, cy - ry * 0.25, 0, cx, cy, rx);\n      tight.addColorStop(0, \"rgba(255,251,240,0.2)\");\n      tight.addColorStop(1, \"rgba(255,251,240,0)\");\n      c.fillStyle = tight;\n      c.fillRect(0, 0, W, H);\n      c.shadowColor = \"rgba(50,32,10,0.55)\";\n      c.shadowBlur = 16;\n      c.shadowOffsetX = 3 * ratio;\n      c.shadowOffsetY = 6 * ratio;\n      oval(c, rx + 6, ry + 6);\n      c.lineWidth = 12;\n      c.strokeStyle = \"#7c5a30\";\n      c.stroke();\n      c.restore();\n\n      // Two beech rings: the outer one is split at the top and drawn shut by a brass screw.\n      const wood = (a: number, b: number, width: number, light: string, dark: string) => {\n        const g = c.createLinearGradient(cx - a, cy - b, cx + a, cy + b);\n        g.addColorStop(0, light);\n        g.addColorStop(0.5, mix(light, 0.9));\n        g.addColorStop(1, dark);\n        oval(c, a, b);\n        c.lineWidth = width;\n        c.strokeStyle = g;\n        c.stroke();\n        c.lineWidth = 0.8;\n        c.strokeStyle = \"rgba(70,44,16,0.55)\";\n        oval(c, a + width / 2, b + width / 2);\n        c.stroke();\n        oval(c, a - width / 2, b - width / 2);\n        c.stroke();\n        // Grain follows the bend of the ring.\n        for (let i = 0; i < 90; i++) {\n          const off = (rand(i * 5.1 + a) - 0.5) * (width - 2);\n          const from = rand(i * 2.7 + a) * Math.PI * 2;\n          c.beginPath();\n          c.ellipse(cx, cy, a + off, b + off, 0, from, from + 0.25 + rand(i * 8.3) * 1.1);\n          c.lineWidth = 0.5 + rand(i * 3.9) * 0.7;\n          c.strokeStyle = rand(i * 6.1) > 0.4 ? \"rgba(96,62,24,0.2)\" : \"rgba(255,240,208,0.22)\";\n          c.stroke();\n        }\n        // A rounded edge catches the light along its upper left.\n        c.beginPath();\n        c.ellipse(cx, cy, a + width / 2 - 1.6, b + width / 2 - 1.6, 0, Math.PI * 0.95, Math.PI * 1.7);\n        c.lineWidth = 1.4;\n        c.strokeStyle = \"rgba(255,246,222,0.6)\";\n        c.stroke();\n      };\n      wood(rx + 16, ry + 16, 18, \"#e8ca98\", \"#ad8048\");\n      wood(rx + 2.5, ry + 2.5, 9, \"#dcbb86\", \"#a27640\");\n      // The clamp.\n      const top = cy - ry - 25;\n      c.fillStyle = mix(LINEN, 0.9);\n      c.fillRect(cx - 3, top - 2, 6, 20);\n      for (const side of [-1, 1]) {\n        const g = c.createLinearGradient(0, top - 16, 0, top + 6);\n        g.addColorStop(0, \"#ecd09f\");\n        g.addColorStop(1, \"#b38a52\");\n        c.fillStyle = g;\n        c.beginPath();\n        c.roundRect(cx + side * 3 - (side < 0 ? 17 : 0), top - 22, 17, 31, 3);\n        c.fill();\n        c.strokeStyle = \"rgba(70,44,16,0.6)\";\n        c.lineWidth = 0.8;\n        c.stroke();\n      }\n      const brass = c.createLinearGradient(0, top - 15, 0, top - 2);\n      brass.addColorStop(0, \"#fff3c2\");\n      brass.addColorStop(0.35, \"#e0b950\");\n      brass.addColorStop(1, \"#8a6a1e\");\n      c.fillStyle = brass;\n      c.fillRect(cx - 32, top - 12, 68, 7);\n      c.beginPath();\n      c.roundRect(cx - 38, top - 15.5, 7, 14, 2);\n      c.fill();\n      c.beginPath();\n      c.roundRect(cx + 26, top - 18, 19, 19, 2.5);\n      c.fill();\n      c.fillStyle = \"rgba(80,56,10,0.45)\";\n      for (let i = 0; i < 7; i++) c.fillRect(cx + 28 + i * 2.5, top - 17.5, 1, 18);\n      c.fillStyle = \"rgba(255,250,220,0.7)\";\n      c.fillRect(cx - 32, top - 11.2, 58, 1);\n    };\n\n    const buildStitches = () => {\n      const piece = pieces[current % pieces.length];\n      const words = W < 640 && piece.text.includes(\" \") ? piece.text.split(\" \") : [piece.text];\n      const mw = Math.ceil(rx * 2);\n      const mh = Math.ceil(ry * 2);\n      const m = make();\n      m.width = mw;\n      m.height = mh;\n      const c = m.getContext(\"2d\", { willReadFrequently: true })!;\n      const face = (px: number) => `italic 700 ${px}px Georgia, \"Times New Roman\", serif`;\n      let px = Math.min(ry * (words.length > 1 ? 0.62 : 0.92), 240);\n      c.font = face(px);\n      const widest = Math.max(...words.map((w) => c.measureText(w).width));\n      px *= Math.min(1, (rx * (words.length > 1 ? 1.36 : 1.6)) / widest);\n      c.font = face(px);\n      c.textAlign = \"center\";\n      c.textBaseline = \"middle\";\n      c.lineJoin = \"round\";\n      c.lineWidth = Math.max(2, px * 0.03);\n      const lead = px * 0.92;\n      words.forEach((w, i) => {\n        const y = mh / 2 + (i - (words.length - 1) / 2) * lead - px * 0.03;\n        c.fillText(w, mw / 2, y);\n        c.strokeText(w, mw / 2, y);\n      });\n      const data = c.getImageData(0, 0, mw, mh).data;\n      const inside = (x: number, y: number) => x >= 0 && y >= 0 && x < mw && y < mh && data[((y | 0) * mw + (x | 0)) * 4 + 3] > 110;\n\n      const dx = Math.cos(SLANT);\n      const dy = Math.sin(SLANT);\n      const gap = 2.05;\n      const longest = Math.max(15, px * 0.16);\n      const reach = Math.hypot(mw, mh) / 2;\n      const ox = cx - rx;\n      const oy = cy - ry;\n      bandWidth = Math.max(8, px * 0.09);\n      const list: (Stitch & { row: number })[] = [];\n      let row = 0;\n      for (let k = -reach; k < reach; k += gap, row++) {\n        const lx = mw / 2 - dy * k;\n        const ly = mh / 2 + dx * k;\n        let start = NaN;\n        for (let t = -reach; t <= reach; t += 1) {\n          const hit = inside(lx + dx * t, ly + dy * t);\n          if (hit && Number.isNaN(start)) start = t;\n          if (!hit && !Number.isNaN(start)) {\n            const length = t - 1 - start;\n            if (length >= 1.5) {\n              // A long run is laid as several stitches, with the joins staggered row to row like bricks.\n              const parts = Math.ceil(length / longest);\n              const each = length / parts;\n              const shift = parts > 1 ? (((row % 3) - 1) / 3) * each * 0.8 : 0;\n              for (let j = 0; j < parts; j++) {\n                const a = start + j * each + (j > 0 ? shift : 0) - 1.6;\n                const b = start + (j + 1) * each + (j < parts - 1 ? shift : 0) + 1.6;\n                const n = list.length;\n                const x0 = ox + lx + dx * a + (rand(n) - 0.5) * 0.7;\n                const y0 = oy + ly + dy * a + (rand(n + 0.3) - 0.5) * 0.7;\n                const x1 = ox + lx + dx * b + (rand(n + 0.6) - 0.5) * 0.7;\n                const y1 = oy + ly + dy * b + (rand(n + 0.9) - 0.5) * 0.7;\n                list.push({ x0, y0, x1, y1, tone: 0.84 + rand(n * 1.7) * 0.2, band: Math.floor(((x0 + x1) / 2 - ox) / bandWidth), row });\n              }\n            }\n            start = NaN;\n          }\n        }\n      }\n      // The needle works along the word, a narrow band at a time, filling each band from the top down.\n      list.sort((p, q) => p.band - q.band || p.row - q.row);\n      stitches = list;\n      for (const layer of [shadows, threads]) {\n        layer.width = cv.width;\n        layer.height = cv.height;\n      }\n      drawn = 0;\n    };\n\n    const lay = (from: number, to: number) => {\n      const piece = pieces[current % pieces.length];\n      const s = shadows.getContext(\"2d\")!;\n      const t = threads.getContext(\"2d\")!;\n      for (const c of [s, t]) {\n        c.setTransform(ratio, 0, 0, ratio, 0, 0);\n        c.lineCap = \"round\";\n      }\n      s.strokeStyle = \"rgba(64,40,14,0.2)\";\n      s.lineWidth = 3.6;\n      for (let i = from; i < to; i++) {\n        const q = stitches[i];\n        s.beginPath();\n        s.moveTo(q.x0 + 1.5, q.y0 + 2.4);\n        s.lineTo(q.x1 + 1.5, q.y1 + 2.4);\n        s.stroke();\n        // A strand of floss is a small cylinder: dark underneath, its own colour, then a pale ridge.\n        t.strokeStyle = mix(piece.thread, 0.6 * q.tone);\n        t.lineWidth = 2.9;\n        t.beginPath();\n        t.moveTo(q.x0, q.y0);\n        t.lineTo(q.x1, q.y1);\n        t.stroke();\n        t.strokeStyle = mix(piece.thread, q.tone);\n        t.lineWidth = 2;\n        t.beginPath();\n        t.moveTo(q.x0 - 0.25, q.y0 - 0.35);\n        t.lineTo(q.x1 - 0.25, q.y1 - 0.35);\n        t.stroke();\n        t.strokeStyle = mix(piece.thread, 0.2 + (q.tone - 0.84), 1);\n        t.lineWidth = 0.8;\n        t.beginPath();\n        t.moveTo(q.x0 * 0.9 + q.x1 * 0.1 - 0.55, q.y0 * 0.9 + q.y1 * 0.1 - 0.65);\n        t.lineTo(q.x0 * 0.1 + q.x1 * 0.9 - 0.55, q.y0 * 0.1 + q.y1 * 0.9 - 0.65);\n        t.stroke();\n      }\n    };\n    /** Pull out everything from stitch number `keep` on: the band it sits in and every band to its right. */\n    const unpick = (keep: number) => {\n      const edge = keep <= 0 ? 0 : cx - rx + stitches[Math.min(keep, stitches.length - 1)].band * bandWidth - 2;\n      for (const layer of [shadows, threads]) {\n        const c = layer.getContext(\"2d\")!;\n        c.setTransform(ratio, 0, 0, ratio, 0, 0);\n        c.clearRect(edge, 0, W, H);\n      }\n    };\n\n    const drawNeedle = (x: number, y: number, lift: number, thread: string, tx: number, ty: number) => {\n      const length = Math.min(165, rx * 0.5);\n      const angle = -0.95;\n      const ux = Math.cos(angle);\n      const uy = Math.sin(angle);\n      // lift 0: the needle is deep in the cloth with only its top half showing. lift 1: nearly all of it is out.\n      const out = length * (0.42 + 0.55 * lift);\n      const ex = x + ux * out;\n      const ey = y + uy * out;\n      const nx = -uy;\n      const ny = ux;\n      const shape = (offX: number, offY: number) => {\n        ctx.beginPath();\n        ctx.moveTo(x + offX, y + offY);\n        ctx.lineTo(x + ux * out * 0.3 + nx * 2 + offX, y + uy * out * 0.3 + ny * 2 + offY);\n        ctx.lineTo(ex + nx * 2.7 + offX, ey + ny * 2.7 + offY);\n        ctx.quadraticCurveTo(ex + ux * 3 + offX, ey + uy * 3 + offY, ex - nx * 2.7 + offX, ey - ny * 2.7 + offY);\n        ctx.lineTo(x + ux * out * 0.3 - nx * 2 + offX, y + uy * out * 0.3 - ny * 2 + offY);\n        ctx.closePath();\n      };\n      ctx.save();\n      ctx.filter = \"none\";\n      ctx.fillStyle = \"rgba(50,32,10,0.26)\";\n      shape(5 + lift * 5, 8 + lift * 8);\n      ctx.fill();\n      // The working thread, from the last stitch up to the eye.\n      const ix = ex - ux * 7;\n      const iy = ey - uy * 7;\n      ctx.lineCap = \"round\";\n      ctx.strokeStyle = mix(thread, 0.6);\n      ctx.lineWidth = 2.1;\n      ctx.beginPath();\n      ctx.moveTo(tx, ty);\n      ctx.quadraticCurveTo((tx + ix) / 2 + 26, (ty + iy) / 2 + 18, ix, iy);\n      ctx.stroke();\n      ctx.strokeStyle = mix(thread, 1);\n      ctx.lineWidth = 1.2;\n      ctx.beginPath();\n      ctx.moveTo(tx - 0.3, ty - 0.4);\n      ctx.quadraticCurveTo((tx + ix) / 2 + 25.6, (ty + iy) / 2 + 17.5, ix - 0.3, iy - 0.4);\n      ctx.stroke();\n      const steel = ctx.createLinearGradient(x + nx * 2.7, y + ny * 2.7, x - nx * 2.7, y - ny * 2.7);\n      steel.addColorStop(0, \"#6b7078\");\n      steel.addColorStop(0.45, \"#f7f8fa\");\n      steel.addColorStop(0.7, \"#aeb3bb\");\n      steel.addColorStop(1, \"#565a62\");\n      ctx.fillStyle = steel;\n      shape(0, 0);\n      ctx.fill();\n      ctx.strokeStyle = \"rgba(34,38,46,0.8)\";\n      ctx.lineWidth = 0.7;\n      ctx.stroke();\n      // The eye, with the thread through it.\n      ctx.strokeStyle = \"rgba(30,32,38,0.85)\";\n      ctx.lineWidth = 1.5;\n      ctx.beginPath();\n      ctx.moveTo(ex - ux * 4.5, ey - uy * 4.5);\n      ctx.lineTo(ex - ux * 11, ey - uy * 11);\n      ctx.stroke();\n      // Where it enters the cloth.\n      ctx.fillStyle = \"rgba(40,24,6,0.45)\";\n      ctx.beginPath();\n      ctx.ellipse(x, y, 2, 1.3, 0, 0, Math.PI * 2);\n      ctx.fill();\n      ctx.restore();\n    };\n\n    const paint = (stamp: number) => {\n      if (!W) return;\n      const piece = pieces[current % pieces.length];\n      const total = stitches.length;\n      const t = (stamp - phaseStart) / 1000;\n      const hold = 4.2;\n      const pull = 0.9;\n      let amount = 1;\n      let pulling = false;\n      if (!reduce) {\n        amount = Math.min(1, headStart + t / PACE);\n        const after = t - (1 - headStart) * PACE - hold;\n        if (after > 0) {\n          pulling = true;\n          amount = Math.max(0, 1 - after / pull);\n          amount *= amount;\n        }\n        if (after > pull + 0.35 || seenSkip !== skip.current) {\n          seenSkip = skip.current;\n          current += 1;\n          setIndex(current % pieces.length);\n          headStart = 0;\n          phaseStart = stamp;\n          buildStitches();\n          return paint(stamp);\n        }\n      }\n      const want = Math.round(total * amount);\n      if (want > drawn) lay(drawn, want);\n      else if (want < drawn) unpick(want);\n      drawn = want;\n\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.globalCompositeOperation = \"copy\";\n      ctx.drawImage(base, 0, 0);\n      ctx.globalCompositeOperation = \"source-over\";\n      ctx.drawImage(shadows, 0, 0);\n      ctx.drawImage(threads, 0, 0);\n      ctx.setTransform(ratio, 0, 0, ratio, 0, 0);\n\n      // Satin sheen: every stitch arches over the cloth, so each shows one bright stretch, and where that falls depends on the lamp.\n      const idle = stamp - lampAim.current.at > 2400;\n      const ax = idle && !reduce ? Math.sin(stamp / 1900) * 0.75 : lampAim.current.x;\n      const ay = idle && !reduce ? Math.cos(stamp / 2600) * 0.6 : lampAim.current.y;\n      lamp.x += (ax - lamp.x) * (reduce ? 1 : 0.08);\n      lamp.y += (ay - lamp.y) * (reduce ? 1 : 0.08);\n      const ux = Math.cos(SLANT);\n      const uy = Math.sin(SLANT);\n      const lx = cx + lamp.x * rx * 1.3;\n      const ly = cy + lamp.y * ry * 1.3;\n      ctx.lineCap = \"round\";\n      for (let pass = 0; pass < 2; pass++) {\n        ctx.beginPath();\n        for (let i = 0; i < drawn; i++) {\n          const q = stitches[i];\n          const mx = (q.x0 + q.x1) / 2;\n          const my = (q.y0 + q.y1) / 2;\n          const pullTo = Math.max(-1, Math.min(1, ((lx - mx) * ux + (ly - my) * uy) / (rx * 0.9)));\n          const mid = 0.5 + pullTo * 0.3;\n          const half = pass ? 0.11 : 0.24;\n          const a = Math.max(0.08, mid - half);\n          const b = Math.min(0.92, mid + half);\n          ctx.moveTo(q.x0 + (q.x1 - q.x0) * a - 0.4, q.y0 + (q.y1 - q.y0) * a - 0.5);\n          ctx.lineTo(q.x0 + (q.x1 - q.x0) * b - 0.4, q.y0 + (q.y1 - q.y0) * b - 0.5);\n        }\n        ctx.strokeStyle = pass ? \"rgba(255,255,255,0.36)\" : \"rgba(255,255,255,0.08)\";\n        ctx.lineWidth = pass ? 0.9 : 1.7;\n        ctx.stroke();\n      }\n      // The lamp also warms the cloth nearest to it.\n      const glow = ctx.createRadialGradient(lx, ly, 0, lx, ly, rx * 1.2);\n      glow.addColorStop(0, \"rgba(255,248,226,0.16)\");\n      glow.addColorStop(1, \"rgba(255,248,226,0)\");\n      ctx.fillStyle = glow;\n      ctx.fillRect(0, 0, W, H);\n\n      // The needle follows the newest stitch, then parks beside the word.\n      const head = stitches[Math.max(0, Math.min(total - 1, drawn - 1))];\n      const working = !reduce && !pulling && drawn < total && drawn > 0;\n      const restX = cx + rx * 0.62;\n      const restY = cy + ry * 0.52;\n      const goX = working && head ? head.x1 : restX;\n      const goY = working && head ? head.y1 : restY;\n      if (!needle.ready || reduce) {\n        needle.x = goX;\n        needle.y = goY;\n        needle.ready = true;\n      }\n      needle.x += (goX - needle.x) * 0.3;\n      needle.y += (goY - needle.y) * 0.3;\n      const goLift = working ? 0.5 + 0.5 * Math.sin(stamp / 70) : 0.25;\n      needle.lift += (goLift - needle.lift) * 0.25;\n      const last = stitches[total - 1];\n      const tail = working || !last || drawn < total ? { x: needle.x, y: needle.y } : { x: last.x1, y: last.y1 };\n      if (total) drawNeedle(needle.x, needle.y, needle.lift, piece.thread, tail.x, tail.y);\n    };\n\n    const resize = () => {\n      W = el.clientWidth;\n      H = el.clientHeight;\n      if (!W || !H) return;\n      ratio = Math.min(window.devicePixelRatio || 1, 2);\n      cv.width = Math.round(W * ratio);\n      cv.height = Math.round(H * ratio);\n      cx = W / 2;\n      cy = H * 0.47;\n      rx = Math.min(W * 0.5 - 30, 340);\n      ry = W < 640 ? rx : rx * 0.74;\n      buildBase();\n      const keep = drawn && stitches.length ? drawn / stitches.length : 0;\n      buildStitches();\n      if (keep) {\n        drawn = Math.round(stitches.length * keep);\n        lay(0, drawn);\n      }\n      needle.ready = false;\n      paint(performance.now());\n    };\n\n    const loop = (stamp: number) => {\n      paint(stamp);\n      raf = visible && !document.hidden && !reduce ? requestAnimationFrame(loop) : 0;\n    };\n    const wake = () => {\n      if (!raf && visible && !document.hidden && !reduce) raf = requestAnimationFrame(loop);\n    };\n    redraw.current = () => {\n      if (!reduce) return;\n      if (seenSkip !== skip.current) {\n        seenSkip = skip.current;\n        current += 1;\n        setIndex(current % pieces.length);\n        buildStitches();\n      }\n      paint(performance.now());\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  function aim(e: ReactPointerEvent<HTMLDivElement>) {\n    const r = e.currentTarget.getBoundingClientRect();\n    lampAim.current = { x: ((e.clientX - r.left) / r.width) * 2 - 1, y: ((e.clientY - r.top) / r.height) * 2 - 1, at: performance.now() };\n    redraw.current();\n  }\n  function next() {\n    skip.current += 1;\n    redraw.current();\n  }\n\n  return (\n    <section className=\"relative isolate flex min-h-[760px] w-full flex-col items-center overflow-hidden px-4\" style={{ backgroundColor: LINEN }}>\n      <div ref={stage} onPointerMove={aim} onPointerDown={aim} className=\"absolute inset-0 touch-pan-y\">\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n      </div>\n      <h2 className=\"sr-only\" aria-live=\"polite\">\n        {pieces[index].text}\n      </h2>\n\n      <div className=\"pointer-events-none relative mt-auto flex w-full max-w-[880px] items-center justify-between gap-4 pb-8 text-[13px]\">\n        <p className=\"rounded-full bg-[#f3ebdb]/90 px-4 py-2.5 text-[#5b4526] shadow-[0_1px_0_rgba(255,255,255,0.7)_inset,0_6px_14px_-8px_rgba(60,40,14,0.5)]\">\n          <span className=\"font-semibold text-[#2f2210]\">Satin stitch</span>\n          <span className=\"hidden sm:inline\">\n            <span className=\"mx-2 opacity-50\">/</span>\n            move the lamp with your pointer\n          </span>\n        </p>\n        <button\n          type=\"button\"\n          onClick={next}\n          className=\"pointer-events-auto min-h-[40px] shrink-0 rounded-full bg-[#2f2210] px-5 font-medium text-[#f6efe0] shadow-[0_10px_20px_-10px_rgba(47,34,16,0.8)] transition-transform hover:-translate-y-0.5 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#2f2210] focus-visible:ring-offset-2 focus-visible:ring-offset-[#e6dbc4]\"\n        >\n          Unpick and restitch\n        </button>\n      </div>\n    </section>\n  );\n}\n"}]}
