-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
176 lines (168 loc) · 5.43 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Invert and Rotate</title>
<meta
name="viewport"
content="initial-scale=1,maximum-scale=1,user-scalable=no"
/>
<link
href="https://api.mapbox.com/mapbox-gl-js/v2.10.0/mapbox-gl.css"
rel="stylesheet"
/>
<script src="https://api.mapbox.com/mapbox-gl-js/v2.10.0/mapbox-gl.js"></script>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<style>
body {
overflow: hidden;
}
body * {
-webkit-touch-callout: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-compare/v0.4.0/mapbox-gl-compare.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/chroma-js/2.4.2/chroma.min.js"
integrity="sha512-zInFF17qBFVvvvFpIfeBzo7Tj7+rQxLeTJDmbxjBz5/zIr89YVbTNelNhdTT+/DCrxoVzBeUPVFJsczKbB7sew=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
></script>
<link
rel="stylesheet"
href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-compare/v0.4.0/mapbox-gl-compare.css"
type="text/css"
/>
<div id="comparison-container">
<div id="before" class="map"></div>
<div id="after" class="map"></div>
</div>
<script>
mapboxgl.accessToken =
"pk.eyJ1Ijoic3RhbWVuIiwiYSI6ImNsODRrcHZlYjAwZjczdm1jNm9hODh4MTUifQ.U4n1mMIjwHVjaTqXc4Et-g";
const query = new URLSearchParams(window.location.search);
const styleUrl =
query.get("style") || "mapbox://styles/mapbox/streets-v11";
if (!query.get("style")) {
query.set("style", styleUrl);
window.location.search = query.toString();
}
const beforeMap = new mapboxgl.Map({
container: "before",
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: styleUrl,
center: [0, 0],
zoom: 0,
hash: true,
});
const afterMap = new mapboxgl.Map({
container: "after", // container ID
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: styleUrl, // style URL
center: [0, 0], // starting position [lng, lat]
zoom: 0, // starting zoom
hash: true,
});
const deepIter = (o) => {
return o.map((v) => {
if (chroma.valid(v) && typeof v !== "number") {
const color = chroma.rgb(
chroma(v)
.rgb()
.map((color) => {
return 255 - color;
})
);
return color.set("hsl.h", (color.get("hsl.h") + 180) % 360).hex();
} else if (Array.isArray(v)) {
return deepIter(v);
} else {
return v;
}
});
};
afterMap.once("style.load", () => {
afterMap.setFog({}); // Set the default atmosphere style
const style = afterMap.getStyle();
style.layers = style.layers.map((layer, i) => {
if (layer.type == "raster") {
// raster styling needs different handling than vectors
let rotate = 180;
if ("paint" in layer && "raster-hue-rotate" in layer.paint) {
rotate = (layer.paint["raster-hue-rotate"] + 180) % 360;
}
let minBrightness = 1;
if ("paint" in layer && "raster-brightness-min" in layer.paint) {
minBrightness = 1 - layer.paint["raster-brightness-min"]
}
let maxBrightness = 0;
if ("paint" in layer && "raster-brightness-max" in layer.paint) {
maxBrightness = 1 - layer.paint["raster-brightness-max"];
}
layer.paint = {
"raster-hue-rotate": rotate,
"raster-brightness-min": minBrightness,
"raster-brightness-max": maxBrightness
}
} else if (layer.paint) {
for (const [k, v] of Object.entries(layer.paint)) {
console.log(v)
if (/.*color/.test(k)) {
if (!Array.isArray(v)) {
const color = chroma.rgb(
chroma(v)
.rgb()
.map((color) => {
return 255 - color;
})
);
layer.paint[k] = color
.set("hsl.h", (color.get("hsl.h") + 180) % 360)
.hex();
} else {
const expression = deepIter(v);
layer.paint[k] = expression;
}
}
}
}
return layer;
});
afterMap.setStyle(style);
});
// A selector or reference to HTML element
const container = "#comparison-container";
const map = new mapboxgl.Compare(beforeMap, afterMap, container, {
// Set this to enable comparing two maps by mouse movement:
// mousemove: true
});
window.api = {
beforeMap: beforeMap,
afterMap: afterMap
}
</script>
</body>
</html>