Skip to content

Commit afdb1df

Browse files
author
Les Moffat
committed
feat(radialgradient/spacebox): implement stylejson for styling of RadialGradientLayer and Spacebox
1 parent c9ebf8a commit afdb1df

File tree

6 files changed

+79
-24
lines changed

6 files changed

+79
-24
lines changed

demos/07-spacebox.html

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@
4040
zoom: 1,
4141
projection: "globe",
4242
center: [0, -20],
43+
// space: {
44+
// color: "red",
45+
// },
46+
halo: true,
47+
// space: true,
4348
space: {
4449
color: "#111122",
45-
path: {
46-
baseUrl: "spacebox/transparent",
47-
format: "png", //defaults to png
48-
},
50+
// path: {
51+
// baseUrl: "spacebox/transparent",
52+
// format: "png", //defaults to png
53+
// },
4954
// OR
5055
// faces: {
5156
// pX: '/som-path-to-an-image/pX',
@@ -58,14 +63,14 @@
5863
// OR
5964
// preset?: "universe-dark";
6065
},
61-
halo: {
62-
scale: 1,
63-
stops: [
64-
[0.0, "transparent"],
65-
[0.2, "skyblue"],
66-
[0.3, "transparent"]
67-
],
68-
}
66+
// halo: {
67+
// scale: 1,
68+
// stops: [
69+
// [0.0, "transparent"],
70+
// [0.2, "skyblue"],
71+
// [0.3, "transparent"]
72+
// ],
73+
// }
6974
});
7075

7176
// Or alternatively, you can set the space and halo imperatively

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@
5454
"*.ts": "npm run lint:fix"
5555
},
5656
"devDependencies": {
57+
"@eslint/js": "^9.21.0",
5758
"@types/color-convert": "^2.0.4",
5859
"@types/color-name": "^2.0.0",
59-
"@eslint/js": "^9.21.0",
6060
"@types/uuid": "^10.0.0",
6161
"@types/xmldom": "^0.1.31",
6262
"@xmldom/xmldom": "^0.8.10",

src/Map.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { MaptilerProjectionControl } from "./MaptilerProjectionControl";
4141
import { Telemetry } from "./Telemetry";
4242
import { CubemapDefinition, CubemapLayer, CubemapLayerConstructorOptions } from "./custom-layers/CubemapLayer";
4343
import { GradientDefinition, RadialGradientLayer, RadialGradientLayerOptions } from "./custom-layers/RadialGradientLayer";
44+
import extractCustomLayerStyle from "./custom-layers/extractCustomLayerStyle";
4445

4546
export type LoadWithTerrainEvent = {
4647
type: "loadWithTerrain";
@@ -198,8 +199,8 @@ export type MapOptions = Omit<MapOptionsML, "style" | "maplibreLogo"> & {
198199
*
199200
* Default: { color: "#1D29F1" }
200201
*/
201-
space?: CubemapLayerConstructorOptions;
202-
halo?: RadialGradientLayerOptions;
202+
space?: CubemapLayerConstructorOptions | boolean;
203+
halo?: RadialGradientLayerOptions | boolean;
203204
};
204205

205206
/**
@@ -716,6 +717,7 @@ export class Map extends maplibregl.Map {
716717
}
717718

718719
// Display a message if WebGL context is lost
720+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
719721
this.once("load", () => {
720722
this.getCanvas().addEventListener("webglcontextlost", (event) => {
721723
if (this._removed === true) {
@@ -734,12 +736,14 @@ export class Map extends maplibregl.Map {
734736
const firstLayer = this.getLayersOrder()[0];
735737

736738
if (options.space) {
737-
this.space = new CubemapLayer(options.space);
739+
const optionsFromStyleSpec = extractCustomLayerStyle({ map: this, property: "space" });
740+
this.space = new CubemapLayer(options.space ?? optionsFromStyleSpec);
738741
this.addLayer(this.space, firstLayer);
739742
}
740743

741744
if (options.halo) {
742-
this.halo = new RadialGradientLayer(options.halo);
745+
const optionsFromStyleSpec = extractCustomLayerStyle({ map: this, property: "halo" });
746+
this.halo = new RadialGradientLayer(options.halo ?? optionsFromStyleSpec);
743747
this.addLayer(this.halo, firstLayer);
744748
}
745749
});
@@ -865,7 +869,6 @@ export class Map extends maplibregl.Map {
865869
this.once("idle", () => {
866870
this.forceLanguageUpdate = false;
867871
});
868-
869872
const styleInfo = styleToStyle(style);
870873

871874
if (styleInfo.requiresUrlMonitoring) {

src/custom-layers/RadialGradientLayer/RadialGradientLayer.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,11 @@ const VERTICES = [
2929
];
3030

3131
const defaultConstructorOptions: RadialGradientLayerOptions = {
32-
scale: 1,
32+
scale: 0.9,
3333
stops: [
3434
[0.0, "rgba(176, 208, 240, 1)"],
35-
[0.2, "rgba(98, 168, 229, 0.8)"],
36-
[0.4, "rgba(32, 112, 208, 0.6)"],
37-
[0.6, "rgba(16, 42, 85, 0.4)"],
38-
[0.8, "rgba(10, 15, 37, 0.2)"],
39-
[1.0, "rgba(0, 0, 0, 0)"],
35+
[0.1, "rgba(98, 168, 229, 0.3)"],
36+
[0.2, "rgba(98, 168, 229, 0.0)"],
4037
],
4138
};
4239

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Map as MapSDK } from "../Map";
2+
// import { CubemapLayerConstructorOptions } from "./CubemapLayer";
3+
// import { RadialGradientLayerOptions } from "./RadialGradientLayer";
4+
import { StyleSpecification } from "maplibre-gl";
5+
import type { CubemapLayerConstructorOptions, RadialGradientLayerOptions } from "custom-layers";
6+
7+
export type StyleSpecificationWithMetaData = StyleSpecification & {
8+
metadata?: {
9+
"maptiler:copyright": string;
10+
maptiler?: {
11+
halo: RadialGradientLayerOptions;
12+
space: CubemapLayerConstructorOptions;
13+
};
14+
};
15+
};
16+
17+
export interface IExtractCustomLayerStyleOptions {
18+
map: MapSDK;
19+
property: "halo" | "space";
20+
}
21+
22+
export type CustomLayerDefinitionType = CubemapLayerConstructorOptions | RadialGradientLayerOptions | null
23+
;
24+
25+
export default function extractCustomLayerStyle(options: IExtractCustomLayerStyleOptions): CustomLayerDefinitionType {
26+
const { map, property } = options;
27+
28+
const style = map.getStyle() as StyleSpecificationWithMetaData;
29+
30+
if (!style.metadata?.maptiler) {
31+
console.warn(`No custom layer metadata found in the style for property ${property}. The property will use default values.`);
32+
return null;
33+
}
34+
35+
// Because this data is from an external source
36+
// we ignore typescript and check anyway.
37+
if (style.metadata.maptiler[property]) {
38+
return style.metadata.maptiler[property];
39+
}
40+
41+
return null;
42+
}

0 commit comments

Comments
 (0)