{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tile-field",
  "title": "Tile Field",
  "description": "Render a theme-aware wordmark with moving tiles, a color wave, and pointer glow.",
  "files": [
    {
      "path": "src/registry/components/tile-field/tile-field.tsx",
      "content": "\"use client\";\n\nimport type { ComponentProps } from \"react\";\nimport { useEffect, useRef } from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\nconst TAU = Math.PI * 2;\nconst MAX_SIZE = 0.9;\nconst SPEED = 0.02;\nconst ALPHA_STEPS = 6;\n\nconst clamp = (value: number, min: number, max: number) =>\n\tMath.min(max, Math.max(min, value));\n\nfunction smoothstep(value: number, edge0: number, edge1: number) {\n\tconst t = clamp((value - edge0) / (edge1 - edge0), 0, 1);\n\treturn t * t * (3 - 2 * t);\n}\n\nfunction hash(x: number, y: number) {\n\tconst value = Math.sin(x * 127.1 + y * 311.7) * 43758.5453;\n\treturn value - Math.floor(value);\n}\n\nclass TileFieldEngine {\n\tprivate canvas: HTMLCanvasElement;\n\tprivate ctx: CanvasRenderingContext2D;\n\tprivate host: HTMLElement;\n\tprivate color: string;\n\tprivate reducedMotion: boolean;\n\tprivate restColor = \"gray\";\n\tprivate word: string;\n\n\tprivate dpr = Math.min(2, window.devicePixelRatio || 1);\n\tprivate viewW = 0;\n\tprivate viewH = 0;\n\tprivate cell = 10;\n\tprivate time = 0;\n\n\tprivate n = 0;\n\tprivate px = new Float32Array(0);\n\tprivate py = new Float32Array(0);\n\tprivate drawX = new Float32Array(0);\n\tprivate drawY = new Float32Array(0);\n\tprivate spark = new Uint8Array(0);\n\tprivate lit = new Float32Array(0);\n\tprivate seed = new Float32Array(0);\n\n\tprivate hasPointer = false;\n\tprivate rawX = 0;\n\tprivate rawY = 0;\n\tprivate lightX = 0;\n\tprivate lightY = 0;\n\tprivate lightSpeed = 0;\n\tprivate raf = 0;\n\n\tconstructor(host: HTMLElement, word: string, color: string) {\n\t\tthis.host = host;\n\t\tthis.word = word;\n\t\tthis.color = color;\n\t\tthis.canvas = document.createElement(\"canvas\");\n\t\tthis.canvas.className =\n\t\t\t\"pointer-events-none absolute inset-0 block size-full\";\n\t\tthis.canvas.setAttribute(\"aria-hidden\", \"true\");\n\t\tthis.ctx = this.canvas.getContext(\"2d\")!;\n\t\tthis.host.appendChild(this.canvas);\n\t\tthis.reducedMotion = window.matchMedia(\n\t\t\t\"(prefers-reduced-motion: reduce)\",\n\t\t).matches;\n\n\t\tthis.resize();\n\t\tthis.host.addEventListener(\"pointermove\", this.onMove, { passive: true });\n\t\tthis.host.addEventListener(\"pointerleave\", this.onLeave);\n\t}\n\n\tresize() {\n\t\tconst rect = this.host.getBoundingClientRect();\n\t\tthis.viewW = rect.width;\n\t\tthis.viewH = rect.height;\n\t\tthis.dpr = Math.min(2, window.devicePixelRatio || 1);\n\t\tthis.cell = Math.max(2, Math.round(this.viewW / 460));\n\n\t\tthis.canvas.style.width = `${Math.round(this.viewW)}px`;\n\t\tthis.canvas.style.height = `${Math.round(this.viewH)}px`;\n\t\tthis.canvas.width = Math.ceil(this.viewW * this.dpr);\n\t\tthis.canvas.height = Math.ceil(this.viewH * this.dpr);\n\t\tthis.ctx.setTransform(this.dpr, 0, 0, this.dpr, 0, 0);\n\t\tthis.ctx.imageSmoothingEnabled = false;\n\t\tthis.updateTheme();\n\n\t\tconst source = document.createElement(\"canvas\");\n\t\tsource.width = Math.max(1, Math.floor(this.viewW));\n\t\tsource.height = Math.max(1, Math.floor(this.viewH));\n\t\tconst sourceCtx = source.getContext(\"2d\")!;\n\t\tconst spacedCtx = sourceCtx as CanvasRenderingContext2D & {\n\t\t\tletterSpacing?: string;\n\t\t};\n\t\tsourceCtx.fillStyle = \"#000\";\n\t\tsourceCtx.textAlign = \"center\";\n\t\tsourceCtx.textBaseline = \"middle\";\n\n\t\tlet fontSize = this.viewH * 0.9;\n\t\tspacedCtx.letterSpacing = `${-0.02 * fontSize}px`;\n\t\tsourceCtx.font = `600 ${fontSize}px ui-sans-serif, system-ui, Arial, sans-serif`;\n\t\tconst measuredWidth = sourceCtx.measureText(this.word).width || 1;\n\t\tfontSize *= (this.viewW * 0.92) / measuredWidth;\n\t\tspacedCtx.letterSpacing = `${-0.02 * fontSize}px`;\n\t\tsourceCtx.font = `600 ${fontSize}px ui-sans-serif, system-ui, Arial, sans-serif`;\n\t\tsourceCtx.fillText(this.word, this.viewW / 2, this.viewH / 2);\n\n\t\tconst data = sourceCtx.getImageData(0, 0, source.width, source.height).data;\n\t\tconst xs: number[] = [];\n\t\tconst ys: number[] = [];\n\t\tconst sparks: number[] = [];\n\t\tconst seeds: number[] = [];\n\t\tconst columns = Math.ceil(this.viewW / this.cell);\n\t\tconst rows = Math.ceil(this.viewH / this.cell);\n\n\t\tfor (let row = 0; row < rows; row++) {\n\t\t\tfor (let column = 0; column < columns; column++) {\n\t\t\t\tconst x = Math.floor(column * this.cell + this.cell / 2);\n\t\t\t\tconst y = Math.floor(row * this.cell + this.cell / 2);\n\t\t\t\tif (x >= source.width || y >= source.height) continue;\n\n\t\t\t\tconst alpha = (data[(y * source.width + x) * 4 + 3] ?? 0) / 255;\n\t\t\t\tif (alpha <= 0.5) continue;\n\n\t\t\t\txs.push(x);\n\t\t\t\tys.push(y);\n\t\t\t\tsparks.push(hash(x + 7, y - 3) > 0.82 ? 1 : 0);\n\t\t\t\tseeds.push(hash(x * 1.3, y * 0.7));\n\t\t\t}\n\t\t}\n\n\t\tthis.n = xs.length;\n\t\tthis.px = new Float32Array(xs);\n\t\tthis.py = new Float32Array(ys);\n\t\tthis.drawX = new Float32Array(xs);\n\t\tthis.drawY = new Float32Array(ys);\n\t\tthis.spark = new Uint8Array(sparks);\n\t\tthis.seed = new Float32Array(seeds);\n\t\tthis.lit = new Float32Array(this.n);\n\n\t\tif (this.reducedMotion) this.renderStatic();\n\t}\n\n\tprivate distSegSq(\n\t\tqx: number,\n\t\tqy: number,\n\t\tax: number,\n\t\tay: number,\n\t\tbx: number,\n\t\tby: number,\n\t) {\n\t\tconst dx = bx - ax;\n\t\tconst dy = by - ay;\n\t\tif (dx === 0 && dy === 0) {\n\t\t\tconst ex = qx - ax;\n\t\t\tconst ey = qy - ay;\n\t\t\treturn ex * ex + ey * ey;\n\t\t}\n\n\t\tconst t = clamp(\n\t\t\t((qx - ax) * dx + (qy - ay) * dy) / (dx * dx + dy * dy),\n\t\t\t0,\n\t\t\t1,\n\t\t);\n\t\tconst ex = qx - (ax + dx * t);\n\t\tconst ey = qy - (ay + dy * t);\n\t\treturn ex * ex + ey * ey;\n\t}\n\n\tprivate frame = (timestamp: number) => {\n\t\tconst { viewW, viewH, cell, n, px, py } = this;\n\t\tconst drawX = this.drawX;\n\t\tconst drawY = this.drawY;\n\t\tconst spark = this.spark;\n\t\tconst lit = this.lit;\n\t\tconst seed = this.seed;\n\t\tthis.ctx.clearRect(0, 0, viewW, viewH);\n\t\tthis.time += SPEED;\n\t\tconst time = this.time;\n\n\t\tconst previousX = this.lightX;\n\t\tconst previousY = this.lightY;\n\t\tif (this.hasPointer) {\n\t\t\tthis.lightX += (this.rawX - this.lightX) * 0.5;\n\t\t\tthis.lightY += (this.rawY - this.lightY) * 0.5;\n\t\t}\n\n\t\tconst lightX = this.lightX;\n\t\tconst lightY = this.lightY;\n\t\tconst stepDistance = Math.hypot(lightX - previousX, lightY - previousY);\n\t\tthis.lightSpeed = 0.9 * this.lightSpeed + 0.1 * stepDistance;\n\n\t\tconst reach = clamp(\n\t\t\tviewH * 0.22 + this.lightSpeed * 0.9,\n\t\t\tviewH * 0.18,\n\t\t\tviewH * 0.42,\n\t\t);\n\t\tconst influence = 1.5 * reach;\n\t\tconst influenceSq = influence * influence;\n\t\tconst minX = Math.min(previousX, lightX) - influence;\n\t\tconst maxX = Math.max(previousX, lightX) + influence;\n\t\tconst minY = Math.min(previousY, lightY) - influence;\n\t\tconst maxY = Math.max(previousY, lightY) + influence;\n\n\t\tconst grayPath = new Path2D();\n\t\tconst litList: number[] = [];\n\t\tconst buckets = Array.from({ length: ALPHA_STEPS }, () => new Path2D());\n\n\t\tfor (let index = 0; index < n; index++) {\n\t\t\tconst x = px[index];\n\t\t\tconst y = py[index];\n\t\t\tlet target = 0;\n\n\t\t\tif (this.hasPointer && x >= minX && x <= maxX && y >= minY && y <= maxY) {\n\t\t\t\tconst distanceSq = this.distSegSq(\n\t\t\t\t\tx,\n\t\t\t\t\ty,\n\t\t\t\t\tpreviousX,\n\t\t\t\t\tpreviousY,\n\t\t\t\t\tlightX,\n\t\t\t\t\tlightY,\n\t\t\t\t);\n\t\t\t\tif (distanceSq <= influenceSq) {\n\t\t\t\t\tconst angle = Math.atan2(y - lightY, x - lightX);\n\t\t\t\t\tconst wobble =\n\t\t\t\t\t\t1 +\n\t\t\t\t\t\t0.3 * Math.sin(3 * angle + time * 1.6) +\n\t\t\t\t\t\t0.16 * Math.sin(5 * angle - time * 1.1 + 1.3);\n\t\t\t\t\tconst amount = clamp(\n\t\t\t\t\t\t1 - Math.sqrt(distanceSq) / (reach * wobble),\n\t\t\t\t\t\t0,\n\t\t\t\t\t\t1,\n\t\t\t\t\t);\n\t\t\t\t\ttarget = amount * amount * (3 - 2 * amount);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst rate = target > lit[index] ? 0.24 : 0.02;\n\t\t\tlit[index] += (target - lit[index]) * rate;\n\n\t\t\tconst pointerX = x - lightX;\n\t\t\tconst pointerY = y - lightY;\n\t\t\tconst pointerDistance = Math.max(1, Math.hypot(pointerX, pointerY));\n\t\t\tconst motion =\n\t\t\t\tlit[index] *\n\t\t\t\tcell *\n\t\t\t\t(0.65 + 0.2 * Math.sin(seed[index] * TAU + time * 2));\n\t\t\tconst drift = lit[index] * cell * 0.18;\n\t\t\tconst tileX =\n\t\t\t\tx +\n\t\t\t\t(pointerX / pointerDistance) * motion +\n\t\t\t\tMath.sin(seed[index] * TAU + time * 2.4) * drift;\n\t\t\tconst tileY =\n\t\t\t\ty +\n\t\t\t\t(pointerY / pointerDistance) * motion +\n\t\t\t\tMath.cos(seed[index] * TAU - time * 2) * drift;\n\t\t\tdrawX[index] = tileX;\n\t\t\tdrawY[index] = tileY;\n\n\t\t\tconst u = x / Math.max(viewW, 1);\n\t\t\tconst v = y / Math.max(viewH, 1);\n\t\t\tconst flow =\n\t\t\t\tMath.sin((u * 1.6 + 0.4 * Math.sin(time * 0.3)) * TAU + time * 0.8) +\n\t\t\t\t0.7 * Math.sin((v * 2.1 - u * 0.9) * TAU - time * 0.6 + 1.7) +\n\t\t\t\t0.5 * Math.sin((u * 3.3 + v * 2.7) * TAU + time * 0.4 + 4.2) +\n\t\t\t\t0.4 *\n\t\t\t\t\tMath.cos((v * 1.3 - 1.1 * Math.sin(time * 0.2)) * TAU - time * 0.5);\n\t\t\tlet colorAmount = smoothstep(flow, 0.1, 1.6);\n\t\t\tcolorAmount = Math.max(colorAmount, lit[index]);\n\n\t\t\tconst breathe = 0.5 + 0.5 * Math.sin(seed[index] * TAU + time * 1.3);\n\t\t\tconst base = 0.42 + 0.08 * breathe;\n\t\t\tconst size = cell * (base + (MAX_SIZE - base) * colorAmount);\n\t\t\tconst half = size / 2;\n\t\t\tgrayPath.rect(tileX - half, tileY - half, size, size);\n\n\t\t\tif (colorAmount > 0.04) {\n\t\t\t\tconst alphaStep = Math.min(\n\t\t\t\t\tALPHA_STEPS - 1,\n\t\t\t\t\tMath.floor(colorAmount * ALPHA_STEPS),\n\t\t\t\t);\n\t\t\t\tbuckets[alphaStep].rect(tileX - half, tileY - half, size, size);\n\t\t\t}\n\n\t\t\tif (lit[index] > 0.02) litList.push(index);\n\t\t}\n\n\t\tthis.ctx.fillStyle = this.restColor;\n\t\tthis.ctx.fill(grayPath);\n\n\t\tthis.ctx.fillStyle = this.color;\n\t\tfor (let alphaStep = 0; alphaStep < ALPHA_STEPS; alphaStep++) {\n\t\t\tthis.ctx.globalAlpha = (alphaStep + 1) / ALPHA_STEPS;\n\t\t\tthis.ctx.fill(buckets[alphaStep]);\n\t\t}\n\t\tthis.ctx.globalAlpha = 1;\n\n\t\tfor (const index of litList) {\n\t\t\tconst light = lit[index];\n\t\t\tconst x = drawX[index];\n\t\t\tconst y = drawY[index];\n\t\t\tconst glowSize = cell * (0.7 + (MAX_SIZE - 0.7) * light);\n\t\t\tconst glowHalf = glowSize / 2;\n\n\t\t\tif (spark[index]) {\n\t\t\t\tconst phase = seed[index];\n\t\t\t\tconst sparkTime = 0.00025 * timestamp;\n\t\t\t\tconst amplitude = (0.45 + phase) * cell * 0.28;\n\t\t\t\tconst jitterX =\n\t\t\t\t\tx + Math.sin(0.05 * x + 1.3 * sparkTime + phase * TAU) * amplitude;\n\t\t\t\tconst jitterY =\n\t\t\t\t\ty + Math.cos(0.04 * y - 0.9 * sparkTime + phase * TAU) * amplitude;\n\t\t\t\tthis.ctx.globalAlpha = 0.1 * light;\n\t\t\t\tthis.ctx.fillStyle = this.color;\n\t\t\t\tthis.ctx.fillRect(\n\t\t\t\t\tjitterX - glowHalf * 1.5,\n\t\t\t\t\tjitterY - glowHalf * 1.5,\n\t\t\t\t\tglowSize * 1.5,\n\t\t\t\t\tglowSize * 1.5,\n\t\t\t\t);\n\t\t\t\tthis.ctx.globalAlpha = 0.55 * light;\n\t\t\t\tthis.ctx.fillRect(\n\t\t\t\t\tjitterX - glowHalf,\n\t\t\t\t\tjitterY - glowHalf,\n\t\t\t\t\tglowSize,\n\t\t\t\t\tglowSize,\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\tthis.ctx.globalAlpha = 0.85 * light;\n\t\t\t\tthis.ctx.fillStyle = this.color;\n\t\t\t\tthis.ctx.fillRect(x - glowHalf, y - glowHalf, glowSize, glowSize);\n\t\t\t}\n\t\t}\n\t\tthis.ctx.globalAlpha = 1;\n\n\t\tthis.raf = requestAnimationFrame(this.frame);\n\t};\n\n\trenderStatic() {\n\t\tthis.ctx.clearRect(0, 0, this.viewW, this.viewH);\n\t\tconst path = new Path2D();\n\t\tfor (let index = 0; index < this.n; index++) {\n\t\t\tconst size = this.cell * 0.5;\n\t\t\tconst half = size / 2;\n\t\t\tpath.rect(this.px[index] - half, this.py[index] - half, size, size);\n\t\t}\n\t\tthis.ctx.fillStyle = this.restColor;\n\t\tthis.ctx.fill(path);\n\t}\n\n\tupdateTheme() {\n\t\tthis.restColor = getComputedStyle(this.host).color;\n\t\tif (this.reducedMotion) this.renderStatic();\n\t}\n\n\tsetColor(color: string) {\n\t\tthis.color = color;\n\t}\n\n\tsetReducedMotion(reducedMotion: boolean) {\n\t\tthis.reducedMotion = reducedMotion;\n\t\tif (reducedMotion) this.renderStatic();\n\t}\n\n\tprivate onMove = (event: PointerEvent) => {\n\t\tif (event.pointerType === \"touch\") return;\n\n\t\tconst rect = this.host.getBoundingClientRect();\n\t\tconst x = event.clientX - rect.left;\n\t\tconst y = event.clientY - rect.top;\n\t\tif (!this.hasPointer) {\n\t\t\tthis.hasPointer = true;\n\t\t\tthis.lightX = x;\n\t\t\tthis.lightY = y;\n\t\t\tthis.lightSpeed = 0;\n\t\t}\n\t\tthis.rawX = x;\n\t\tthis.rawY = y;\n\t};\n\n\tprivate onLeave = () => {\n\t\tthis.hasPointer = false;\n\t};\n\n\tstart() {\n\t\tif (this.reducedMotion || this.raf) return;\n\t\tthis.raf = requestAnimationFrame(this.frame);\n\t}\n\n\tstop() {\n\t\tif (this.raf) cancelAnimationFrame(this.raf);\n\t\tthis.raf = 0;\n\t}\n\n\tdestroy() {\n\t\tthis.stop();\n\t\tthis.host.removeEventListener(\"pointermove\", this.onMove);\n\t\tthis.host.removeEventListener(\"pointerleave\", this.onLeave);\n\t\tthis.canvas.remove();\n\t}\n}\n\nexport interface TileFieldProps\n\textends Omit<ComponentProps<\"div\">, \"children\" | \"ref\"> {\n\t/** Color used by the wave, pointer glow, and sparks. */\n\tcolor?: string;\n\t/** Text rendered into the tile grid. */\n\tword?: string;\n}\n\nexport function TileField({\n\t\"aria-label\": ariaLabel,\n\tclassName,\n\tcolor = \"#6366f1\",\n\tword = \"Localhost\",\n\t...props\n}: TileFieldProps) {\n\tconst hostRef = useRef<HTMLDivElement>(null);\n\tconst engineRef = useRef<TileFieldEngine>(null);\n\tconst colorRef = useRef(color);\n\tcolorRef.current = color;\n\n\tuseEffect(() => {\n\t\tconst host = hostRef.current;\n\t\tif (!host) return;\n\n\t\tconst engine = new TileFieldEngine(host, word, colorRef.current);\n\t\tengineRef.current = engine;\n\t\tconst motionQuery = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n\t\tlet isInView = true;\n\n\t\tconst syncAnimation = () => {\n\t\t\tengine.setReducedMotion(motionQuery.matches);\n\t\t\tif (isInView && !document.hidden && !motionQuery.matches) {\n\t\t\t\tengine.start();\n\t\t\t} else {\n\t\t\t\tengine.stop();\n\t\t\t}\n\t\t};\n\t\tconst resizeObserver = new ResizeObserver(() => engine.resize());\n\t\tconst themeObserver = new MutationObserver(() => engine.updateTheme());\n\t\tconst intersectionObserver = new IntersectionObserver(([entry]) => {\n\t\t\tisInView = entry?.isIntersecting ?? true;\n\t\t\tsyncAnimation();\n\t\t});\n\n\t\tresizeObserver.observe(host);\n\t\tthemeObserver.observe(document.documentElement, {\n\t\t\tattributeFilter: [\"class\", \"style\"],\n\t\t\tattributes: true,\n\t\t});\n\t\tintersectionObserver.observe(host);\n\t\tdocument.addEventListener(\"visibilitychange\", syncAnimation);\n\t\tmotionQuery.addEventListener(\"change\", syncAnimation);\n\t\tsyncAnimation();\n\n\t\treturn () => {\n\t\t\tresizeObserver.disconnect();\n\t\t\tthemeObserver.disconnect();\n\t\t\tintersectionObserver.disconnect();\n\t\t\tdocument.removeEventListener(\"visibilitychange\", syncAnimation);\n\t\t\tmotionQuery.removeEventListener(\"change\", syncAnimation);\n\t\t\tengine.destroy();\n\t\t\tengineRef.current = null;\n\t\t};\n\t}, [word]);\n\n\tuseEffect(() => engineRef.current?.setColor(color), [color]);\n\n\treturn (\n\t\t<div\n\t\t\taria-label={ariaLabel ?? word}\n\t\t\tclassName={cn(\n\t\t\t\t\"relative isolate min-h-48 w-full overflow-hidden text-muted-foreground\",\n\t\t\t\tclassName,\n\t\t\t)}\n\t\t\tdata-slot=\"tile-field\"\n\t\t\tref={hostRef}\n\t\t\trole=\"img\"\n\t\t\t{...props}\n\t\t/>\n\t);\n}\n\nexport default TileField;\n",
      "type": "registry:component",
      "target": "@components/tile-field.tsx"
    }
  ],
  "meta": {
    "createdAt": "2026-07-30",
    "image": "https://cdn.tentui.com/images/components/tile-field",
    "video": "https://cdn.tentui.com/videos/components/tile-field"
  },
  "docs": "https://tentui.com/components/tile-field",
  "categories": [
    "typography"
  ],
  "type": "registry:component"
}