|
| 1 | +import React, { useEffect, useRef } from "react"; |
| 2 | + |
| 3 | +const FuzzyText = ({ |
| 4 | + children, |
| 5 | + fontSize = "clamp(2rem, 10vw, 10rem)", |
| 6 | + fontWeight = 900, |
| 7 | + fontFamily = "inherit", |
| 8 | + color = "#fff", |
| 9 | + enableHover = true, |
| 10 | + baseIntensity = 0.18, |
| 11 | + hoverIntensity = 0.5, |
| 12 | +}) => { |
| 13 | + const canvasRef = useRef(null); |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + const canvas = canvasRef.current; |
| 17 | + if (!canvas) return; |
| 18 | + const ctx = canvas.getContext("2d"); |
| 19 | + if (!ctx) return; |
| 20 | + |
| 21 | + const computedFontFamily = |
| 22 | + fontFamily === "inherit" |
| 23 | + ? window.getComputedStyle(canvas).fontFamily || "sans-serif" |
| 24 | + : fontFamily; |
| 25 | + |
| 26 | + const fontSizeStr = |
| 27 | + typeof fontSize === "number" ? `${fontSize}px` : fontSize; |
| 28 | + let numericFontSize; |
| 29 | + if (typeof fontSize === "number") { |
| 30 | + numericFontSize = fontSize; |
| 31 | + } else { |
| 32 | + const temp = document.createElement("span"); |
| 33 | + temp.style.fontSize = fontSize; |
| 34 | + document.body.appendChild(temp); |
| 35 | + const computedSize = window.getComputedStyle(temp).fontSize; |
| 36 | + numericFontSize = parseFloat(computedSize); |
| 37 | + document.body.removeChild(temp); |
| 38 | + } |
| 39 | + |
| 40 | + const text = React.Children.toArray(children).join(""); |
| 41 | + |
| 42 | + const offscreen = document.createElement("canvas"); |
| 43 | + const offCtx = offscreen.getContext("2d"); |
| 44 | + if (!offCtx) return; |
| 45 | + |
| 46 | + offCtx.font = `${fontWeight} ${fontSizeStr} ${computedFontFamily}`; |
| 47 | + offCtx.textBaseline = "alphabetic"; |
| 48 | + const metrics = offCtx.measureText(text); |
| 49 | + |
| 50 | + const actualLeft = metrics.actualBoundingBoxLeft ?? 0; |
| 51 | + const actualRight = metrics.actualBoundingBoxRight ?? metrics.width; |
| 52 | + const actualAscent = metrics.actualBoundingBoxAscent ?? numericFontSize; |
| 53 | + const actualDescent = |
| 54 | + metrics.actualBoundingBoxDescent ?? numericFontSize * 0.2; |
| 55 | + |
| 56 | + const textBoundingWidth = Math.ceil(actualLeft + actualRight); |
| 57 | + const tightHeight = Math.ceil(actualAscent + actualDescent); |
| 58 | + |
| 59 | + const extraWidthBuffer = 10; |
| 60 | + const offscreenWidth = textBoundingWidth + extraWidthBuffer; |
| 61 | + |
| 62 | + offscreen.width = offscreenWidth; |
| 63 | + offscreen.height = tightHeight; |
| 64 | + |
| 65 | + const xOffset = extraWidthBuffer / 2; |
| 66 | + offCtx.font = `${fontWeight} ${fontSizeStr} ${computedFontFamily}`; |
| 67 | + offCtx.textBaseline = "alphabetic"; |
| 68 | + offCtx.fillStyle = color; |
| 69 | + offCtx.fillText(text, xOffset - actualLeft, actualAscent); |
| 70 | + |
| 71 | + const horizontalMargin = 50; |
| 72 | + const verticalMargin = 0; |
| 73 | + canvas.width = offscreenWidth + horizontalMargin * 2; |
| 74 | + canvas.height = tightHeight + verticalMargin * 2; |
| 75 | + ctx.translate(horizontalMargin, verticalMargin); |
| 76 | + |
| 77 | + const interactiveLeft = horizontalMargin + xOffset; |
| 78 | + const interactiveTop = verticalMargin; |
| 79 | + const interactiveRight = interactiveLeft + textBoundingWidth; |
| 80 | + const interactiveBottom = interactiveTop + tightHeight; |
| 81 | + |
| 82 | + let isHovering = false; |
| 83 | + const fuzzRange = 30; |
| 84 | + let animationFrameId; |
| 85 | + |
| 86 | + const run = () => { |
| 87 | + ctx.clearRect( |
| 88 | + -fuzzRange, |
| 89 | + -fuzzRange, |
| 90 | + offscreenWidth + 2 * fuzzRange, |
| 91 | + tightHeight + 2 * fuzzRange |
| 92 | + ); |
| 93 | + const intensity = isHovering ? hoverIntensity : baseIntensity; |
| 94 | + for (let j = 0; j < tightHeight; j++) { |
| 95 | + const dx = Math.floor(intensity * (Math.random() - 0.5) * fuzzRange); |
| 96 | + ctx.drawImage( |
| 97 | + offscreen, |
| 98 | + 0, |
| 99 | + j, |
| 100 | + offscreenWidth, |
| 101 | + 1, |
| 102 | + dx, |
| 103 | + j, |
| 104 | + offscreenWidth, |
| 105 | + 1 |
| 106 | + ); |
| 107 | + } |
| 108 | + animationFrameId = window.requestAnimationFrame(run); |
| 109 | + }; |
| 110 | + |
| 111 | + run(); |
| 112 | + |
| 113 | + const isInsideTextArea = (x, y) => { |
| 114 | + return ( |
| 115 | + x >= interactiveLeft && |
| 116 | + x <= interactiveRight && |
| 117 | + y >= interactiveTop && |
| 118 | + y <= interactiveBottom |
| 119 | + ); |
| 120 | + }; |
| 121 | + |
| 122 | + const handleMouseMove = (e) => { |
| 123 | + if (!enableHover) return; |
| 124 | + const rect = canvas.getBoundingClientRect(); |
| 125 | + const x = e.clientX - rect.left; |
| 126 | + const y = e.clientY - rect.top; |
| 127 | + isHovering = isInsideTextArea(x, y); |
| 128 | + }; |
| 129 | + |
| 130 | + const handleMouseLeave = () => { |
| 131 | + isHovering = false; |
| 132 | + }; |
| 133 | + |
| 134 | + const handleTouchMove = (e) => { |
| 135 | + if (!enableHover) return; |
| 136 | + e.preventDefault(); |
| 137 | + const rect = canvas.getBoundingClientRect(); |
| 138 | + const touch = e.touches[0]; |
| 139 | + const x = touch.clientX - rect.left; |
| 140 | + const y = touch.clientY - rect.top; |
| 141 | + isHovering = isInsideTextArea(x, y); |
| 142 | + }; |
| 143 | + |
| 144 | + const handleTouchEnd = () => { |
| 145 | + isHovering = false; |
| 146 | + }; |
| 147 | + |
| 148 | + if (enableHover) { |
| 149 | + canvas.addEventListener("mousemove", handleMouseMove); |
| 150 | + canvas.addEventListener("mouseleave", handleMouseLeave); |
| 151 | + canvas.addEventListener("touchmove", handleTouchMove, { passive: false }); |
| 152 | + canvas.addEventListener("touchend", handleTouchEnd); |
| 153 | + } |
| 154 | + |
| 155 | + return () => { |
| 156 | + window.cancelAnimationFrame(animationFrameId); |
| 157 | + if (enableHover) { |
| 158 | + canvas.removeEventListener("mousemove", handleMouseMove); |
| 159 | + canvas.removeEventListener("mouseleave", handleMouseLeave); |
| 160 | + canvas.removeEventListener("touchmove", handleTouchMove); |
| 161 | + canvas.removeEventListener("touchend", handleTouchEnd); |
| 162 | + } |
| 163 | + }; |
| 164 | + }, [ |
| 165 | + children, |
| 166 | + fontSize, |
| 167 | + fontWeight, |
| 168 | + fontFamily, |
| 169 | + color, |
| 170 | + enableHover, |
| 171 | + baseIntensity, |
| 172 | + hoverIntensity, |
| 173 | + ]); |
| 174 | + |
| 175 | + return <canvas ref={canvasRef} />; |
| 176 | +}; |
| 177 | + |
| 178 | +export default FuzzyText; |
0 commit comments