@versatiles/svg-renderer 2.0.0-rc.0 → 2.1.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 +60 -40
- package/dist/index.d.ts +492 -44
- package/dist/index.js +11803 -11371
- package/package.json +8 -4
package/dist/index.d.ts
CHANGED
|
@@ -2559,19 +2559,407 @@ type BackgroundLayerSpecification = {
|
|
|
2559
2559
|
};
|
|
2560
2560
|
};
|
|
2561
2561
|
type LayerSpecification = FillLayerSpecification | LineLayerSpecification | SymbolLayerSpecification | CircleLayerSpecification | HeatmapLayerSpecification | FillExtrusionLayerSpecification | RasterLayerSpecification | HillshadeLayerSpecification | ColorReliefLayerSpecification | BackgroundLayerSpecification;
|
|
2562
|
+
//#endregion
|
|
2563
|
+
//#region src/expression/types/color_spaces.d.ts
|
|
2564
|
+
/**
|
|
2565
|
+
* @param r Red component 0..1
|
|
2566
|
+
* @param g Green component 0..1
|
|
2567
|
+
* @param b Blue component 0..1
|
|
2568
|
+
* @param alpha Alpha component 0..1
|
|
2569
|
+
*/
|
|
2570
|
+
type RGBColor = [r: number, g: number, b: number, alpha: number];
|
|
2571
|
+
/**
|
|
2572
|
+
* @param h Hue as degrees 0..360
|
|
2573
|
+
* @param c Chroma 0..~230
|
|
2574
|
+
* @param l Lightness as percentage 0..100
|
|
2575
|
+
* @param alpha Alpha component 0..1
|
|
2576
|
+
*/
|
|
2577
|
+
type HCLColor = [h: number, c: number, l: number, alpha: number];
|
|
2578
|
+
/**
|
|
2579
|
+
* @param l Lightness as percentage 0..100
|
|
2580
|
+
* @param a A axis value -125..125
|
|
2581
|
+
* @param b B axis value -125..125
|
|
2582
|
+
* @param alpha Alpha component 0..1
|
|
2583
|
+
*/
|
|
2584
|
+
type LABColor = [l: number, a: number, b: number, alpha: number];
|
|
2585
|
+
//#endregion
|
|
2586
|
+
//#region src/expression/types/color.d.ts
|
|
2587
|
+
type InterpolationColorSpace = 'rgb' | 'hcl' | 'lab';
|
|
2588
|
+
/**
|
|
2589
|
+
* Color representation used by WebGL.
|
|
2590
|
+
* Defined in sRGB color space and pre-blended with alpha.
|
|
2591
|
+
* @private
|
|
2592
|
+
*/
|
|
2593
|
+
declare class Color {
|
|
2594
|
+
readonly r: number;
|
|
2595
|
+
readonly g: number;
|
|
2596
|
+
readonly b: number;
|
|
2597
|
+
readonly a: number;
|
|
2598
|
+
/**
|
|
2599
|
+
* @param r Red component premultiplied by `alpha` 0..1
|
|
2600
|
+
* @param g Green component premultiplied by `alpha` 0..1
|
|
2601
|
+
* @param b Blue component premultiplied by `alpha` 0..1
|
|
2602
|
+
* @param [alpha=1] Alpha component 0..1
|
|
2603
|
+
* @param [premultiplied=true] Whether the `r`, `g` and `b` values have already
|
|
2604
|
+
* been multiplied by alpha. If `true` nothing happens if `false` then they will
|
|
2605
|
+
* be multiplied automatically.
|
|
2606
|
+
*/
|
|
2607
|
+
constructor(r: number, g: number, b: number, alpha?: number, premultiplied?: boolean);
|
|
2608
|
+
static black: Color;
|
|
2609
|
+
static white: Color;
|
|
2610
|
+
static transparent: Color;
|
|
2611
|
+
static red: Color;
|
|
2612
|
+
/**
|
|
2613
|
+
* Parses CSS color strings and converts colors to sRGB color space if needed.
|
|
2614
|
+
* Officially supported color formats:
|
|
2615
|
+
* - keyword, e.g. 'aquamarine' or 'steelblue'
|
|
2616
|
+
* - hex (with 3, 4, 6 or 8 digits), e.g. '#f0f' or '#e9bebea9'
|
|
2617
|
+
* - rgb and rgba, e.g. 'rgb(0,240,120)' or 'rgba(0%,94%,47%,0.1)' or 'rgb(0 240 120 / .3)'
|
|
2618
|
+
* - hsl and hsla, e.g. 'hsl(0,0%,83%)' or 'hsla(0,0%,83%,.5)' or 'hsl(0 0% 83% / 20%)'
|
|
2619
|
+
*
|
|
2620
|
+
* @param input CSS color string to parse.
|
|
2621
|
+
* @returns A `Color` instance, or `undefined` if the input is not a valid color string.
|
|
2622
|
+
*/
|
|
2623
|
+
static parse(input: Color | string | undefined | null): Color | undefined;
|
|
2624
|
+
/**
|
|
2625
|
+
* Used in color interpolation and by 'to-rgba' expression.
|
|
2626
|
+
*
|
|
2627
|
+
* @returns Gien color, with reversed alpha blending, in sRGB color space.
|
|
2628
|
+
*/
|
|
2629
|
+
get rgb(): RGBColor;
|
|
2630
|
+
/**
|
|
2631
|
+
* Used in color interpolation.
|
|
2632
|
+
*
|
|
2633
|
+
* @returns Gien color, with reversed alpha blending, in HCL color space.
|
|
2634
|
+
*/
|
|
2635
|
+
get hcl(): HCLColor;
|
|
2636
|
+
/**
|
|
2637
|
+
* Used in color interpolation.
|
|
2638
|
+
*
|
|
2639
|
+
* @returns Gien color, with reversed alpha blending, in LAB color space.
|
|
2640
|
+
*/
|
|
2641
|
+
get lab(): LABColor;
|
|
2642
|
+
/**
|
|
2643
|
+
* Lazy getter pattern. When getter is called for the first time lazy value
|
|
2644
|
+
* is calculated and then overwrites getter function in given object instance.
|
|
2645
|
+
*
|
|
2646
|
+
* @example:
|
|
2647
|
+
* const redColor = Color.parse('red');
|
|
2648
|
+
* let x = redColor.hcl; // this will invoke `get hcl()`, which will calculate
|
|
2649
|
+
* // the value of red in HCL space and invoke this `overwriteGetter` function
|
|
2650
|
+
* // which in turn will set a field with a key 'hcl' in the `redColor` object.
|
|
2651
|
+
* // In other words it will override `get hcl()` from its `Color` prototype
|
|
2652
|
+
* // with its own property: hcl = [calculated red value in hcl].
|
|
2653
|
+
* let y = redColor.hcl; // next call will no longer invoke getter but simply
|
|
2654
|
+
* // return the previously calculated value
|
|
2655
|
+
* x === y; // true - `x` is exactly the same object as `y`
|
|
2656
|
+
*
|
|
2657
|
+
* @param getterKey Getter key
|
|
2658
|
+
* @param lazyValue Lazily calculated value to be memoized by current instance
|
|
2659
|
+
* @private
|
|
2660
|
+
*/
|
|
2661
|
+
private overwriteGetter;
|
|
2662
|
+
/**
|
|
2663
|
+
* Used by 'to-string' expression.
|
|
2664
|
+
*
|
|
2665
|
+
* @returns Serialized color in format `rgba(r,g,b,a)`
|
|
2666
|
+
* where r,g,b are numbers within 0..255 and alpha is number within 1..0
|
|
2667
|
+
*
|
|
2668
|
+
* @example
|
|
2669
|
+
* var purple = new Color.parse('purple');
|
|
2670
|
+
* purple.toString; // = "rgba(128,0,128,1)"
|
|
2671
|
+
* var translucentGreen = new Color.parse('rgba(26, 207, 26, .73)');
|
|
2672
|
+
* translucentGreen.toString(); // = "rgba(26,207,26,0.73)"
|
|
2673
|
+
*/
|
|
2674
|
+
toString(): string;
|
|
2675
|
+
static interpolate(from: Color, to: Color, t: number, spaceKey?: InterpolationColorSpace): Color;
|
|
2676
|
+
}
|
|
2677
|
+
//#endregion
|
|
2678
|
+
//#region src/point2d.d.ts
|
|
2679
|
+
interface Point2D$1 {
|
|
2680
|
+
x: number;
|
|
2681
|
+
y: number;
|
|
2682
|
+
}
|
|
2683
|
+
//#endregion
|
|
2684
|
+
//#region src/expression/index.d.ts
|
|
2685
|
+
type Feature$1 = {
|
|
2686
|
+
readonly type: 0 | 1 | 2 | 3 | 'Unknown' | 'Point' | 'MultiPoint' | 'LineString' | 'MultiLineString' | 'Polygon' | 'MultiPolygon';
|
|
2687
|
+
readonly id?: any;
|
|
2688
|
+
readonly properties: {
|
|
2689
|
+
[_: string]: any;
|
|
2690
|
+
};
|
|
2691
|
+
readonly patterns?: {
|
|
2692
|
+
[_: string]: {
|
|
2693
|
+
min: string;
|
|
2694
|
+
mid: string;
|
|
2695
|
+
max: string;
|
|
2696
|
+
};
|
|
2697
|
+
};
|
|
2698
|
+
readonly dashes?: {
|
|
2699
|
+
[_: string]: {
|
|
2700
|
+
min: string;
|
|
2701
|
+
mid: string;
|
|
2702
|
+
max: string;
|
|
2703
|
+
};
|
|
2704
|
+
};
|
|
2705
|
+
readonly geometry?: Array<Array<Point2D$1>>;
|
|
2706
|
+
};
|
|
2707
|
+
|
|
2708
|
+
type Properties = Record<string, unknown>;
|
|
2709
|
+
type Patterns = Record<string, {
|
|
2710
|
+
min: string;
|
|
2711
|
+
mid: string;
|
|
2712
|
+
max: string;
|
|
2713
|
+
}>;
|
|
2714
|
+
type Geometry = Point2D[][];
|
|
2715
|
+
type Bbox = [number, number, number, number];
|
|
2716
|
+
declare class Point2D implements Point2D$1 {
|
|
2717
|
+
x: number;
|
|
2718
|
+
y: number;
|
|
2719
|
+
constructor(x: number, y: number);
|
|
2720
|
+
isZero(): boolean;
|
|
2721
|
+
scale(factor: number): this;
|
|
2722
|
+
translate(offset: Point2D): this;
|
|
2723
|
+
getProject2Pixel(): Point2D;
|
|
2724
|
+
}
|
|
2725
|
+
declare class Feature implements Feature$1 {
|
|
2726
|
+
#private;
|
|
2727
|
+
readonly type: 'LineString' | 'Point' | 'Polygon';
|
|
2728
|
+
readonly id: unknown;
|
|
2729
|
+
readonly properties: Properties;
|
|
2730
|
+
readonly patterns?: Patterns;
|
|
2731
|
+
readonly geometry: Geometry;
|
|
2732
|
+
/**
|
|
2733
|
+
* The boundary of a polygon for the fill-antialias outline, as open polylines — when it
|
|
2734
|
+
* differs from the polygon's rings, i.e. when the polygon was clipped to its tile: the
|
|
2735
|
+
* clipped edges along the tile border are not part of the outline.
|
|
2736
|
+
*/
|
|
2737
|
+
readonly outline?: Geometry;
|
|
2738
|
+
constructor(opt: {
|
|
2739
|
+
type: 'LineString' | 'Point' | 'Polygon';
|
|
2740
|
+
id?: unknown;
|
|
2741
|
+
properties: Properties;
|
|
2742
|
+
patterns?: Patterns;
|
|
2743
|
+
geometry: Geometry;
|
|
2744
|
+
outline?: Geometry;
|
|
2745
|
+
});
|
|
2746
|
+
getBbox(): Bbox;
|
|
2747
|
+
doesOverlap(bbox: Bbox): boolean;
|
|
2748
|
+
}
|
|
2749
|
+
|
|
2750
|
+
/**
|
|
2751
|
+
* The part of a `Response` the renderer uses. A real `Response` has all of it; the type
|
|
2752
|
+
* is spelled out so that using this package needs neither the DOM nor Node's type
|
|
2753
|
+
* definitions.
|
|
2754
|
+
*/
|
|
2755
|
+
interface FetchResponse {
|
|
2756
|
+
readonly ok: boolean;
|
|
2757
|
+
readonly status: number;
|
|
2758
|
+
readonly headers: {
|
|
2759
|
+
get(name: string): string | null;
|
|
2760
|
+
};
|
|
2761
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
2762
|
+
json(): Promise<unknown>;
|
|
2763
|
+
}
|
|
2764
|
+
/**
|
|
2765
|
+
* Loads a URL, like `fetch`: the renderer loads tiles and sprites through it. It is called
|
|
2766
|
+
* with the URL only, and should resolve to a `Response`.
|
|
2767
|
+
*/
|
|
2768
|
+
type FetchFunction = (url: string) => Promise<FetchResponse>;
|
|
2769
|
+
|
|
2770
|
+
interface SpriteEntry {
|
|
2771
|
+
width: number;
|
|
2772
|
+
height: number;
|
|
2773
|
+
x: number;
|
|
2774
|
+
y: number;
|
|
2775
|
+
pixelRatio: number;
|
|
2776
|
+
sdf: boolean;
|
|
2777
|
+
sheetDataUri: string;
|
|
2778
|
+
sheetWidth: number;
|
|
2779
|
+
sheetHeight: number;
|
|
2780
|
+
}
|
|
2781
|
+
type SpriteAtlas = Map<string, SpriteEntry>;
|
|
2782
|
+
|
|
2783
|
+
interface ClipCircle {
|
|
2784
|
+
x: number;
|
|
2785
|
+
y: number;
|
|
2786
|
+
radius: number;
|
|
2787
|
+
}
|
|
2562
2788
|
|
|
2563
|
-
/**
|
|
2564
|
-
|
|
2789
|
+
/**
|
|
2790
|
+
* A drawing surface the render pipeline paints a map onto. It only describes *how* to
|
|
2791
|
+
* draw; how the result is handed back is up to the backend, because those differ in
|
|
2792
|
+
* kind: the SVG backend serializes to a string ({@link StringRenderer}), while a raster
|
|
2793
|
+
* backend produces an encoded image buffer.
|
|
2794
|
+
*/
|
|
2795
|
+
interface Renderer {
|
|
2796
|
+
readonly width: number;
|
|
2797
|
+
readonly height: number;
|
|
2798
|
+
drawBackgroundFill(style: BackgroundStyle): void;
|
|
2799
|
+
drawPolygons(id: string, features: [Feature, FillStyle][]): void;
|
|
2800
|
+
drawLineStrings(id: string, features: [Feature, LineStyle][]): void;
|
|
2801
|
+
drawCircles(id: string, features: [Feature, CircleStyle][]): void;
|
|
2802
|
+
drawLabels(id: string, features: [Feature, SymbolStyle][]): void;
|
|
2803
|
+
/**
|
|
2804
|
+
* These two may be asynchronous. Their images arrive as data URIs, which a raster
|
|
2805
|
+
* backend has to decode before it can paint them, while the SVG backend simply embeds
|
|
2806
|
+
* the URI and stays synchronous. The pipeline awaits both, so layers keep their order
|
|
2807
|
+
* either way.
|
|
2808
|
+
*/
|
|
2809
|
+
drawIcons(id: string, features: [Feature, IconStyle][], spriteAtlas: SpriteAtlas): void | Promise<void>;
|
|
2810
|
+
drawRasterTiles(id: string, tiles: RasterTile[], style: RasterStyle): void | Promise<void>;
|
|
2811
|
+
/** Restricts all drawing to a circle (the silhouette of the globe). */
|
|
2812
|
+
setClipCircle?(circle: ClipCircle): void;
|
|
2813
|
+
/**
|
|
2814
|
+
* Called once the map is complete: undoes any state set up for drawing it (such as the
|
|
2815
|
+
* clip circle), so that whoever gets the result can keep drawing on it.
|
|
2816
|
+
*/
|
|
2817
|
+
finish?(): void;
|
|
2818
|
+
}
|
|
2819
|
+
interface BackgroundStyle {
|
|
2820
|
+
color: Color;
|
|
2821
|
+
opacity: number;
|
|
2822
|
+
}
|
|
2823
|
+
interface FillStyle {
|
|
2824
|
+
color: Color;
|
|
2825
|
+
opacity: number;
|
|
2826
|
+
translate: [number, number];
|
|
2827
|
+
/**
|
|
2828
|
+
* fill-outline-color. When omitted the fill `color` is used (MapLibre's
|
|
2829
|
+
* default: the antialias outline is drawn in the fill color).
|
|
2830
|
+
*/
|
|
2831
|
+
outlineColor?: Color;
|
|
2832
|
+
/**
|
|
2833
|
+
* fill-antialias. When truthy, a ~1px edge outline in `outlineColor` is drawn
|
|
2834
|
+
* over all fills of the layer (a separate pass) so a lower feature's border
|
|
2835
|
+
* composites on top of later overlapping fills — matching MapLibre for
|
|
2836
|
+
* choropleths and stacked polygons.
|
|
2837
|
+
*/
|
|
2838
|
+
antialias?: boolean;
|
|
2839
|
+
}
|
|
2840
|
+
interface LineStyle {
|
|
2841
|
+
blur: number;
|
|
2842
|
+
cap: 'butt' | 'round' | 'square';
|
|
2843
|
+
color: Color;
|
|
2844
|
+
dasharray?: number[];
|
|
2845
|
+
join: 'bevel' | 'miter' | 'round';
|
|
2846
|
+
miterLimit: number;
|
|
2847
|
+
offset: number;
|
|
2848
|
+
opacity: number;
|
|
2849
|
+
translate: [number, number];
|
|
2850
|
+
width: number;
|
|
2851
|
+
}
|
|
2852
|
+
interface CircleStyle {
|
|
2853
|
+
color: Color;
|
|
2854
|
+
opacity: number;
|
|
2855
|
+
radius: number;
|
|
2856
|
+
translate: [number, number];
|
|
2857
|
+
strokeWidth: number;
|
|
2858
|
+
strokeColor: Color;
|
|
2859
|
+
}
|
|
2860
|
+
interface RasterStyle {
|
|
2861
|
+
opacity: number;
|
|
2862
|
+
hueRotate: number;
|
|
2863
|
+
brightnessMin: number;
|
|
2864
|
+
brightnessMax: number;
|
|
2865
|
+
saturation: number;
|
|
2866
|
+
contrast: number;
|
|
2867
|
+
resampling: 'linear' | 'nearest';
|
|
2868
|
+
}
|
|
2869
|
+
interface SymbolStyle {
|
|
2870
|
+
text: string;
|
|
2871
|
+
size: number;
|
|
2872
|
+
font: string[];
|
|
2873
|
+
anchor: string;
|
|
2874
|
+
offset: [number, number];
|
|
2875
|
+
rotate: number;
|
|
2876
|
+
color: Color;
|
|
2877
|
+
opacity: number;
|
|
2878
|
+
haloColor: Color;
|
|
2879
|
+
haloWidth: number;
|
|
2880
|
+
}
|
|
2881
|
+
interface IconStyle {
|
|
2882
|
+
image: string;
|
|
2883
|
+
size: number;
|
|
2884
|
+
anchor: string;
|
|
2885
|
+
offset: [number, number];
|
|
2886
|
+
rotate: number;
|
|
2887
|
+
opacity: number;
|
|
2888
|
+
sdf: boolean;
|
|
2889
|
+
color: Color;
|
|
2890
|
+
haloColor: Color;
|
|
2891
|
+
haloWidth: number;
|
|
2892
|
+
}
|
|
2893
|
+
interface RasterTile {
|
|
2894
|
+
x: number;
|
|
2895
|
+
y: number;
|
|
2896
|
+
width: number;
|
|
2897
|
+
height: number;
|
|
2898
|
+
dataUri: string;
|
|
2899
|
+
/**
|
|
2900
|
+
* On the globe a tile cannot be drawn as a rectangle. Instead it is split into a mesh of
|
|
2901
|
+
* triangles, each drawn with its own affine transform.
|
|
2902
|
+
*/
|
|
2903
|
+
triangles?: RasterTriangle[];
|
|
2904
|
+
}
|
|
2905
|
+
type Triangle = [[number, number], [number, number], [number, number]];
|
|
2906
|
+
interface RasterTriangle {
|
|
2907
|
+
/** Corners of the triangle within the tile image, in tile units (0..1). */
|
|
2908
|
+
source: Triangle;
|
|
2909
|
+
/** The same corners on screen, in pixels. */
|
|
2910
|
+
target: Triangle;
|
|
2911
|
+
}
|
|
2912
|
+
|
|
2913
|
+
/** Options for {@link SVGMapRenderer}: what stays the same for every view of the map. */
|
|
2914
|
+
interface SVGMapRendererOptions {
|
|
2565
2915
|
/**
|
|
2566
2916
|
* The MapLibre style to render.
|
|
2567
2917
|
*
|
|
2568
2918
|
* Its sources must list their tile URLs directly (`tiles: [...]`). A style whose
|
|
2569
2919
|
* sources only point at a TileJSON document (`url: '.../tiles.json'`) renders as an
|
|
2570
|
-
* **empty map without any error**, because the renderer does not fetch TileJSON.
|
|
2571
|
-
*
|
|
2572
|
-
* `inlineSources()` first
|
|
2920
|
+
* **empty map without any error**, because the renderer does not fetch TileJSON. Hosted
|
|
2921
|
+
* styles such as those of VersaTiles list their tiles; a style built with
|
|
2922
|
+
* `@versatiles/style` needs its `inlineSources()` first.
|
|
2573
2923
|
*/
|
|
2574
2924
|
style: StyleSpecification;
|
|
2925
|
+
/**
|
|
2926
|
+
* Draw the style's symbol layers: text labels and icons.
|
|
2927
|
+
*
|
|
2928
|
+
* Off by default, because labels are the least faithful part of the output. They are
|
|
2929
|
+
* drawn as horizontal text at the middle of their feature: labels that MapLibre curves
|
|
2930
|
+
* along a line (`symbol-placement: "line"`, typically street names) come out straight,
|
|
2931
|
+
* and there is no collision detection, so crowded label layers can overlap.
|
|
2932
|
+
*
|
|
2933
|
+
* The SVG names each label's font (`text-font`) and leaves resolving it to whatever
|
|
2934
|
+
* displays the SVG, so labels use the intended typeface only where that font is
|
|
2935
|
+
* installed or provided with `@font-face`.
|
|
2936
|
+
* @defaultValue `false`
|
|
2937
|
+
*/
|
|
2938
|
+
renderLabels?: boolean;
|
|
2939
|
+
/**
|
|
2940
|
+
* How much memory the fetched tiles may take, in bytes. Tiles are kept so that
|
|
2941
|
+
* rendering an overlapping view does not fetch them again; beyond this size, the least
|
|
2942
|
+
* recently used ones are dropped. `0` keeps none.
|
|
2943
|
+
*
|
|
2944
|
+
* Tiles are kept for the lifetime of the instance, regardless of the HTTP caching
|
|
2945
|
+
* headers they were served with; see {@link SVGMapRenderer.clearCache}.
|
|
2946
|
+
* @defaultValue `134217728` (128 MB)
|
|
2947
|
+
*/
|
|
2948
|
+
tileCacheSize?: number;
|
|
2949
|
+
/**
|
|
2950
|
+
* Loads tiles and sprites, like `fetch`, which is the default. Pass your own to send
|
|
2951
|
+
* headers, go through a proxy, or keep tiles in a cache on disk.
|
|
2952
|
+
*
|
|
2953
|
+
* It must return a real `Response`, and its status matters: a 404 or 204 means the
|
|
2954
|
+
* server has no such tile, which is remembered (see {@link SVGMapRendererOptions.tileCacheSize}); any other
|
|
2955
|
+
* error status, or a rejected promise, counts as failed, and the next render tries
|
|
2956
|
+
* again.
|
|
2957
|
+
* @defaultValue the global `fetch`
|
|
2958
|
+
*/
|
|
2959
|
+
fetch?: FetchFunction;
|
|
2960
|
+
}
|
|
2961
|
+
/** The view to render: size, centre and zoom. */
|
|
2962
|
+
interface ViewOptions {
|
|
2575
2963
|
/**
|
|
2576
2964
|
* Width of the image in pixels.
|
|
2577
2965
|
* @defaultValue `1024`
|
|
@@ -2598,20 +2986,100 @@ interface RenderToSVGOptions {
|
|
|
2598
2986
|
* @defaultValue `2`
|
|
2599
2987
|
*/
|
|
2600
2988
|
zoom?: number;
|
|
2989
|
+
}
|
|
2990
|
+
/**
|
|
2991
|
+
* Renders many views of one MapLibre style as SVG.
|
|
2992
|
+
*
|
|
2993
|
+
* Does the work that depends only on the style once, when constructed or on the first
|
|
2994
|
+
* render, instead of on every call as {@link renderToSVG} does: the style is parsed once,
|
|
2995
|
+
* and the sprite (the icons, needed with `renderLabels`) is fetched once. Tiles are kept
|
|
2996
|
+
* too (up to {@link SVGMapRendererOptions.tileCacheSize}), so overlapping views share
|
|
2997
|
+
* them. Use it to render a batch of views, e.g. thumbnails or a series of map sections.
|
|
2998
|
+
*
|
|
2999
|
+
* The style is read when the instance is created and is expected not to change
|
|
3000
|
+
* afterwards; to render a changed style, create a new instance. Renders may run
|
|
3001
|
+
* concurrently.
|
|
3002
|
+
*
|
|
3003
|
+
* @example Render several views of one style
|
|
3004
|
+
* ```ts
|
|
3005
|
+
* import { SVGMapRenderer } from '@versatiles/svg-renderer';
|
|
3006
|
+
*
|
|
3007
|
+
* const map = new SVGMapRenderer({ style });
|
|
3008
|
+
*
|
|
3009
|
+
* const berlin = await map.renderSVG({ lon: 13.4, lat: 52.52, zoom: 12 });
|
|
3010
|
+
* const potsdam = await map.renderSVG({ lon: 13.06, lat: 52.4, zoom: 12 });
|
|
3011
|
+
* ```
|
|
3012
|
+
*/
|
|
3013
|
+
declare class SVGMapRenderer {
|
|
3014
|
+
#private;
|
|
2601
3015
|
/**
|
|
2602
|
-
*
|
|
3016
|
+
* @param options - The style, whether to draw labels, the tile cache size, and how to
|
|
3017
|
+
* load tiles and sprites.
|
|
3018
|
+
* @throws If `tileCacheSize` is negative or not a number.
|
|
3019
|
+
*/
|
|
3020
|
+
constructor(options: SVGMapRendererOptions);
|
|
3021
|
+
/**
|
|
3022
|
+
* Renders one view of the map as SVG. The result is a single self-contained SVG
|
|
3023
|
+
* document, as described on {@link renderToSVG}.
|
|
2603
3024
|
*
|
|
2604
|
-
*
|
|
2605
|
-
*
|
|
2606
|
-
*
|
|
2607
|
-
|
|
3025
|
+
* @param view - Size, centre and zoom. All optional.
|
|
3026
|
+
* @returns The SVG document, as a string beginning with `<svg`.
|
|
3027
|
+
* @throws If `width` or `height` is not positive.
|
|
3028
|
+
*/
|
|
3029
|
+
renderSVG(view?: ViewOptions): Promise<string>;
|
|
3030
|
+
/**
|
|
3031
|
+
* Forgets the fetched tiles and sprite, so the next render fetches them again, e.g.
|
|
3032
|
+
* after they were updated on the server, or to free their memory.
|
|
3033
|
+
*/
|
|
3034
|
+
clearCache(): void;
|
|
3035
|
+
/** Draws `view` onto `renderer`, which must already have the view's size. */
|
|
3036
|
+
protected draw<R extends Renderer>(renderer: R, view: ViewOptions): Promise<R>;
|
|
3037
|
+
/**
|
|
3038
|
+
* Where a coordinate lands in the image of `view`: its position in the units of
|
|
3039
|
+
* `width` and `height`, the same in the SVG and on the canvas of `renderCanvas`. Use
|
|
3040
|
+
* it to place your own drawing on the map.
|
|
2608
3041
|
*
|
|
2609
|
-
*
|
|
2610
|
-
*
|
|
2611
|
-
*
|
|
2612
|
-
*
|
|
3042
|
+
* On the globe, a coordinate on its far side is hidden; then this returns `undefined`.
|
|
3043
|
+
* A position outside the image is returned as is, so it can lie beyond `width` and
|
|
3044
|
+
* `height`, or be negative. The map ends at about ±85.05° latitude, as in MapLibre: a
|
|
3045
|
+
* latitude beyond that, up to the poles, gives the position of the map's edge.
|
|
3046
|
+
*
|
|
3047
|
+
* @example Mark a place on a rendered canvas
|
|
3048
|
+
* ```ts
|
|
3049
|
+
* const view = { lon: 13.4, lat: 52.52, zoom: 12 };
|
|
3050
|
+
* const canvas = await map.renderCanvas(view);
|
|
3051
|
+
* const [x, y] = map.project(view, [13.3777, 52.5163])!; // Brandenburg Gate
|
|
3052
|
+
* const ctx = canvas.getContext('2d');
|
|
3053
|
+
* ctx.beginPath();
|
|
3054
|
+
* ctx.arc(x, y, 6, 0, 2 * Math.PI);
|
|
3055
|
+
* ctx.fill();
|
|
3056
|
+
* ```
|
|
3057
|
+
*
|
|
3058
|
+
* @param view - The view the image was rendered with.
|
|
3059
|
+
* @param lonLat - Longitude and latitude, in degrees.
|
|
3060
|
+
* @returns `[x, y]`, or `undefined` for a point hidden on the globe, and for a
|
|
3061
|
+
* longitude or latitude that is not a finite number.
|
|
3062
|
+
* @throws If `width` or `height` is not positive.
|
|
2613
3063
|
*/
|
|
2614
|
-
|
|
3064
|
+
project(view: ViewOptions, lonLat: [number, number]): [number, number] | undefined;
|
|
3065
|
+
/**
|
|
3066
|
+
* The opposite of {@link SVGMapRenderer.project}: the coordinate shown at a position in
|
|
3067
|
+
* the image of `view`, e.g. where a user clicked on it.
|
|
3068
|
+
*
|
|
3069
|
+
* Where the image shows no map — next to the globe, or beyond the poles of the mercator
|
|
3070
|
+
* map (about ±85°) — this returns `undefined`. The longitude is between -180 and 180.
|
|
3071
|
+
*
|
|
3072
|
+
* @param view - The view the image was rendered with.
|
|
3073
|
+
* @param xy - A position in the units of `width` and `height`.
|
|
3074
|
+
* @returns `[lon, lat]` in degrees, or `undefined` where there is no map, and for a
|
|
3075
|
+
* position that is not a finite number.
|
|
3076
|
+
* @throws If `width` or `height` is not positive.
|
|
3077
|
+
*/
|
|
3078
|
+
unproject(view: ViewOptions, xy: [number, number]): [number, number] | undefined;
|
|
3079
|
+
}
|
|
3080
|
+
|
|
3081
|
+
/** Options for {@link renderToSVG}: the style and the view, in one object. */
|
|
3082
|
+
interface RenderToSVGOptions extends Omit<SVGMapRendererOptions, 'tileCacheSize'>, ViewOptions {
|
|
2615
3083
|
}
|
|
2616
3084
|
/**
|
|
2617
3085
|
* Renders a MapLibre style to an SVG image.
|
|
@@ -2625,38 +3093,18 @@ interface RenderToSVGOptions {
|
|
|
2625
3093
|
* `@versatiles/svg-renderer`, `@versatiles/png-renderer` and
|
|
2626
3094
|
* `@versatiles/maplibre-svg-export`; the examples import it from the first.
|
|
2627
3095
|
*
|
|
2628
|
-
*
|
|
2629
|
-
*
|
|
2630
|
-
*
|
|
2631
|
-
* import { inlineSources, osm } from '@versatiles/style';
|
|
2632
|
-
* import { writeFile } from 'node:fs/promises';
|
|
2633
|
-
*
|
|
2634
|
-
* // inlineSources() resolves the style's TileJSON sources into tile URLs,
|
|
2635
|
-
* // which the renderer needs. Without it the map comes out empty.
|
|
2636
|
-
* const style = await inlineSources(osm({ theme: 'colorful' }));
|
|
2637
|
-
*
|
|
2638
|
-
* const svg = await renderToSVG({
|
|
2639
|
-
* style,
|
|
2640
|
-
* width: 800,
|
|
2641
|
-
* height: 600,
|
|
2642
|
-
* lon: 13.4, // Berlin
|
|
2643
|
-
* lat: 52.52,
|
|
2644
|
-
* zoom: 12,
|
|
2645
|
-
* });
|
|
2646
|
-
*
|
|
2647
|
-
* await writeFile('berlin.svg', svg);
|
|
2648
|
-
* ```
|
|
3096
|
+
* Each call starts from scratch: it parses the style and fetches the tiles and the
|
|
3097
|
+
* sprite again. To render many views of one style, use {@link SVGMapRenderer}, which
|
|
3098
|
+
* keeps them between renders.
|
|
2649
3099
|
*
|
|
2650
|
-
* @example Render a
|
|
3100
|
+
* @example Render a map
|
|
2651
3101
|
* ```ts
|
|
2652
3102
|
* import { renderToSVG } from '@versatiles/svg-renderer';
|
|
2653
3103
|
*
|
|
2654
|
-
*
|
|
2655
|
-
* const style = await fetch(
|
|
2656
|
-
* 'https://tiles.versatiles.org/assets/styles/colorful/style.json',
|
|
2657
|
-
* ).then((response) => response.json());
|
|
3104
|
+
* const url = 'https://tiles.versatiles.org/assets/styles/colorful/style.json';
|
|
3105
|
+
* const style = await (await fetch(url)).json();
|
|
2658
3106
|
*
|
|
2659
|
-
*
|
|
3107
|
+
* const svg = await renderToSVG({ style, lon: 13.4, lat: 52.52, zoom: 12 });
|
|
2660
3108
|
* ```
|
|
2661
3109
|
*
|
|
2662
3110
|
* @example Include labels and icons
|
|
@@ -2670,5 +3118,5 @@ interface RenderToSVGOptions {
|
|
|
2670
3118
|
*/
|
|
2671
3119
|
declare function renderToSVG(options: RenderToSVGOptions): Promise<string>;
|
|
2672
3120
|
|
|
2673
|
-
export { renderToSVG };
|
|
2674
|
-
export type { RenderToSVGOptions };
|
|
3121
|
+
export { SVGMapRenderer, renderToSVG };
|
|
3122
|
+
export type { FetchFunction, FetchResponse, RenderToSVGOptions, SVGMapRendererOptions, ViewOptions };
|