Spaces:
Paused
Paused
File size: 9,695 Bytes
d8804ab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | "use client"
import type React from "react"
import { motion } from "motion/react"
import { useMemo, useState, useCallback, useEffect, useRef, createContext, useContext } from "react"
import { Volume2, VolumeX } from "lucide-react"
interface AudioContextType {
isMuted: boolean
toggleMute: () => void
playClick: () => void
}
const SplitFlapAudioContext = createContext<AudioContextType | null>(null)
export function useSplitFlapAudio() {
return useContext(SplitFlapAudioContext)
}
export function SplitFlapAudioProvider({ children }: { children: React.ReactNode }) {
const [isMuted, setIsMuted] = useState(true)
const audioContextRef = useRef<AudioContext | null>(null)
const getAudioContext = useCallback(() => {
if (typeof window === "undefined") return null
if (!audioContextRef.current) {
const AudioContextClass = window.AudioContext || (window as Window & { webkitAudioContext?: typeof AudioContext }).webkitAudioContext
if (AudioContextClass) {
audioContextRef.current = new AudioContextClass()
}
}
return audioContextRef.current
}, [])
const triggerHaptic = useCallback(() => {
if (isMuted) return
if (typeof navigator !== "undefined" && "vibrate" in navigator) {
navigator.vibrate(10)
}
}, [isMuted])
const playClick = useCallback(() => {
if (isMuted) return
triggerHaptic()
try {
const ctx = getAudioContext()
if (!ctx) return
if (ctx.state === "suspended") {
ctx.resume()
}
const oscillator = ctx.createOscillator()
const gainNode = ctx.createGain()
const filter = ctx.createBiquadFilter()
const lowpass = ctx.createBiquadFilter()
oscillator.type = "square"
oscillator.frequency.setValueAtTime(800 + Math.random() * 400, ctx.currentTime)
oscillator.frequency.exponentialRampToValueAtTime(200, ctx.currentTime + 0.015)
filter.type = "bandpass"
filter.frequency.setValueAtTime(1200, ctx.currentTime)
filter.Q.setValueAtTime(0.8, ctx.currentTime)
lowpass.type = "lowpass"
lowpass.frequency.value = 2500
lowpass.Q.value = 0.5
gainNode.gain.setValueAtTime(0.05, ctx.currentTime)
gainNode.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + 0.02)
oscillator.connect(filter)
filter.connect(gainNode)
gainNode.connect(lowpass)
lowpass.connect(ctx.destination)
oscillator.start(ctx.currentTime)
oscillator.stop(ctx.currentTime + 0.02)
} catch {
// Audio not supported
}
}, [isMuted, getAudioContext, triggerHaptic])
const toggleMute = useCallback(() => {
setIsMuted((prev) => !prev)
if (isMuted) {
try {
const ctx = getAudioContext()
if (ctx && ctx.state === "suspended") {
ctx.resume()
}
} catch {
// Audio not supported
}
}
}, [isMuted, getAudioContext])
const value = useMemo(() => ({ isMuted, toggleMute, playClick }), [isMuted, toggleMute, playClick])
return <SplitFlapAudioContext.Provider value={value}>{children}</SplitFlapAudioContext.Provider>
}
export function SplitFlapMuteToggle({ className = "" }: { className?: string }) {
const audio = useSplitFlapAudio()
if (!audio) return null
return (
<button
onClick={audio.toggleMute}
className={`inline-flex items-center gap-2 font-mono text-[10px] uppercase tracking-widest text-muted-foreground hover:text-foreground transition-colors duration-200 ${className}`}
aria-label={audio.isMuted ? "Unmute sound effects" : "Mute sound effects"}
>
{audio.isMuted ? <VolumeX className="w-4 h-4" /> : <Volume2 className="w-4 h-4" />}
<span>{audio.isMuted ? "Sound Off" : "Sound On"}</span>
</button>
)
}
interface SplitFlapTextProps {
text: string
className?: string
speed?: number
fontSize?: string | number
}
const CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split("")
function SplitFlapTextInner({ text, className = "", speed = 50, fontSize = "inherit" }: SplitFlapTextProps) {
const chars = useMemo(() => text.split(""), [text])
const [animationKey, setAnimationKey] = useState(0)
const [hasInitialized, setHasInitialized] = useState(false)
const audio = useSplitFlapAudio()
const handleMouseEnter = useCallback(() => {
setAnimationKey((prev) => prev + 1)
}, [])
useEffect(() => {
const timer = setTimeout(() => {
setHasInitialized(true)
}, 1000)
return () => clearTimeout(timer)
}, [])
return (
<div
className={`inline-flex gap-[0.08em] items-center cursor-pointer ${className}`}
aria-label={text}
onMouseEnter={handleMouseEnter}
style={{ perspective: "1000px" }}
>
{chars.map((char, index) => (
<SplitFlapChar
key={index}
char={char.toUpperCase()}
index={index}
animationKey={animationKey}
skipEntrance={hasInitialized}
speed={speed}
fontSize={fontSize}
playClick={audio?.playClick}
/>
))}
</div>
)
}
export function SplitFlapText(props: SplitFlapTextProps) {
return <SplitFlapTextInner {...props} />
}
interface SplitFlapCharProps {
char: string
index: number
animationKey: number
skipEntrance: boolean
speed: number
fontSize: string | number
playClick?: () => void
}
function SplitFlapChar({ char, index, animationKey, skipEntrance, speed, fontSize, playClick }: SplitFlapCharProps) {
const displayChar = CHARSET.includes(char) ? char : " "
const isSpace = char === " "
const [currentChar, setCurrentChar] = useState(skipEntrance ? displayChar : " ")
const [isSettled, setIsSettled] = useState(skipEntrance)
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null)
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
const tileDelay = 0.15 * index
const bgColor = isSettled ? "hsl(0, 0%, 0%)" : "rgba(249, 115, 22, 0.2)"
const textColor = isSettled ? "#ffffff" : "#f97316"
useEffect(() => {
if (isSpace) return
if (intervalRef.current) clearInterval(intervalRef.current)
if (timeoutRef.current) clearTimeout(timeoutRef.current)
const baseFlips = 8
const startDelay = skipEntrance ? tileDelay * 400 : tileDelay * 800
let flipIndex = 0
let hasStartedSettling = false
timeoutRef.current = setTimeout(() => {
setIsSettled(false)
setCurrentChar(CHARSET[Math.floor(Math.random() * CHARSET.length)])
intervalRef.current = setInterval(() => {
const settleThreshold = baseFlips + index * 3
if (flipIndex >= settleThreshold && !hasStartedSettling) {
hasStartedSettling = true
if (intervalRef.current) clearInterval(intervalRef.current)
setCurrentChar(displayChar)
setIsSettled(true)
if (playClick) playClick()
return
}
setCurrentChar(CHARSET[Math.floor(Math.random() * CHARSET.length)])
if (flipIndex % 2 === 0 && playClick) playClick()
flipIndex++
}, speed)
}, startDelay)
return () => {
if (intervalRef.current) clearInterval(intervalRef.current)
if (timeoutRef.current) clearTimeout(timeoutRef.current)
}
}, [displayChar, isSpace, tileDelay, animationKey, skipEntrance, index, speed, playClick])
if (isSpace) {
return (
<div
style={{
width: "0.3em",
fontSize: fontSize,
}}
/>
)
}
return (
<motion.div
initial={skipEntrance ? { opacity: 1, y: 0 } : { opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: tileDelay, duration: 0.3, ease: "easeOut" }}
className="relative overflow-hidden flex items-center justify-center font-[family-name:var(--font-bebas)]"
style={{
fontSize: fontSize,
width: "0.65em",
height: "1.05em",
backgroundColor: bgColor,
transformStyle: "preserve-3d",
transition: "background-color 0.15s ease",
}}
>
<div className="absolute inset-x-0 top-1/2 h-[1px] bg-black/20 pointer-events-none z-10" />
<div className="absolute inset-x-0 top-0 bottom-1/2 flex items-end justify-center overflow-hidden">
<span
className="block translate-y-[0.52em] leading-none transition-colors duration-150"
style={{ color: textColor }}
>
{currentChar}
</span>
</div>
<div className="absolute inset-x-0 top-1/2 bottom-0 flex items-start justify-center overflow-hidden">
<span
className="-translate-y-[0.52em] leading-none transition-colors duration-150"
style={{ color: textColor }}
>
{currentChar}
</span>
</div>
<motion.div
key={`${animationKey}-${isSettled}`}
initial={{ rotateX: -90 }}
animate={{ rotateX: 0 }}
transition={{
delay: skipEntrance ? tileDelay * 0.5 : tileDelay + 0.15,
duration: 0.25,
ease: [0.22, 0.61, 0.36, 1],
}}
className="absolute inset-x-0 top-0 bottom-1/2 origin-bottom overflow-hidden"
style={{
backgroundColor: bgColor,
transformStyle: "preserve-3d",
backfaceVisibility: "hidden",
transition: "background-color 0.15s ease",
}}
>
<div className="flex h-full items-end justify-center">
<span
className="translate-y-[0.52em] leading-none transition-colors duration-150"
style={{ color: textColor }}
>
{currentChar}
</span>
</div>
</motion.div>
</motion.div>
)
}
|