forked from visgl/hubble.gl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.ts
More file actions
95 lines (87 loc) · 2.39 KB
/
hooks.ts
File metadata and controls
95 lines (87 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// hubble.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import {useState, useCallback, useMemo, RefObject} from 'react';
import {DeckAdapter, DeckAnimation, DeckAnimationConstructor} from '@hubble.gl/core';
import type {Layer, MapViewState, Deck} from '@deck.gl/core';
import type {MapRef} from 'react-map-gl';
export function useNextFrame() {
const [, updateState] = useState({});
return useCallback(() => updateState({}), []);
}
export function useDeckAdapter(
deckAnimation: DeckAnimation,
initialViewState: MapViewState = undefined
) {
const [layers, setLayers] = useState<Layer[]>([]);
const [cameraFrame, setCameraFrame] = useState<MapViewState>(initialViewState);
const adapter = useMemo(() => {
const a = new DeckAdapter({});
deckAnimation.setOnLayersUpdate(setLayers);
if (initialViewState) {
deckAnimation.setOnCameraUpdate(setCameraFrame);
}
a.animationManager.attachAnimation(deckAnimation);
deckAnimation.draw();
return a;
}, []);
return {adapter, layers, cameraFrame, setCameraFrame};
}
export function useDeckAnimation(params: DeckAnimationConstructor) {
return useMemo(() => new DeckAnimation(params), []);
}
export function useHubbleGl<ReactMapRef extends MapRef>({
deckRef,
mapRef = undefined,
deckAnimation,
initialViewState = undefined
}: {
deckRef: RefObject<Deck>;
mapRef?: RefObject<ReactMapRef>;
deckAnimation: DeckAnimation;
initialViewState?: MapViewState;
}) {
const deck = useMemo(() => deckRef.current, [deckRef.current]);
const nextFrame = useNextFrame();
const {adapter, layers, cameraFrame, setCameraFrame} = useDeckAdapter(
deckAnimation,
initialViewState
);
const onMapLoad = useCallback(() => {
if (mapRef) {
const map = mapRef.current.getMap();
map.on('render', () => adapter.onAfterRender(nextFrame, map.areTilesLoaded()));
}
}, [adapter, nextFrame]);
if (!mapRef) {
return {
adapter,
cameraFrame,
setCameraFrame,
mapProps: {},
deckProps: adapter.getProps({
deck,
onNextFrame: nextFrame,
extraProps: {
layers
}
})
};
}
return {
adapter,
cameraFrame,
setCameraFrame,
onMapLoad,
mapProps: {
onLoad: onMapLoad,
preventStyleDiffing: true
},
deckProps: adapter.getProps({
deck,
extraProps: {
layers
}
})
};
}