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
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "example-maplibre",
3
+ "version": "0.0.0",
4
+ "license": "MIT",
5
+ "scripts": {
6
+ "dev": "yarn ladle serve",
7
+ "ts": "tsc -w"
8
+ },
9
+ "dependencies": {
10
+ "@ladle/react": "^2.15.0",
11
+ "@react-three/drei": "^9.77.10",
12
+ "@react-three/fiber": "^8.13.4",
13
+ "leva": "^0.9.35",
14
+ "maplibre-gl": "^3.1.0",
15
+ "react": "^18.2.0",
16
+ "react-dom": "^18.2.0",
17
+ "react-map-gl": "^7.1.0",
18
+ "react-three-map": "^0.2.1",
19
+ "three": "~0.153.0",
20
+ "three-stdlib": "^2.23.10"
21
+ },
22
+ "devDependencies": {
23
+ "@types/react": "^18.2.14",
24
+ "@types/react-dom": "^18.2.6",
25
+ "@types/three": "^0.152.1",
26
+ "typescript": "^5.1.6",
27
+ "vite": "^4.3.9"
28
+ }
29
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "port": 61000
3
+ }
@@ -0,0 +1,61 @@
1
+ import { useFrame, Vector3 } from "@react-three/fiber";
2
+ import 'maplibre-gl/dist/maplibre-gl.css';
3
+ import { FC, useRef, useState } from "react";
4
+ import Map from 'react-map-gl/maplibre';
5
+ import { Canvas } from "react-three-map/maplibre";
6
+ import { Mesh } from "three";
7
+
8
+ export default { title: 'Canvas' }
9
+
10
+ export function BasicExample() {
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
+
36
+ const Box: FC<{ position: Vector3 }> = (props) => {
37
+ // This reference gives us direct access to the THREE.Mesh object
38
+ const ref = useRef<Mesh>(null)
39
+ // Hold state for hovered and clicked events
40
+ const [hovered, hover] = useState(false)
41
+ const [clicked, click] = useState(false)
42
+ // Subscribe this component to the render-loop, rotate the mesh every frame
43
+ useFrame((_state, delta) => {
44
+ if (!ref.current) return;
45
+ ref.current.rotation.x += delta;
46
+ ref.current.rotation.z -= delta;
47
+ })
48
+ // Return the view, these are regular Threejs elements expressed in JSX
49
+ return (
50
+ <mesh
51
+ {...props}
52
+ ref={ref}
53
+ scale={clicked ? 1.5 : 1}
54
+ onClick={() => click(!clicked)}
55
+ onPointerOver={() => hover(true)}
56
+ onPointerOut={() => hover(false)}>
57
+ <boxGeometry args={[1, 1, 1]} />
58
+ <meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
59
+ </mesh>
60
+ )
61
+ }
@@ -0,0 +1,22 @@
1
+ import { MapControls } from "@react-three/drei";
2
+ import { Canvas as FiberCanvas } from "@react-three/fiber";
3
+ import { Canvas } from "react-three-map/maplibre";
4
+ import { MyScene } from "./my-scene";
5
+ import { StoryMap } from "./story-map";
6
+
7
+ export function WithMap() {
8
+ return <StoryMap latitude={51.5073218} longitude={-0.1276473} zoom={18}>
9
+ <Canvas latitude={51.5073218} longitude={-0.1276473} shadows="variance">
10
+ <MyScene />
11
+ </Canvas>
12
+ </StoryMap>
13
+ }
14
+
15
+ export const WithoutMap = () => {
16
+ return <div style={{ height: '100vh' }}>
17
+ <FiberCanvas camera={{ position: [100, 100, 100] }} shadows="variance">
18
+ <MyScene />
19
+ <MapControls makeDefault />
20
+ </FiberCanvas>
21
+ </div>
22
+ }
@@ -0,0 +1,24 @@
1
+ import { Box, Html } from "@react-three/drei";
2
+ import { useState } from "react";
3
+ import { Canvas } from "react-three-map/maplibre";
4
+ import { MathUtils } from "three";
5
+ import { StoryMap } from "./story-map";
6
+
7
+ export function Default() {
8
+
9
+ const [hovered, hover] = useState(false);
10
+
11
+ return <StoryMap latitude={51} longitude={0} zoom={13}>
12
+ <Canvas latitude={51} longitude={0} frameloop="demand">
13
+ <Html>ola</Html>
14
+ <Box
15
+ args={[500, 500, 500]}
16
+ position={[0, 250, 0]}
17
+ rotation={[0, 45 * MathUtils.DEG2RAD, 0]}
18
+ onPointerOver={() => hover(true)}
19
+ onPointerOut={() => hover(false)}
20
+ material-color={hovered ? 'purple' : 'orange'}
21
+ />
22
+ </Canvas>
23
+ </StoryMap>
24
+ }
@@ -0,0 +1,97 @@
1
+ import { Box, Plane, useHelper } from "@react-three/drei";
2
+ import { MeshProps, useFrame, useThree } from '@react-three/fiber';
3
+ import { useControls } from "leva";
4
+ import 'maplibre-gl/dist/maplibre-gl.css';
5
+ import { useCallback, useRef, useState } from 'react';
6
+ import { CameraHelper, MathUtils, Mesh, OrthographicCamera } from "three";
7
+
8
+ export function MyScene() {
9
+ return <>
10
+ <Lights />
11
+ <Floor />
12
+ <MyBox position={[-8 * 3, 8 * 1.5, 0]} />
13
+ <MyBox position={[8 * 3, 8 * 1.5, 0]} />
14
+ </>
15
+ }
16
+
17
+
18
+ function MyBox(props: MeshProps) {
19
+ const [hovered, hover] = useState(false);
20
+ const mesh = useRef<Mesh>(null)
21
+ const invalidate = useThree(st => st.invalidate);
22
+
23
+ const onOver = useCallback(() => {
24
+ hover(true);
25
+ }, [])
26
+
27
+ const onOut = useCallback(() => {
28
+ hover(false);
29
+ }, [])
30
+
31
+ useFrame((_st, dt) => {
32
+ if (!mesh.current) return;
33
+ mesh.current.rotateY(dt);
34
+ invalidate();
35
+ })
36
+
37
+ return (
38
+ <Box
39
+ {...props}
40
+ ref={mesh}
41
+ args={[16, 16, 16]}
42
+ receiveShadow
43
+ castShadow
44
+ onClick={onOver}
45
+ onPointerOver={onOver}
46
+ onPointerOut={onOut}
47
+ >
48
+ <meshStandardMaterial
49
+ color={hovered ? "red" : "orange"}
50
+ metalness={.75}
51
+ roughness={.5}
52
+ />
53
+ </Box>
54
+ );
55
+ }
56
+ function Lights() {
57
+ const cam = useRef<OrthographicCamera>(null);
58
+ const noCam = useRef<OrthographicCamera>(null);
59
+ const { showCamHelper } = useControls({
60
+ showCamHelper: {
61
+ value: false,
62
+ label: 'show camera helper'
63
+ }
64
+ });
65
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
66
+ useHelper((showCamHelper ? cam : noCam) as any, CameraHelper)
67
+ const camSize = 100;
68
+ return <>
69
+ <ambientLight intensity={0.5} />
70
+ <directionalLight
71
+ castShadow
72
+ position={[2.5, 50, 5]}
73
+ intensity={1.5}
74
+ shadow-mapSize={1024}
75
+ >
76
+ <orthographicCamera
77
+ ref={cam}
78
+ attach="shadow-camera"
79
+ args={[-camSize, camSize, -camSize, camSize, 0.1, 100]}
80
+ rotation={[0, 90 * MathUtils.DEG2RAD, 0]}
81
+ />
82
+ </directionalLight>
83
+ <pointLight position={[-10, 0, -20]} color="white" intensity={1} />
84
+ <pointLight position={[0, -10, 0]} intensity={1} />
85
+ </>
86
+ }
87
+
88
+ function Floor() {
89
+ return <Plane
90
+ args={[200, 200]}
91
+ position={[0, 0, 0]}
92
+ rotation={[-90 * MathUtils.DEG2RAD, 0, 0]}
93
+ receiveShadow
94
+ >
95
+ <shadowMaterial opacity={.5} />
96
+ </Plane>
97
+ }
@@ -0,0 +1,32 @@
1
+ import { Box, Stats } from "@react-three/drei";
2
+ import { useRef, useState } from "react";
3
+ import { Canvas } from "react-three-map/maplibre";
4
+ import { MathUtils } from "three";
5
+ import { StoryMap } from "./story-map";
6
+
7
+ export function Default() {
8
+
9
+ const [hovered, hover] = useState(false);
10
+
11
+ const ref = useRef<HTMLDivElement>(null)
12
+
13
+ return <div ref={ref} style={{ height: '100vh', position: 'relative' }}>
14
+ <style>{`.stats{position:absolute !important}`}</style>
15
+ <StoryMap latitude={51} longitude={0} zoom={13}>
16
+ <Canvas latitude={51} longitude={0} frameloop="demand">
17
+ <Box
18
+ args={[500, 500, 500]}
19
+ position={[0, 250, 0]}
20
+ rotation={[0, 45 * MathUtils.DEG2RAD, 0]}
21
+ onPointerOver={() => hover(true)}
22
+ onPointerOut={() => hover(false)}
23
+ material-color={hovered ? 'purple' : 'orange'}
24
+ />
25
+ <Stats className="stats" parent={ref} />
26
+ </Canvas>
27
+ </StoryMap>
28
+ <div style={{ position: 'absolute', bottom: 0, right: 0, left: 0, background: '#ffffffc2', padding: '15px 20px 40px' }}>
29
+ Hover over the box, it will only render once to change colour, or when you move the camera. Look at the stats to confirm.
30
+ </div>
31
+ </div>
32
+ }
@@ -0,0 +1,38 @@
1
+ import { ThemeState, useLadleContext } from '@ladle/react';
2
+ import 'maplibre-gl/dist/maplibre-gl.css';
3
+ import { FC, PropsWithChildren } from "react";
4
+ import Map from 'react-map-gl/maplibre';
5
+
6
+ export interface StoryMapProps extends PropsWithChildren {
7
+ latitude: number,
8
+ longitude: number,
9
+ zoom?: number,
10
+ pitch?: number,
11
+ }
12
+
13
+ /** `<Map>` styled for stories */
14
+ export const StoryMap: FC<StoryMapProps> = ({
15
+ latitude, longitude, zoom, children
16
+ }) => {
17
+
18
+ const theme = useLadleContext().globalState.theme;
19
+
20
+ const mapStyle = theme === ThemeState.Dark
21
+ ? "https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json"
22
+ : "https://basemaps.cartocdn.com/gl/positron-gl-style/style.json";
23
+
24
+ return <div style={{ height: '100vh', position: 'relative' }}>
25
+ <Map
26
+ antialias
27
+ initialViewState={{
28
+ latitude,
29
+ longitude,
30
+ zoom,
31
+ pitch: 60,
32
+ }}
33
+ mapStyle={mapStyle}
34
+ >
35
+ {children}
36
+ </Map>
37
+ </div>
38
+ }
@@ -0,0 +1 @@
1
+ /// <reference types="vite/client" />
@@ -0,0 +1,25 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "useDefineForClassFields": true,
5
+ "lib": ["ES2020", "DOM", "DOM.Iterable"],
6
+ "module": "ESNext",
7
+ "skipLibCheck": true,
8
+
9
+ /* Bundler mode */
10
+ "moduleResolution": "Node",
11
+ "allowImportingTsExtensions": true,
12
+ "resolveJsonModule": true,
13
+ "isolatedModules": true,
14
+ "noEmit": true,
15
+ "jsx": "react-jsx",
16
+
17
+ /* Linting */
18
+ "strict": true,
19
+ "noUnusedLocals": true,
20
+ "noUnusedParameters": true,
21
+ "noFallthroughCasesInSwitch": true
22
+ },
23
+ "include": ["src"],
24
+ "references": [{ "path": "./tsconfig.node.json" }]
25
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "compilerOptions": {
3
+ "composite": true,
4
+ "skipLibCheck": true,
5
+ "module": "ESNext",
6
+ "moduleResolution": "bundler",
7
+ "allowSyntheticDefaultImports": true
8
+ },
9
+ "include": ["vite.config.ts"]
10
+ }