@versatiles/svg-renderer 1.1.0 → 2.0.0-rc.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.
- package/README.md +42 -140
- package/dist/index.d.ts +2665 -4
- package/dist/index.js +2171 -1053
- package/package.json +14 -92
- package/dist/index.cjs +0 -10579
- package/dist/maplibre-svg-export.d.ts +0 -42
- package/dist/maplibre-svg-export.js +0 -11028
- package/dist/maplibre-svg-export.umd.js +0 -11037
package/README.md
CHANGED
|
@@ -1,36 +1,47 @@
|
|
|
1
|
-
[](https://www.npmjs.com/package/@versatiles/svg-renderer)
|
|
3
|
-
[](https://codecov.io/gh/versatiles-org/versatiles-svg-renderer)
|
|
1
|
+
[](https://www.npmjs.com/package/@versatiles/svg-renderer)
|
|
4
2
|
[](https://github.com/versatiles-org/versatiles-svg-renderer/actions/workflows/ci.yml)
|
|
5
|
-
[](LICENSE)
|
|
3
|
+
[](https://github.com/versatiles-org/versatiles-svg-renderer/blob/main/LICENSE)
|
|
6
4
|
|
|
7
|
-
#
|
|
5
|
+
# @versatiles/svg-renderer
|
|
8
6
|
|
|
9
|
-
Renders
|
|
7
|
+
Renders MapLibre styles as SVG, in Node.js or the browser.
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
The result is a single self-contained SVG document: vector layers become paths, raster tiles and icons are embedded, and nothing refers to external files. It can be saved, inlined into HTML or edited in a vector editor.
|
|
12
10
|
|
|
13
|
-
[
|
|
11
|
+

|
|
14
12
|
|
|
15
13
|
Supported layer types: background, fill, line, circle, symbol, and raster.
|
|
16
14
|
|
|
15
|
+
**[API documentation](https://versatiles.org/versatiles-svg-renderer/doc-typescript/)** · **[Visual comparison with MapLibre](https://versatiles.org/versatiles-svg-renderer/e2e/report.html)**
|
|
16
|
+
|
|
17
|
+
> [!TIP]
|
|
18
|
+
> **Need PNG output?** Use [`@versatiles/png-renderer`](https://www.npmjs.com/package/@versatiles/png-renderer). It renders PNG in Node.js and includes this package's `renderToSVG` as well.
|
|
19
|
+
>
|
|
20
|
+
> **Want an "export as SVG" button on a MapLibre map?** Use [`@versatiles/maplibre-svg-export`](https://www.npmjs.com/package/@versatiles/maplibre-svg-export).
|
|
21
|
+
|
|
17
22
|
## Installation
|
|
18
23
|
|
|
19
24
|
```bash
|
|
20
25
|
npm install @versatiles/svg-renderer
|
|
21
26
|
```
|
|
22
27
|
|
|
28
|
+
No native code. The only dependency is `@types/geojson` (types only). The package is ESM; Node.js 22 or later, or any modern bundler.
|
|
29
|
+
|
|
23
30
|
## Usage
|
|
24
31
|
|
|
25
32
|
### Node.js
|
|
26
33
|
|
|
27
34
|
```typescript
|
|
28
35
|
import { renderToSVG } from '@versatiles/svg-renderer';
|
|
29
|
-
import {
|
|
30
|
-
import {
|
|
36
|
+
import { inlineSources, osm } from '@versatiles/style';
|
|
37
|
+
import { writeFile } from 'node:fs/promises';
|
|
38
|
+
|
|
39
|
+
// inlineSources() resolves the style's TileJSON sources into tile URLs, which the
|
|
40
|
+
// renderer needs. Without it, the map comes out empty.
|
|
41
|
+
const style = await inlineSources(osm({ theme: 'colorful' }));
|
|
31
42
|
|
|
32
43
|
const svg = await renderToSVG({
|
|
33
|
-
style
|
|
44
|
+
style,
|
|
34
45
|
width: 800,
|
|
35
46
|
height: 600,
|
|
36
47
|
lon: 13.4,
|
|
@@ -38,74 +49,29 @@ const svg = await renderToSVG({
|
|
|
38
49
|
zoom: 10,
|
|
39
50
|
});
|
|
40
51
|
|
|
41
|
-
|
|
52
|
+
await writeFile('map.svg', svg);
|
|
42
53
|
```
|
|
43
54
|
|
|
55
|
+
> [!IMPORTANT]
|
|
56
|
+
> The style's sources must list their tile URLs directly (`"tiles": [...]`). A source that only points at a TileJSON document (`"url": ".../tiles.json"`) is **not fetched, and the map comes out empty without an error**. Styles built with `@versatiles/style` are in that form, so pass them through its `inlineSources()` as above. Hosted style files usually list their tiles already.
|
|
57
|
+
|
|
44
58
|
### Browser
|
|
45
59
|
|
|
46
60
|
```typescript
|
|
47
61
|
import { renderToSVG } from '@versatiles/svg-renderer';
|
|
48
62
|
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
63
|
+
const style = await fetch('https://tiles.versatiles.org/assets/styles/colorful/style.json').then(
|
|
64
|
+
(r) => r.json(),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
document.body.innerHTML = await renderToSVG({
|
|
68
|
+
style,
|
|
53
69
|
width: 800,
|
|
54
70
|
height: 600,
|
|
55
71
|
lon: 13.4,
|
|
56
72
|
lat: 52.5,
|
|
57
73
|
zoom: 10,
|
|
58
74
|
});
|
|
59
|
-
|
|
60
|
-
document.body.innerHTML = svg;
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
### MapLibre Plugin
|
|
64
|
-
|
|
65
|
-
The package includes an `SVGExportControl` that adds an export button to any MapLibre GL JS map.
|
|
66
|
-
|
|
67
|
-
With a bundler, import it from the `/maplibre` subpath:
|
|
68
|
-
|
|
69
|
-
```typescript
|
|
70
|
-
import { SVGExportControl } from '@versatiles/svg-renderer/maplibre';
|
|
71
|
-
|
|
72
|
-
map.addControl(new SVGExportControl(), 'top-right');
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
Or load it directly in the browser via the UMD bundle, which exposes a `VersaTilesSVG` global:
|
|
76
|
-
|
|
77
|
-
```html
|
|
78
|
-
<!DOCTYPE html>
|
|
79
|
-
<html>
|
|
80
|
-
<head>
|
|
81
|
-
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.css" />
|
|
82
|
-
<script src="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.js"></script>
|
|
83
|
-
<script src="https://unpkg.com/@versatiles/svg-renderer/dist/maplibre-svg-export.umd.js"></script>
|
|
84
|
-
</head>
|
|
85
|
-
<body>
|
|
86
|
-
<div id="map"></div>
|
|
87
|
-
<script>
|
|
88
|
-
const map = new maplibregl.Map({
|
|
89
|
-
container: 'map',
|
|
90
|
-
style: 'https://tiles.versatiles.org/assets/styles/colorful/style.json',
|
|
91
|
-
center: [13.4, 52.5],
|
|
92
|
-
zoom: 10,
|
|
93
|
-
});
|
|
94
|
-
map.addControl(new VersaTilesSVG.SVGExportControl(), 'top-right');
|
|
95
|
-
</script>
|
|
96
|
-
</body>
|
|
97
|
-
</html>
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
The control opens a panel where the user can set width and height, preview the SVG, download it, or open it in a new tab. Map interactions are disabled while the panel is open.
|
|
101
|
-
|
|
102
|
-
Options:
|
|
103
|
-
|
|
104
|
-
```typescript
|
|
105
|
-
new SVGExportControl({
|
|
106
|
-
defaultWidth: 1024, // default: 1024
|
|
107
|
-
defaultHeight: 1024, // default: 1024
|
|
108
|
-
});
|
|
109
75
|
```
|
|
110
76
|
|
|
111
77
|
## API
|
|
@@ -114,7 +80,7 @@ new SVGExportControl({
|
|
|
114
80
|
|
|
115
81
|
| Option | Type | Default | Description |
|
|
116
82
|
| -------------- | -------------------- | ------------ | ----------------------------------------- |
|
|
117
|
-
| `style` | `StyleSpecification` |
|
|
83
|
+
| `style` | `StyleSpecification` | _(required)_ | MapLibre style specification |
|
|
118
84
|
| `width` | `number` | `1024` | Output width in pixels |
|
|
119
85
|
| `height` | `number` | `1024` | Output height in pixels |
|
|
120
86
|
| `lon` | `number` | `0` | Center longitude |
|
|
@@ -126,82 +92,18 @@ new SVGExportControl({
|
|
|
126
92
|
|
|
127
93
|
When `renderLabels` is set to `true`, symbol layers are rendered, including text labels and sprite-based icons. By default, this option is disabled.
|
|
128
94
|
|
|
95
|
+
The SVG names each label's font (`text-font`) and leaves resolving it to whatever displays the SVG, so labels use the intended typeface only where that font is installed or provided with `@font-face`.
|
|
96
|
+
|
|
129
97
|
> [!WARNING]
|
|
130
98
|
> The rendering of labels and icons is experimental and may produce imperfect results. Since we cannot use the original layouting engine of MapLibre GL JS, there are known limitations:
|
|
131
99
|
>
|
|
132
100
|
> - **No collision detection:** Text labels are rendered without collision detection, so labels may overlap.
|
|
133
101
|
> - **Simplified text placement:** Labels can not be positioned along lines.
|
|
134
102
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
<!--- This chapter is generated automatically --->
|
|
144
|
-
|
|
145
|
-
```mermaid
|
|
146
|
-
---
|
|
147
|
-
config:
|
|
148
|
-
layout: elk
|
|
149
|
-
---
|
|
150
|
-
flowchart TB
|
|
151
|
-
|
|
152
|
-
subgraph 0["src"]
|
|
153
|
-
1["demo.ts"]
|
|
154
|
-
2["index.ts"]
|
|
155
|
-
subgraph 3["pipeline"]
|
|
156
|
-
4["render.ts"]
|
|
157
|
-
E["style_layer.ts"]
|
|
158
|
-
end
|
|
159
|
-
subgraph 5["sources"]
|
|
160
|
-
6["index.ts"]
|
|
161
|
-
7["geojson.ts"]
|
|
162
|
-
9["raster.ts"]
|
|
163
|
-
A["base64.ts"]
|
|
164
|
-
B["tiles.ts"]
|
|
165
|
-
C["sprite.ts"]
|
|
166
|
-
D["vector.ts"]
|
|
167
|
-
end
|
|
168
|
-
8["geometry.ts"]
|
|
169
|
-
subgraph F["renderer"]
|
|
170
|
-
G["svg.ts"]
|
|
171
|
-
H["color.ts"]
|
|
172
|
-
I["svg_path.ts"]
|
|
173
|
-
N["types.ts"]
|
|
174
|
-
end
|
|
175
|
-
subgraph J["maplibre"]
|
|
176
|
-
K["control.ts"]
|
|
177
|
-
L["panel_css.ts"]
|
|
178
|
-
M["index.ts"]
|
|
179
|
-
end
|
|
180
|
-
end
|
|
181
|
-
1-->2
|
|
182
|
-
2-->4
|
|
183
|
-
2-->G
|
|
184
|
-
4-->6
|
|
185
|
-
4-->C
|
|
186
|
-
4-->E
|
|
187
|
-
6-->7
|
|
188
|
-
6-->9
|
|
189
|
-
6-->C
|
|
190
|
-
6-->D
|
|
191
|
-
7-->8
|
|
192
|
-
9-->A
|
|
193
|
-
9-->B
|
|
194
|
-
B-->8
|
|
195
|
-
C-->A
|
|
196
|
-
D-->8
|
|
197
|
-
D-->B
|
|
198
|
-
G-->H
|
|
199
|
-
G-->I
|
|
200
|
-
K-->2
|
|
201
|
-
K-->L
|
|
202
|
-
M-->2
|
|
203
|
-
M-->K
|
|
204
|
-
|
|
205
|
-
class 0,3,5,F,J subgraphs;
|
|
206
|
-
classDef subgraphs fill-opacity:0.1, fill:#888, color:#888, stroke:#888;
|
|
207
|
-
```
|
|
103
|
+
### Projections
|
|
104
|
+
|
|
105
|
+
The renderer follows the style's [`projection`](https://maplibre.org/maplibre-style-spec/projection/): `mercator` (the default), `vertical-perspective` and `globe`, which — like in MapLibre GL JS — shows the globe up to zoom 11 and blends into mercator between zoom 11 and 12. Zoom-dependent `step`/`interpolate` expressions are supported as well. On the globe, geometry is clipped at the horizon and the map is clipped to the globe's outline; everything outside of it stays transparent. Raster tiles are drawn as a mesh of small triangles, each mapped exactly onto the globe by its own affine transform.
|
|
106
|
+
|
|
107
|
+
## License
|
|
108
|
+
|
|
109
|
+
MIT. Part of [VersaTiles](https://github.com/versatiles-org/versatiles-svg-renderer): source code, issues and the changelog are on GitHub.
|