"use client"; import Image from "next/image"; import { useEffect, useRef, useState, useCallback } from "react"; const titles = [ "Sustainable Architecture.", "Built for Tomorrow.", "Eco-Responsible.", ]; export function PhilosophySection() { const sectionRef = useRef(null); const descriptionRef = useRef(null); const [titleOpacity, setTitleOpacity] = useState(0); const [descriptionProgress, setDescriptionProgress] = useState(0); const rafRef = useRef(null); const updateTransforms = useCallback(() => { if (!sectionRef.current) return; const rect = sectionRef.current.getBoundingClientRect(); const windowHeight = window.innerHeight; const sectionHeight = sectionRef.current.offsetHeight; // Calculate progress based on scroll position const scrollableRange = sectionHeight - windowHeight; const scrolled = -rect.top; const progress = Math.max(0, Math.min(1, scrolled / scrollableRange)); // Title rotates through 3 texts based on scroll progress setTitleOpacity(progress); // Description word animation if (descriptionRef.current) { const descRect = descriptionRef.current.getBoundingClientRect(); const descTop = descRect.top; const descHeight = descRect.height; // Start animation when element enters viewport const startTrigger = windowHeight * 0.8; const endTrigger = windowHeight * 0.2; if (descTop < startTrigger && descTop > endTrigger - descHeight) { const descProgress = Math.max(0, Math.min(1, (startTrigger - descTop) / (startTrigger - endTrigger))); setDescriptionProgress(descProgress); } } }, []); useEffect(() => { const handleScroll = () => { // Cancel any pending animation frame if (rafRef.current) { cancelAnimationFrame(rafRef.current); } // Use requestAnimationFrame for smooth updates rafRef.current = requestAnimationFrame(updateTransforms); }; window.addEventListener("scroll", handleScroll, { passive: true }); updateTransforms(); return () => { window.removeEventListener("scroll", handleScroll); if (rafRef.current) { cancelAnimationFrame(rafRef.current); } }; }, [updateTransforms]); return (
{/* Scroll-Animated Product Grid */}
{/* Title - centered with 3D rotation */}
{titles.map((title, index) => { // Last text "Built to last" stays visible at the end const isLastText = index === titles.length - 1; // Calculate which text should be visible based on scroll progress const segmentSize = 1 / titles.length; const startProgress = index * segmentSize; const endProgress = (index + 1) * segmentSize; let rotateX = 0; let opacity = 0; if (titleOpacity >= startProgress && titleOpacity < endProgress) { // Active text - rotating in const localProgress = (titleOpacity - startProgress) / segmentSize; rotateX = (1 - localProgress) * 90; opacity = localProgress; } else if (titleOpacity >= endProgress) { // Text that has passed - last text stays visible if (isLastText) { rotateX = 0; opacity = 1; } else { rotateX = -90; opacity = 0; } } else { // Text that hasn't appeared yet rotateX = 90; opacity = 0; } return (

{title}

); })}
{/* Description */}

{("A design home that combines contemporary aesthetics and energy performance. Built with eco-friendly materials, it minimizes carbon footprint while offering optimal comfort.").split(" ").map((word, index, array) => { const wordProgress = Math.max(0, Math.min(1, (descriptionProgress * array.length) - index)); const opacity = wordProgress; const blur = (1 - wordProgress) * 40; return ( {word}{index < array.length - 1 ? " " : ""} ); })}

); }