import React, { FC, memo, useCallback, useEffect, useRef, useState, } from "react"; import { useWindowSize } from "~/hooks/dom/useWindowSize"; import styles from "./styles.module.scss"; interface LoginSceneProps {} interface Layer { src: string; velocity: number; width: number; height: number; } const layers: Layer[] = [ { src: "/images/clouds__bg.svg", velocity: -0.3, width: 3840, height: 1080, }, { src: "/images/clouds__cube.svg", velocity: -0.1, width: 3840, height: 1080, }, { src: "/images/clouds__cloud.svg", velocity: 0.2, width: 3840, height: 1080, }, { src: "/images/clouds__dudes.svg", velocity: 0.5, width: 3840, height: 1080, }, { src: "/images/clouds__trash.svg", velocity: 0.8, width: 3840, height: 1080, }, ]; const LoginScene: FC = memo(() => { const containerRef = useRef(null); const [loaded, setLoaded] = useState(false); const imageRefs = useRef>([]); const { isMobile } = useWindowSize(); const onMouseMove = useCallback( (event: MouseEvent): any => { if (!containerRef.current) { return; } const { x, width } = containerRef.current.getBoundingClientRect(); const middle = (width - x) / 2; const shift = event.pageX / middle / 2 - 0.5; layers.forEach((it, index) => { const target = imageRefs.current[index]; if (target) { target.style.transform = `translate(${shift * it.velocity * 200}px, 0)`; } }); }, [containerRef], ); useEffect(() => { document.addEventListener("mousemove", onMouseMove); return () => document.removeEventListener("mousemove", onMouseMove); }, []); if (isMobile) { return null; } return (
{layers.map((it, index) => ( imageRefs.current.push(it)} key={it.src} href={it.src} width={it.width} height={it.height} x={1920 / 2 - it.width / 2} y={0} opacity={loaded ? 1 : 0} onLoad={() => setLoaded(true)} className={styles.image} /> ))}
); }); export { LoginScene };