|
1 | | -import React, { useEffect, useState } from 'react'; |
| 1 | +import React, { useEffect, useState, useRef } from 'react'; |
| 2 | +import { gsap } from 'gsap'; |
2 | 3 | import styles from './FlyingBats.module.css'; |
3 | 4 |
|
4 | 5 | function FlyingBats({ count = 5, maxHeight = 280, isHomePage = false }) { |
5 | 6 | const [bats, setBats] = useState([]); |
| 7 | + const batsRef = useRef([]); |
| 8 | + const [isMobile, setIsMobile] = useState(false); |
| 9 | + |
| 10 | + // Detect mobile for performance optimization |
| 11 | + useEffect(() => { |
| 12 | + setIsMobile(window.innerWidth < 768); |
| 13 | + }, []); |
6 | 14 |
|
7 | 15 | useEffect(() => { |
8 | 16 | // Generate random bat configurations with varied paths |
9 | 17 | // Distribute bats across limited page height to not go below footer |
10 | | - const paths = ['path1', 'path2', 'path3', 'path4']; |
11 | 18 | const generatedBats = Array.from({ length: count }, (_, index) => ({ |
12 | 19 | id: `bat-${index}-${Math.random()}`, |
13 | 20 | startX: Math.random() * 100, |
14 | 21 | startY: Math.random() * (maxHeight - 10) + 10, // Distribute based on maxHeight |
15 | | - duration: Math.random() * 10 + 8, // 8-18 seconds for faster movement |
| 22 | + duration: isMobile ? Math.random() * 8 + 6 : Math.random() * 10 + 8, // Faster on mobile |
16 | 23 | delay: Math.random() * 6, |
17 | 24 | size: Math.random() * 0.4 + 0.5, // 0.5-0.9x size |
18 | | - path: paths[Math.floor(Math.random() * paths.length)], |
| 25 | + pathType: Math.floor(Math.random() * 4), // 0-3 for different paths |
19 | 26 | })); |
20 | 27 | setBats(generatedBats); |
21 | | - }, [count, maxHeight]); |
| 28 | + }, [count, maxHeight, isMobile]); |
| 29 | + |
| 30 | + // GSAP animations for each bat |
| 31 | + useEffect(() => { |
| 32 | + if (bats.length === 0) { |
| 33 | + return undefined; |
| 34 | + } |
| 35 | + |
| 36 | + const animations = []; |
| 37 | + |
| 38 | + bats.forEach((bat, index) => { |
| 39 | + const batElement = batsRef.current[index]; |
| 40 | + if (!batElement) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + // Set initial position |
| 45 | + gsap.set(batElement, { |
| 46 | + x: `${bat.startX}vw`, |
| 47 | + y: `${bat.startY}vh`, |
| 48 | + scale: bat.size, |
| 49 | + force3D: true, // GPU acceleration |
| 50 | + }); |
| 51 | + |
| 52 | + // Get path coordinates based on pathType |
| 53 | + const paths = [ |
| 54 | + // Path 1: Wave pattern |
| 55 | + [ |
| 56 | + { x: '20vw', y: '-10vh' }, |
| 57 | + { x: '40vw', y: '15vh' }, |
| 58 | + { x: '60vw', y: '-5vh' }, |
| 59 | + { x: '80vw', y: '20vh' }, |
| 60 | + { x: '110vw', y: '10vh' }, |
| 61 | + ], |
| 62 | + // Path 2: Zigzag |
| 63 | + [ |
| 64 | + { x: '15vw', y: '20vh' }, |
| 65 | + { x: '35vw', y: '-15vh' }, |
| 66 | + { x: '55vw', y: '25vh' }, |
| 67 | + { x: '75vw', y: '-10vh' }, |
| 68 | + { x: '110vw', y: '5vh' }, |
| 69 | + ], |
| 70 | + // Path 3: Smooth curve |
| 71 | + [ |
| 72 | + { x: '25vw', y: '10vh' }, |
| 73 | + { x: '50vw', y: '-20vh' }, |
| 74 | + { x: '75vw', y: '15vh' }, |
| 75 | + { x: '110vw', y: '-5vh' }, |
| 76 | + ], |
| 77 | + // Path 4: Steep dive |
| 78 | + [ |
| 79 | + { x: '30vw', y: '-15vh' }, |
| 80 | + { x: '50vw', y: '30vh' }, |
| 81 | + { x: '70vw', y: '0vh' }, |
| 82 | + { x: '110vw', y: '20vh' }, |
| 83 | + ], |
| 84 | + ]; |
| 85 | + |
| 86 | + const pathCoords = paths[bat.pathType]; |
| 87 | + |
| 88 | + // Create motion path animation with GSAP |
| 89 | + const tl = gsap.timeline({ |
| 90 | + delay: bat.delay, |
| 91 | + repeat: -1, |
| 92 | + repeatDelay: 2, |
| 93 | + }); |
| 94 | + |
| 95 | + // Animate through path coordinates |
| 96 | + pathCoords.forEach((coord, i) => { |
| 97 | + tl.to( |
| 98 | + batElement, |
| 99 | + { |
| 100 | + x: coord.x, |
| 101 | + y: coord.y, |
| 102 | + duration: bat.duration / pathCoords.length, |
| 103 | + ease: 'sine.inOut', |
| 104 | + force3D: true, |
| 105 | + }, |
| 106 | + i === 0 ? 0 : '>' |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + // Wing flapping animation (simpler on mobile) |
| 111 | + const leftWing = batElement.querySelector('[data-wing="left"]'); |
| 112 | + const rightWing = batElement.querySelector('[data-wing="right"]'); |
| 113 | + |
| 114 | + if (leftWing && rightWing) { |
| 115 | + const flapSpeed = isMobile ? 0.15 : 0.12; |
| 116 | + |
| 117 | + gsap.to(leftWing, { |
| 118 | + rotateY: -75, |
| 119 | + duration: flapSpeed, |
| 120 | + repeat: -1, |
| 121 | + yoyo: true, |
| 122 | + ease: 'sine.inOut', |
| 123 | + force3D: true, |
| 124 | + }); |
| 125 | + |
| 126 | + gsap.to(rightWing, { |
| 127 | + rotateY: 75, |
| 128 | + duration: flapSpeed, |
| 129 | + repeat: -1, |
| 130 | + yoyo: true, |
| 131 | + ease: 'sine.inOut', |
| 132 | + force3D: true, |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + animations.push(tl); |
| 137 | + }); |
| 138 | + |
| 139 | + // Cleanup |
| 140 | + return () => { |
| 141 | + animations.forEach((anim) => anim.kill()); |
| 142 | + }; |
| 143 | + }, [bats, isMobile]); |
22 | 144 |
|
23 | 145 | return ( |
24 | 146 | <div |
25 | 147 | className={`${styles.batsContainer} ${isHomePage ? styles.homePage : ''}`} |
26 | 148 | > |
27 | | - {bats.map((bat) => ( |
| 149 | + {bats.map((bat, index) => ( |
28 | 150 | <div |
29 | 151 | key={bat.id} |
30 | | - className={`${styles.bat} ${styles[bat.path]}`} |
31 | | - style={{ |
32 | | - '--start-x': `${bat.startX}vw`, |
33 | | - '--start-y': `${bat.startY}vh`, |
34 | | - '--duration': `${bat.duration}s`, |
35 | | - '--delay': `${bat.delay}s`, |
36 | | - '--size': bat.size, |
| 152 | + ref={(el) => { |
| 153 | + batsRef.current[index] = el; |
37 | 154 | }} |
| 155 | + className={styles.bat} |
38 | 156 | > |
39 | 157 | <div className={styles.batBody}> |
40 | 158 | <div className={styles.batWing} data-wing="left" /> |
|
0 commit comments