@icgcat/toggle3d 0.0.1
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/README.md +131 -0
- package/package.json +23 -0
- package/src/Toggle3d.svelte +140 -0
- package/src/config.js +2 -0
- package/src/index.js +3 -0
- package/vite.config.js +23 -0
package/README.md
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
# 3D Terrain Toggle Component
|
2
|
+
|
3
|
+
This Svelte component provides a button to toggle between 2D and 3D terrain visualization on a MapLibre-based map. The component dynamically adjusts terrain sources based on the map's location and zoom level.
|
4
|
+
|
5
|
+
---
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the component via npm:
|
10
|
+
|
11
|
+
```bash
|
12
|
+
npm install @icgcat/toggle3d
|
13
|
+
```
|
14
|
+
|
15
|
+
---
|
16
|
+
|
17
|
+
## Features
|
18
|
+
|
19
|
+
- **3D Terrain Visualization**: Enable and disable 3D terrain on a MapLibre map.
|
20
|
+
- **Dynamic Terrain Source**: Switches between two terrain sources based on location and zoom.
|
21
|
+
- **ICGC Terrain**: Used for areas within Catalonia.
|
22
|
+
- **MapZen Terrain**: Used for other regions.
|
23
|
+
- **Customizable Options**: Set pitch angle, terrain exaggeration, and button position.
|
24
|
+
- **Smooth Transitions**: Includes animation when toggling terrain.
|
25
|
+
|
26
|
+
---
|
27
|
+
|
28
|
+
## Props
|
29
|
+
|
30
|
+
The component accepts the following properties:
|
31
|
+
|
32
|
+
| Property | Default | Description |
|
33
|
+
|-----------------------|---------------|-----------------------------------------------------------------------------|
|
34
|
+
| `map` | `null` | **Required.** The MapLibre map instance. |
|
35
|
+
| `pitch` | `60` | The pitch angle (in degrees) for 3D visualization. |
|
36
|
+
| `terrainExaggeration` | `1.5` | Exaggeration factor for the terrain. |
|
37
|
+
| `position` | `"bottomright"` | Button position: `topleft`, `topright`, `bottomleft`, `bottomright`. |
|
38
|
+
|
39
|
+
---
|
40
|
+
|
41
|
+
## Usage Example
|
42
|
+
|
43
|
+
### Svelte
|
44
|
+
|
45
|
+
```svelte
|
46
|
+
<script>
|
47
|
+
import Toggle3d from "@icgcat/toggle3d";
|
48
|
+
import maplibre from "maplibre-gl";
|
49
|
+
|
50
|
+
let map;
|
51
|
+
|
52
|
+
// Initialize the map
|
53
|
+
onMount(() => {
|
54
|
+
map = new maplibre.Map({
|
55
|
+
container: "map",
|
56
|
+
style: "https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json",
|
57
|
+
center: [2.1734, 41.3851], // Barcelona
|
58
|
+
zoom: 9,
|
59
|
+
});
|
60
|
+
});
|
61
|
+
</script>
|
62
|
+
|
63
|
+
<div id="map" style="width: 100%; height: 400px;"></div>
|
64
|
+
<Toggle3d {map} pitch={45} terrainExaggeration={2} position="topright" />
|
65
|
+
```
|
66
|
+
|
67
|
+
---
|
68
|
+
|
69
|
+
## Terrain Sources
|
70
|
+
|
71
|
+
The component uses the following terrain sources:
|
72
|
+
|
73
|
+
- **ICGC Terrain**: `urlTerrainIcgc` (specific to Catalonia).
|
74
|
+
- **MapZen Terrain**: `urlTerrainMapZen` (global fallback).
|
75
|
+
|
76
|
+
Ensure you define these URLs in your `config.js` file:
|
77
|
+
|
78
|
+
```javascript
|
79
|
+
export const urlTerrainIcgc = "https://your-icgc-terrain-url/{z}/{x}/{y}.png";
|
80
|
+
export const urlTerrainMapZen = "https://your-mapzen-terrain-url/{z}/{x}/{y}.png";
|
81
|
+
```
|
82
|
+
|
83
|
+
---
|
84
|
+
|
85
|
+
## API Methods
|
86
|
+
|
87
|
+
### `toggle()`
|
88
|
+
Toggles between 2D and 3D terrain.
|
89
|
+
|
90
|
+
- **Behavior**:
|
91
|
+
- Activates 3D terrain with dynamic source selection.
|
92
|
+
- Restores 2D map when toggled off.
|
93
|
+
|
94
|
+
---
|
95
|
+
|
96
|
+
## Styling
|
97
|
+
|
98
|
+
The button is styled by default but can be customized using the following CSS classes:
|
99
|
+
|
100
|
+
| Class | Description |
|
101
|
+
|---------------|-----------------------|
|
102
|
+
| `.topleft` | Positions button at top-left corner. |
|
103
|
+
| `.topright` | Positions button at top-right corner. |
|
104
|
+
| `.bottomleft` | Positions button at bottom-left corner. |
|
105
|
+
| `.bottomright`| Positions button at bottom-right corner. |
|
106
|
+
|
107
|
+
Default button styles:
|
108
|
+
|
109
|
+
```css
|
110
|
+
button {
|
111
|
+
padding: 6px;
|
112
|
+
font-size: 14px;
|
113
|
+
background-color: #c4c4c4;
|
114
|
+
color: black;
|
115
|
+
border: none;
|
116
|
+
border-radius: 5px;
|
117
|
+
cursor: pointer;
|
118
|
+
transition: background-color 0.3s;
|
119
|
+
z-index: 9999;
|
120
|
+
}
|
121
|
+
|
122
|
+
button:hover {
|
123
|
+
background-color: #949393;
|
124
|
+
}
|
125
|
+
```
|
126
|
+
|
127
|
+
---
|
128
|
+
|
129
|
+
## License
|
130
|
+
|
131
|
+
This project is licensed under the [MIT License](LICENSE).
|
package/package.json
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
{
|
2
|
+
"name": "@icgcat/toggle3d",
|
3
|
+
"version": "0.0.1",
|
4
|
+
"main": "src/index.js",
|
5
|
+
"type": "module",
|
6
|
+
"scripts": {
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
8
|
+
"build": "vite build"
|
9
|
+
},
|
10
|
+
"publishConfig": {
|
11
|
+
"access": "public"
|
12
|
+
},
|
13
|
+
"keywords": [],
|
14
|
+
"author": "Geostarters",
|
15
|
+
"license": "ISC",
|
16
|
+
"description": "A Svelte Toggle 3D component for Maplibre",
|
17
|
+
"devDependencies": {
|
18
|
+
"@sveltejs/vite-plugin-svelte": "^5.0.3",
|
19
|
+
"svelte": "^5.0.0",
|
20
|
+
"svelte-preprocess": "^6.0.3",
|
21
|
+
"vite": "^6.0.3"
|
22
|
+
}
|
23
|
+
}
|
@@ -0,0 +1,140 @@
|
|
1
|
+
<script>
|
2
|
+
import { urlTerrainIcgc, urlTerrainMapZen } from "./config.js";
|
3
|
+
|
4
|
+
export const componentProps = {
|
5
|
+
map: { default: null }, // L'objecte map (per defecte null)
|
6
|
+
pitch: { default: 60 },
|
7
|
+
terrainExaggeration: { default: 1.5 },
|
8
|
+
position: { default: "bottomright" }, // Posició del botó (per defecte 'bottomright')
|
9
|
+
};
|
10
|
+
|
11
|
+
let terrain;
|
12
|
+
|
13
|
+
const { map, pitch, terrainExaggeration, position } = $props();
|
14
|
+
|
15
|
+
let active = $state(false);
|
16
|
+
|
17
|
+
// Validem que l'objecte map sigui vàlid
|
18
|
+
if (!map) {
|
19
|
+
throw new Error("L'objecte 'map' passat no és vàlid.");
|
20
|
+
}
|
21
|
+
|
22
|
+
const cataloniaBounds = {
|
23
|
+
x0: 0.0,
|
24
|
+
y0: 38.5,
|
25
|
+
x1: 6.0,
|
26
|
+
y1: 62.33,
|
27
|
+
};
|
28
|
+
|
29
|
+
let minPitchZoom = 10;
|
30
|
+
|
31
|
+
// Funció per activar o desactivar el terreny
|
32
|
+
function toggle() {
|
33
|
+
active = !active;
|
34
|
+
|
35
|
+
if (active) {
|
36
|
+
const { longitude, latitude } = map.getCenter();
|
37
|
+
const zoom = map.getZoom();
|
38
|
+
|
39
|
+
// Configuració de terreny segons la zona i zoom
|
40
|
+
if (zoom < minPitchZoom) {
|
41
|
+
setTerrainSource("terrainMapZen", terrainExaggeration);
|
42
|
+
terrain = urlTerrainMapZen;
|
43
|
+
} else if (
|
44
|
+
longitude >= cataloniaBounds.x0 &&
|
45
|
+
longitude <= cataloniaBounds.x1 &&
|
46
|
+
latitude >= cataloniaBounds.y0 &&
|
47
|
+
latitude <= cataloniaBounds.y1
|
48
|
+
) {
|
49
|
+
terrain = urlTerrainIcgc;
|
50
|
+
setTerrainSource("terrainICGC", terrainExaggeration);
|
51
|
+
} else {
|
52
|
+
setTerrainSource("terrainMapZen", terrainExaggeration);
|
53
|
+
terrain = urlTerrainMapZen;
|
54
|
+
}
|
55
|
+
|
56
|
+
map.easeTo({
|
57
|
+
pitch: pitch,
|
58
|
+
duration: 500,
|
59
|
+
});
|
60
|
+
} else {
|
61
|
+
// Restableix l'estat del terreny i la inclinació
|
62
|
+
map.setTerrain(null);
|
63
|
+
|
64
|
+
map.easeTo({
|
65
|
+
pitch: 0,
|
66
|
+
duration: 500,
|
67
|
+
});
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
function setTerrainSource(sourceId, exaggeration) {
|
72
|
+
console.log("sourceId", sourceId);
|
73
|
+
if (!map.getSource(sourceId)) {
|
74
|
+
try {
|
75
|
+
map.addSource(sourceId, {
|
76
|
+
type: "raster-dem",
|
77
|
+
tiles: [terrain],
|
78
|
+
tileSize: 512,
|
79
|
+
maxzoom: 14,
|
80
|
+
});
|
81
|
+
} catch (error) {
|
82
|
+
console.error("Error afegint la font de terreny:", error);
|
83
|
+
}
|
84
|
+
}
|
85
|
+
map.setTerrain({
|
86
|
+
source: sourceId,
|
87
|
+
exaggeration,
|
88
|
+
});
|
89
|
+
}
|
90
|
+
</script>
|
91
|
+
|
92
|
+
<button onclick={toggle} class={position}>
|
93
|
+
{#if active}
|
94
|
+
2D
|
95
|
+
{:else}
|
96
|
+
3D
|
97
|
+
{/if}
|
98
|
+
</button>
|
99
|
+
|
100
|
+
<style>
|
101
|
+
button {
|
102
|
+
padding: 6px;
|
103
|
+
font-size: 14px;
|
104
|
+
background-color: #c4c4c4;
|
105
|
+
color: black;
|
106
|
+
border: none;
|
107
|
+
border-radius: 5px;
|
108
|
+
cursor: pointer;
|
109
|
+
transition: background-color 0.3s;
|
110
|
+
z-index: 9999;
|
111
|
+
}
|
112
|
+
|
113
|
+
button:hover {
|
114
|
+
background-color: #949393;
|
115
|
+
}
|
116
|
+
|
117
|
+
/* Estils per a diferents posicions */
|
118
|
+
.topleft {
|
119
|
+
position: fixed;
|
120
|
+
top: 10px;
|
121
|
+
left: 10px;
|
122
|
+
}
|
123
|
+
|
124
|
+
.topright {
|
125
|
+
position: fixed;
|
126
|
+
top: 10px;
|
127
|
+
right: 10px;
|
128
|
+
}
|
129
|
+
|
130
|
+
.bottomleft {
|
131
|
+
bottom: 10px;
|
132
|
+
left: 10px;
|
133
|
+
}
|
134
|
+
|
135
|
+
.bottomright {
|
136
|
+
position: fixed;
|
137
|
+
bottom: 10px;
|
138
|
+
right: 10px;
|
139
|
+
}
|
140
|
+
</style>
|
package/src/config.js
ADDED
package/src/index.js
ADDED
package/vite.config.js
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
import { defineConfig } from "vite";
|
2
|
+
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
3
|
+
import { sveltePreprocess } from "svelte-preprocess";
|
4
|
+
|
5
|
+
export default defineConfig({
|
6
|
+
plugins: [svelte({ preprocess: sveltePreprocess() })],
|
7
|
+
build: {
|
8
|
+
lib: {
|
9
|
+
entry: "src/index.js",
|
10
|
+
name: "Toggle3d",
|
11
|
+
formats: ["es", "cjs", "umd"],
|
12
|
+
fileName: (format) => `toggle3d.${format}.js`,
|
13
|
+
},
|
14
|
+
rollupOptions: {
|
15
|
+
external: ["svelte"],
|
16
|
+
output: {
|
17
|
+
globals: {
|
18
|
+
svelte: "Svelte",
|
19
|
+
},
|
20
|
+
},
|
21
|
+
},
|
22
|
+
},
|
23
|
+
});
|