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.
Files changed (57) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +2 -2
  3. package/dist/cjs/main.js +327 -1
  4. package/dist/es/main.mjs +4 -4
  5. package/dist/es/main10.mjs +14 -6
  6. package/dist/es/main10.mjs.map +1 -1
  7. package/dist/es/main11.mjs +48 -34
  8. package/dist/es/main11.mjs.map +1 -1
  9. package/dist/es/main12.mjs +8 -5
  10. package/dist/es/main12.mjs.map +1 -1
  11. package/dist/es/main13.mjs +54 -37
  12. package/dist/es/main13.mjs.map +1 -1
  13. package/dist/es/main2.mjs +18 -17
  14. package/dist/es/main2.mjs.map +1 -1
  15. package/dist/es/main3.mjs +46 -32
  16. package/dist/es/main3.mjs.map +1 -1
  17. package/dist/es/main4.mjs +20 -18
  18. package/dist/es/main4.mjs.map +1 -1
  19. package/dist/es/main5.mjs +3 -3
  20. package/dist/es/main5.mjs.map +1 -1
  21. package/dist/es/main6.mjs +33 -5
  22. package/dist/es/main6.mjs.map +1 -1
  23. package/dist/es/main7.mjs +11 -10
  24. package/dist/es/main7.mjs.map +1 -1
  25. package/dist/es/main8.mjs +19 -13
  26. package/dist/es/main8.mjs.map +1 -1
  27. package/dist/es/main9.mjs +17 -9
  28. package/dist/es/main9.mjs.map +1 -1
  29. package/dist/maplibre/cjs/main.js +327 -1
  30. package/dist/maplibre/es/main.mjs +4 -4
  31. package/dist/maplibre/es/main10.mjs +19 -6
  32. package/dist/maplibre/es/main10.mjs.map +1 -1
  33. package/dist/maplibre/es/main11.mjs +48 -34
  34. package/dist/maplibre/es/main11.mjs.map +1 -1
  35. package/dist/maplibre/es/main12.mjs +8 -5
  36. package/dist/maplibre/es/main12.mjs.map +1 -1
  37. package/dist/maplibre/es/main13.mjs +54 -37
  38. package/dist/maplibre/es/main13.mjs.map +1 -1
  39. package/dist/maplibre/es/main2.mjs +18 -17
  40. package/dist/maplibre/es/main2.mjs.map +1 -1
  41. package/dist/maplibre/es/main3.mjs +46 -32
  42. package/dist/maplibre/es/main3.mjs.map +1 -1
  43. package/dist/maplibre/es/main4.mjs +20 -18
  44. package/dist/maplibre/es/main4.mjs.map +1 -1
  45. package/dist/maplibre/es/main5.mjs +3 -3
  46. package/dist/maplibre/es/main5.mjs.map +1 -1
  47. package/dist/maplibre/es/main6.mjs +33 -5
  48. package/dist/maplibre/es/main6.mjs.map +1 -1
  49. package/dist/maplibre/es/main7.mjs +11 -10
  50. package/dist/maplibre/es/main7.mjs.map +1 -1
  51. package/dist/maplibre/es/main8.mjs +15 -15
  52. package/dist/maplibre/es/main8.mjs.map +1 -1
  53. package/dist/maplibre/es/main9.mjs +20 -11
  54. package/dist/maplibre/es/main9.mjs.map +1 -1
  55. package/package.json +1 -1
  56. package/stories/package.json +1 -1
  57. package/stories/yarn.lock +4 -4
@@ -1 +1,327 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("react/jsx-runtime"),l=require("@react-three/fiber"),b=require("maplibre-gl"),s=require("react"),S=require("react-map-gl/maplibre"),m=require("three");function L(n){const c=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(n){for(const t in n)if(t!=="default"){const e=Object.getOwnPropertyDescriptor(n,t);Object.defineProperty(c,t,e.get?e:{enumerable:!0,get:()=>n[t]})}}return c.default=n,Object.freeze(c)}const O=L(m),h=new m.Quaternion,T=new m.Euler,M=new m.Vector3,R=new m.Vector3,A=new m.Matrix4;function z({longitude:n,latitude:c,altitude:t,fromLngLat:e}){const o=e([n,c],t),r=o.meterInMercatorCoordinateUnits();return M.set(o.x,o.y,o.z||0),R.set(r,-r,r),h.setFromEuler(T.set(-Math.PI*.5,0,0)),A.compose(M,h,R).toArray()}function C({latitude:n,longitude:c,altitude:t,fromLngLat:e}){return s.useMemo(()=>z({latitude:n,longitude:c,altitude:t,fromLngLat:e}),[n,c,t,e])}const x={onClick:["click",!1],onContextMenu:["contextmenu",!1],onDoubleClick:["dblclick",!1],onWheel:["wheel",!0],onPointerDown:["pointerdown",!0],onPointerUp:["pointerup",!0],onPointerLeave:["pointerleave",!0],onPointerMove:["pointermove",!0],onPointerCancel:["pointercancel",!0],onLostPointerCapture:["lostpointercapture",!0]};function q(){return n=>{const{handlePointer:c}=l.createEvents(n);return{priority:1,enabled:!0,compute(t,e){e.size.width=e.gl.domElement.width/window.devicePixelRatio,e.size.height=e.gl.domElement.height/window.devicePixelRatio,e.pointer.x=t.offsetX/e.size.width*2-1,e.pointer.y=1-t.offsetY/e.size.height*2,e.raycaster.camera=e.camera,e.raycaster.ray.origin.setScalar(0).applyMatrix4(e.camera.projectionMatrixInverse),e.raycaster.ray.direction.set(e.pointer.x,e.pointer.y,1).applyMatrix4(e.camera.projectionMatrixInverse).sub(e.raycaster.ray.origin).normalize()},connected:void 0,handlers:Object.keys(x).reduce((t,e)=>({...t,[e]:c(e)}),{}),update:()=>{var o;const{events:t,internal:e}=n.getState();(o=e.lastEvent)!=null&&o.current&&t.handlers&&t.handlers.onPointerMove(e.lastEvent.current)},connect:t=>{var r;const{set:e,events:o}=n.getState();(r=o.disconnect)==null||r.call(o),e(i=>({events:{...i.events,connected:t.parentNode}})),Object.entries(o.handlers??[]).forEach(([i,u])=>{const[a,f]=x[i];t.addEventListener(a,u,{passive:f})})},disconnect:()=>{const{set:t,events:e}=n.getState();e.connected&&(Object.entries(e.handlers??[]).forEach(([o,r])=>{if(e&&e.connected instanceof HTMLElement){const[i]=x[o];e.connected.removeEventListener(i,r)}}),t(o=>({events:{...o.events,connected:void 0}})))}}}}const y=n=>{const c=s.useRef(n);return c.current=n,s.useCallback((...t)=>c.current(...t),[])};function k(n,{frameloop:c,...t}){const[e,o]=s.useState(!1),r=s.useRef({fromLngLat:n}),i=y((a,f)=>{const p=a.getCanvas(),v=l.createRoot(p);v.configure({dpr:window.devicePixelRatio,events:q(),...t,frameloop:"never",gl:{context:f,depth:!0,autoClear:!1,antialias:!0,...t==null?void 0:t.gl},onCreated:j=>{j.gl.forceContextLoss=()=>{}},camera:{matrixAutoUpdate:!1},size:{width:p.clientWidth,height:p.clientHeight,top:0,left:0,...t==null?void 0:t.size}});const g=l._roots.get(p).store;r.current.map=a,r.current.root=v,r.current.state=g.getState(),g.setState({r3m:r.current}),c==="demand"&&g.setState({frameloop:c,invalidate:()=>{a.triggerRepaint()}}),setTimeout(()=>o(!0))}),u=y(()=>{setTimeout(()=>{r.current.root&&r.current.root.unmount()})});return{onAdd:i,onRemove:u,mounted:e,r3mRef:r}}const D=({children:n,frameloop:c,fromLngLat:t,...e})=>{const o=s.useId(),{onAdd:r,onRemove:i,mounted:u,r3mRef:a}=k(t,{frameloop:c,...e});return s.useEffect(()=>{u&&a.current.root&&a.current.root.render(d.jsx(d.Fragment,{children:n}))},[a,u,n]),{id:o,onAdd:r,onRemove:i,r3mRef:a}},F=new m.Matrix4;function w(n,c,t){n.projectionMatrix.fromArray(t).multiply(F.fromArray(c)),n.projectionMatrixInverse.copy(n.projectionMatrix).invert()}function I(n,c,t){return y((o,r)=>{const i=c.current;if(!i.state||!i.map)return;const u=i.state.camera,a=i.state.gl,f=i.state.advance;i.mapCamMx=r,w(u,n,r),a.resetState(),f(Date.now()*.001,!0),t==="always"&&i.map.triggerRepaint()})}function N({longitude:n,latitude:c,altitude:t=0,frameloop:e="always",fromLngLat:o,...r}){const i=C({latitude:c,longitude:n,altitude:t,fromLngLat:o}),{id:u,onAdd:a,onRemove:f,r3mRef:p}=D({frameloop:e,fromLngLat:o,...r}),v=I(i,p,e);return{id:u,onAdd:a,onRemove:f,render:v}}l.extend(O);const P=b.MercatorCoordinate.fromLngLat,_=s.memo(n=>{const{id:c,onAdd:t,onRemove:e,render:o}=N({...n,fromLngLat:P});return d.jsx(S.Layer,{id:c,type:"custom",renderingMode:"3d",onAdd:t,onRemove:e,render:o})}),H=()=>l.useThree(n=>n.r3m),E=s.memo(({latitude:n,longitude:c,altitude:t=0,children:e})=>{const[o]=s.useState(()=>new m.Scene),r=H(),i=C({latitude:n,longitude:c,altitude:t,fromLngLat:r.fromLngLat});return d.jsx(d.Fragment,{children:l.createPortal(d.jsxs(d.Fragment,{children:[d.jsx(U,{r3m:r,origin:i}),e]}),o,{events:{priority:2}})})});E.displayName="Coordinates";function U({r3m:n,origin:c}){const{gl:t,scene:e,set:o}=l.useThree(),r=s.useRef(null);return l.useFrame(()=>{n.mapCamMx&&r.current&&(w(r.current,c,n.mapCamMx),t.render(e,r.current))}),s.useLayoutEffect(()=>{r.current&&o({invalidate:()=>{n.map&&n.map.triggerRepaint()},camera:r.current})},[o,n]),d.jsx("perspectiveCamera",{ref:r})}exports.Canvas=_;exports.Coordinates=E;
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 as a } from "./main2.mjs";
2
- import { Coordinates as t } from "./main3.mjs";
1
+ import { Canvas } from "./main2.mjs";
2
+ import { Coordinates } from "./main3.mjs";
3
3
  export {
4
- a as Canvas,
5
- t as Coordinates
4
+ Canvas,
5
+ Coordinates
6
6
  };
7
7
  //# sourceMappingURL=main.mjs.map
@@ -1,10 +1,23 @@
1
- import { Quaternion as m, Euler as l, Vector3 as s, Matrix4 as w } from "three";
2
- const o = new m(), p = new l(), n = new s(), r = new s(), x = new w();
3
- function d({ longitude: c, latitude: a, altitude: i, fromLngLat: u }) {
4
- const t = u([c, a], i), e = t.meterInMercatorCoordinateUnits();
5
- return n.set(t.x, t.y, t.z || 0), r.set(e, -e, e), o.setFromEuler(p.set(-Math.PI * 0.5, 0, 0)), x.compose(n, o, r).toArray();
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
- d as coordsToMatrix
21
+ useRender
9
22
  };
10
23
  //# sourceMappingURL=main10.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"main10.mjs","sources":["../../../src/core/coords-to-matrix.ts"],"sourcesContent":["import { Euler, Matrix4, Quaternion, Vector3 } from \"three\";\nimport { FromLngLat } from \"./generic-map\";\n\nconst quat = new Quaternion();\nconst euler = new Euler();\nconst pos = new Vector3();\nconst scale = new Vector3();\nconst m4 = new Matrix4();\n\n/** calculate Matrix4 from coordinates */\nexport function coordsToMatrix({ longitude, latitude, altitude, fromLngLat }: {\n longitude: number, latitude: number, altitude: number, fromLngLat: FromLngLat\n}) {\n const center = fromLngLat([longitude, latitude], altitude);\n const scaleUnit = center.meterInMercatorCoordinateUnits();\n pos.set(center.x, center.y, center.z || 0);\n scale.set(scaleUnit, -scaleUnit, scaleUnit);\n quat.setFromEuler(euler.set(-Math.PI * .5, 0, 0));\n return m4.compose(pos, quat, scale).toArray();\n}\n"],"names":["quat","Quaternion","euler","Euler","pos","Vector3","scale","m4","Matrix4","coordsToMatrix","longitude","latitude","altitude","fromLngLat","center","scaleUnit"],"mappings":";AAGA,MAAMA,IAAO,IAAIC,KACXC,IAAQ,IAAIC,KACZC,IAAM,IAAIC,KACVC,IAAQ,IAAID,KACZE,IAAK,IAAIC;AAGR,SAASC,EAAe,EAAE,WAAAC,GAAW,UAAAC,GAAU,UAAAC,GAAU,YAAAC,KAE7D;AACD,QAAMC,IAASD,EAAW,CAACH,GAAWC,CAAQ,GAAGC,CAAQ,GACnDG,IAAYD,EAAO;AACzB,SAAAV,EAAI,IAAIU,EAAO,GAAGA,EAAO,GAAGA,EAAO,KAAK,CAAC,GACzCR,EAAM,IAAIS,GAAW,CAACA,GAAWA,CAAS,GACrCf,EAAA,aAAaE,EAAM,IAAI,CAAC,KAAK,KAAK,KAAI,GAAG,CAAC,CAAC,GACzCK,EAAG,QAAQH,GAAKJ,GAAMM,CAAK,EAAE;AACtC;"}
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 as d, _roots as h } from "@react-three/fiber";
2
- import { useState as R, useRef as x } from "react";
3
- import { createEvents as C } from "./main13.mjs";
4
- import { useFunction as c } from "./main12.mjs";
5
- function T(s, { frameloop: a, ...t }) {
6
- const [r, m] = R(!1), e = x({ fromLngLat: s }), f = c((n, g) => {
7
- const o = n.getCanvas(), u = d(o);
8
- u.configure({
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: C(),
11
- ...t,
13
+ events: createEvents(),
14
+ ...renderProps,
12
15
  frameloop: "never",
13
16
  gl: {
14
- context: g,
15
- depth: !0,
16
- autoClear: !1,
17
- antialias: !0,
18
- ...t == null ? void 0 : t.gl
17
+ context: gl,
18
+ depth: true,
19
+ autoClear: false,
20
+ antialias: true,
21
+ ...renderProps == null ? void 0 : renderProps.gl
19
22
  },
20
- onCreated: (v) => {
21
- v.gl.forceContextLoss = () => {
23
+ onCreated: (state) => {
24
+ state.gl.forceContextLoss = () => {
22
25
  };
23
26
  },
24
27
  camera: {
25
- matrixAutoUpdate: !1
28
+ matrixAutoUpdate: false,
29
+ near: 0
26
30
  },
27
31
  size: {
28
- width: o.clientWidth,
29
- height: o.clientHeight,
32
+ width: canvas.clientWidth,
33
+ height: canvas.clientHeight,
30
34
  top: 0,
31
35
  left: 0,
32
- ...t == null ? void 0 : t.size
36
+ ...renderProps == null ? void 0 : renderProps.size
33
37
  }
34
38
  });
35
- const i = h.get(o).store;
36
- e.current.map = n, e.current.root = u, e.current.state = i.getState(), i.setState({ r3m: e.current }), a === "demand" && i.setState({
37
- frameloop: a,
38
- invalidate: () => {
39
- n.triggerRepaint();
40
- }
41
- }), setTimeout(() => m(!0));
42
- }), l = c(() => {
43
- setTimeout(() => {
44
- e.current.root && e.current.root.unmount();
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
- return { onAdd: f, onRemove: l, mounted: r, r3mRef: e };
60
+ useEffect(() => () => onRemove(), [onRemove]);
61
+ return { onAdd, onRemove, mounted, r3mRef };
48
62
  }
49
63
  export {
50
- T as useOnAdd
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 setTimeout(() => {\n if (!r3mRef.current.root) return;\n r3mRef.current.root.unmount();\n })\n })\n\n return { onAdd, onRemove, mounted, r3mRef };\n}"],"names":["useOnAdd","fromLngLat","frameloop","renderProps","mounted","setMounted","useState","r3mRef","useRef","onAdd","useFunction","map","gl","canvas","root","createRoot","createEvents","state","store","_roots","onRemove"],"mappings":";;;;AAOO,SAASA,EACdC,GACA,EAAE,WAAAC,GAAW,GAAGC,KAChB;AAEA,QAAM,CAACC,GAASC,CAAU,IAAIC,EAAS,EAAK,GAEtCC,IAASC,EAAiB,EAAE,YAAAP,EAAY,CAAA,GAExCQ,IAAQC,EAAY,CAACC,GAAkBC,MAA8B;AAEnE,UAAAC,IAASF,EAAI,aACbG,IAAOC,EAAWF,CAAM;AAC9B,IAAAC,EAAK,UAAU;AAAA,MACb,KAAK,OAAO;AAAA,MACZ,QAAQE,EAAa;AAAA,MACrB,GAAGb;AAAA,MACH,WAAW;AAAA,MACX,IAAI;AAAA,QACF,SAASS;AAAA,QACT,OAAO;AAAA,QACP,WAAW;AAAA,QACX,WAAW;AAAA,QACX,GAAGT,KAAA,gBAAAA,EAAa;AAAA,MAClB;AAAA,MACA,WAAW,CAACc,MAAU;AAEd,QAAAA,EAAA,GAAG,mBAAmB,MAAM;AAAA,QAAA;AAAA,MACpC;AAAA,MACA,QAAQ;AAAA,QACN,kBAAkB;AAAA,MACpB;AAAA,MACA,MAAM;AAAA,QACJ,OAAOJ,EAAO;AAAA,QACd,QAAQA,EAAO;AAAA,QACf,KAAK;AAAA,QACL,MAAM;AAAA,QACN,GAAGV,KAAA,gBAAAA,EAAa;AAAA,MAClB;AAAA,IAAA,CACD;AAGD,UAAMe,IAAQC,EAAO,IAAIN,CAAM,EAAG;AAElC,IAAAN,EAAO,QAAQ,MAAMI,GACrBJ,EAAO,QAAQ,OAAOO,GACfP,EAAA,QAAQ,QAAQW,EAAM,SAAS,GAGtCA,EAAM,SAAS,EAAE,KAAKX,EAAO,QAAgB,CAAA,GAEzCL,MAAc,YAChBgB,EAAM,SAAS;AAAA,MACb,WAAAhB;AAAA,MACA,YAAY,MAAM;AAChB,QAAAS,EAAI,eAAe;AAAA,MACrB;AAAA,IAAA,CACD,GAGQ,WAAA,MAAMN,EAAW,EAAI,CAAC;AAAA,EAAA,CAElC,GAEKe,IAAWV,EAAY,MAAM;AACjC,eAAW,MAAM;AACX,MAACH,EAAO,QAAQ,QACbA,EAAA,QAAQ,KAAK;IAAQ,CAC7B;AAAA,EAAA,CACF;AAED,SAAO,EAAE,OAAAE,GAAO,UAAAW,GAAU,SAAAhB,GAAS,QAAAG,EAAO;AAC5C;"}
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 as n, useCallback as u } from "react";
2
- const o = (r) => {
3
- const e = n(r);
4
- return e.current = r, u((...t) => e.current(...t), []);
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
- o as useFunction
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":["useFunction","callback","callbackRef","useRef","useCallback","args"],"mappings":";AAGa,MAAAA,IAAc,CAAoCC,MAAmB;AAC1E,QAAAC,IAAcC,EAAOF,CAAQ;AACnC,SAAAC,EAAY,UAAUD,GACfG,EAAY,IAAIC,MACdH,EAAY,QAAQ,GAAGG,CAAI,GACjC,CAAE,CAAA;AACP;"}
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;"}