MapScale Docs
Visualizations

A night-city restyle with accent colours and layer groups

Restyle a basemap from one accent colour: neon road glow, palette switching and layer-group toggles, then the same idea with @mapscale/gl.

Beginner

What you'll build

A Bangkok night map where one accent colour drives the neon glow on the highways, five swatches repaint it live, a theme button swaps to a light palette, and checkboxes show or hide whole groups of layers. The keyless demo path below hand-builds a style over the public Thailand archive so it runs with no key and no build step; the @mapscale/gl path comes in step 5. Basemap data is © OpenStreetMap contributors.

What you'll build
cd docs/mapscale/visualizations/night-city
python3 -m http.server 4105
# open http://localhost:4105/

Keep colours in palettes and tag layers with groups

Every colour is a $token resolved against one of two palettes plus one accent, and every layer carries a group tag. That gives one source of truth you can repaint on demand. Steps 2 to 4 build a single script in order; the page below is the start of it.

Keep colours in palettes and tag layers with groups
<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	<title>Night city</title>
	<style>
		html,
		body,
		#map {
			height: 100%;
			margin: 0;
		}
	</style>
	<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.css" />
	<script src="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.js"></script>
	<script src="https://unpkg.com/pmtiles@4/dist/pmtiles.js"></script>
</head>
<body>
	<div id="map"></div>
	<div style="position:absolute;top:12px;left:12px;z-index:1;background:rgba(10,13,18,0.85);color:#cfe7ff;font:12px/1.6 ui-monospace,monospace;padding:8px 10px;border-radius:6px">
		<div id="swatches"></div>
		<div id="toggles"></div>
		<button id="theme" type="button">Switch to light</button>
	</div>
	<script>
maplibregl.addProtocol("pmtiles", new pmtiles.Protocol().tile)

// Every non-accent colour lives in one of two palettes, so the theme button
// is just "apply the other palette".
const PALETTES = {
	dark: {
		bg: "#05070b", land: "#080b11", water: "#04101c", waterway: "#0a2236",
		building: "#1c2436", minor: "#1a2230", secondary: "#2a3446", glowOpacity: 0.45,
	},
	light: {
		bg: "#f3f1ec", land: "#eceae3", water: "#bcd8ec", waterway: "#a8cbe3",
		building: "#d8d4cb", minor: "#ffffff", secondary: "#fbfaf6", glowOpacity: 0.28,
	},
}
const ACCENTS = ["#ff2bd6", "#00e5ff", "#a6ff00", "#ff9100", "#7c4dff"]
let accent = ACCENTS[0]
let theme = "dark"

// Layer definitions tagged with a group. Colours are "$tokens": "$water" means
// PALETTES[theme].water and "$accent" means the current accent.
const lines = ["==", "$type", "LineString"]
const w = (stops) => ({ base: 1.2, stops })
const road = (id, group, cls, color, width, extra = {}) => ({
	id, group, type: "line", "source-layer": "transportation",
	filter: ["all", lines, cls],
	layout: { "line-cap": "round", "line-join": "round" },
	paint: { "line-color": color, "line-width": w(width), ...extra },
})
const motorway = ["in", "class", "motorway", "trunk"]
const primary = ["==", "class", "primary"]

const DEFS = [
	{ id: "background", group: "land", type: "background", paint: { "background-color": "$bg" } },
	{ id: "landuse", group: "land", type: "fill", "source-layer": "landuse", paint: { "fill-color": "$land" } },
	{ id: "water", group: "water", type: "fill", "source-layer": "water", paint: { "fill-color": "$water" } },
	{
		id: "waterway", group: "water", type: "line", "source-layer": "waterway",
		paint: { "line-color": "$waterway", "line-width": w([[10, 0.5], [18, 5]]) },
	},
	{
		// The archive only carries buildings from about z13: zoom in to see them.
		id: "buildings", group: "buildings", type: "fill-extrusion", "source-layer": "building", minzoom: 12,
		paint: {
			"fill-extrusion-color": "$building",
			"fill-extrusion-height": ["coalesce", ["get", "render_height"], 8],
			"fill-extrusion-base": ["coalesce", ["get", "render_min_height"], 0],
			"fill-extrusion-opacity": 0.92,
		},
	},
	road("street-minor", "streets", ["in", "class", "minor", "service", "track"], "$minor", [[13, 0.4], [18, 7]]),
	road("street-secondary", "streets", ["in", "class", "secondary", "tertiary"], "$secondary", [[11, 0.5], [18, 10]]),
	// Step 3 adds the glowing highways here (DEFS.push(...)).
]
	</script>
</body>
</html>

Add the neon glow

The glow is a wide, low-opacity, heavily blurred line stacked under the crisp road line. Both use $accent, so changing one colour restyles the whole highway: glow, line and a thin white-hot core that makes it read as neon tubing. Paste this where the DEFS comment says.

Add the neon glow
DEFS.push(
	road("glow-primary", "highways", primary, "$accent", [[9, 3], [18, 28]], { "line-blur": 10, "line-opacity": "$glow" }),
	road("glow-motorway", "highways", motorway, "$accent", [[6, 4], [18, 44]], { "line-blur": 16, "line-opacity": "$glow" }),
	road("road-primary", "highways", primary, "$accent", [[9, 0.6], [18, 9]], { "line-opacity": 0.8 }),
	road("road-motorway", "highways", motorway, "$accent", [[6, 0.8], [18, 12]]),
	road("core-motorway", "highways", motorway, "$core", [[8, 0.2], [18, 3.5]], { "line-opacity": 0.85 })
)

Resolve tokens, build the map, repaint and toggle groups

GROUPS maps a group name to its layer ids, the same idea as MAP_LAYER_GROUPS in @mapscale/gl. resolve() turns $tokens into real colours and the group tag is stripped before the style is built. To change the accent or theme later, push every token colour back through setPaintProperty instead of reloading the style. Group checkboxes use setLayoutProperty with visibility.

Resolve tokens, build the map, repaint and toggle groups
const GROUPS = {}
for (const d of DEFS) {
	if (d.id !== "background") (GROUPS[d.group] ??= []).push(d.id)
}

const resolve = (value, p) => {
	if (typeof value !== "string" || value[0] !== "$") return value
	const key = value.slice(1)
	if (key === "accent") return accent
	if (key === "core") return theme === "dark" ? "#ffffff" : accent
	if (key === "glow") return p.glowOpacity
	return p[key]
}
const resolvePaint = (paint, p) =>
	Object.fromEntries(Object.entries(paint).map(([k, v]) => [k, resolve(v, p)]))

// Strip our group tag (not valid style spec) and resolve colours.
const layers = DEFS.map(({ group, ...layer }) => ({
	...layer,
	...(layer.type === "background" ? {} : { source: "ms" }),
	paint: resolvePaint(layer.paint, PALETTES[theme]),
}))

const map = new maplibregl.Map({
	container: "map",
	center: [100.5018, 13.7563], // Bangkok
	zoom: 12,
	pitch: 45,
	bearing: -12,
	minZoom: 4,
	style: {
		version: 8,
		glyphs: "https://maps.thaipropertiesnetwork.com/{fontstack}/{range}.pbf",
		sources: {
			ms: {
				type: "vector",
				url: "pmtiles://https://maps.thaipropertiesnetwork.com/thailand-v2.pmtiles",
				attribution: "© OpenStreetMap contributors · MapScale",
			},
		},
		layers,
	},
})

// Repaint without reloading the style.
function repaint() {
	const p = PALETTES[theme]
	for (const d of DEFS) {
		for (const [prop, value] of Object.entries(d.paint)) {
			if (typeof value === "string" && value[0] === "$") {
				map.setPaintProperty(d.id, prop, resolve(value, p))
			}
		}
	}
}

// One swatch per accent colour.
const swatches = document.getElementById("swatches")
for (const color of ACCENTS) {
	const b = document.createElement("button")
	b.type = "button"
	b.style.background = color
	b.title = color
	b.onclick = () => {
		accent = color
		repaint()
	}
	swatches.appendChild(b)
}

// One checkbox per group.
const toggles = document.getElementById("toggles")
for (const [group, ids] of Object.entries(GROUPS)) {
	const l = document.createElement("label")
	l.style.display = "block"
	const cb = document.createElement("input")
	cb.type = "checkbox"
	cb.checked = true
	cb.onchange = () => {
		for (const id of ids) map.setLayoutProperty(id, "visibility", cb.checked ? "visible" : "none")
	}
	l.append(cb, " " + group)
	toggles.appendChild(l)
}

// Theme button: re-apply the other palette.
const themeBtn = document.getElementById("theme")
themeBtn.onclick = () => {
	theme = theme === "dark" ? "light" : "dark"
	themeBtn.textContent = theme === "dark" ? "Switch to light" : "Switch to dark"
	repaint()
}

The same idea with @mapscale/gl

In a bundler app, createMapStyle(lang, { accentColor, transformStyle }) from @mapscale/gl/style returns a style object. Be aware that accentColor only retints paint values equal to the default #0c4a4a, which the shipped base style no longer uses, so on its own it changes nothing visible. Do the real restyle in transformStyle: a pure function that must not mutate its input. The layer ids below (background, water, building, highway-*) are from the shipped style. Layer groups come from @mapscale/gl/layer-groups: MAP_LAYER_GROUPS has the keys streets, transit, districts, buildings, water and landuse; resolveHiddenLayerIds turns a visibility record into layer ids and applyLayerVisibility(style, hiddenLayerIds) returns a copy with those layers hidden. Without a build step, the hosted style also accepts accent and hide parameters. This part is the documented API; the runnable example uses the keyless route above instead.

The same idea with @mapscale/gl
import { createMapStyle } from "@mapscale/gl/style"
import { applyLayerVisibility, DEFAULT_LAYER_VISIBILITY, resolveHiddenLayerIds } from "@mapscale/gl/layer-groups"

const ACCENT = "#ff2bd6"

// Pure: build new layer objects, never mutate the input.
const nightStyle = createMapStyle("en", {
	transformStyle: (style) => ({
		...style,
		layers: style.layers.map((layer) => {
			const sourceLayer = layer["source-layer"]
			if (layer.id === "background") return { ...layer, paint: { "background-color": "#05070b" } }
			if (layer.id === "water-pattern") return { ...layer, layout: { ...layer.layout, visibility: "none" } }
			if (layer.type === "fill" && sourceLayer === "water") return { ...layer, paint: { ...layer.paint, "fill-color": "#04101c" } }
			if (layer.type === "fill" && sourceLayer === "building") return { ...layer, paint: { ...layer.paint, "fill-color": "#1c2436" } }
			if (layer.type === "line" && sourceLayer === "transportation") {
				const isMajor = /motorway|trunk|primary/.test(layer.id)
				const color = layer.id.includes("casing") ? "#05070b" : isMajor ? ACCENT : "#1a2230"
				return { ...layer, paint: { ...layer.paint, "line-color": color } }
			}
			return layer
		}),
	}),
})

// Hide groups: here, hide buildings.
const hidden = resolveHiddenLayerIds({ ...DEFAULT_LAYER_VISIBILITY, buildings: false })
const withoutBuildings = applyLayerVisibility(nightStyle, hidden)

// No build step: the hosted style takes the same ideas as query parameters.
// https://api.mapscale.io/styles/v1/streets?lang=th&accent=RRGGBB&hide=<groups>&key=pk_test_your_key_here

Where to go next

The complete, tested keyless page is docs/mapscale/visualizations/night-city (it also adds road labels and rail). Buildings only exist in the tiles from about zoom 13, so zoom in to see the 3D extrusions. Try a different accent palette, a longer glow, or a group of your own. Keep © OpenStreetMap contributors visible on any map you publish.

On this page

Analytics settings