Skip to content

Commit

Permalink
feat(globe): fade in on mount
Browse files Browse the repository at this point in the history
  • Loading branch information
pwambach committed Jun 14, 2022
1 parent 99081f1 commit 32377b4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
18 changes: 18 additions & 0 deletions src/scripts/components/main/globe/globe.styl
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@
flex-grow: 1
flex-basis: 50%
height: 100%
opacity: 0

.fadeIn
animation: fadeIn 2s
animation-timing-function: ease-out
animation-fill-mode: forwards

.fastFadeIn
animation: fadeIn 0.5s
animation-timing-function: ease-out
animation-fill-mode: forwards

@keyframes fadeIn
0%
opacity: 0

100%
opacity: 1
32 changes: 30 additions & 2 deletions src/scripts/components/main/globe/globe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Viewer,
ImageryLayer
} from 'cesium';
import cx from 'classnames';

import {
getGlobeView,
Expand Down Expand Up @@ -112,6 +113,7 @@ const Globe: FunctionComponent<Props> = ({
onMouseDown
}) => {
const [viewer, setViewer] = useState<Viewer | null>(null);
const [firstTilesLoaded, setFirstTilesLoaded] = useState(false);
const ref = useRef<HTMLDivElement>(null);
const lastNowRef = useRef<number | null>(null);

Expand Down Expand Up @@ -322,11 +324,37 @@ const Globe: FunctionComponent<Props> = ({
}
}, [spinning, viewer, projectionState.morphTime, spin]);

useMarkers(viewer, markers);
// check that all visible tiles have been loaded
useEffect(() => {
const handler = (tiles: number) => {
if (tiles < 1) {
setFirstTilesLoaded(true);
// eslint-disable-next-line no-unused-expressions
viewer?.scene.globe.tileLoadProgressEvent.removeEventListener(handler);
}
};

// eslint-disable-next-line no-unused-expressions
viewer?.scene.globe.tileLoadProgressEvent.addEventListener(handler);
}, [viewer]);

const markersReady = useMarkers(viewer, markers, firstTilesLoaded);

let fadeClass = null;

// slow fade in when we have markers and everything is ready to render
if (firstTilesLoaded && markersReady && markers.length) {
fadeClass = styles.fadeIn;
}

// fast fade in when no markers and everything is ready to render
if (firstTilesLoaded && markersReady && markers.length === 0) {
fadeClass = styles.fastFadeIn;
}

return (
<div
className={styles.globe}
className={cx(styles.globe, fadeClass)}
onMouseEnter={() => onMouseEnter()}
onTouchStart={() => onTouchStart()}
ref={ref}
Expand Down
16 changes: 11 additions & 5 deletions src/scripts/hooks/use-markers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useEffect} from 'react';
import {useEffect, useState} from 'react';
import {useDispatch} from 'react-redux';
import {useHistory} from 'react-router-dom';
import {
Expand All @@ -12,13 +12,18 @@ import {createMarker} from '../libs/create-marker';

import {Marker} from '../types/marker-type';

export const useMarkers = (viewer: Viewer | null, markers: Marker[]) => {
export const useMarkers = (
viewer: Viewer | null,
markers: Marker[],
firstTilesLoaded: boolean
) => {
const history = useHistory();
const dispatch = useDispatch();
const [ready, setReady] = useState(false);

// create marker for each story
useEffect(() => {
if (!viewer) {
if (!viewer || !firstTilesLoaded) {
return;
}

Expand All @@ -38,11 +43,12 @@ export const useMarkers = (viewer: Viewer | null, markers: Marker[]) => {
Promise.all(markers.map(marker => createMarker(marker))).then(entities => {
viewer.entities.removeAll();
entities.forEach(entity => viewer.entities.add(entity));
setReady(true);
});

// eslint-disable-next-line consistent-return
return () => handler.destroy();
}, [dispatch, history, markers, viewer]);
}, [dispatch, history, markers, viewer, firstTilesLoaded]);

return;
return ready;
};

0 comments on commit 32377b4

Please sign in to comment.