From 2a0891eff2b3b61dc0eb0b8d0e06e1764ef27e33 Mon Sep 17 00:00:00 2001 From: Alvaro Date: Tue, 15 Oct 2024 11:45:01 +0200 Subject: [PATCH] FIx lines and globe reset - WIP --- src/components/globe-map/camera.tsx | 34 +++++++++---- src/components/globe-map/index.tsx | 18 +++++-- src/components/section-2/index.tsx | 79 +++++++++++++++++++++++------ 3 files changed, 102 insertions(+), 29 deletions(-) diff --git a/src/components/globe-map/camera.tsx b/src/components/globe-map/camera.tsx index 6416a29..215cf39 100644 --- a/src/components/globe-map/camera.tsx +++ b/src/components/globe-map/camera.tsx @@ -4,8 +4,9 @@ import { useRef } from "react"; import { convertLatLonToGlobalPosition } from "@/lib/globe-utils"; import type { MarkerType } from "./marker"; -export const Camera = ({ marker }: { +export const Camera = ({ marker, disabled = false }: { marker: MarkerType | undefined + disabled: boolean }) => { const cameraControlsRef = useRef(null!); const [resetControls, setResetControls] = useState(true); @@ -18,19 +19,34 @@ export const Camera = ({ marker }: { }, [marker]); useEffect(() => { - if (marker !== undefined) { - const [x, y, z] = convertLatLonToGlobalPosition(marker.lat, marker.lng, 2); - cameraControlsRef.current.disconnect(); - cameraControlsRef.current.setPosition(x, y, z, true); + if (cameraControlsRef.current) { + if (marker !== undefined) { + const [x, y, z] = convertLatLonToGlobalPosition(marker.lat, marker.lng, 2); + cameraControlsRef.current.disconnect(); + cameraControlsRef.current.setPosition(x, y, z, true); + } + + if (resetControls) { + cameraControlsRef.current.connect(canvasElement); + cameraControlsRef.current.setPosition(0, 1, 4.9, true); + cameraControlsRef.current.setTarget(0, 0, 0, true); + setResetControls(false); + } } + }, [marker, resetControls, canvasElement]); - if (resetControls) { + useEffect(() => { + if (disabled && cameraControlsRef.current) { cameraControlsRef.current.connect(canvasElement); - cameraControlsRef.current.setPosition(0, 1, 4.9, true); + cameraControlsRef.current.setPosition(0, 1, 2, true); cameraControlsRef.current.setTarget(0, 0, 0, true); - setResetControls(false); + setResetControls(true); } - }, [marker, resetControls, canvasElement]); + }, [disabled, cameraControlsRef.current]); + + if (disabled) { + return null; + } return (
- {hasMarkers && } + {hasMarkers && markers.map((marker, index) => ( - + Click to explore the phenomenon ))} diff --git a/src/components/section-2/index.tsx b/src/components/section-2/index.tsx index c2e6359..abdb8ab 100644 --- a/src/components/section-2/index.tsx +++ b/src/components/section-2/index.tsx @@ -1,5 +1,5 @@ 'use client'; -import { useRef, useEffect, useState, useCallback } from 'react'; +import { useRef, useEffect, useState, useCallback, useMemo } from 'react'; import Lines from "@/components/lines"; import GlobeMap from "@/components/globe-map"; import { Resizable } from 're-resizable'; @@ -24,6 +24,20 @@ const ResizeButton = () => ( ); +const useScreenWidthWithResize = () => { + const [width, setWidth] = useState(window.innerWidth); + const handleResize = useCallback(() => { + setWidth(window.innerWidth); + }, []); + + useEffect(() => { + window.addEventListener('resize', handleResize); + return () => window.removeEventListener('resize', handleResize); + }, []); + + return width; +} + export default function Section2() { const scrollSectionRef = useRef(null); const { scrollYProgress } = useScroll({ @@ -32,6 +46,7 @@ export default function Section2() { const [globePhase, setGlobePhase] = useState(0); const [resizableWidth, setResizableWidth] = useState(400); + const screenWidth = useScreenWidthWithResize(); const scrollToSection = (section: string) => { const element = document.getElementById(section); if (element) { @@ -51,12 +66,12 @@ export default function Section2() { useMotionValueEvent(scrollYProgress, "change", (latest) => { if (latest >= 0 && latest < 0.1 && globePhase !== 0) { setGlobePhase(0); - setResizableWidth(400); + setResizableWidth(screenWidth / 2); } if (latest >= 0.1 && latest < 0.66 && globePhase !== 1) { setGlobePhase(1); - setResizableWidth(800); + setResizableWidth(screenWidth); } if (latest >= 0.66 && globePhase < 2) { @@ -64,14 +79,29 @@ export default function Section2() { } }); + const descriptionRef = useRef(null); + + const descriptionLeft = useMemo(() => { + if (descriptionRef.current) { + return descriptionRef.current.getBoundingClientRect().left; + } + return 0; + }, [descriptionRef.current, screenWidth]); + + const titleX = useTransform(scrollYProgress, [0.2, 0.33], [0, -200]); - const titleY = useTransform(scrollYProgress, [0.2, 0.33], [0, -200]); - const descriptionY = useTransform(scrollYProgress, [0.2, 0.33, 0.6, 0.7], [500, 0, 0, -1000]); + const titleY = useTransform(scrollYProgress, [0.2, 0.33], [0, -204]); + const descriptionY = useTransform(scrollYProgress, [0.2, 0.33, 0.6, 0.7], [500, -4, -4, -1000]); + + const opacityLine = useTransform(scrollYProgress, [0.2, 0.33, 0.6, 0.7], [0, 1, 1, 0]); + const lineY = useTransform(scrollYProgress, [0.2, 0.33, 0.6, 0.7], [500, -40, -40, -1000]); + const lineX = useTransform(scrollYProgress, [0.2, 0.33, 0.6, 0.7], [resizableWidth, descriptionLeft, descriptionLeft, descriptionLeft]); + return (
- +
@@ -79,7 +109,8 @@ export default function Section2() {
{/* High globe */}
@@ -94,7 +125,8 @@ export default function Section2() { enable={{ right: globePhase === 0, }} - maxWidth="800px" + + maxWidth={screenWidth} minWidth="1" handleComponent={{ right: , @@ -103,31 +135,46 @@ export default function Section2() {
{/* Low globe */} 1} rotate={globePhase === 1} + hasMarkers={globePhase > 1} + rotate={globePhase === 1} />
+ {/* Vertical description line */} + 1 ? 0 : 1, }} > + {/* Horizontal description line */} +
-
UNLOCKING CLIMATE POTENTIAL
-
- High-quality information from global to local scale +
+
UNLOCKING CLIMATE POTENTIAL
+
+ High-quality information from global to local scale +

At the resolutions that global climate models use today, a number of small-scale processes that are important for the simulation of extreme events and the evolution of the climate system, are not directly represented. Increasing the model resolution (i.e. reducing the size of grid cells used in climate models both horizontally and vertically) allows researchers to represent these processes more directly. @@ -146,7 +193,7 @@ export default function Section2() { {/* Empty divs for the snap scroll */}

-
-
+
+ ); };