react-three-map 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +2 -2
- package/dist/cjs/main.js +327 -1
- package/dist/es/main.mjs +4 -4
- package/dist/es/main10.mjs +14 -6
- package/dist/es/main10.mjs.map +1 -1
- package/dist/es/main11.mjs +48 -34
- package/dist/es/main11.mjs.map +1 -1
- package/dist/es/main12.mjs +8 -5
- package/dist/es/main12.mjs.map +1 -1
- package/dist/es/main13.mjs +54 -37
- package/dist/es/main13.mjs.map +1 -1
- package/dist/es/main2.mjs +18 -17
- package/dist/es/main2.mjs.map +1 -1
- package/dist/es/main3.mjs +46 -32
- package/dist/es/main3.mjs.map +1 -1
- package/dist/es/main4.mjs +20 -18
- package/dist/es/main4.mjs.map +1 -1
- package/dist/es/main5.mjs +3 -3
- package/dist/es/main5.mjs.map +1 -1
- package/dist/es/main6.mjs +33 -5
- package/dist/es/main6.mjs.map +1 -1
- package/dist/es/main7.mjs +11 -10
- package/dist/es/main7.mjs.map +1 -1
- package/dist/es/main8.mjs +19 -13
- package/dist/es/main8.mjs.map +1 -1
- package/dist/es/main9.mjs +17 -9
- package/dist/es/main9.mjs.map +1 -1
- package/dist/maplibre/cjs/main.js +327 -1
- package/dist/maplibre/es/main.mjs +4 -4
- package/dist/maplibre/es/main10.mjs +19 -6
- package/dist/maplibre/es/main10.mjs.map +1 -1
- package/dist/maplibre/es/main11.mjs +48 -34
- package/dist/maplibre/es/main11.mjs.map +1 -1
- package/dist/maplibre/es/main12.mjs +8 -5
- package/dist/maplibre/es/main12.mjs.map +1 -1
- package/dist/maplibre/es/main13.mjs +54 -37
- package/dist/maplibre/es/main13.mjs.map +1 -1
- package/dist/maplibre/es/main2.mjs +18 -17
- package/dist/maplibre/es/main2.mjs.map +1 -1
- package/dist/maplibre/es/main3.mjs +46 -32
- package/dist/maplibre/es/main3.mjs.map +1 -1
- package/dist/maplibre/es/main4.mjs +20 -18
- package/dist/maplibre/es/main4.mjs.map +1 -1
- package/dist/maplibre/es/main5.mjs +3 -3
- package/dist/maplibre/es/main5.mjs.map +1 -1
- package/dist/maplibre/es/main6.mjs +33 -5
- package/dist/maplibre/es/main6.mjs.map +1 -1
- package/dist/maplibre/es/main7.mjs +11 -10
- package/dist/maplibre/es/main7.mjs.map +1 -1
- package/dist/maplibre/es/main8.mjs +15 -15
- package/dist/maplibre/es/main8.mjs.map +1 -1
- package/dist/maplibre/es/main9.mjs +20 -11
- package/dist/maplibre/es/main9.mjs.map +1 -1
- package/package.json +1 -1
- package/stories/package.json +1 -1
- package/stories/yarn.lock +4 -4
|
@@ -1 +1,327 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const jsxRuntime = require("react/jsx-runtime");
|
|
4
|
+
const fiber = require("@react-three/fiber");
|
|
5
|
+
const maplibreGl = require("maplibre-gl");
|
|
6
|
+
const react = require("react");
|
|
7
|
+
const maplibre = require("react-map-gl/maplibre");
|
|
8
|
+
const THREE = require("three");
|
|
9
|
+
function _interopNamespaceDefault(e) {
|
|
10
|
+
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
11
|
+
if (e) {
|
|
12
|
+
for (const k in e) {
|
|
13
|
+
if (k !== "default") {
|
|
14
|
+
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
15
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: () => e[k]
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
n.default = e;
|
|
23
|
+
return Object.freeze(n);
|
|
24
|
+
}
|
|
25
|
+
const THREE__namespace = /* @__PURE__ */ _interopNamespaceDefault(THREE);
|
|
26
|
+
const quat = new THREE.Quaternion();
|
|
27
|
+
const euler = new THREE.Euler();
|
|
28
|
+
const pos = new THREE.Vector3();
|
|
29
|
+
const scale = new THREE.Vector3();
|
|
30
|
+
const m4 = new THREE.Matrix4();
|
|
31
|
+
function coordsToMatrix({ longitude, latitude, altitude, fromLngLat: fromLngLat2 }) {
|
|
32
|
+
const center = fromLngLat2([longitude, latitude], altitude);
|
|
33
|
+
const scaleUnit = center.meterInMercatorCoordinateUnits();
|
|
34
|
+
pos.set(center.x, center.y, center.z || 0);
|
|
35
|
+
scale.set(scaleUnit, -scaleUnit, scaleUnit);
|
|
36
|
+
quat.setFromEuler(euler.set(-Math.PI * 0.5, 0, 0));
|
|
37
|
+
return m4.compose(pos, quat, scale).toArray();
|
|
38
|
+
}
|
|
39
|
+
function useCoords({ latitude, longitude, altitude, fromLngLat: fromLngLat2 }) {
|
|
40
|
+
const m42 = react.useMemo(() => coordsToMatrix({
|
|
41
|
+
latitude,
|
|
42
|
+
longitude,
|
|
43
|
+
altitude,
|
|
44
|
+
fromLngLat: fromLngLat2
|
|
45
|
+
}), [latitude, longitude, altitude, fromLngLat2]);
|
|
46
|
+
return m42;
|
|
47
|
+
}
|
|
48
|
+
const DOM_EVENTS = {
|
|
49
|
+
onClick: ["click", false],
|
|
50
|
+
onContextMenu: ["contextmenu", false],
|
|
51
|
+
onDoubleClick: ["dblclick", false],
|
|
52
|
+
onWheel: ["wheel", true],
|
|
53
|
+
onPointerDown: ["pointerdown", true],
|
|
54
|
+
onPointerUp: ["pointerup", true],
|
|
55
|
+
onPointerLeave: ["pointerleave", true],
|
|
56
|
+
onPointerMove: ["pointermove", true],
|
|
57
|
+
onPointerCancel: ["pointercancel", true],
|
|
58
|
+
onLostPointerCapture: ["lostpointercapture", true]
|
|
59
|
+
};
|
|
60
|
+
const projByViewInv$1 = new THREE.Matrix4();
|
|
61
|
+
function createEvents() {
|
|
62
|
+
return (store) => {
|
|
63
|
+
const { handlePointer } = fiber.createEvents(store);
|
|
64
|
+
return {
|
|
65
|
+
priority: 1,
|
|
66
|
+
enabled: true,
|
|
67
|
+
compute(event, state) {
|
|
68
|
+
state.size.width = state.gl.domElement.width / window.devicePixelRatio;
|
|
69
|
+
state.size.height = state.gl.domElement.height / window.devicePixelRatio;
|
|
70
|
+
state.pointer.x = event.offsetX / state.size.width * 2 - 1;
|
|
71
|
+
state.pointer.y = 1 - event.offsetY / state.size.height * 2;
|
|
72
|
+
if (state.camera.userData.projByViewInv)
|
|
73
|
+
projByViewInv$1.fromArray(state.camera.userData.projByViewInv);
|
|
74
|
+
state.raycaster.camera = state.camera;
|
|
75
|
+
state.raycaster.ray.origin.setScalar(0).applyMatrix4(projByViewInv$1);
|
|
76
|
+
state.raycaster.ray.direction.set(state.pointer.x, state.pointer.y, 1).applyMatrix4(projByViewInv$1).sub(state.raycaster.ray.origin).normalize();
|
|
77
|
+
},
|
|
78
|
+
connected: void 0,
|
|
79
|
+
handlers: Object.keys(DOM_EVENTS).reduce(
|
|
80
|
+
(acc, key) => ({ ...acc, [key]: handlePointer(key) }),
|
|
81
|
+
{}
|
|
82
|
+
),
|
|
83
|
+
update: () => {
|
|
84
|
+
var _a;
|
|
85
|
+
const { events, internal } = store.getState();
|
|
86
|
+
if (((_a = internal.lastEvent) == null ? void 0 : _a.current) && events.handlers) {
|
|
87
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
connect: (target) => {
|
|
91
|
+
var _a;
|
|
92
|
+
const { set, events } = store.getState();
|
|
93
|
+
(_a = events.disconnect) == null ? void 0 : _a.call(events);
|
|
94
|
+
set((state) => ({ events: { ...state.events, connected: target.parentNode } }));
|
|
95
|
+
Object.entries(events.handlers ?? []).forEach(([name, event]) => {
|
|
96
|
+
const [eventName, passive] = DOM_EVENTS[name];
|
|
97
|
+
target.addEventListener(eventName, event, { passive });
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
disconnect: () => {
|
|
101
|
+
const { set, events } = store.getState();
|
|
102
|
+
if (events.connected) {
|
|
103
|
+
Object.entries(events.handlers ?? []).forEach(([name, event]) => {
|
|
104
|
+
if (events && events.connected instanceof HTMLElement) {
|
|
105
|
+
const [eventName] = DOM_EVENTS[name];
|
|
106
|
+
events.connected.removeEventListener(eventName, event);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
set((state) => ({ events: { ...state.events, connected: void 0 } }));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const useFunction = (callback) => {
|
|
116
|
+
const callbackRef = react.useRef(callback);
|
|
117
|
+
callbackRef.current = callback;
|
|
118
|
+
return react.useCallback((...args) => {
|
|
119
|
+
return callbackRef.current(...args);
|
|
120
|
+
}, []);
|
|
121
|
+
};
|
|
122
|
+
function useOnAdd(fromLngLat2, { frameloop, ...renderProps }) {
|
|
123
|
+
const [mounted, setMounted] = react.useState(false);
|
|
124
|
+
const r3mRef = react.useRef({ fromLngLat: fromLngLat2 });
|
|
125
|
+
const onAdd = useFunction((map, gl) => {
|
|
126
|
+
const canvas = map.getCanvas();
|
|
127
|
+
const root = fiber.createRoot(canvas);
|
|
128
|
+
root.configure({
|
|
129
|
+
dpr: window.devicePixelRatio,
|
|
130
|
+
events: createEvents(),
|
|
131
|
+
...renderProps,
|
|
132
|
+
frameloop: "never",
|
|
133
|
+
gl: {
|
|
134
|
+
context: gl,
|
|
135
|
+
depth: true,
|
|
136
|
+
autoClear: false,
|
|
137
|
+
antialias: true,
|
|
138
|
+
...renderProps == null ? void 0 : renderProps.gl
|
|
139
|
+
},
|
|
140
|
+
onCreated: (state) => {
|
|
141
|
+
state.gl.forceContextLoss = () => {
|
|
142
|
+
};
|
|
143
|
+
},
|
|
144
|
+
camera: {
|
|
145
|
+
matrixAutoUpdate: false,
|
|
146
|
+
near: 0
|
|
147
|
+
},
|
|
148
|
+
size: {
|
|
149
|
+
width: canvas.clientWidth,
|
|
150
|
+
height: canvas.clientHeight,
|
|
151
|
+
top: 0,
|
|
152
|
+
left: 0,
|
|
153
|
+
...renderProps == null ? void 0 : renderProps.size
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
const store = fiber._roots.get(canvas).store;
|
|
157
|
+
r3mRef.current.map = map;
|
|
158
|
+
r3mRef.current.root = root;
|
|
159
|
+
r3mRef.current.state = store.getState();
|
|
160
|
+
store.setState({ r3m: r3mRef.current });
|
|
161
|
+
if (frameloop === "demand") {
|
|
162
|
+
store.setState({
|
|
163
|
+
frameloop,
|
|
164
|
+
invalidate: () => {
|
|
165
|
+
map.triggerRepaint();
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
setTimeout(() => setMounted(true));
|
|
170
|
+
});
|
|
171
|
+
const onRemove = useFunction(() => {
|
|
172
|
+
if (!r3mRef.current.root)
|
|
173
|
+
return;
|
|
174
|
+
r3mRef.current.root.unmount();
|
|
175
|
+
r3mRef.current.root = void 0;
|
|
176
|
+
});
|
|
177
|
+
react.useEffect(() => () => onRemove(), [onRemove]);
|
|
178
|
+
return { onAdd, onRemove, mounted, r3mRef };
|
|
179
|
+
}
|
|
180
|
+
const useCreateRoot = ({
|
|
181
|
+
children,
|
|
182
|
+
frameloop,
|
|
183
|
+
fromLngLat: fromLngLat2,
|
|
184
|
+
...renderProps
|
|
185
|
+
}) => {
|
|
186
|
+
const id = react.useId();
|
|
187
|
+
const { onAdd, onRemove, mounted, r3mRef } = useOnAdd(fromLngLat2, { frameloop, ...renderProps });
|
|
188
|
+
react.useEffect(() => {
|
|
189
|
+
if (!mounted)
|
|
190
|
+
return;
|
|
191
|
+
if (!r3mRef.current.root)
|
|
192
|
+
return;
|
|
193
|
+
r3mRef.current.root.render(/* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children }));
|
|
194
|
+
}, [r3mRef, mounted, children]);
|
|
195
|
+
return { id, onAdd, onRemove, r3mRef };
|
|
196
|
+
};
|
|
197
|
+
const originMx = new THREE.Matrix4();
|
|
198
|
+
const projByView = new THREE.Matrix4();
|
|
199
|
+
const projByViewInv = new THREE.Matrix4();
|
|
200
|
+
const fwd = new THREE.Vector3();
|
|
201
|
+
function syncCamera(camera, origin, mapCamMx) {
|
|
202
|
+
projByView.fromArray(mapCamMx).multiply(originMx.fromArray(origin));
|
|
203
|
+
projByViewInv.copy(projByView).invert();
|
|
204
|
+
updateCamera(camera, projByViewInv);
|
|
205
|
+
camera.updateMatrix();
|
|
206
|
+
camera.updateMatrixWorld(true);
|
|
207
|
+
camera.projectionMatrix.copy(camera.matrix).premultiply(projByView);
|
|
208
|
+
camera.projectionMatrixInverse.copy(camera.projectionMatrix).invert();
|
|
209
|
+
camera.far = calculateFar(
|
|
210
|
+
camera.matrix.elements[10],
|
|
211
|
+
camera.matrix.elements[14],
|
|
212
|
+
camera.near
|
|
213
|
+
);
|
|
214
|
+
camera.userData.projByView = projByView.toArray();
|
|
215
|
+
camera.userData.projByViewInv = projByViewInv.toArray();
|
|
216
|
+
}
|
|
217
|
+
const updateCamera = (target, projByViewInv2) => {
|
|
218
|
+
target.position.setScalar(0).applyMatrix4(projByViewInv2);
|
|
219
|
+
target.up.set(0, -1, 0).applyMatrix4(projByViewInv2).negate().add(target.position).normalize();
|
|
220
|
+
fwd.set(0, 0, 1).applyMatrix4(projByViewInv2);
|
|
221
|
+
target.lookAt(fwd);
|
|
222
|
+
};
|
|
223
|
+
function calculateFar(c, d, near) {
|
|
224
|
+
const numerator = d * (c - 1);
|
|
225
|
+
const denominator = c * near + near;
|
|
226
|
+
const far = numerator / denominator;
|
|
227
|
+
return far;
|
|
228
|
+
}
|
|
229
|
+
function useRender(origin, r3mRef, frameloop) {
|
|
230
|
+
const render = useFunction((_gl, mapCamMx) => {
|
|
231
|
+
const r3m = r3mRef.current;
|
|
232
|
+
if (!r3m.state || !r3m.map)
|
|
233
|
+
return;
|
|
234
|
+
const camera = r3m.state.camera;
|
|
235
|
+
const gl = r3m.state.gl;
|
|
236
|
+
const advance = r3m.state.advance;
|
|
237
|
+
r3m.mapCamMx = mapCamMx;
|
|
238
|
+
syncCamera(camera, origin, mapCamMx);
|
|
239
|
+
gl.resetState();
|
|
240
|
+
advance(Date.now() * 1e-3, true);
|
|
241
|
+
if (frameloop === "always")
|
|
242
|
+
r3m.map.triggerRepaint();
|
|
243
|
+
});
|
|
244
|
+
return render;
|
|
245
|
+
}
|
|
246
|
+
function useCanvas({
|
|
247
|
+
longitude,
|
|
248
|
+
latitude,
|
|
249
|
+
altitude = 0,
|
|
250
|
+
frameloop = "always",
|
|
251
|
+
fromLngLat: fromLngLat2,
|
|
252
|
+
...renderProps
|
|
253
|
+
}) {
|
|
254
|
+
const m42 = useCoords({
|
|
255
|
+
latitude,
|
|
256
|
+
longitude,
|
|
257
|
+
altitude,
|
|
258
|
+
fromLngLat: fromLngLat2
|
|
259
|
+
});
|
|
260
|
+
const { id, onAdd, onRemove, r3mRef } = useCreateRoot({ frameloop, fromLngLat: fromLngLat2, ...renderProps });
|
|
261
|
+
const render = useRender(m42, r3mRef, frameloop);
|
|
262
|
+
return { id, onAdd, onRemove, render };
|
|
263
|
+
}
|
|
264
|
+
fiber.extend(THREE__namespace);
|
|
265
|
+
const fromLngLat = maplibreGl.MercatorCoordinate.fromLngLat;
|
|
266
|
+
const Canvas = react.memo((props) => {
|
|
267
|
+
const { id, onAdd, onRemove, render } = useCanvas({ ...props, fromLngLat });
|
|
268
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
269
|
+
maplibre.Layer,
|
|
270
|
+
{
|
|
271
|
+
id,
|
|
272
|
+
type: "custom",
|
|
273
|
+
renderingMode: "3d",
|
|
274
|
+
onAdd,
|
|
275
|
+
onRemove,
|
|
276
|
+
render
|
|
277
|
+
}
|
|
278
|
+
);
|
|
279
|
+
});
|
|
280
|
+
const useR3M = () => fiber.useThree((s) => s.r3m);
|
|
281
|
+
const Coordinates = react.memo(({
|
|
282
|
+
latitude,
|
|
283
|
+
longitude,
|
|
284
|
+
altitude = 0,
|
|
285
|
+
children
|
|
286
|
+
}) => {
|
|
287
|
+
const [scene] = react.useState(() => new THREE.Scene());
|
|
288
|
+
const r3m = useR3M();
|
|
289
|
+
const origin = useCoords({
|
|
290
|
+
latitude,
|
|
291
|
+
longitude,
|
|
292
|
+
altitude,
|
|
293
|
+
fromLngLat: r3m.fromLngLat
|
|
294
|
+
});
|
|
295
|
+
return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: fiber.createPortal(/* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
296
|
+
/* @__PURE__ */ jsxRuntime.jsx(RenderAtCoords, { r3m, origin }),
|
|
297
|
+
children
|
|
298
|
+
] }), scene, { events: { priority: 2 } }) });
|
|
299
|
+
});
|
|
300
|
+
Coordinates.displayName = "Coordinates";
|
|
301
|
+
function RenderAtCoords({ r3m, origin }) {
|
|
302
|
+
const { gl, scene, set } = fiber.useThree();
|
|
303
|
+
const cameraRef = react.useRef(null);
|
|
304
|
+
fiber.useFrame(() => {
|
|
305
|
+
if (!r3m.mapCamMx)
|
|
306
|
+
return;
|
|
307
|
+
if (!cameraRef.current)
|
|
308
|
+
return;
|
|
309
|
+
syncCamera(cameraRef.current, origin, r3m.mapCamMx);
|
|
310
|
+
gl.render(scene, cameraRef.current);
|
|
311
|
+
});
|
|
312
|
+
react.useLayoutEffect(() => {
|
|
313
|
+
if (!cameraRef.current)
|
|
314
|
+
return;
|
|
315
|
+
set({
|
|
316
|
+
invalidate: () => {
|
|
317
|
+
if (!r3m.map)
|
|
318
|
+
return;
|
|
319
|
+
r3m.map.triggerRepaint();
|
|
320
|
+
},
|
|
321
|
+
camera: cameraRef.current
|
|
322
|
+
});
|
|
323
|
+
}, [set, r3m]);
|
|
324
|
+
return /* @__PURE__ */ jsxRuntime.jsx("perspectiveCamera", { ref: cameraRef });
|
|
325
|
+
}
|
|
326
|
+
exports.Canvas = Canvas;
|
|
327
|
+
exports.Coordinates = Coordinates;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Canvas
|
|
2
|
-
import { Coordinates
|
|
1
|
+
import { Canvas } from "./main2.mjs";
|
|
2
|
+
import { Coordinates } from "./main3.mjs";
|
|
3
3
|
export {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
Canvas,
|
|
5
|
+
Coordinates
|
|
6
6
|
};
|
|
7
7
|
//# sourceMappingURL=main.mjs.map
|
|
@@ -1,10 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
function
|
|
4
|
-
const
|
|
5
|
-
|
|
1
|
+
import { syncCamera } from "./main6.mjs";
|
|
2
|
+
import { useFunction } from "./main12.mjs";
|
|
3
|
+
function useRender(origin, r3mRef, frameloop) {
|
|
4
|
+
const render = useFunction((_gl, mapCamMx) => {
|
|
5
|
+
const r3m = r3mRef.current;
|
|
6
|
+
if (!r3m.state || !r3m.map)
|
|
7
|
+
return;
|
|
8
|
+
const camera = r3m.state.camera;
|
|
9
|
+
const gl = r3m.state.gl;
|
|
10
|
+
const advance = r3m.state.advance;
|
|
11
|
+
r3m.mapCamMx = mapCamMx;
|
|
12
|
+
syncCamera(camera, origin, mapCamMx);
|
|
13
|
+
gl.resetState();
|
|
14
|
+
advance(Date.now() * 1e-3, true);
|
|
15
|
+
if (frameloop === "always")
|
|
16
|
+
r3m.map.triggerRepaint();
|
|
17
|
+
});
|
|
18
|
+
return render;
|
|
6
19
|
}
|
|
7
20
|
export {
|
|
8
|
-
|
|
21
|
+
useRender
|
|
9
22
|
};
|
|
10
23
|
//# sourceMappingURL=main10.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main10.mjs","sources":["../../../src/core/
|
|
1
|
+
{"version":3,"file":"main10.mjs","sources":["../../../src/core/use-render.ts"],"sourcesContent":["import { MutableRefObject } from \"react\";\nimport { Matrix4Tuple, PerspectiveCamera } from \"three\";\nimport { R3mStore } from \"./store\";\nimport { syncCamera } from \"./sync-camera\";\nimport { useFunction } from \"./use-function\";\n\nexport function useRender(\n origin: Matrix4Tuple, r3mRef: MutableRefObject<R3mStore>, frameloop: 'always' | 'demand'\n) {\n\n const render = useFunction((_gl: WebGL2RenderingContext, mapCamMx: number[]) => {\n const r3m = r3mRef.current;\n if (!r3m.state || !r3m.map) return;\n const camera = r3m.state.camera;\n const gl = r3m.state.gl;\n const advance = r3m.state.advance;\n r3m.mapCamMx = mapCamMx as Matrix4Tuple;\n syncCamera(camera as PerspectiveCamera, origin, mapCamMx as Matrix4Tuple);\n gl.resetState();\n advance(Date.now() * 0.001, true);\n if (frameloop === 'always') r3m.map.triggerRepaint();\n })\n\n return render;\n\n}"],"names":[],"mappings":";;AAMgB,SAAA,UACd,QAAsB,QAAoC,WAC1D;AAEA,QAAM,SAAS,YAAY,CAAC,KAA6B,aAAuB;AAC9E,UAAM,MAAM,OAAO;AACnB,QAAI,CAAC,IAAI,SAAS,CAAC,IAAI;AAAK;AACtB,UAAA,SAAS,IAAI,MAAM;AACnB,UAAA,KAAK,IAAI,MAAM;AACf,UAAA,UAAU,IAAI,MAAM;AAC1B,QAAI,WAAW;AACJ,eAAA,QAA6B,QAAQ,QAAwB;AACxE,OAAG,WAAW;AACd,YAAQ,KAAK,IAAQ,IAAA,MAAO,IAAI;AAChC,QAAI,cAAc;AAAU,UAAI,IAAI;EAAe,CACpD;AAEM,SAAA;AAET;"}
|
|
@@ -1,52 +1,66 @@
|
|
|
1
|
-
import { createRoot
|
|
2
|
-
import { useState
|
|
3
|
-
import { createEvents
|
|
4
|
-
import { useFunction
|
|
5
|
-
function
|
|
6
|
-
const [
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { createRoot, _roots } from "@react-three/fiber";
|
|
2
|
+
import { useState, useRef, useEffect } from "react";
|
|
3
|
+
import { createEvents } from "./main13.mjs";
|
|
4
|
+
import { useFunction } from "./main12.mjs";
|
|
5
|
+
function useOnAdd(fromLngLat, { frameloop, ...renderProps }) {
|
|
6
|
+
const [mounted, setMounted] = useState(false);
|
|
7
|
+
const r3mRef = useRef({ fromLngLat });
|
|
8
|
+
const onAdd = useFunction((map, gl) => {
|
|
9
|
+
const canvas = map.getCanvas();
|
|
10
|
+
const root = createRoot(canvas);
|
|
11
|
+
root.configure({
|
|
9
12
|
dpr: window.devicePixelRatio,
|
|
10
|
-
events:
|
|
11
|
-
...
|
|
13
|
+
events: createEvents(),
|
|
14
|
+
...renderProps,
|
|
12
15
|
frameloop: "never",
|
|
13
16
|
gl: {
|
|
14
|
-
context:
|
|
15
|
-
depth:
|
|
16
|
-
autoClear:
|
|
17
|
-
antialias:
|
|
18
|
-
...
|
|
17
|
+
context: gl,
|
|
18
|
+
depth: true,
|
|
19
|
+
autoClear: false,
|
|
20
|
+
antialias: true,
|
|
21
|
+
...renderProps == null ? void 0 : renderProps.gl
|
|
19
22
|
},
|
|
20
|
-
onCreated: (
|
|
21
|
-
|
|
23
|
+
onCreated: (state) => {
|
|
24
|
+
state.gl.forceContextLoss = () => {
|
|
22
25
|
};
|
|
23
26
|
},
|
|
24
27
|
camera: {
|
|
25
|
-
matrixAutoUpdate:
|
|
28
|
+
matrixAutoUpdate: false,
|
|
29
|
+
near: 0
|
|
26
30
|
},
|
|
27
31
|
size: {
|
|
28
|
-
width:
|
|
29
|
-
height:
|
|
32
|
+
width: canvas.clientWidth,
|
|
33
|
+
height: canvas.clientHeight,
|
|
30
34
|
top: 0,
|
|
31
35
|
left: 0,
|
|
32
|
-
...
|
|
36
|
+
...renderProps == null ? void 0 : renderProps.size
|
|
33
37
|
}
|
|
34
38
|
});
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
const store = _roots.get(canvas).store;
|
|
40
|
+
r3mRef.current.map = map;
|
|
41
|
+
r3mRef.current.root = root;
|
|
42
|
+
r3mRef.current.state = store.getState();
|
|
43
|
+
store.setState({ r3m: r3mRef.current });
|
|
44
|
+
if (frameloop === "demand") {
|
|
45
|
+
store.setState({
|
|
46
|
+
frameloop,
|
|
47
|
+
invalidate: () => {
|
|
48
|
+
map.triggerRepaint();
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
setTimeout(() => setMounted(true));
|
|
53
|
+
});
|
|
54
|
+
const onRemove = useFunction(() => {
|
|
55
|
+
if (!r3mRef.current.root)
|
|
56
|
+
return;
|
|
57
|
+
r3mRef.current.root.unmount();
|
|
58
|
+
r3mRef.current.root = void 0;
|
|
46
59
|
});
|
|
47
|
-
|
|
60
|
+
useEffect(() => () => onRemove(), [onRemove]);
|
|
61
|
+
return { onAdd, onRemove, mounted, r3mRef };
|
|
48
62
|
}
|
|
49
63
|
export {
|
|
50
|
-
|
|
64
|
+
useOnAdd
|
|
51
65
|
};
|
|
52
66
|
//# sourceMappingURL=main11.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main11.mjs","sources":["../../../src/core/use-on-add.ts"],"sourcesContent":["import { RenderProps, _roots, createRoot } from \"@react-three/fiber\";\nimport { useRef, useState } from \"react\";\nimport { createEvents } from \"./create-events\";\nimport { FromLngLat, MapInstance } from \"./generic-map\";\nimport { R3mStore } from \"./store\";\nimport { useFunction } from \"./use-function\";\n\nexport function useOnAdd(\n fromLngLat: FromLngLat,\n { frameloop, ...renderProps }: RenderProps<HTMLCanvasElement>\n) {\n\n const [mounted, setMounted] = useState(false);\n\n const r3mRef = useRef<R3mStore>({ fromLngLat });\n\n const onAdd = useFunction((map: MapInstance, gl: WebGLRenderingContext) => {\n\n const canvas = map.getCanvas();\n const root = createRoot(canvas);\n root.configure({\n dpr: window.devicePixelRatio,\n events: createEvents(),\n ...renderProps,\n frameloop: 'never',\n gl: {\n context: gl,\n depth: true,\n autoClear: false,\n antialias: true,\n ...renderProps?.gl,\n },\n onCreated: (state) => {\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n state.gl.forceContextLoss = () => { };\n },\n camera: {\n matrixAutoUpdate: false,\n },\n size: {\n width: canvas.clientWidth,\n height: canvas.clientHeight,\n top: 0,\n left: 0,\n ...renderProps?.size,\n },\n });\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const store = _roots.get(canvas)!.store;\n\n r3mRef.current.map = map;\n r3mRef.current.root = root;\n r3mRef.current.state = store.getState();\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n store.setState({ r3m: r3mRef.current } as any);\n\n if (frameloop === 'demand') {\n store.setState({\n frameloop,\n invalidate: () => {\n map.triggerRepaint();\n },\n })\n }\n\n setTimeout(() => setMounted(true));\n\n })\n\n const onRemove = useFunction(() => {\n
|
|
1
|
+
{"version":3,"file":"main11.mjs","sources":["../../../src/core/use-on-add.ts"],"sourcesContent":["import { RenderProps, _roots, createRoot } from \"@react-three/fiber\";\nimport { useEffect, useRef, useState } from \"react\";\nimport { createEvents } from \"./create-events\";\nimport { FromLngLat, MapInstance } from \"./generic-map\";\nimport { R3mStore } from \"./store\";\nimport { useFunction } from \"./use-function\";\n\nexport function useOnAdd(\n fromLngLat: FromLngLat,\n { frameloop, ...renderProps }: RenderProps<HTMLCanvasElement>\n) {\n\n const [mounted, setMounted] = useState(false);\n\n const r3mRef = useRef<R3mStore>({ fromLngLat });\n\n const onAdd = useFunction((map: MapInstance, gl: WebGLRenderingContext) => {\n\n const canvas = map.getCanvas();\n const root = createRoot(canvas);\n root.configure({\n dpr: window.devicePixelRatio,\n events: createEvents(),\n ...renderProps,\n frameloop: 'never',\n gl: {\n context: gl,\n depth: true,\n autoClear: false,\n antialias: true,\n ...renderProps?.gl,\n },\n onCreated: (state) => {\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n state.gl.forceContextLoss = () => { };\n },\n camera: {\n matrixAutoUpdate: false,\n near: 0,\n },\n size: {\n width: canvas.clientWidth,\n height: canvas.clientHeight,\n top: 0,\n left: 0,\n ...renderProps?.size,\n },\n });\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const store = _roots.get(canvas)!.store;\n\n r3mRef.current.map = map;\n r3mRef.current.root = root;\n r3mRef.current.state = store.getState();\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n store.setState({ r3m: r3mRef.current } as any);\n\n if (frameloop === 'demand') {\n store.setState({\n frameloop,\n invalidate: () => {\n map.triggerRepaint();\n },\n })\n }\n\n setTimeout(() => setMounted(true));\n\n })\n\n const onRemove = useFunction(() => {\n if (!r3mRef.current.root) return;\n r3mRef.current.root.unmount();\n r3mRef.current.root = undefined;\n })\n\n // on unmount\n useEffect(() => () => onRemove(), [onRemove])\n\n return { onAdd, onRemove, mounted, r3mRef };\n}"],"names":[],"mappings":";;;;AAOO,SAAS,SACd,YACA,EAAE,WAAW,GAAG,eAChB;AAEA,QAAM,CAAC,SAAS,UAAU,IAAI,SAAS,KAAK;AAE5C,QAAM,SAAS,OAAiB,EAAE,WAAY,CAAA;AAE9C,QAAM,QAAQ,YAAY,CAAC,KAAkB,OAA8B;AAEnE,UAAA,SAAS,IAAI;AACb,UAAA,OAAO,WAAW,MAAM;AAC9B,SAAK,UAAU;AAAA,MACb,KAAK,OAAO;AAAA,MACZ,QAAQ,aAAa;AAAA,MACrB,GAAG;AAAA,MACH,WAAW;AAAA,MACX,IAAI;AAAA,QACF,SAAS;AAAA,QACT,OAAO;AAAA,QACP,WAAW;AAAA,QACX,WAAW;AAAA,QACX,GAAG,2CAAa;AAAA,MAClB;AAAA,MACA,WAAW,CAAC,UAAU;AAEd,cAAA,GAAG,mBAAmB,MAAM;AAAA,QAAA;AAAA,MACpC;AAAA,MACA,QAAQ;AAAA,QACN,kBAAkB;AAAA,QAClB,MAAM;AAAA,MACR;AAAA,MACA,MAAM;AAAA,QACJ,OAAO,OAAO;AAAA,QACd,QAAQ,OAAO;AAAA,QACf,KAAK;AAAA,QACL,MAAM;AAAA,QACN,GAAG,2CAAa;AAAA,MAClB;AAAA,IAAA,CACD;AAGD,UAAM,QAAQ,OAAO,IAAI,MAAM,EAAG;AAElC,WAAO,QAAQ,MAAM;AACrB,WAAO,QAAQ,OAAO;AACf,WAAA,QAAQ,QAAQ,MAAM,SAAS;AAGtC,UAAM,SAAS,EAAE,KAAK,OAAO,QAAgB,CAAA;AAE7C,QAAI,cAAc,UAAU;AAC1B,YAAM,SAAS;AAAA,QACb;AAAA,QACA,YAAY,MAAM;AAChB,cAAI,eAAe;AAAA,QACrB;AAAA,MAAA,CACD;AAAA,IACH;AAEW,eAAA,MAAM,WAAW,IAAI,CAAC;AAAA,EAAA,CAElC;AAEK,QAAA,WAAW,YAAY,MAAM;AAC7B,QAAA,CAAC,OAAO,QAAQ;AAAM;AACnB,WAAA,QAAQ,KAAK;AACpB,WAAO,QAAQ,OAAO;AAAA,EAAA,CACvB;AAGD,YAAU,MAAM,MAAM,SAAY,GAAA,CAAC,QAAQ,CAAC;AAE5C,SAAO,EAAE,OAAO,UAAU,SAAS,OAAO;AAC5C;"}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { useRef
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
|
|
1
|
+
import { useRef, useCallback } from "react";
|
|
2
|
+
const useFunction = (callback) => {
|
|
3
|
+
const callbackRef = useRef(callback);
|
|
4
|
+
callbackRef.current = callback;
|
|
5
|
+
return useCallback((...args) => {
|
|
6
|
+
return callbackRef.current(...args);
|
|
7
|
+
}, []);
|
|
5
8
|
};
|
|
6
9
|
export {
|
|
7
|
-
|
|
10
|
+
useFunction
|
|
8
11
|
};
|
|
9
12
|
//# sourceMappingURL=main12.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main12.mjs","sources":["../../../src/core/use-function.ts"],"sourcesContent":["import { useCallback, useRef } from 'react';\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\nexport const useFunction = <T extends (...args: any[]) => any>(callback: T): T => {\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n return useCallback((...args: any[]) => {\n return callbackRef.current(...args);\n }, []) as T;\n};\n"],"names":[
|
|
1
|
+
{"version":3,"file":"main12.mjs","sources":["../../../src/core/use-function.ts"],"sourcesContent":["import { useCallback, useRef } from 'react';\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\nexport const useFunction = <T extends (...args: any[]) => any>(callback: T): T => {\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n return useCallback((...args: any[]) => {\n return callbackRef.current(...args);\n }, []) as T;\n};\n"],"names":[],"mappings":";AAGa,MAAA,cAAc,CAAoC,aAAmB;AAC1E,QAAA,cAAc,OAAO,QAAQ;AACnC,cAAY,UAAU;AACf,SAAA,YAAY,IAAI,SAAgB;AAC9B,WAAA,YAAY,QAAQ,GAAG,IAAI;AAAA,EACpC,GAAG,CAAE,CAAA;AACP;"}
|