{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ink-trail",
  "type": "registry:component",
  "title": "Ink Trail",
  "description": "A glossy ink trail that follows the cursor, widens as if bleeding into paper, and frays into strands until it fades away.",
  "author": "Marcelo Treviño Velázquez <https://furin.dev>",
  "dependencies": [],
  "categories": [
    "Cursors"
  ],
  "docs": "Installed in components/furin/ink-trail/. Docs, live controls and props table: https://furin.dev/components/ink-trail/",
  "files": [
    {
      "path": "registry/ink-trail/InkTrail.tsx",
      "content": "\"use client\";\nimport { useEffect, useRef } from \"react\";\nimport type { CSSProperties, ReactNode } from \"react\";\nimport \"./InkTrail.css\";\n\nexport interface InkTrailProps {\n  /** Color de la tinta. */\n  color?: string;\n  /** Color del brillo húmedo sobre la tinta. */\n  sheen?: string;\n  /** Grosor del trazo. */\n  size?: number;\n  /** Segundos que tarda la tinta en secarse y desaparecer. */\n  lifetime?: number;\n  /** Dibuja solo un trazo cuando nadie mueve el cursor. */\n  autoplay?: boolean;\n  /** Contenido opcional debajo de la estela. */\n  children?: ReactNode;\n  /** Clase extra para el contenedor. */\n  className?: string;\n  /** Estilos extra para el contenedor. */\n  style?: CSSProperties;\n}\n\nlet colorCtx: CanvasRenderingContext2D | null = null;\nfunction rgb(color: string): [number, number, number] {\n  colorCtx ??= document.createElement(\"canvas\").getContext(\"2d\", { willReadFrequently: true })!;\n  colorCtx.clearRect(0, 0, 1, 1);\n  colorCtx.fillStyle = \"#000\";\n  colorCtx.fillStyle = color;\n  colorCtx.fillRect(0, 0, 1, 1);\n  const d = colorCtx.getImageData(0, 0, 1, 1).data;\n  return [d[0] / 255, d[1] / 255, d[2] / 255];\n}\n\nconst VS = `attribute vec2 a; void main(){ gl_Position = vec4(a, 0.0, 1.0); }`;\n\nconst FS = `precision highp float;\nuniform sampler2D uD;\nuniform vec2 uRes, uTexel;\nuniform vec3 uInk, uSheen;\nuniform float uAsp;\n\nfloat h2(vec2 p){ p = fract(p * vec2(123.34, 456.21)); p += dot(p, p + 45.32); return fract(p.x * p.y); }\nfloat noise(vec2 p){\n  vec2 i = floor(p), f = fract(p);\n  f = f * f * (3.0 - 2.0 * f);\n  return mix(mix(h2(i), h2(i + vec2(1.0, 0.0)), f.x), mix(h2(i + vec2(0.0, 1.0)), h2(i + vec2(1.0, 1.0)), f.x), f.y);\n}\nfloat D(vec2 p){ return texture2D(uD, p).r; }\n\nvoid main(){\n  vec2 uv = gl_FragCoord.xy / uRes;\n  vec2 t = vec2(uv.x, 1.0 - uv.y);\n  float d = D(t);\n  vec2 q5 = uTexel * 5.0;\n  float fresh = max(max(D(t + vec2(q5.x, 0.0)), D(t - vec2(q5.x, 0.0))), max(D(t + vec2(0.0, q5.y)), D(t - vec2(0.0, q5.y))));\n  fresh = max(fresh, d);\n  float old = 1.0 - smoothstep(0.5, 0.95, fresh);\n  // al secarse, el borde se come con ruido y la tinta se vuelve agua: hebras translúcidas\n  float n = noise(uv * vec2(uAsp, 1.0) * 26.0) * 0.6 + noise(uv * vec2(uAsp, 1.0) * 70.0) * 0.4 - 0.5;\n  float thr = 0.42 + n * (0.07 + 0.12 * old);\n  float sw = 0.035 + 0.09 * old;\n  float cov = smoothstep(thr - sw, thr + sw, d);\n  float body = mix(0.97, 0.30, old);\n  // sombra de contacto sobre el papel\n  vec2 off = vec2(1.5, 2.5) * uTexel;\n  float sh = smoothstep(0.25, 0.7, D(t - off) ) * 0.14;\n  if (cov < 0.004 && sh < 0.004) { gl_FragColor = vec4(0.0); return; }\n  vec2 e = uTexel * 1.6;\n  float gx = D(t + vec2(e.x, 0.0)) - D(t - vec2(e.x, 0.0));\n  float gy = D(t + vec2(0.0, e.y)) - D(t - vec2(0.0, e.y));\n  vec3 nrm = normalize(vec3(-gx * 2.4, gy * 2.4, 1.0));\n  vec3 L = normalize(vec3(-0.5, 0.6, 0.65));\n  float wet = smoothstep(0.55, 1.0, d);\n  float spec = pow(max(dot(nrm, normalize(L + vec3(0.0, 0.0, 1.0))), 0.0), 38.0) * (0.35 + 0.65 * wet);\n  float rim = 1.0 - smoothstep(0.0, 0.16, d - thr);\n  vec3 col = uInk * (0.92 + 0.25 * (1.0 - wet));\n  col += uSheen * (spec * 0.75 + rim * 0.08 * wet);\n  col += vec3(1.0) * pow(spec, 2.0) * 0.28;\n  float al = cov * body;\n  gl_FragColor = vec4(col * al, max(al, sh));\n}`;\n\ninterface Pt { x: number; y: number; r: number; born: number; life: number }\n\n/** Estela de tinta: trazo húmedo y brillante que se ensancha, se deshace en hebras y se seca. */\nexport function InkTrail({\n  color = \"#12162B\",\n  sheen = \"#9DB4FF\",\n  size = 1,\n  lifetime = 1.9,\n  autoplay = false,\n  children,\n  className = \"\",\n  style,\n}: InkTrailProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const live = useRef({ color, sheen, size, lifetime, autoplay });\n  live.current = { color, sheen, size, lifetime, autoplay };\n  const wakeRef = useRef<() => void>(() => {});\n\n  useEffect(() => {\n    const el = host.current;\n    if (!el) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const canvas = document.createElement(\"canvas\");\n    canvas.className = \"furin-ink-trail__canvas\";\n    canvas.setAttribute(\"aria-hidden\", \"true\");\n    el.appendChild(canvas);\n    const gl = canvas.getContext(\"webgl\", { antialias: false, alpha: true, premultipliedAlpha: true });\n    if (!gl) {\n      canvas.remove();\n      return;\n    }\n    const compile = (type: number, src: string) => {\n      const s = gl.createShader(type)!;\n      gl.shaderSource(s, src);\n      gl.compileShader(s);\n      if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) console.error(\"[ink-trail] shader:\", gl.getShaderInfoLog(s));\n      return s;\n    };\n    const prog = gl.createProgram()!;\n    gl.attachShader(prog, compile(gl.VERTEX_SHADER, VS));\n    gl.attachShader(prog, compile(gl.FRAGMENT_SHADER, FS));\n    gl.linkProgram(prog);\n    if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) console.error(\"[ink-trail] link:\", gl.getProgramInfoLog(prog));\n    gl.useProgram(prog);\n    const quad = gl.createBuffer()!;\n    gl.bindBuffer(gl.ARRAY_BUFFER, quad);\n    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);\n    const loc = gl.getAttribLocation(prog, \"a\");\n    gl.enableVertexAttribArray(loc);\n    gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);\n    const u = (n: string) => gl.getUniformLocation(prog, n);\n    const U = { d: u(\"uD\"), res: u(\"uRes\"), texel: u(\"uTexel\"), ink: u(\"uInk\"), sheen: u(\"uSheen\"), asp: u(\"uAsp\") };\n    const tex = gl.createTexture()!;\n    gl.bindTexture(gl.TEXTURE_2D, tex);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n    gl.uniform1i(U.d, 0);\n    gl.enable(gl.BLEND);\n    gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);\n\n    // Sprite suave de una gota de tinta; se acumula con \"lighter\" para formar el cuerpo del trazo.\n    const sprite = document.createElement(\"canvas\");\n    sprite.width = sprite.height = 64;\n    {\n      const sc = sprite.getContext(\"2d\")!;\n      const g = sc.createRadialGradient(32, 32, 0, 32, 32, 32);\n      g.addColorStop(0, \"rgba(255,255,255,1)\");\n      g.addColorStop(0.45, \"rgba(255,255,255,0.9)\");\n      g.addColorStop(0.8, \"rgba(255,255,255,0.25)\");\n      g.addColorStop(1, \"rgba(255,255,255,0)\");\n      sc.fillStyle = g;\n      sc.fillRect(0, 0, 64, 64);\n    }\n    const dcv = document.createElement(\"canvas\");\n    const dctx = dcv.getContext(\"2d\")!;\n\n    const pts: Pt[] = [];\n    let W = 1, H = 1, dW = 1, dH = 1;\n    let time = 0, last = 0, raf = 0, visible = true;\n    let ink = rgb(color), shn = rgb(sheen);\n    let inkKey = color, shnKey = sheen;\n    const ptr = { x: 0, y: 0, t: 0, has: false, r: 7, lastReal: -1e9 };\n    let ap = 0; // fase del trazo automático\n    let apPos: { x: number; y: number } | null = null;\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      W = el.clientWidth || 300;\n      H = el.clientHeight || 300;\n      canvas.width = Math.max(2, Math.round(W * dpr));\n      canvas.height = Math.max(2, Math.round(H * dpr));\n      gl.viewport(0, 0, canvas.width, canvas.height);\n      dW = Math.max(2, Math.ceil(W / 2));\n      dH = Math.max(2, Math.ceil(H / 2));\n      dcv.width = dW;\n      dcv.height = dH;\n      draw();\n    };\n\n    const draw = () => {\n      if (inkKey !== live.current.color) {\n        inkKey = live.current.color;\n        ink = rgb(inkKey);\n      }\n      if (shnKey !== live.current.sheen) {\n        shnKey = live.current.sheen;\n        shn = rgb(shnKey);\n      }\n      dctx.setTransform(1, 0, 0, 1, 0, 0);\n      dctx.globalCompositeOperation = \"source-over\";\n      dctx.clearRect(0, 0, dW, dH);\n      dctx.fillStyle = \"#000\";\n      dctx.fillRect(0, 0, dW, dH);\n      dctx.globalCompositeOperation = \"lighter\";\n      for (const p of pts) {\n        const f = (time - p.born) / p.life;\n        if (f >= 1) continue;\n        // la tinta fresca es densa; al secarse pierde cuerpo y se ensancha un poco, como si sangrara\n        const a = Math.pow(1 - f, 0.9);\n        const rr = p.r * (1 + 0.5 * f) * 2.3;\n        dctx.globalAlpha = Math.min(1, a * 0.46);\n        dctx.drawImage(sprite, (p.x - rr) / 2, (p.y - rr) / 2, rr, rr);\n      }\n      dctx.globalAlpha = 1;\n      gl.activeTexture(gl.TEXTURE0);\n      gl.bindTexture(gl.TEXTURE_2D, tex);\n      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, dcv);\n      gl.clearColor(0, 0, 0, 0);\n      gl.clear(gl.COLOR_BUFFER_BIT);\n      gl.uniform2f(U.res, canvas.width, canvas.height);\n      gl.uniform2f(U.texel, 1 / dW, 1 / dH);\n      gl.uniform3f(U.ink, ink[0], ink[1], ink[2]);\n      gl.uniform3f(U.sheen, shn[0], shn[1], shn[2]);\n      gl.uniform1f(U.asp, canvas.width / canvas.height);\n      gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);\n    };\n\n    const life = () => (reduce ? Math.min(live.current.lifetime, 0.6) : live.current.lifetime);\n\n    const stroke = (x: number, y: number, tNow: number) => {\n      if (!ptr.has) {\n        ptr.x = x;\n        ptr.y = y;\n        ptr.t = tNow;\n        ptr.has = true;\n        pts.push({ x, y, r: Math.max(2.2, ptr.r) * live.current.size, born: time, life: life() });\n        return;\n      }\n      const dx = x - ptr.x;\n      const dy = y - ptr.y;\n      const dist = Math.hypot(dx, dy);\n      if (dist < 0.5) return;\n      const dtMs = Math.max(1, tNow - ptr.t);\n      const v = dist / dtMs; // px/ms\n      // rápido = trazo fino, lento = trazo grueso (como un pincel cargado)\n      const target = 9.5 - Math.min(v * 3.0, 6.0);\n      const steps = Math.max(1, Math.ceil(dist / 2.2));\n      for (let i = 1; i <= steps; i++) {\n        const k = i / steps;\n        ptr.r += (target - ptr.r) * 0.12;\n        const jitter = 1 + (Math.random() - 0.5) * 0.08;\n        pts.push({ x: ptr.x + dx * k, y: ptr.y + dy * k, r: Math.max(2.0, ptr.r) * live.current.size * jitter, born: time, life: life() });\n      }\n      // salpicaduras en los movimientos rápidos\n      if (v > 1.1 && Math.random() < 0.55) {\n        const nx = -dy / dist;\n        const ny = dx / dist;\n        const off = (Math.random() - 0.5) * 34;\n        pts.push({ x: x + nx * off, y: y + ny * off, r: (1.2 + Math.random() * 2.2) * live.current.size, born: time, life: life() * 0.7 });\n      }\n      ptr.x = x;\n      ptr.y = y;\n      ptr.t = tNow;\n      if (pts.length > 900) pts.splice(0, pts.length - 900);\n    };\n\n    const autoStep = (dt: number) => {\n      ap += dt;\n      const t = ap * 1.25;\n      const x = W * (0.5 + 0.36 * Math.sin(t * 0.9 + 0.6 * Math.sin(t * 0.37)));\n      const y = H * (0.5 + 0.30 * Math.sin(t * 1.35 + 1.1));\n      const now = performance.now();\n      if (apPos) stroke(x, y, now);\n      else {\n        ptr.has = false;\n        stroke(x, y, now);\n      }\n      apPos = { x, y };\n    };\n\n    const frame = (now: number) => {\n      raf = 0;\n      const dt = Math.min(0.05, Math.max(0.001, (now - last) / 1000));\n      last = now;\n      time += dt;\n      const realIdle = performance.now() - ptr.lastReal > 1800;\n      if (live.current.autoplay && !reduce && realIdle) autoStep(dt);\n      else apPos = null;\n      while (pts.length && time - pts[0].born >= pts[0].life) pts.shift();\n      draw();\n      const active = pts.length > 0 || (live.current.autoplay && !reduce);\n      if (active && visible) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => {\n      if (raf || !visible) return;\n      last = performance.now();\n      raf = requestAnimationFrame(frame);\n    };\n    wakeRef.current = () => {\n      draw();\n      wake();\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const r = el.getBoundingClientRect();\n      ptr.lastReal = performance.now();\n      if (apPos) {\n        apPos = null;\n        ptr.has = false;\n      }\n      stroke(e.clientX - r.left, e.clientY - r.top, e.timeStamp);\n      wake();\n    };\n    const onLeave = () => {\n      ptr.has = false;\n      ptr.r = 7;\n    };\n    el.addEventListener(\"pointermove\", onMove);\n    el.addEventListener(\"pointerdown\", onMove);\n    el.addEventListener(\"pointerleave\", onLeave);\n    el.addEventListener(\"pointercancel\", onLeave);\n\n    const io = new IntersectionObserver(([en]) => {\n      visible = en.isIntersecting;\n      if (visible) wake();\n      else if (raf) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(el);\n    const ro = new ResizeObserver(resize);\n    ro.observe(el);\n    resize();\n    if (live.current.autoplay && !reduce) wake();\n\n    return () => {\n      cancelAnimationFrame(raf);\n      io.disconnect();\n      ro.disconnect();\n      el.removeEventListener(\"pointermove\", onMove);\n      el.removeEventListener(\"pointerdown\", onMove);\n      el.removeEventListener(\"pointerleave\", onLeave);\n      el.removeEventListener(\"pointercancel\", onLeave);\n      wakeRef.current = () => {};\n      gl.deleteTexture(tex);\n      gl.deleteBuffer(quad);\n      gl.deleteProgram(prog);\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    };\n  }, []);\n\n  useEffect(() => {\n    wakeRef.current();\n  }, [color, sheen, size, lifetime, autoplay]);\n\n  return (\n    <div ref={host} className={`furin-ink-trail ${className}`} style={style}>\n      {children ? <div className=\"furin-ink-trail__contenido\">{children}</div> : null}\n    </div>\n  );\n}\n\nexport default InkTrail;\n",
      "type": "registry:component",
      "target": "components/furin/ink-trail/InkTrail.tsx"
    },
    {
      "path": "registry/ink-trail/InkTrail.css",
      "content": ".furin-ink-trail {\n  position: relative;\n  width: 100%;\n  height: 100%;\n  min-height: 200px;\n  overflow: hidden;\n  touch-action: pan-y;\n}\n.furin-ink-trail__canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; z-index: 2; }\n.furin-ink-trail__contenido { position: relative; z-index: 1; width: 100%; height: 100%; display: grid; place-items: center; pointer-events: none; }\n.furin-ink-trail__contenido > * { pointer-events: auto; }\n",
      "type": "registry:file",
      "target": "components/furin/ink-trail/InkTrail.css"
    }
  ]
}
