fix: streamline tool page to single upload and single compare, make vertical-horizontal transitions fluid and seamless
This commit is contained in:
@@ -41,12 +41,46 @@ export function CinematicHorizontalScrollSection({
|
||||
}: CinematicHorizontalProps) {
|
||||
const containerRef = useRef<HTMLElement>(null);
|
||||
const trackRef = useRef<HTMLDivElement>(null);
|
||||
const [translateX, setTranslateX] = useState(0);
|
||||
|
||||
const targetXRef = useRef(0);
|
||||
const currentXRef = useRef(0);
|
||||
const animFrameRef = useRef<number>(0);
|
||||
const isLoopRunningRef = useRef(false);
|
||||
|
||||
const [activeIndex, setActiveIndex] = useState(0);
|
||||
const [canScrollLeft, setCanScrollLeft] = useState(false);
|
||||
const [canScrollRight, setCanScrollRight] = useState(true);
|
||||
|
||||
// Synchronize horizontal track position with vertical scroll progress through sticky section
|
||||
// High-performance, responsive LERP animation loop to smooth wheel notches into liquid momentum
|
||||
const startLerpLoop = useCallback(() => {
|
||||
if (isLoopRunningRef.current) return;
|
||||
isLoopRunningRef.current = true;
|
||||
|
||||
const loop = () => {
|
||||
const track = trackRef.current;
|
||||
if (!track) {
|
||||
isLoopRunningRef.current = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const diff = targetXRef.current - currentXRef.current;
|
||||
|
||||
if (Math.abs(diff) > 0.4) {
|
||||
// Responsive 0.28 damping factor: silky smoothing with zero perceptible lag
|
||||
currentXRef.current += diff * 0.28;
|
||||
track.style.transform = `translate3d(-${currentXRef.current.toFixed(2)}px, 0, 0)`;
|
||||
animFrameRef.current = requestAnimationFrame(loop);
|
||||
} else {
|
||||
currentXRef.current = targetXRef.current;
|
||||
track.style.transform = `translate3d(-${currentXRef.current.toFixed(2)}px, 0, 0)`;
|
||||
isLoopRunningRef.current = false;
|
||||
}
|
||||
};
|
||||
|
||||
animFrameRef.current = requestAnimationFrame(loop);
|
||||
}, []);
|
||||
|
||||
// Compute fluid horizontal translation directly from vertical scroll progress
|
||||
const updateTranslation = useCallback(() => {
|
||||
const container = containerRef.current;
|
||||
const track = trackRef.current;
|
||||
@@ -56,7 +90,9 @@ export function CinematicHorizontalScrollSection({
|
||||
const totalScrollable = container.offsetHeight - window.innerHeight;
|
||||
if (totalScrollable <= 0) return;
|
||||
|
||||
// Progress: 0.0 when section top hits viewport top, 1.0 when bottom unpins
|
||||
// Direct proportional progress through the sticky section [0.0, 1.0]
|
||||
// 0.0: Card 0 centered, exactly as section pins
|
||||
// 1.0: Final card centered, seamlessly unpins into vertical scroll
|
||||
const progress = Math.max(0, Math.min(1, -rect.top / totalScrollable));
|
||||
|
||||
const cards = Array.from(track.querySelectorAll('article'));
|
||||
@@ -66,9 +102,10 @@ export function CinematicHorizontalScrollSection({
|
||||
const lastCenter = cards[cards.length - 1].offsetLeft + cards[cards.length - 1].offsetWidth / 2;
|
||||
const maxDistance = lastCenter - firstCenter;
|
||||
|
||||
const targetX = progress * maxDistance;
|
||||
setTranslateX(targetX);
|
||||
targetXRef.current = progress * maxDistance;
|
||||
startLerpLoop();
|
||||
|
||||
// Sync active card index and button states
|
||||
const active = Math.min(
|
||||
cards.length - 1,
|
||||
Math.max(0, Math.round(progress * (cards.length - 1)))
|
||||
@@ -77,18 +114,11 @@ export function CinematicHorizontalScrollSection({
|
||||
|
||||
setCanScrollLeft(progress > 0.02);
|
||||
setCanScrollRight(progress < 0.98);
|
||||
}, []);
|
||||
}, [startLerpLoop]);
|
||||
|
||||
useEffect(() => {
|
||||
let ticking = false;
|
||||
const handleScroll = () => {
|
||||
if (!ticking) {
|
||||
requestAnimationFrame(() => {
|
||||
updateTranslation();
|
||||
ticking = false;
|
||||
});
|
||||
ticking = true;
|
||||
}
|
||||
updateTranslation();
|
||||
};
|
||||
|
||||
updateTranslation();
|
||||
@@ -98,10 +128,11 @@ export function CinematicHorizontalScrollSection({
|
||||
return () => {
|
||||
window.removeEventListener('scroll', handleScroll);
|
||||
window.removeEventListener('resize', handleScroll);
|
||||
if (animFrameRef.current) cancelAnimationFrame(animFrameRef.current);
|
||||
};
|
||||
}, [updateTranslation, items.length]);
|
||||
|
||||
// Slide between cards using side arrows
|
||||
// Slide smoothly between cards using side arrows
|
||||
const scrollDirection = (direction: 'left' | 'right') => {
|
||||
const container = containerRef.current;
|
||||
if (!container) return;
|
||||
@@ -113,10 +144,11 @@ export function CinematicHorizontalScrollSection({
|
||||
|
||||
const rect = container.getBoundingClientRect();
|
||||
const containerTopInDoc = window.scrollY + rect.top;
|
||||
const currentProgress = Math.max(0, Math.min(1, -rect.top / totalScrollable));
|
||||
const progress = Math.max(0, Math.min(1, -rect.top / totalScrollable));
|
||||
|
||||
const currentCardIndex = Math.min(
|
||||
cardsCount - 1,
|
||||
Math.max(0, Math.round(currentProgress * (cardsCount - 1)))
|
||||
Math.max(0, Math.round(progress * (cardsCount - 1)))
|
||||
);
|
||||
|
||||
let targetIndex = currentCardIndex;
|
||||
@@ -137,7 +169,7 @@ export function CinematicHorizontalScrollSection({
|
||||
|
||||
if (!items.length) return null;
|
||||
|
||||
// Vertical scroll distance: 100vh base + 85vh per additional card
|
||||
// Fluid vertical scroll distance: 100vh base + 85vh per additional card
|
||||
const containerHeightVh = 100 + (items.length - 1) * 85;
|
||||
|
||||
return (
|
||||
@@ -176,7 +208,7 @@ export function CinematicHorizontalScrollSection({
|
||||
}`}
|
||||
/>
|
||||
|
||||
{/* Left Side Arrow Button (Always visible on left side) */}
|
||||
{/* Left Side Arrow Button */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => scrollDirection('left')}
|
||||
@@ -194,7 +226,7 @@ export function CinematicHorizontalScrollSection({
|
||||
style={{
|
||||
paddingLeft: 'calc((100vw - min(780px, 88vw)) / 2)',
|
||||
paddingRight: 'calc((100vw - min(780px, 88vw)) / 2)',
|
||||
transform: `translate3d(-${translateX}px, 0, 0)`,
|
||||
transform: 'translate3d(0px, 0px, 0px)',
|
||||
}}
|
||||
>
|
||||
{items.map((item) => (
|
||||
@@ -202,7 +234,7 @@ export function CinematicHorizontalScrollSection({
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Right Side Arrow Button (Always visible on right side) */}
|
||||
{/* Right Side Arrow Button */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => scrollDirection('right')}
|
||||
|
||||
Reference in New Issue
Block a user