2022-06-03 16:36:24 +00:00
|
|
|
import React, { Fragment, useEffect, useState } from 'react';
|
|
|
|
import { gsap } from "gsap";
|
2022-01-06 05:27:13 +00:00
|
|
|
|
|
|
|
let timer = 0;
|
|
|
|
let keyNum = 0;
|
|
|
|
let textTimeout;
|
2022-06-04 07:26:53 +00:00
|
|
|
let textInterval;
|
2022-06-03 16:36:24 +00:00
|
|
|
// let chars = ("abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-;.,/?><][}{:`~").split("");
|
2022-06-04 07:26:53 +00:00
|
|
|
let interval = 200;
|
2022-01-06 05:27:13 +00:00
|
|
|
|
2022-06-03 16:36:24 +00:00
|
|
|
const TextShuffler = ({ textInput, delay }) => {
|
2022-01-06 05:27:13 +00:00
|
|
|
|
2022-06-04 07:26:53 +00:00
|
|
|
// let [text, setText] = useState(textInput);
|
|
|
|
// let [refresh, setRefresh] = useState(0);
|
2022-06-03 16:36:24 +00:00
|
|
|
let [readyState, setReadyState] = useState(false);
|
|
|
|
|
|
|
|
const spanRef = React.useRef();
|
2022-01-06 05:27:13 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-06-03 16:36:24 +00:00
|
|
|
const spanObserver = new IntersectionObserver((entries, observer) => {
|
|
|
|
if (entries[0].isIntersecting) {
|
2022-06-04 07:26:53 +00:00
|
|
|
if (delay) {
|
|
|
|
setTimeout(() => {
|
2022-06-03 16:36:24 +00:00
|
|
|
setReadyState(true);
|
2022-06-04 07:26:53 +00:00
|
|
|
}, delay);
|
|
|
|
} else {
|
|
|
|
setReadyState(true);
|
|
|
|
}
|
2022-06-03 16:36:24 +00:00
|
|
|
|
|
|
|
observer.unobserve(spanRef.current)
|
|
|
|
}
|
|
|
|
|
|
|
|
}, {
|
|
|
|
rootMargin: "0px 0px 0px 0px"
|
|
|
|
});
|
|
|
|
|
|
|
|
spanObserver.observe(spanRef.current)
|
|
|
|
}, [])
|
|
|
|
|
2022-06-04 07:26:53 +00:00
|
|
|
// useEffect(() => {
|
|
|
|
// if (!readyState) return;
|
|
|
|
|
|
|
|
// requestAnimationFrame(() => {
|
|
|
|
// textTimeout = setTimeout(() => {
|
|
|
|
// if (timer === 7) {
|
|
|
|
// setText(textInput);
|
|
|
|
// return window.clearTimeout(textTimeout);
|
|
|
|
// };
|
|
|
|
|
|
|
|
// setRefresh(prev => prev + 1);
|
|
|
|
// timer++
|
|
|
|
// }, 300);
|
|
|
|
// })
|
|
|
|
// }, [refresh, readyState])
|
|
|
|
|
2022-06-03 16:36:24 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!readyState) return;
|
|
|
|
|
2022-06-04 07:26:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
let chars = textInput.split("");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let charsSpans = chars.map(char => `<span style="opacity:0">${char}</span>`)
|
|
|
|
|
|
|
|
spanRef.current.innerHTML = charsSpans.join("");
|
|
|
|
|
|
|
|
gsap.to(spanRef.current, {
|
|
|
|
opacity: 1,
|
|
|
|
duration: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
let textSpans = spanRef.current.querySelectorAll("span");
|
|
|
|
|
|
|
|
// textInterval = setInterval(() => {
|
|
|
|
// if (timer >= 600) {
|
|
|
|
// window.clearInterval(textInterval);
|
|
|
|
|
|
|
|
// requestAnimationFrame(() => {
|
|
|
|
// textSpans.forEach(span => {
|
|
|
|
// // gsap.killTweensOf(span, "opacity");
|
|
|
|
|
|
|
|
// gsap.to(span, {
|
|
|
|
// opacity: 1,
|
|
|
|
// duration: Math.random(),
|
|
|
|
// delay: Math.random()
|
|
|
|
// })
|
|
|
|
// })
|
|
|
|
// })
|
|
|
|
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// requestAnimationFrame(() => {
|
|
|
|
// textSpans.forEach(span => {
|
|
|
|
// gsap.to(span, {
|
|
|
|
// opacity: 1,
|
|
|
|
// duration: Math.random() * 2,
|
|
|
|
// delay: Math.random() * 1.5
|
|
|
|
// })
|
|
|
|
// })
|
|
|
|
|
|
|
|
// // textSpans.forEach(span => {
|
|
|
|
// // gsap.to(span, {
|
|
|
|
// // opacity: 0,
|
|
|
|
// // duration: Math.random() * 2,
|
|
|
|
// // delay: Math.random() * 1.5
|
|
|
|
// // })
|
|
|
|
// // })
|
|
|
|
|
|
|
|
// // gsap.to(spanRef.current, {
|
|
|
|
// // opacity: 0,
|
|
|
|
// // duration: 2
|
|
|
|
// // });
|
|
|
|
|
|
|
|
// timer += interval;
|
|
|
|
// })
|
|
|
|
|
|
|
|
// }, interval)
|
|
|
|
|
2022-06-03 16:36:24 +00:00
|
|
|
requestAnimationFrame(() => {
|
2022-06-04 07:26:53 +00:00
|
|
|
// let charsSpans = chars.map(char => `<span>${char}</span>`)
|
|
|
|
|
|
|
|
// spanRef.current.innerHTML = charsSpans.join("");
|
|
|
|
|
|
|
|
// let textSpans = spanRef.current.querySelectorAll("span");
|
|
|
|
|
|
|
|
textSpans.forEach(span => {
|
|
|
|
gsap.to(span, {
|
|
|
|
opacity: 1,
|
|
|
|
duration: Math.random() * 1.5,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
textSpans.forEach(span => {
|
|
|
|
gsap.killTweensOf(span, "opacity");
|
|
|
|
gsap.to(span, {
|
|
|
|
opacity: 0,
|
|
|
|
duration: Math.random() * 1.5,
|
|
|
|
delay: Math.random() * 0.5
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
textSpans.forEach(span => {
|
|
|
|
gsap.killTweensOf(span, "opacity");
|
|
|
|
|
|
|
|
gsap.to(span, {
|
|
|
|
opacity: 1,
|
|
|
|
duration: Math.random() * 1.5,
|
|
|
|
delay: Math.random()
|
|
|
|
})
|
|
|
|
})
|
2022-06-03 16:36:24 +00:00
|
|
|
})
|
2022-06-04 07:26:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
}, [readyState])
|
2022-01-06 05:27:13 +00:00
|
|
|
|
2022-06-03 16:36:24 +00:00
|
|
|
return (
|
|
|
|
<span className="shuffled-text-span" ref={ spanRef } style={ { opacity: 0 } }>
|
2022-06-04 07:26:53 +00:00
|
|
|
{/* { text.split("").map(char => <span key={ keyNum++ } style={ { animationDelay: Math.random() * 1.5 + "s", animationDuration: (Math.random() * 2) + "s" } }>{ char }</span>) } */ }
|
|
|
|
{ textInput }
|
2022-06-03 16:36:24 +00:00
|
|
|
</span>
|
|
|
|
)
|
2022-01-06 05:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default TextShuffler
|