1+ class FlowingText {
2+ constructor ( textArray , speed = 0.8 ) {
3+ this . textArray = textArray ;
4+ this . speed = speed ;
5+ this . currentIndex = 0 ;
6+ this . textElements = document . querySelectorAll ( '.FlowingText' ) ;
7+ this . loadAnimeJS ( ) . then ( ( ) => this . startTextAnimation ( ) ) ;
8+ }
9+
10+ loadAnimeJS ( ) {
11+ return new Promise ( ( resolve , reject ) => {
12+ if ( typeof anime !== 'undefined' ) {
13+ resolve ( ) ;
14+ } else {
15+ const script = document . createElement ( 'script' ) ;
16+ script . src = 'https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js' ;
17+ script . onload = resolve ;
18+ script . onerror = reject ;
19+ document . head . appendChild ( script ) ;
20+ }
21+ } ) ;
22+ }
23+
24+ animateText ( element ) {
25+ if ( element ) {
26+ const animationConfig = {
27+ opacityIn : [ 0 , 1 ] ,
28+ scaleIn : [ 0.2 , 1 ] ,
29+ scaleOut : 3 ,
30+ durationIn : 800 / this . speed ,
31+ durationOut : 600 / this . speed ,
32+ delay : 500 / this . speed
33+ } ;
34+
35+ element . innerHTML = this . textArray [ this . currentIndex ] . split ( '' ) . map ( char => `<span style='display: inline-block; opacity: 0;'>${ char } </span>` ) . join ( '' ) ;
36+
37+ const timeline = anime . timeline ( { loop : false } ) ;
38+
39+ timeline
40+ . add ( {
41+ targets : element . children ,
42+ opacity : animationConfig . opacityIn ,
43+ scale : animationConfig . scaleIn ,
44+ duration : animationConfig . durationIn ,
45+ easing : 'easeOutExpo' ,
46+ delay : anime . stagger ( 100 / this . speed )
47+ } )
48+ . add ( {
49+ targets : element . children ,
50+ opacity : 0 ,
51+ scale : animationConfig . scaleOut ,
52+ duration : animationConfig . durationOut ,
53+ easing : "easeInExpo" ,
54+ delay : animationConfig . delay
55+ } ) ;
56+ }
57+ }
58+
59+ startTextAnimation ( ) {
60+ this . textElements . forEach ( element => this . animateText ( element ) ) ;
61+ setInterval ( ( ) => {
62+ this . currentIndex = ( this . currentIndex + 1 ) % this . textArray . length ;
63+ this . textElements . forEach ( element => this . animateText ( element ) ) ;
64+ } , 2500 / this . speed ) ;
65+ }
66+ }
67+
68+ // Usage example:
69+ // const animatio = new FlowingText(["Capture", "Animation"], 0.8);
70+
0 commit comments