|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import LayoutChildren from "../../types/layoutChildren"; |
| 4 | +import {useEffect, useState} from "react"; |
| 5 | + |
| 6 | +const images: string[] = [ |
| 7 | + "https://unitystationfile.b-cdn.net/Website-Statics/heroImages/bar-engine.png", |
| 8 | + "https://unitystationfile.b-cdn.net/Website-Statics/heroImages/clowns.png", |
| 9 | + "https://unitystationfile.b-cdn.net/Website-Statics/heroImages/conveyor.jpg", |
| 10 | + "https://unitystationfile.b-cdn.net/Website-Statics/heroImages/df.jpg", |
| 11 | + "https://unitystationfile.b-cdn.net/Website-Statics/heroImages/go-outsid.png", |
| 12 | + "https://unitystationfile.b-cdn.net/Website-Statics/heroImages/honk.jpg", |
| 13 | + "https://unitystationfile.b-cdn.net/Website-Statics/heroImages/hugger.png", |
| 14 | + "https://unitystationfile.b-cdn.net/Website-Statics/heroImages/lemons.png", |
| 15 | + "https://unitystationfile.b-cdn.net/Website-Statics/heroImages/shuttlecrash.png", |
| 16 | + "https://unitystationfile.b-cdn.net/Website-Statics/heroImages/chairs.jpg", |
| 17 | +]; |
| 18 | + |
| 19 | +let currentIndex = 0; |
| 20 | + |
| 21 | +const shuffleImages = () => { |
| 22 | + for (let i = images.length - 1; i > 0; i--) { |
| 23 | + const j = Math.floor(Math.random() * (i + 1)); |
| 24 | + [images[i], images[j]] = [images[j], images[i]]; |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +const getNextImage = () => { |
| 29 | + currentIndex = (currentIndex + 1) % images.length; |
| 30 | + return images[currentIndex]; |
| 31 | +}; |
| 32 | + |
| 33 | +const HeroRandomImageClient = (props: LayoutChildren) => { |
| 34 | + const {children} = props; |
| 35 | + shuffleImages(); |
| 36 | + const [image1, setImage1] = useState<string>(getNextImage()); |
| 37 | + const [image2, setImage2] = useState<string>(getNextImage()); |
| 38 | + const [showImage1, setShowImage1] = useState<boolean>(true); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + const intervalId = setInterval(() => { |
| 42 | + setShowImage1(!showImage1); |
| 43 | + if (showImage1) { |
| 44 | + setImage2(getNextImage()); |
| 45 | + } else { |
| 46 | + setImage1(getNextImage()); |
| 47 | + } |
| 48 | + }, 10000); |
| 49 | + |
| 50 | + return () => clearInterval(intervalId); |
| 51 | + }, [showImage1]); |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className="relative w-full p-20 overflow-hidden"> |
| 55 | + <div className="absolute inset-0 z-0"> |
| 56 | + <div className={`absolute inset-0 bg-cover bg-center bg-no-repeat filter blur-xs transition-opacity duration-1000 ease-in-out ${showImage1 ? 'opacity-100' : 'opacity-0'}`} style={{backgroundImage: `url(${image1})`, backgroundPosition: `center`}}></div> |
| 57 | + <div className={`absolute inset-0 bg-cover bg-center bg-no-repeat filter blur-xs transition-opacity duration-1000 ease-in-out ${showImage1 ? 'opacity-0' : 'opacity-100'}`} style={{backgroundImage: `url(${image2})`, backgroundPosition: `center`}}></div> |
| 58 | + <div className="absolute inset-0 bg-black opacity-50 "></div> |
| 59 | + </div> |
| 60 | + <div className="relative z-10"> |
| 61 | + {children} |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +export default HeroRandomImageClient; |
0 commit comments