| 398 | } |
| 399 | |
| 400 | export default function ASCIIText({ |
| 401 | text = 'David!', |
| 402 | asciiFontSize = 8, |
| 403 | textFontSize = 200, |
| 404 | textColor = '#fdf9f3', |
| 405 | planeBaseHeight = 8, |
| 406 | enableWaves = true |
| 407 | }) { |
| 408 | const containerRef = useRef(null); |
| 409 | const asciiRef = useRef(null); |
| 410 | |
| 411 | useEffect(() => { |
| 412 | if (!containerRef.current) return; |
| 413 | |
| 414 | let cancelled = false; |
| 415 | let observer = null; |
| 416 | let ro = null; |
| 417 | |
| 418 | const createAndInit = async (container, w, h) => { |
| 419 | const instance = new CanvAscii( |
| 420 | { text, asciiFontSize, textFontSize, textColor, planeBaseHeight, enableWaves }, |
| 421 | container, |
| 422 | w, |
| 423 | h |
| 424 | ); |
| 425 | await instance.init(); |
| 426 | return instance; |
| 427 | }; |
| 428 | |
| 429 | const setup = async () => { |
| 430 | const { width, height } = containerRef.current.getBoundingClientRect(); |
| 431 | |
| 432 | if (width === 0 || height === 0) { |
| 433 | observer = new IntersectionObserver( |
| 434 | async ([entry]) => { |
| 435 | if (cancelled) return; |
| 436 | if (entry.isIntersecting && entry.boundingClientRect.width > 0 && entry.boundingClientRect.height > 0) { |
| 437 | const { width: w, height: h } = entry.boundingClientRect; |
| 438 | observer.disconnect(); |
| 439 | observer = null; |
| 440 | |
| 441 | if (!cancelled) { |
| 442 | asciiRef.current = await createAndInit(containerRef.current, w, h); |
| 443 | if (!cancelled && asciiRef.current) { |
| 444 | asciiRef.current.load(); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | }, |
| 449 | { threshold: 0.1 } |
| 450 | ); |
| 451 | observer.observe(containerRef.current); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | asciiRef.current = await createAndInit(containerRef.current, width, height); |
| 456 | if (!cancelled && asciiRef.current) { |
| 457 | asciiRef.current.load(); |