{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"finger-written-words-in-window-condensation-that-drip-fog-over-and-wipe-clear","type":"registry:component","title":"Finger-Written Words in Window Condensation That Drip, Fog Over and Wipe Clear","description":"Canvas text effect: a line is written by fingertip on a misted window, drops run from the letters and dragging wipes the glass. Free React and HTML code.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/finger-written-words-in-window-condensation-that-drip-fog-over-and-wipe-clear.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// The lines a finger writes on the glass, one after another. Short ones read best.\nconst phrases = [\"grow slow\", \"stay a while\", \"open late\"];\n\nconst photo = {\n  src: \"https://images.pexels.com/photos/19516025/pexels-photo-19516025.jpeg?auto=compress&cs=tinysrgb&w=1600\",\n  alt: \"A mossy walkway running between dense tropical plants under the glass roof of a greenhouse\",\n};\n\n/** Colour the mist is tinted with. A warmer value reads as a kitchen window, a bluer one as a winter morning. */\nconst MIST = \"224, 238, 231\";\n/** Seconds a wiped patch stays clear before the mist starts to creep back, and how long it takes to close. */\nconst WIPE_HOLD = 2.6;\nconst WIPE_CLOSE = 3.4;\n\ntype Swipe = { x0: number; y0: number; x1: number; y1: number; at: number; w: number };\ntype Drip = { x: number; y: number; from: number; r: number; speed: number; wait: number; born: number; end: number; trail: number[]; phase: number };\ntype Line = { text: string; x0: number; x1: number; y: number };\n\nfunction bead(c: CanvasRenderingContext2D, x: number, y: number, r: number, alpha: number) {\n  c.globalAlpha = alpha;\n  const g = c.createRadialGradient(x - r * 0.32, y - r * 0.38, r * 0.05, x, y, r);\n  g.addColorStop(0, \"rgba(255,255,255,0.95)\");\n  g.addColorStop(0.22, \"rgba(255,255,255,0.22)\");\n  g.addColorStop(0.7, \"rgba(14,36,26,0.1)\");\n  g.addColorStop(1, \"rgba(8,22,16,0.42)\");\n  c.fillStyle = g;\n  c.beginPath();\n  c.ellipse(x, y, r * 0.92, r * 1.08, 0, 0, Math.PI * 2);\n  c.fill();\n  // Light that has gone through the drop gathers on its lower edge.\n  c.strokeStyle = \"rgba(255,255,255,0.6)\";\n  c.lineWidth = Math.max(0.6, r * 0.2);\n  c.beginPath();\n  c.arc(x, y, r * 0.66, Math.PI * 0.18, Math.PI * 0.7);\n  c.stroke();\n  c.globalAlpha = 1;\n}\n\nexport default function FingerWrittenWordsInWindowCondensationThatDripFogOverAndWipeClear() {\n  const reduce = useReducedMotion();\n  const pane = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const picture = useRef<HTMLImageElement>(null);\n  const swipes = useRef<Swipe[]>([]);\n  const lastPoint = useRef<{ x: number; y: number } | null>(null);\n  const skip = useRef(0);\n  const redraw = useRef<() => void>(() => {});\n  const [index, setIndex] = useState(0);\n\n  useEffect(() => {\n    const el = pane.current;\n    const cv = canvas.current;\n    const img = picture.current;\n    if (!el || !cv || !img) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n\n    const make = () => document.createElement(\"canvas\");\n    const fog = make();\n    const word = make();\n    const rim = make();\n    const cut = make();\n    const cutRim = make();\n    let W = 0;\n    let H = 0;\n    let ratio = 1;\n    let lines: Line[] = [];\n    let finger = 14;\n    let size = 100;\n    let current = 0;\n    let began = performance.now();\n    let drips: Drip[] = [];\n    let raf = 0;\n    let visible = true;\n    let seenSkip = skip.current;\n\n    /** Small repeatable random numbers, so the same phrase always wobbles the same way. */\n    const noise = (n: number) => {\n      const s = Math.sin(n * 127.1 + 311.7) * 43758.5453;\n      return s - Math.floor(s);\n    };\n\n    const buildFog = () => {\n      fog.width = cv.width;\n      fog.height = cv.height;\n      const f = fog.getContext(\"2d\")!;\n      f.setTransform(ratio, 0, 0, ratio, 0, 0);\n      const back = f.createLinearGradient(0, 0, 0, H);\n      back.addColorStop(0, \"#7fa692\");\n      back.addColorStop(1, \"#3d5f4c\");\n      f.fillStyle = back;\n      f.fillRect(0, 0, W, H);\n      if (img.complete && img.naturalWidth) {\n        // The view through misted glass: the photograph shrunk to a few dozen pixels and stretched back out.\n        const tiny = make();\n        tiny.width = 28;\n        tiny.height = Math.max(8, Math.round((28 * H) / W));\n        const iw = img.naturalWidth;\n        const ih = img.naturalHeight;\n        const scale = Math.max(W / iw, H / ih);\n        const sw = W / scale;\n        const sh = H / scale;\n        try {\n          tiny.getContext(\"2d\")!.drawImage(img, (iw - sw) / 2, (ih - sh) / 2, sw, sh, 0, 0, tiny.width, tiny.height);\n          // Doubling it a step at a time keeps the smear smooth instead of blocky.\n          let step = tiny;\n          while (step.width < W / 2) {\n            const bigger = make();\n            bigger.width = step.width * 2;\n            bigger.height = step.height * 2;\n            const b = bigger.getContext(\"2d\")!;\n            b.imageSmoothingQuality = \"high\";\n            b.drawImage(step, 0, 0, bigger.width, bigger.height);\n            step = bigger;\n          }\n          f.imageSmoothingQuality = \"high\";\n          f.drawImage(step, 0, 0, W, H);\n        } catch {\n          // The gradient above stands in for the picture.\n        }\n      }\n      f.fillStyle = `rgba(${MIST}, 0.66)`;\n      f.fillRect(0, 0, W, H);\n      // Mist sits thicker toward the cold edges of the pane.\n      const edge = f.createRadialGradient(W / 2, H / 2, Math.min(W, H) * 0.25, W / 2, H / 2, Math.max(W, H) * 0.72);\n      edge.addColorStop(0, `rgba(${MIST}, 0)`);\n      edge.addColorStop(1, `rgba(${MIST}, 0.5)`);\n      f.fillStyle = edge;\n      f.fillRect(0, 0, W, H);\n      // The mist is thousands of tiny beads: a pale speck with a darker one beside it.\n      const specks = Math.round((W * H) / 26);\n      for (let i = 0; i < specks; i++) {\n        const x = noise(i * 1.31) * W;\n        const y = noise(i * 2.17 + 9) * H;\n        const r = 0.35 + noise(i * 3.3) * 0.75;\n        f.fillStyle = \"rgba(255,255,255,0.16)\";\n        f.fillRect(x, y, r, r);\n        f.fillStyle = \"rgba(20,48,36,0.07)\";\n        f.fillRect(x + r, y + r, r, r);\n      }\n      // A few fat drops that have already gathered and are sitting still.\n      const fat = Math.round((W * H) / 8000);\n      for (let i = 0; i < fat; i++) {\n        bead(f, noise(i * 7.7 + 2) * W, noise(i * 5.1 + 4) * H, 0.9 + noise(i * 9.9) * noise(i * 3.1) * 3.6, 0.7);\n      }\n    };\n\n    const buildWord = () => {\n      const phrase = phrases[current % phrases.length];\n      const parts = W < 560 ? phrase.split(\" \") : [phrase];\n      const font = (px: number) => `100 ${px}px \"Helvetica Neue\", \"Segoe UI\", Roboto, system-ui, sans-serif`;\n      const w = word.getContext(\"2d\")!;\n      size = Math.min(H * (parts.length > 1 ? 0.21 : 0.36), 210);\n      w.setTransform(1, 0, 0, 1, 0, 0);\n      w.font = font(size);\n      const gap = 0.07;\n      const widest = Math.max(...parts.map((p) => w.measureText(p).width + p.length * size * gap));\n      size *= Math.min(1, (W * 0.78) / widest);\n      finger = Math.max(6, size * 0.07);\n      const lead = size * 1.06;\n      const top = H * 0.46 - ((parts.length - 1) * lead) / 2;\n\n      lines = [];\n      for (const c of [word, rim]) {\n        c.width = cv.width;\n        c.height = cv.height;\n      }\n      const r = rim.getContext(\"2d\")!;\n      const draw = (c: CanvasRenderingContext2D, width: number) => {\n        c.setTransform(ratio, 0, 0, ratio, 0, 0);\n        c.clearRect(0, 0, W, H);\n        c.font = font(size);\n        c.lineJoin = \"round\";\n        c.lineCap = \"round\";\n        c.lineWidth = width;\n        c.strokeStyle = \"#fff\";\n        c.fillStyle = \"#fff\";\n        c.textBaseline = \"middle\";\n        parts.forEach((text, row) => {\n          const full = c.measureText(text).width + text.length * size * gap;\n          let x = (W - full) / 2;\n          const y = top + row * lead;\n          if (c === w) lines.push({ text, x0: x - finger, x1: x + full + finger, y });\n          [...text].forEach((ch, i) => {\n            const seed = current * 31 + row * 17 + i;\n            const adv = c.measureText(ch).width + size * gap;\n            c.save();\n            // Each letter leans and sits a touch off the line, the way a hand writes on a wall of glass.\n            c.translate(x + adv / 2, y + (noise(seed) - 0.5) * size * 0.045);\n            c.rotate((noise(seed + 0.5) - 0.5) * 0.055);\n            c.transform(1, 0, -0.14, 1, 0, 0);\n            c.strokeText(ch, -adv / 2 + (size * gap) / 2, 0);\n            c.fillText(ch, -adv / 2 + (size * gap) / 2, 0);\n            c.restore();\n            x += adv;\n          });\n        });\n      };\n      draw(w, finger);\n      // The bank of water a fingertip pushes to each side of its stroke.\n      draw(r, finger + 3.2);\n      r.globalCompositeOperation = \"destination-out\";\n      const hold = make();\n      hold.width = cv.width;\n      hold.height = cv.height;\n      const h = hold.getContext(\"2d\")!;\n      draw(h, Math.max(2, finger - 0.5));\n      r.setTransform(1, 0, 0, 1, 0, 0);\n      r.drawImage(hold, 0, 0);\n      r.globalCompositeOperation = \"source-over\";\n\n      // Where the drops will let go: the lowest wet point under a handful of columns.\n      drips = [];\n      const data = w.getImageData(0, 0, word.width, word.height).data;\n      const wanted = Math.max(3, Math.round(W / 170));\n      const span = lines.reduce((a, l) => ({ x0: Math.min(a.x0, l.x0), x1: Math.max(a.x1, l.x1) }), { x0: W, x1: 0 });\n      for (let i = 0; i < wanted * 3 && drips.length < wanted; i++) {\n        const x = span.x0 + finger + noise(current * 13 + i * 3.7) * (span.x1 - span.x0 - finger * 2);\n        const px = Math.round(x * ratio);\n        let bottom = -1;\n        for (let y = word.height - 1; y > 0; y--) {\n          if (data[(y * word.width + px) * 4 + 3] > 200) {\n            bottom = y / ratio;\n            break;\n          }\n        }\n        if (bottom < 0 || drips.some((d) => Math.abs(d.x - x) < finger * 1.6)) continue;\n        const k = noise(i * 4.4 + current);\n        drips.push({\n          x,\n          y: bottom - 1,\n          from: bottom - 1,\n          r: 2 + k * 2.6,\n          speed: 46 + noise(i + 3.3) * 90,\n          wait: 0,\n          born: 0.25 + noise(i * 2.2 + 1) * 3.4,\n          end: Math.min(H - 6, bottom + H * (0.1 + noise(i * 6.1) * 0.34)),\n          trail: [],\n          phase: noise(i * 8.8) * 6,\n        });\n      }\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, 1.5);\n      for (const c of [cv, cut, cutRim]) {\n        c.width = Math.round(W * ratio);\n        c.height = Math.round(H * ratio);\n      }\n      buildFog();\n      buildWord();\n      paint(performance.now());\n    };\n\n    /** Copy a layer, keeping only what the finger has reached so far. */\n    const reveal = (target: HTMLCanvasElement, source: HTMLCanvasElement, written: number) => {\n      const c = target.getContext(\"2d\")!;\n      c.setTransform(1, 0, 0, 1, 0, 0);\n      c.globalCompositeOperation = \"copy\";\n      c.drawImage(source, 0, 0);\n      c.globalCompositeOperation = \"destination-out\";\n      c.setTransform(ratio, 0, 0, ratio, 0, 0);\n      let done = 0;\n      const soft = finger * 1.4;\n      for (const line of lines) {\n        const n = line.text.length;\n        const part = Math.min(1, Math.max(0, (written - done) / n));\n        done += n;\n        if (part >= 1) continue;\n        const pen = line.x0 + (line.x1 - line.x0 + soft) * part;\n        const g = c.createLinearGradient(pen - soft, 0, pen, 0);\n        g.addColorStop(0, \"rgba(0,0,0,0)\");\n        g.addColorStop(1, \"rgba(0,0,0,1)\");\n        c.fillStyle = g;\n        c.fillRect(pen - soft, line.y - size * 0.53, W, size * 1.06);\n      }\n      c.globalCompositeOperation = \"source-over\";\n    };\n\n    let lastStamp = 0;\n    const paint = (stamp: number) => {\n      if (!W) return;\n      const dt = Math.min(lastStamp ? (stamp - lastStamp) / 1000 : 0.016, 0.05);\n      lastStamp = stamp;\n      const letters = lines.reduce((n, l) => n + l.text.length, 0);\n      const writeFor = letters * 0.15;\n      const t = reduce ? 1e3 : (stamp - began) / 1000;\n      const holdUntil = writeFor + 7.2;\n      const written = reduce ? letters : Math.max(0, t / 0.15);\n      const ink = reduce ? 1 : 1 - Math.min(1, Math.max(0, (t - holdUntil) / 2.2));\n\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.globalCompositeOperation = \"copy\";\n      ctx.drawImage(fog, 0, 0);\n      ctx.globalCompositeOperation = \"destination-out\";\n\n      // The word.\n      const whole = written >= letters;\n      if (!whole) {\n        reveal(cut, word, written);\n        reveal(cutRim, rim, written);\n      }\n      ctx.globalAlpha = ink;\n      ctx.drawImage(whole ? word : cut, 0, 0);\n      ctx.globalAlpha = 1;\n\n      // Patches wiped by hand, closing over as the mist returns.\n      ctx.setTransform(ratio, 0, 0, ratio, 0, 0);\n      ctx.lineCap = \"round\";\n      ctx.lineJoin = \"round\";\n      const now = stamp / 1000;\n      swipes.current = swipes.current.filter((s) => now - s.at < WIPE_HOLD + WIPE_CLOSE);\n      for (const s of swipes.current) {\n        const age = now - s.at;\n        const shut = reduce ? 0 : Math.max(0, (age - WIPE_HOLD) / WIPE_CLOSE);\n        if (shut >= 1) continue;\n        ctx.strokeStyle = \"#000\";\n        // A patch closes by narrowing, because mist grows back in from its edges.\n        ctx.lineWidth = s.w * Math.pow(1 - shut, 0.7);\n        ctx.beginPath();\n        ctx.moveTo(s.x0, s.y0);\n        ctx.lineTo(s.x1, s.y1 + 0.01);\n        ctx.stroke();\n      }\n\n      // Runs of water falling from the letters.\n      const dripping = !reduce && whole;\n      for (const d of drips) {\n        const age = t - writeFor - d.born;\n        if (!dripping || age < 0) continue;\n        if (d.y < d.end) {\n          // Stick and slip: a drop hesitates, then lets go again.\n          d.wait -= dt;\n          if (d.wait <= 0) {\n            d.y += d.speed * dt * (0.6 + 0.4 * Math.sin(age * 3.1 + d.phase));\n            if (noise(Math.floor(age * 6) + d.phase) > 0.93) d.wait = 0.12 + noise(age) * 0.5;\n          }\n          const sway = Math.sin(d.y * 0.045 + d.phase) * 2.2 + Math.sin(d.y * 0.013 + d.phase * 2) * 3;\n          const n = d.trail.length;\n          if (!n || d.y - d.trail[n - 1] > 3) d.trail.push(d.x + sway, d.y);\n        }\n        if (d.trail.length >= 4) {\n          ctx.strokeStyle = `rgba(0,0,0,${0.92 * ink})`;\n          ctx.lineWidth = d.r * 1.35;\n          ctx.beginPath();\n          ctx.moveTo(d.x, d.from - finger * 0.3);\n          for (let i = 0; i < d.trail.length; i += 2) ctx.lineTo(d.trail[i], d.trail[i + 1]);\n          ctx.stroke();\n        }\n      }\n\n      ctx.globalCompositeOperation = \"source-over\";\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.globalAlpha = 0.34 * ink;\n      ctx.drawImage(whole ? rim : cutRim, 0, 0);\n      ctx.globalAlpha = 1;\n      ctx.setTransform(ratio, 0, 0, ratio, 0, 0);\n      for (const d of drips) {\n        const n = d.trail.length;\n        if (!dripping || n < 2) continue;\n        bead(ctx, d.trail[n - 2], d.trail[n - 1] + d.r * 0.4, d.r * (d.y < d.end ? 1.25 : 1.05), ink);\n        // Small beads left behind along the run.\n        for (let i = 6; i < n - 6; i += 14) {\n          if (noise(i + d.phase) > 0.45) bead(ctx, d.trail[i] + (noise(i) - 0.5) * 2, d.trail[i + 1], d.r * 0.34, ink * 0.8);\n        }\n      }\n\n      if (!reduce && (t > holdUntil + 2.5 || seenSkip !== skip.current)) {\n        seenSkip = skip.current;\n        current += 1;\n        setIndex(current % phrases.length);\n        began = stamp;\n        buildWord();\n      }\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) {\n        lastStamp = 0;\n        raf = requestAnimationFrame(loop);\n      }\n    };\n    redraw.current = () => {\n      if (!reduce) return;\n      if (seenSkip !== skip.current) {\n        seenSkip = skip.current;\n        current += 1;\n        setIndex(current % phrases.length);\n        swipes.current = [];\n        buildWord();\n      }\n      paint(performance.now());\n    };\n\n    // The first line is already half written when the pane appears.\n    began = performance.now() - phrases[0].length * 0.15 * 0.62 * 1000;\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    const loaded = () => {\n      buildFog();\n      paint(performance.now());\n    };\n    img.addEventListener(\"load\", loaded);\n    document.addEventListener(\"visibilitychange\", wake);\n    resize();\n    wake();\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      img.removeEventListener(\"load\", loaded);\n      document.removeEventListener(\"visibilitychange\", wake);\n    };\n  }, [reduce]);\n\n  function wipe(e: ReactPointerEvent<HTMLDivElement>) {\n    const r = e.currentTarget.getBoundingClientRect();\n    const p = { x: e.clientX - r.left, y: e.clientY - r.top };\n    const from = lastPoint.current ?? p;\n    if (lastPoint.current && e.type === \"pointermove\" && Math.hypot(p.x - from.x, p.y - from.y) < 3) return;\n    const list = swipes.current;\n    list.push({ x0: from.x, y0: from.y, x1: p.x, y1: p.y, at: performance.now() / 1000, w: Math.min(64, Math.max(34, r.width * 0.07)) });\n    if (list.length > 260) list.splice(0, list.length - 260);\n    lastPoint.current = p;\n    redraw.current();\n  }\n\n  function next() {\n    skip.current += 1;\n    redraw.current();\n  }\n\n  const phrase = phrases[index];\n\n  return (\n    <section className=\"relative isolate flex min-h-[760px] w-full flex-col items-center justify-center overflow-hidden bg-[#0b0f0d] px-4 py-14\">\n      <div aria-hidden=\"true\" className=\"absolute inset-0 -z-10\" style={{ background: \"radial-gradient(70% 55% at 50% 40%, #18211c 0%, #0b0f0d 72%)\" }} />\n\n      {/* The window: a painted steel frame, the view, and the misted glass in front of it. */}\n      <div style={{ background: \"linear-gradient(180deg, #3a4f45 0%, #1f2d27 38%, #131c18 100%)\" }} className=\"w-full max-w-[880px] rounded-[5px] bg-[#1d2a24] p-[12px] shadow-[0_70px_100px_-50px_rgba(0,0,0,0.95),inset_0_1px_0_rgba(255,255,255,0.14),inset_0_-1px_0_rgba(0,0,0,0.6),0_0_0_1px_rgba(0,0,0,0.7)] sm:p-[20px]\">\n        <div\n          ref={pane}\n          onPointerMove={wipe}\n          onPointerDown={wipe}\n          onPointerEnter={() => (lastPoint.current = null)}\n          onPointerLeave={() => (lastPoint.current = null)}\n          className=\"relative aspect-[4/5] w-full cursor-crosshair touch-pan-y overflow-hidden rounded-[2px] bg-[#27503a] shadow-[0_0_0_1px_rgba(0,0,0,0.8),0_0_0_3px_#0f1713,0_1px_0_3px_rgba(255,255,255,0.12)] sm:aspect-[16/10]\"\n        >\n          <img ref={picture} src={photo.src} alt={photo.alt} width={1600} height={1067} className=\"absolute inset-0 h-full w-full object-cover\" />\n          <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n          {/* Daylight on the outer face of the glass, and the frame's shadow falling across its top edge. */}\n          <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\" style={{ background: \"linear-gradient(112deg, rgba(255,255,255,0.13) 0%, rgba(255,255,255,0) 26%, rgba(255,255,255,0) 68%, rgba(255,255,255,0.07) 100%)\" }} />\n          <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 shadow-[inset_0_14px_22px_-10px_rgba(0,0,0,0.55),inset_0_0_40px_rgba(6,20,14,0.35)]\" />\n          <h2 className=\"sr-only\" aria-live=\"polite\">\n            {phrase}\n          </h2>\n        </div>\n      </div>\n\n      <div className=\"mt-7 flex w-full max-w-[880px] items-center justify-between gap-4 px-1 text-[13px]\">\n        <p className=\"text-zinc-400\">\n          <span className=\"font-semibold text-zinc-100\">Condensation</span>\n          <span className=\"mx-2 text-zinc-600\">/</span>\n          drag across the glass to wipe it\n        </p>\n        <button\n          type=\"button\"\n          onClick={next}\n          className=\"min-h-[40px] shrink-0 rounded-full border border-white/15 px-4 text-zinc-100 transition-colors hover:bg-white/10 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-emerald-300 focus-visible:ring-offset-2 focus-visible:ring-offset-[#0b0f0d]\"\n        >\n          Write the next line\n        </button>\n      </div>\n    </section>\n  );\n}\n"}]}
