react-three-map 0.2.1 → 0.3.0

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 (83) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +14 -13
  3. package/dist/cjs/main.js.js +1 -1318
  4. package/dist/es/main.js2.mjs +24 -20
  5. package/dist/es/main.js2.mjs.map +1 -1
  6. package/dist/es/main.js3.mjs.map +1 -1
  7. package/dist/es/main.js4.mjs +12 -24
  8. package/dist/es/main.js4.mjs.map +1 -1
  9. package/dist/es/main.js5.mjs +55 -23532
  10. package/dist/es/main.js5.mjs.map +1 -1
  11. package/dist/es/main.js6.mjs +13 -2
  12. package/dist/es/main.js6.mjs.map +1 -1
  13. package/dist/es/main.js7.mjs +53 -2
  14. package/dist/es/main.js7.mjs.map +1 -1
  15. package/dist/es/main.js8.mjs +6 -57
  16. package/dist/es/main.js8.mjs.map +1 -1
  17. package/dist/maplibre/cjs/main.js.js +1 -1
  18. package/dist/maplibre/es/main.js2.mjs +24 -20
  19. package/dist/maplibre/es/main.js2.mjs.map +1 -1
  20. package/dist/maplibre/es/main.js3.mjs.map +1 -1
  21. package/dist/maplibre/es/main.js4.mjs +12 -24
  22. package/dist/maplibre/es/main.js4.mjs.map +1 -1
  23. package/dist/maplibre/es/main.js5.mjs.map +1 -1
  24. package/dist/maplibre/es/main.js6.mjs.map +1 -1
  25. package/dist/maplibre/es/main.js7.mjs.map +1 -1
  26. package/dist/maplibre/es/main.js8.mjs.map +1 -1
  27. package/example-mapbox/package.json +29 -0
  28. package/example-mapbox/sandbox.config.json +3 -0
  29. package/example-mapbox/src/canvas.basic.stories.tsx +69 -0
  30. package/example-mapbox/src/comparison.stories.tsx +24 -0
  31. package/example-mapbox/src/html-on-top.stories.tsx +24 -0
  32. package/example-mapbox/src/my-scene.tsx +96 -0
  33. package/example-mapbox/src/render-on-demand.stories.tsx +32 -0
  34. package/example-mapbox/src/story-map.tsx +46 -0
  35. package/example-mapbox/src/vite-env.d.ts +1 -0
  36. package/example-mapbox/tsconfig.json +27 -0
  37. package/example-mapbox/tsconfig.node.json +10 -0
  38. package/example-mapbox/yarn.lock +4154 -0
  39. package/example-maplibre/package.json +29 -0
  40. package/example-maplibre/sandbox.config.json +3 -0
  41. package/example-maplibre/src/canvas.basic.stories.tsx +61 -0
  42. package/example-maplibre/src/comparison.stories.tsx +22 -0
  43. package/example-maplibre/src/html-on-top.stories.tsx +24 -0
  44. package/example-maplibre/src/my-scene.tsx +97 -0
  45. package/example-maplibre/src/render-on-demand.stories.tsx +32 -0
  46. package/example-maplibre/src/story-map.tsx +38 -0
  47. package/example-maplibre/src/vite-env.d.ts +1 -0
  48. package/example-maplibre/tsconfig.json +25 -0
  49. package/example-maplibre/tsconfig.node.json +10 -0
  50. package/example-maplibre/yarn.lock +4181 -0
  51. package/maplibre/package.json +1 -1
  52. package/package.json +9 -6
  53. package/src/{canvas/core → core}/generic-map.ts +15 -2
  54. package/src/{canvas/core → core}/state-ref.ts +2 -2
  55. package/src/{canvas/core/internal-canvas.tsx → core/use-canvas.tsx} +6 -18
  56. package/src/{canvas/core → core}/use-on-add.ts +3 -3
  57. package/src/{canvas/canvas-mapbox.tsx → mapbox/canvas.tsx} +15 -9
  58. package/src/mapbox/index.ts +1 -0
  59. package/src/{canvas/canvas-maplibre.tsx → maplibre/canvas.tsx} +13 -7
  60. package/src/maplibre/index.ts +1 -0
  61. package/src/stories/comparison.stories.tsx +13 -30
  62. package/src/stories/html-on-top.stories.tsx +11 -16
  63. package/src/stories/mapbox/story-mapbox.tsx +43 -0
  64. package/src/stories/mapbox-example.stories.tsx +69 -0
  65. package/src/stories/maplibre/story-maplibre.tsx +32 -0
  66. package/src/stories/{canvas.basic.stories.tsx → maplibre-example.stories.tsx} +28 -30
  67. package/src/stories/my-scene.tsx +0 -1
  68. package/src/stories/render-on-demand.stories.tsx +18 -37
  69. package/src/stories/story-map.tsx +26 -24
  70. package/tsconfig.json +5 -0
  71. package/dist/es/main.js10.mjs +0 -56
  72. package/dist/es/main.js10.mjs.map +0 -1
  73. package/dist/es/main.js11.mjs +0 -9
  74. package/dist/es/main.js11.mjs.map +0 -1
  75. package/dist/es/main.js9.mjs +0 -16
  76. package/dist/es/main.js9.mjs.map +0 -1
  77. package/src/main-mapbox.ts +0 -1
  78. package/src/main-maplibre.ts +0 -1
  79. /package/src/{canvas/core → core}/coords-to-matrix.ts +0 -0
  80. /package/src/{canvas/core → core}/create-events.ts +0 -0
  81. /package/src/{canvas/core → core}/generic-map.test.ts +0 -0
  82. /package/src/{canvas/core → core}/use-function.ts +0 -0
  83. /package/src/{canvas/core → core}/use-render.ts +0 -0
@@ -2,6 +2,6 @@
2
2
  "internal": true,
3
3
  "main": "../dist/maplibre/cjs/main.js",
4
4
  "module": "../dist/maplibre/es/main.js",
5
- "types": "../src/main-maplibre.ts",
5
+ "types": "../src/maplibre/index.ts",
6
6
  "sideEffects": false
7
7
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "react-three-map",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Use react-three-fiber inside MapLibre and Mapbox",
5
5
  "main": "dist/cjs/main.js",
6
6
  "module": "dist/es/main.js",
7
- "types": "src/main-mapbox.ts",
7
+ "types": "src/mapbox/index.ts",
8
8
  "sideEffects": false,
9
9
  "repository": "https://github.com/RodrigoHamuy/react-three-map",
10
10
  "homepage": "https://github.com/RodrigoHamuy/react-three-map",
@@ -14,7 +14,6 @@
14
14
  "license": "MIT",
15
15
  "author": "RodrigoHamuy",
16
16
  "scripts": {
17
- "dev": "vite",
18
17
  "prebuild": "tsc",
19
18
  "build": "yarn build:maplibre && yarn build:mapbox",
20
19
  "build:maplibre": "cross-env LIB_MODE=1 MAP_MODE=0 vite build",
@@ -22,7 +21,7 @@
22
21
  "build:mapbox": "cross-env LIB_MODE=1 MAP_MODE=1 vite build",
23
22
  "postbuild:mapbox": "cross-env LIB_MODE=2 MAP_MODE=1 vite build",
24
23
  "release": "yarn build && yarn changeset publish",
25
- "lint": "eslint src --ext ts,tsx --fix",
24
+ "lint": "eslint src example-maplibre/src example-mapbox/src --ext ts,tsx --fix",
26
25
  "preview": "vite preview",
27
26
  "ts": "tsc -w",
28
27
  "stories": "yarn ladle serve",
@@ -33,7 +32,7 @@
33
32
  "@changesets/cli": "^2.26.1",
34
33
  "@ladle/react": "^2.13.0",
35
34
  "@react-three/drei": "^9.74.5",
36
- "@react-three/fiber": "^8.13.3",
35
+ "@react-three/fiber": "^8.13.4",
37
36
  "@types/node": "^20.3.1",
38
37
  "@types/react": "^18.0.37",
39
38
  "@types/react-dom": "^18.0.11",
@@ -50,10 +49,14 @@
50
49
  "maplibre-gl": "^3.1.0",
51
50
  "react": "^18.2.0",
52
51
  "react-dom": "^18.2.0",
53
- "react-map-gl": "^7.0.25",
52
+ "react-map-gl": "^7.1.0",
54
53
  "three": "^0.153.0",
55
54
  "three-stdlib": "^2.23.8",
56
55
  "typescript": "~5.0.2",
57
56
  "vite": "^4.3.9"
57
+ },
58
+ "peerDependencies": {
59
+ "@react-three/fiber": "^8.13.4",
60
+ "react-map-gl": "^7.1.0"
58
61
  }
59
62
  }
@@ -1,5 +1,16 @@
1
1
  // mock of functions used by `react-three-map` from `Maplibre` or `Mapbox`
2
2
 
3
+ /** Generic interface of Mapbox/Maplibre `LayerProps` */
4
+ export interface LayerProps {
5
+ id: string;
6
+ type: 'custom';
7
+ renderingMode: '3d';
8
+ onRemove?(map: MapInstance, gl: WebGLRenderingContext): void;
9
+ onAdd?(map: MapInstance, gl: WebGLRenderingContext): void;
10
+ prerender?(gl: WebGLRenderingContext, matrix: number[]): void;
11
+ render(gl: WebGLRenderingContext, matrix: number[]): void;
12
+ }
13
+
3
14
  /** Generic interface of Mapbox/Maplibre `LngLatLike` */
4
15
  export type LngLatLike = {
5
16
  lng: number;
@@ -24,10 +35,12 @@ export interface MercatorCoordinate {
24
35
  }
25
36
 
26
37
  /** Generic interface of Mapbox/Maplibre `Map` */
27
- export interface Map {
38
+ export interface MapInstance {
28
39
  getCanvas(): HTMLCanvasElement;
29
40
  triggerRepaint(): void;
41
+ // eslint-disable-next-line @typescript-eslint/ban-types
30
42
  on<T extends keyof MapEventType>(type: T, listener: (ev: MapEventType[T] & Object) => void): this;
43
+ // eslint-disable-next-line @typescript-eslint/ban-types
31
44
  off<T extends keyof MapEventType>(type: T, listener: (ev: MapEventType[T] & Object) => void): this;
32
45
  }
33
46
 
@@ -39,6 +52,6 @@ export type MapEventType = {
39
52
  /** Generic interface of `MapLibreEvent` or `MapBoxEvent` */
40
53
  export interface MapEvent<TOrig = unknown> {
41
54
  type: string;
42
- target: Map;
55
+ target: MapInstance;
43
56
  originalEvent: TOrig;
44
57
  }
@@ -1,9 +1,9 @@
1
1
  import { ReconcilerRoot, RootState } from "@react-three/fiber";
2
2
  import { MutableRefObject } from "react";
3
- import { Map } from "./generic-map";
3
+ import { MapInstance } from "./generic-map";
4
4
 
5
5
  export type StateRef = MutableRefObject<{
6
- map: Map;
6
+ map: MapInstance;
7
7
  root: ReconcilerRoot<HTMLCanvasElement>;
8
8
  state?: RootState;
9
9
  } | undefined>
@@ -1,19 +1,18 @@
1
1
  import { RenderProps } from "@react-three/fiber";
2
- import { PropsWithChildren, memo, useEffect, useId, useRef } from "react";
3
- import { Layer } from "react-map-gl";
2
+ import { PropsWithChildren, useEffect, useId, useRef } from "react";
4
3
  import { Matrix4Tuple } from "three";
5
4
  import { StateRef } from "./state-ref";
6
5
  import { useOnAdd } from "./use-on-add";
7
6
  import { useRender } from "./use-render";
8
7
 
9
- export interface InternalCanvasProps extends Omit<RenderProps<HTMLCanvasElement>, 'frameloop'>, PropsWithChildren {
8
+ export interface useCanvasProps extends Omit<RenderProps<HTMLCanvasElement>, 'frameloop'>, PropsWithChildren {
10
9
  frameloop: 'always' | 'demand',
11
10
  m4: Matrix4Tuple;
12
11
  }
13
12
 
14
- export const InternalCanvas = memo<InternalCanvasProps>(({
13
+ export const useCanvas = (({
15
14
  m4, children, frameloop, ...renderProps
16
- }) => {
15
+ }: useCanvasProps) => {
17
16
  const id = useId();
18
17
 
19
18
  const stateRef: StateRef = useRef();
@@ -28,16 +27,5 @@ export const InternalCanvas = memo<InternalCanvasProps>(({
28
27
  stateRef.current.root.render(<>{children}</>);
29
28
  }, [stateRef, mounted, children])
30
29
 
31
- return <Layer
32
- id={id}
33
- type="custom"
34
- renderingMode="3d"
35
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
- onAdd={onAdd as any}
37
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
- onRemove={onRemove as any}
39
- render={render}
40
- />
41
- })
42
-
43
- InternalCanvas.displayName = 'InternalCanvas';
30
+ return { id, onAdd, onRemove, render }
31
+ })
@@ -1,15 +1,15 @@
1
1
  import { RenderProps, createRoot } from "@react-three/fiber";
2
- import { Map } from "./generic-map";
3
2
  import { useState } from "react";
4
3
  import { createEvents } from "./create-events";
5
4
  import { StateRef } from "./state-ref";
6
5
  import { useFunction } from "./use-function";
6
+ import { MapInstance } from "./generic-map";
7
7
 
8
8
  export function useOnAdd(ref: StateRef, { frameloop, ...renderProps }: RenderProps<HTMLCanvasElement>) {
9
9
 
10
10
  const [mounted, setMounted] = useState(false);
11
11
 
12
- const onAdd = useFunction((map: Map, gl: WebGLRenderingContext) => {
12
+ const onAdd = useFunction((map: MapInstance, gl: WebGLRenderingContext) => {
13
13
 
14
14
  const canvas = map.getCanvas();
15
15
  const root = createRoot(canvas);
@@ -78,7 +78,7 @@ export function useOnAdd(ref: StateRef, { frameloop, ...renderProps }: RenderPro
78
78
  state.setSize(canvas.width, canvas.height);
79
79
  })
80
80
 
81
- const onRemove = useFunction((map: Map) => {
81
+ const onRemove = useFunction((map: MapInstance) => {
82
82
  setTimeout(() => {
83
83
  if (!ref.current) return;
84
84
  ref.current.root.unmount();
@@ -1,9 +1,10 @@
1
1
  import { RenderProps, extend } from "@react-three/fiber";
2
+ import { MercatorCoordinate } from "mapbox-gl";
2
3
  import { PropsWithChildren, memo, useMemo } from "react";
4
+ import { Layer } from "react-map-gl";
3
5
  import * as THREE from "three";
4
- import { coordsToMatrix } from "./core/coords-to-matrix";
5
- import { InternalCanvas } from "./core/internal-canvas";
6
- import { MercatorCoordinate } from "mapbox-gl";
6
+ import { coordsToMatrix } from "../core/coords-to-matrix";
7
+ import { useCanvas } from "../core/use-canvas";
7
8
 
8
9
  extend(THREE);
9
10
 
@@ -14,7 +15,7 @@ export interface CanvasProps extends Omit<RenderProps<HTMLCanvasElement>, 'frame
14
15
  frameloop?: 'always' | 'demand',
15
16
  }
16
17
 
17
- /** react`-three-fiber` canvas inside `Mapbox` */
18
+ /** react`-three-fiber` canvas inside `MapLibre` */
18
19
  export const Canvas = memo<CanvasProps>(({
19
20
  longitude, latitude, altitude = 0,
20
21
  frameloop = 'always', ...renderProps
@@ -22,11 +23,16 @@ export const Canvas = memo<CanvasProps>(({
22
23
 
23
24
  const m4 = useMemo(() => coordsToMatrix({
24
25
  latitude, longitude, altitude, fromLngLat: MercatorCoordinate.fromLngLat,
25
- }), [latitude, longitude, altitude])
26
+ }), [latitude, longitude, altitude]);
27
+
28
+ const { id, onAdd, onRemove, render } = useCanvas({ m4, frameloop, ...renderProps });
26
29
 
27
- return <InternalCanvas
28
- frameloop={frameloop}
29
- m4={m4}
30
- {...renderProps}
30
+ return <Layer
31
+ id={id}
32
+ type="custom"
33
+ renderingMode="3d"
34
+ onAdd={onAdd}
35
+ onRemove={onRemove}
36
+ render={render}
31
37
  />
32
38
  })
@@ -0,0 +1 @@
1
+ export * from './canvas';
@@ -1,9 +1,10 @@
1
1
  import { RenderProps, extend } from "@react-three/fiber";
2
2
  import { MercatorCoordinate } from "maplibre-gl";
3
3
  import { PropsWithChildren, memo, useMemo } from "react";
4
+ import { Layer } from "react-map-gl/maplibre";
4
5
  import * as THREE from "three";
5
- import { coordsToMatrix } from "./core/coords-to-matrix";
6
- import { InternalCanvas } from "./core/internal-canvas";
6
+ import { coordsToMatrix } from "../core/coords-to-matrix";
7
+ import { useCanvas } from "../core/use-canvas";
7
8
 
8
9
  extend(THREE);
9
10
 
@@ -22,11 +23,16 @@ export const Canvas = memo<CanvasProps>(({
22
23
 
23
24
  const m4 = useMemo(() => coordsToMatrix({
24
25
  latitude, longitude, altitude, fromLngLat: MercatorCoordinate.fromLngLat,
25
- }), [latitude, longitude, altitude])
26
+ }), [latitude, longitude, altitude]);
26
27
 
27
- return <InternalCanvas
28
- frameloop={frameloop}
29
- m4={m4}
30
- {...renderProps}
28
+ const { id, onAdd, onRemove, render } = useCanvas({ m4, frameloop, ...renderProps });
29
+
30
+ return <Layer
31
+ id={id}
32
+ type="custom"
33
+ renderingMode="3d"
34
+ onAdd={onAdd}
35
+ onRemove={onRemove}
36
+ render={render}
31
37
  />
32
38
  })
@@ -0,0 +1 @@
1
+ export * from './canvas';
@@ -1,40 +1,23 @@
1
- import { ThemeState, useLadleContext } from "@ladle/react";
2
- import 'maplibre-gl/dist/maplibre-gl.css';
3
- import Map from 'react-map-gl';
4
- import { Canvas } from "../canvas/canvas-maplibre";
5
- import { Canvas as FiberCanvas } from "@react-three/fiber"
1
+ import { MapControls } from "@react-three/drei";
2
+ import { Canvas as FiberCanvas } from "@react-three/fiber";
6
3
  import { MyScene } from "./my-scene";
7
- import { MapControls } from "@react-three/drei"
8
- import MapLibre from "maplibre-gl";
4
+ import { StoryMap } from "./story-map";
9
5
 
10
6
  export function WithMap() {
11
- const theme = useLadleContext().globalState.theme;
12
- const mapStyle = theme === ThemeState.Dark
13
- ? "https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json"
14
- : "https://basemaps.cartocdn.com/gl/positron-gl-style/style.json"
15
- return <div style={{ height: '100vh' }}>
16
- <Map
17
- mapLib={MapLibre}
18
- antialias
19
- initialViewState={{
20
- latitude: 51.5073218,
21
- longitude: -0.1276473,
22
- zoom: 18,
23
- pitch: 60,
24
- }}
25
- mapStyle={mapStyle}
26
- >
27
- <Canvas latitude={51.5073218} longitude={-0.1276473} shadows="variance">
28
- <MyScene />
29
- </Canvas>
30
- </Map>
31
- </div>
7
+ return <StoryMap
8
+ latitude={51.5073218}
9
+ longitude={-0.1276473}
10
+ zoom={18}
11
+ canvas={{ shadows: 'variance' }}
12
+ >
13
+ <MyScene />
14
+ </StoryMap>
32
15
  }
33
16
 
34
17
  export const WithoutMap = () => {
35
18
  return <div style={{ height: '100vh' }}>
36
- <FiberCanvas camera={{position: [100,100,100]}} shadows="variance">
37
- <MyScene />
19
+ <FiberCanvas camera={{ position: [100, 100, 100] }} shadows="variance">
20
+ <MyScene />
38
21
  <MapControls makeDefault />
39
22
  </FiberCanvas>
40
23
  </div>
@@ -1,26 +1,21 @@
1
- import { Box, Html, Stats } from "@react-three/drei";
2
- import 'maplibre-gl/dist/maplibre-gl.css';
1
+ import { Box, Html } from "@react-three/drei";
3
2
  import { useState } from "react";
4
3
  import { MathUtils } from "three";
5
- import { Canvas } from "../canvas/canvas-maplibre";
6
4
  import { StoryMap } from "./story-map";
7
5
 
8
6
  export function Default() {
9
7
 
10
8
  const [hovered, hover] = useState(false);
11
9
 
12
- return <StoryMap>
13
- <Canvas latitude={51} longitude={0} frameloop="demand">
14
- <Html>ola</Html>
15
- <Box
16
- args={[500, 500, 500]}
17
- position={[0, 250, 0]}
18
- rotation={[0, 45 * MathUtils.DEG2RAD, 0]}
19
- onPointerOver={() => hover(true)}
20
- onPointerOut={() => hover(false)}
21
- material-color={hovered ? 'purple' : 'orange'}
22
- />
23
- <Stats />
24
- </Canvas>
10
+ return <StoryMap latitude={51} longitude={0}>
11
+ <Html>ola</Html>
12
+ <Box
13
+ args={[500, 500, 500]}
14
+ position={[0, 250, 0]}
15
+ rotation={[0, 45 * MathUtils.DEG2RAD, 0]}
16
+ onPointerOver={() => hover(true)}
17
+ onPointerOut={() => hover(false)}
18
+ material-color={hovered ? 'purple' : 'orange'}
19
+ />
25
20
  </StoryMap>
26
21
  }
@@ -0,0 +1,43 @@
1
+ import { ThemeState, useLadleContext } from '@ladle/react';
2
+ import { useControls } from 'leva';
3
+ import Mapbox from "mapbox-gl";
4
+ import 'mapbox-gl/dist/mapbox-gl.css';
5
+ import { FC } from "react";
6
+ import Map from 'react-map-gl';
7
+ import { StoryMapProps } from '../story-map';
8
+ import { Canvas } from 'react-three-map';
9
+
10
+ /** `<Map>` styled for stories */
11
+ export const StoryMapbox: FC<StoryMapProps> = ({
12
+ latitude, longitude, zoom = 18, pitch = 60, canvas, children
13
+ }) => {
14
+
15
+ const { mapboxToken } = useControls({ mapboxToken: import.meta.env.VITE_MAPBOX_TOKEN || '' })
16
+
17
+ const theme = useLadleContext().globalState.theme;
18
+
19
+ const mapStyle = theme === ThemeState.Dark
20
+ ? "mapbox://styles/mapbox/dark-v11"
21
+ : "mapbox://styles/mapbox/streets-v12";
22
+
23
+ Mapbox.accessToken = mapboxToken;
24
+
25
+ return <div style={{ height: '100vh', position: 'relative' }}>
26
+ {!mapboxToken && <>Add a mapbox token to load this component</>}
27
+ {!!mapboxToken && <Map
28
+ antialias
29
+ initialViewState={{
30
+ latitude,
31
+ longitude,
32
+ zoom,
33
+ pitch,
34
+ }}
35
+ mapStyle={mapStyle}
36
+ mapboxAccessToken={mapboxToken}
37
+ >
38
+ <Canvas latitude={latitude} longitude={longitude} {...canvas}>
39
+ {children}
40
+ </Canvas>
41
+ </Map>}
42
+ </div>
43
+ }
@@ -0,0 +1,69 @@
1
+ import { useFrame, Vector3 } from "@react-three/fiber";
2
+ import { useControls } from "leva";
3
+ import Mapbox from "mapbox-gl";
4
+ import 'mapbox-gl/dist/mapbox-gl.css';
5
+ import { FC, useRef, useState } from "react";
6
+ import Map from 'react-map-gl';
7
+ import { Canvas } from "react-three-map";
8
+ import { Mesh } from "three";
9
+
10
+ export default { title: 'Canvas' }
11
+
12
+ export function MapboxExample() {
13
+
14
+ const { mapboxToken } = useControls({ mapboxToken: import.meta.env.VITE_MAPBOX_TOKEN || '' })
15
+
16
+ Mapbox.accessToken = mapboxToken;
17
+
18
+ return <div style={{ height: '100vh' }}>
19
+ {!mapboxToken && <>Add a mapbox token to load this component</>}
20
+ {!!mapboxToken && <Map
21
+ antialias
22
+ initialViewState={{
23
+ latitude: 51,
24
+ longitude: 0,
25
+ zoom: 13,
26
+ pitch: 60,
27
+ }}
28
+ mapStyle="mapbox://styles/mapbox/streets-v12"
29
+ >
30
+ <Canvas latitude={51} longitude={0}>
31
+ <hemisphereLight
32
+ args={["#ffffff", "#60666C"]}
33
+ position={[1, 4.5, 3]}
34
+ />
35
+ <object3D scale={500}>
36
+ <Box position={[-1.2, 1, 0]} />
37
+ <Box position={[1.2, 1, 0]} />
38
+ </object3D>
39
+ </Canvas>
40
+ </Map>}
41
+ </div>
42
+ }
43
+
44
+ const Box: FC<{ position: Vector3 }> = (props) => {
45
+ // This reference gives us direct access to the THREE.Mesh object
46
+ const ref = useRef<Mesh>(null)
47
+ // Hold state for hovered and clicked events
48
+ const [hovered, hover] = useState(false)
49
+ const [clicked, click] = useState(false)
50
+ // Subscribe this component to the render-loop, rotate the mesh every frame
51
+ useFrame((_state, delta) => {
52
+ if (!ref.current) return;
53
+ ref.current.rotation.x += delta;
54
+ ref.current.rotation.z -= delta;
55
+ })
56
+ // Return the view, these are regular Threejs elements expressed in JSX
57
+ return (
58
+ <mesh
59
+ {...props}
60
+ ref={ref}
61
+ scale={clicked ? 1.5 : 1}
62
+ onClick={() => click(!clicked)}
63
+ onPointerOver={() => hover(true)}
64
+ onPointerOut={() => hover(false)}>
65
+ <boxGeometry args={[1, 1, 1]} />
66
+ <meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
67
+ </mesh>
68
+ )
69
+ }
@@ -0,0 +1,32 @@
1
+ import { ThemeState, useLadleContext } from '@ladle/react';
2
+ import MapLibre from "maplibre-gl";
3
+ import 'maplibre-gl/dist/maplibre-gl.css';
4
+ import { FC } from "react";
5
+ import Map from 'react-map-gl/maplibre';
6
+ import { StoryMapProps } from '../story-map';
7
+ import { Canvas } from 'react-three-map/maplibre';
8
+
9
+ /** Maplibre `<Map>` styled for stories */
10
+ export const StoryMaplibre: FC<StoryMapProps> = ({
11
+ latitude, longitude, zoom, pitch, canvas, children
12
+ }) => {
13
+
14
+ const theme = useLadleContext().globalState.theme;
15
+
16
+ const mapStyle = theme === ThemeState.Dark
17
+ ? "https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json"
18
+ : "https://basemaps.cartocdn.com/gl/positron-gl-style/style.json";
19
+
20
+ return <div style={{ height: '100vh', position: 'relative' }}>
21
+ <Map
22
+ mapLib={MapLibre}
23
+ antialias
24
+ initialViewState={{ latitude, longitude, zoom, pitch, }}
25
+ mapStyle={mapStyle}
26
+ >
27
+ <Canvas latitude={latitude} longitude={longitude} {...canvas}>
28
+ {children}
29
+ </Canvas>
30
+ </Map>
31
+ </div>
32
+ }
@@ -1,13 +1,38 @@
1
1
  import { useFrame, Vector3 } from "@react-three/fiber";
2
2
  import 'maplibre-gl/dist/maplibre-gl.css';
3
3
  import { FC, useRef, useState } from "react";
4
- import Map from 'react-map-gl';
4
+ import Map from 'react-map-gl/maplibre';
5
5
  import { Mesh } from "three";
6
- import { Canvas } from "../canvas/canvas-maplibre";
7
- import MapLibre from "maplibre-gl";
6
+ import { Canvas } from "react-three-map/maplibre";
8
7
 
9
8
  export default {title: 'Canvas'}
10
9
 
10
+ export function AMaplibreExample() {
11
+ return <div style={{ height: '100vh' }}>
12
+ <Map
13
+ antialias
14
+ initialViewState={{
15
+ latitude: 51,
16
+ longitude: 0,
17
+ zoom: 13,
18
+ pitch: 60,
19
+ }}
20
+ mapStyle="https://basemaps.cartocdn.com/gl/positron-gl-style/style.json"
21
+ >
22
+ <Canvas latitude={51} longitude={0}>
23
+ <hemisphereLight
24
+ args={["#ffffff", "#60666C"]}
25
+ position={[1, 4.5, 3]}
26
+ />
27
+ <object3D scale={500}>
28
+ <Box position={[-1.2, 1, 0]} />
29
+ <Box position={[1.2, 1, 0]} />
30
+ </object3D>
31
+ </Canvas>
32
+ </Map>
33
+ </div>
34
+ }
35
+
11
36
  const Box : FC<{position: Vector3}> = (props) => {
12
37
  // This reference gives us direct access to the THREE.Mesh object
13
38
  const ref = useRef<Mesh>(null)
@@ -33,31 +58,4 @@ const Box : FC<{position: Vector3}> = (props) => {
33
58
  <meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
34
59
  </mesh>
35
60
  )
36
- }
37
-
38
- export function BasicExample() {
39
- return <div style={{ height: '100vh' }}>
40
- <Map
41
- mapLib={MapLibre}
42
- antialias
43
- initialViewState={{
44
- latitude: 51,
45
- longitude: 0,
46
- zoom: 13,
47
- pitch: 60,
48
- }}
49
- mapStyle="https://basemaps.cartocdn.com/gl/positron-gl-style/style.json"
50
- >
51
- <Canvas latitude={51} longitude={0}>
52
- <hemisphereLight
53
- args={["#ffffff", "#60666C"]}
54
- position={[1, 4.5, 3]}
55
- />
56
- <object3D scale={500}>
57
- <Box position={[-1.2, 1, 0]} />
58
- <Box position={[1.2, 1, 0]} />
59
- </object3D>
60
- </Canvas>
61
- </Map>
62
- </div>
63
61
  }
@@ -1,7 +1,6 @@
1
1
  import { Box, Plane, useHelper } from "@react-three/drei";
2
2
  import { MeshProps, useFrame, useThree } from '@react-three/fiber';
3
3
  import { useControls } from "leva";
4
- import 'maplibre-gl/dist/maplibre-gl.css';
5
4
  import { useCallback, useRef, useState } from 'react';
6
5
  import { CameraHelper, MathUtils, Mesh, OrthographicCamera } from "three";
7
6
 
@@ -1,47 +1,28 @@
1
- import { ThemeState, useLadleContext } from "@ladle/react";
2
1
  import { Box, Stats } from "@react-three/drei";
3
- import 'maplibre-gl/dist/maplibre-gl.css';
4
- import { useState } from "react";
5
- import Map from 'react-map-gl';
2
+ import { useRef, useState } from "react";
6
3
  import { MathUtils } from "three";
7
- import { Canvas } from "../canvas/canvas-maplibre";
8
- import MapLibre from "maplibre-gl";
4
+ import { StoryMap } from "./story-map";
9
5
 
10
6
  export function Default() {
11
7
 
12
- const theme = useLadleContext().globalState.theme;
13
-
14
- const mapStyle = theme === ThemeState.Dark
15
- ? "https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json"
16
- : "https://basemaps.cartocdn.com/gl/positron-gl-style/style.json";
17
-
18
8
  const [hovered, hover] = useState(false);
9
+
10
+ const ref = useRef<HTMLDivElement>(null)
19
11
 
20
- return <div style={{ height: '100vh', position: 'relative' }}>
21
- <Map
22
- mapLib={MapLibre}
23
- antialias
24
- initialViewState={{
25
- latitude: 51,
26
- longitude: 0,
27
- zoom: 13,
28
- pitch: 60,
29
- }}
30
- mapStyle={mapStyle}
31
- >
32
- <Canvas latitude={51} longitude={0} frameloop="demand">
33
- <Box
34
- args={[500, 500, 500]}
35
- position={[0, 250, 0]}
36
- rotation={[0, 45 * MathUtils.DEG2RAD, 0]}
37
- onPointerOver={() => hover(true)}
38
- onPointerOut={() => hover(false)}
39
- material-color={hovered ? 'purple' : 'orange'}
40
- />
41
- <Stats />
42
- </Canvas>
43
- </Map>
44
- <div style={{ position: 'absolute', top: 0, right: 0, left: 80, background: '#ffffffc2', padding: 5 }}>
12
+ return <div ref={ref} style={{ height: '100vh', position: 'relative' }}>
13
+ <style>{`.stats{position:absolute !important}`}</style>
14
+ <StoryMap latitude={51} longitude={0} canvas={{ frameloop: 'demand' }}>
15
+ <Box
16
+ args={[500, 500, 500]}
17
+ position={[0, 250, 0]}
18
+ rotation={[0, 45 * MathUtils.DEG2RAD, 0]}
19
+ onPointerOver={() => hover(true)}
20
+ onPointerOut={() => hover(false)}
21
+ material-color={hovered ? 'purple' : 'orange'}
22
+ />
23
+ <Stats className="stats" parent={ref} />
24
+ </StoryMap>
25
+ <div style={{ position: 'absolute', bottom: 0, right: 0, left: 0, background: '#ffffffc2', padding: '15px 20px 40px' }}>
45
26
  Hover over the box, it will only render once to change colour, or when you move the camera. Look at the stats to confirm.
46
27
  </div>
47
28
  </div>