ipmap/index.html

303 lines
15 KiB
HTML
Raw Normal View History

2024-03-31 20:21:11 -04:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="color-scheme" content="dark light">
<title>IP Map</title>
<script src="https://unpkg.com/maplibre-gl@4.1.2/dist/maplibre-gl.js"></script>
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@4.1.2/dist/maplibre-gl.css" />
<link rel="shortcut icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAABGAAAARgGVRxdWAAAJnUlEQVR4nO1bS2xjVxn+fK+d6/gd5zFJxpNkmEdbOZqGjkphgSaMYBbtYtJSIyEW00pI7ArdwGI20wULkFBTITZIqMNDLAiLjATVUIRIBRJFKiEZ4ekM0MaZpHnZSWzHdvy+6L/nJLGd+7TjzKDOJx3Zvo///Oc//znn/79zbJNlGZ9mCJ/q1j82wMMwwFT0FaWYvd5mHO8cMBW9CeAa//UqIuGb/Do1/G1+/eeIhI/NEMdjgKloAAA19mrDnVf559sN128hEp5ov2LHZ4DanjeLY/GE9s8BU9Ebmo0vllhRxzX+blvRXg+Yio4B+Kfm/dQO+/R79aR8FpHw3JHrxtFuD5jUvbtbYKUVGS2ifQaYio4DuKT7TKXCij4ucU9qC9rpAcazeL7AijHaNhm20wBH2Wv/lx6g7/4PT1YdHucCbZSdekRl1aGdBjjKtbttcYDd9JMsnqfJaLzhzgwi4RmVN2KG84Bo2v4xDZ3GVfUhg0XCSTOCjSPBqegEX4YaE5lapHjAMrlfcX2Gp461BLvc32OkZ23mSB3xHV78Ou/cUhKwSHhaT7C2AZh1qdJhI+1qkOJGuIGp6AiABd2nt/jQDuq1Q8FpRMIxnhsYNbwRi0oHqnupigG0U1crmOdeM6k7DDI59ulx6Yl+jzeadHq6BZ1ucUPUDQ21QXijxcaDK0oW1x2HGa+sFAMkuaxWGg/epkPZpZoHGLvuESBry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aOc7p8sM/sxJfikeS0Kd6BZJJsqqNmaW1K1xqYN4DecVVa8oiqDvI9AnOYVhIZlszoHmLYB8mmOqguvfjAwtFaq5Ogek/RzhHx8uSm5lG7VW3+UDTVQXXFNbcuLbFYVg1weLzuHXe11vi36kJW9v0t028r84HmMVtLSVHrBiCcPGFFxqJGj98wXBLN1WnJANYIETZW9xjjmFKZKFpJiVOaHB1dYweyZkzLE8U9tndc2fBgxdx8wtH6/wXM7yHMcwZHf4Jia7jZ80CqXL8VHMVBSaP0c5HvyIyZmp3pGXqW3jEeEpZSXzVYHQJmlZjnnMG00Tk9TbAtrEk+LCY4GdvoFS0b4CiGQKDmOHuynX9v4fWN1UR6lhhgNTz+7/AjoMNDxWMDPAI6PDwA+B/ygrofO/r6jAAAAABJRU5ErkJggg==" />
2024-03-31 20:21:11 -04:00
<style>
body {
margin: 0;
padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
main {
2024-03-31 20:21:11 -04:00
width: 100vh;
height: 100%;
margin: 0 auto;
}
#map {
width: 100%;
height: 100%;
}
2024-03-31 20:21:11 -04:00
.maplibregl-canvas {
cursor: pointer;
}
.maplibregl-popup-content {
background-color: #222;
font-size: 1rem;
padding: 0.8rem 1.2rem;
}
.map-overlay {
position: absolute;
top: 1rem;
right: 1rem;
padding: 1.5rem;
background-color: #222;
color: #eee;
box-shadow: 3px 3px 2px rgba(0, 0, 0, 0.8);
border-radius: 3px;
}
.map-overlay h2 {
display: block;
margin: 0;
margin-bottom: 1rem;
}
#map-style-controls ul {
padding-left: 1em;
list-style-type: none;
}
#map-style-controls > ul {
padding-left: 0;
}
#map-style-controls ul.hidden {
display: none;
}
#map-style-controls label {
display: block;
padding: 0.5rem;
font-weight: bold;
}
#map-style-controls input[type=radio] {
padding: 0;
margin: 0;
border: 0;
margin-right: 0.5rem;
}
2024-03-31 20:21:11 -04:00
</style>
</head>
<body>
<main>
<div id="map"></div>
<div class="map-overlay">
<h2>Map Style</h2>
<div id="map-style-controls"><p>Loading available styles...</p></div>
</div>
</main>
2024-03-31 20:21:11 -04:00
<script>
const coordsToHilbert = ({ x, y }) => {
2024-04-02 09:43:54 -04:00
let rotation = 0
let reflection = 0
let index = 0
for (let b = 15; b >= 0; b--) {
let bits = reflection
reflection = (y >>> b) & 1
reflection |= ((x >>> b) & 1) << 1
bits = bits ^ reflection
bits = ((bits >>> rotation) | (bits << (2 - rotation))) & 3
index |= bits << (b << 1)
reflection ^= (1 << rotation)
bits = bits & (-bits) & 1
while (bits) {
++rotation
bits >>>= 1
}
if (++rotation >= 2)
rotation -= 2
2024-03-31 20:21:11 -04:00
}
2024-04-02 09:43:54 -04:00
index ^= 0x2aaaaaaa
for (let d = 1; d < 32; d *= 2) {
let t = index >>> d
if (!t) break
index ^= t
2024-03-31 20:21:11 -04:00
}
2024-04-02 09:43:54 -04:00
return index
2024-03-31 20:21:11 -04:00
}
const hilbertToCoords = index => {
2024-04-02 09:43:54 -04:00
let rotation = 0
let reflection = 0
let coord = { x: 0, y: 0 }
index ^= (index >>> 1) ^ 0x2aaaaaaa
for (let b = 15; b >= 0; b--) {
var bits = index >>> (2 * b) & 3
reflection ^= ((bits >>> (2 - rotation)) | (bits << rotation)) & 3
coord.x |= (reflection & 1) << b
coord.y |= ((reflection >>> 1) & 1) << b
reflection ^= (1 << rotation)
bits &= (-bits) & 1
2024-03-31 20:21:11 -04:00
while (bits) {
2024-04-02 09:43:54 -04:00
bits >>>= 1
++rotation
2024-03-31 20:21:11 -04:00
}
if (++rotation >= 2)
2024-04-02 09:43:54 -04:00
rotation -= 2
2024-03-31 20:21:11 -04:00
}
2024-04-02 09:43:54 -04:00
return coord
2024-03-31 20:21:11 -04:00
}
const tilesDir = "tiles"
const sourceId = "ipmap-tiles-source"
const styleControlsDiv = document.getElementById("map-style-controls")
2024-04-02 09:43:54 -04:00
const dateDir = (date = new Date()) => `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}`
const getId = (date, variant, colormap) => `${date.replaceAll("-", "")}-${variant}-${colormap}`
const getTilesUrl = (date, variant, colormap) => `${tilesDir}/${date}/${variant}/${colormap}/{z}/{y}/{x}.png`
const getSourceId = (date, variant, colormap) => `ipmap-tiles-source-${getId(date, variant, colormap)}`
const getLayerId = (date, variant, colormap) => `ipmap-tiles-layer-${getId(date, variant, colormap)}`
2024-04-02 09:43:54 -04:00
2024-03-31 20:21:11 -04:00
const map = new maplibregl.Map({
container: "map",
attributionControl: false,
renderWorldCopies: false,
doubleClickZoom: false,
dragRotate: false,
style: {
2024-04-02 09:43:54 -04:00
version: 8,
sources: {},
layers: []
2024-03-31 20:21:11 -04:00
},
center: [0, 0],
minZoom: -1,
maxZoom: 12,
zoom: 0
})
map.painter.context.extTextureFilterAnisotropic = undefined
const dataP = fetch(`${tilesDir}/tiles.json`).then(res => res.json())
map.once("style.load", async () => {
const data = await dataP
const flatData = Object.entries(data)
.sort(([a], [b]) => a.localeCompare(b))
.flatMap(([date, variantData]) =>
Object.entries(variantData)
.sort(([a], [b]) => a.localeCompare(b))
.flatMap(([variant, colormaps]) => colormaps
.sort((a, b) => a.localeCompare(b))
.flatMap(colormap => ({ date, variant, colormap }))))
if (flatData.length === 0) {
console.log("no data found")
return
}
let { date: curDate, variant: curVariant, colormap: curColormap } = flatData[flatData.length - 1]
map.addSource(sourceId, {
type: "raster",
tiles: [getTilesUrl(curDate, curVariant, curColormap)],
tileSize: 256,
minzoom: 0,
maxzoom: 8,
})
map.addLayer({
id: "ipmap-tiles-layer",
type: "raster",
source: sourceId,
paint: {
"raster-resampling": "nearest"
}
})
const setStyle = (date, variant, colormap) => {
if (date === curDate && variant === curVariant && colormap === curColormap || !data[date]?.[variant]?.includes(colormap))
return
map.getSource(sourceId)?.setTiles([getTilesUrl(date, variant, colormap)])
curDate = date
curVariant = variant
curColormap = colormap
}
const dateList = document.createElement("ul")
for (const [date, variantData] of Object.entries(data).sort(([a], [b]) => a.localeCompare(b))) {
const isCurDate = date === curDate
const dateInput = document.createElement("input")
dateInput.type = "radio"
dateInput.name = "date"
dateInput.value = date
dateInput.checked = isCurDate
const dateLabel = document.createElement("label")
dateLabel.appendChild(dateInput)
dateLabel.appendChild(document.createTextNode(date))
const dateItem = document.createElement("li")
dateItem.appendChild(dateLabel)
const variantList = document.createElement("ul")
if (!isCurDate) variantList.className = "hidden"
for (const [variant, colormaps] of Object.entries(variantData).sort(([a], [b]) => a.localeCompare(b))) {
const isCurVariant = variant === curVariant
const variantInput = document.createElement("input")
variantInput.type = "radio"
variantInput.name = `${date}-variant`
variantInput.value = variant
variantInput.checked = isCurVariant
const variantLabel = document.createElement("label")
variantLabel.appendChild(variantInput)
variantLabel.appendChild(document.createTextNode(variant))
const variantItem = document.createElement("li")
variantItem.appendChild(variantLabel)
const colormapList = document.createElement("ul")
if (!isCurVariant) colormapList.classList.add("hidden")
for (const colormap of colormaps.sort((a, b) => a.localeCompare(b))) {
const isCurColormap = colormap === curColormap
const colormapInput = document.createElement("input")
colormapInput.type = "radio"
colormapInput.name = `${date}-${variant}-colormap`
colormapInput.value = colormap
colormapInput.checked = isCurColormap
const colormapLabel = document.createElement("label")
colormapLabel.appendChild(colormapInput)
colormapLabel.appendChild(document.createTextNode(colormap))
const colormapItem = document.createElement("li")
colormapItem.appendChild(colormapLabel)
colormapItem.addEventListener("click", e => {
;[...dateList.children].forEach(el =>
[...el.lastChild.children].forEach(el =>
[...el.lastChild.children].forEach(el => {
const cb = el.firstChild.firstChild
cb.checked = cb.value === colormap
})))
setStyle(curDate, curVariant, colormap)
})
colormapList.appendChild(colormapItem)
}
variantInput.addEventListener("click", e => {
;[...dateList.children].forEach(el =>
[...el.lastChild.children].forEach(el => {
const cb = el.firstChild.firstChild
const isCur = cb.value === variant
el.lastChild.className = isCur ? "" : "hidden"
cb.checked = isCur
}))
setStyle(curDate, variant, curColormap)
})
variantItem.appendChild(colormapList)
variantList.appendChild(variantItem)
}
dateInput.addEventListener("click", e => {
;[...dateList.children].forEach(el => {
const cb = el.firstChild.firstChild
const isCur = cb.value === date
el.lastChild.className = isCur ? "" : "hidden"
})
setStyle(date, curVariant, curColormap)
})
dateItem.appendChild(variantList)
dateList.appendChild(dateItem)
}
styleControlsDiv.replaceChildren(dateList)
})
2024-03-31 20:21:11 -04:00
map.addControl(new maplibregl.NavigationControl({ showCompass: false }), "top-left")
const toIp = v => `${v >> 24 & 0xFF}.${v >> 16 & 0xFF}.${v >> 8 & 0xFF}.${v >> 0 & 0xFF}`
map.on("click", (e) => {
const { x, y } = maplibregl.MercatorCoordinate.fromLngLat(e.lngLat, 0)
const rawIp = coordsToHilbert({ x: Math.floor(0x10000 * x), y: Math.floor(0x10000 * y) })
2024-03-31 20:21:11 -04:00
const subnet = Math.min(32, Math.round(map.getZoom()) * 2 + 18)
const text = subnet < 32 ?
`Range: ${toIp((rawIp >> (32 - subnet)) << (32 - subnet))}/${subnet}` :
`IP: ${toIp(rawIp)}`
new maplibregl.Popup()
.setHTML(text)
.setLngLat(e.lngLat)
.addTo(map)
})
</script>
</body>
</html>