75 lines
3.2 KiB
JavaScript
75 lines
3.2 KiB
JavaScript
function Unmined() {
|
|
|
|
this.map = function (mapId, options, regions) {
|
|
|
|
const minMapX = options.minRegionX * 512,
|
|
minMapY = options.minRegionZ * 512,
|
|
mapWidth = (options.maxRegionX + 1 - options.minRegionX) * 512,
|
|
mapHeight = (options.maxRegionZ + 1 - options.minRegionZ) * 512,
|
|
zoomOffset = 0 - options.minZoom,
|
|
|
|
unminedLayer = new L.TileLayer.Functional(
|
|
function (view) {
|
|
const zoom = view.zoom - zoomOffset,
|
|
zoomFactor = Math.pow(2, zoom),
|
|
|
|
tileSize = 256,
|
|
|
|
minTileX = Math.floor(minMapX * zoomFactor / tileSize),
|
|
minTileY = Math.floor(minMapY * zoomFactor / tileSize),
|
|
maxTileX = Math.ceil((minMapX + mapWidth) * zoomFactor / tileSize) - 1,
|
|
maxTileY = Math.ceil((minMapY + mapHeight) * zoomFactor / tileSize) - 1,
|
|
|
|
tileX = view.tile.column,
|
|
tileY = view.tile.row,
|
|
|
|
tileBlockSize = tileSize / zoomFactor,
|
|
tileBlockPoint = {
|
|
x: tileX * tileBlockSize,
|
|
z: tileY * tileBlockSize
|
|
};
|
|
|
|
const intersectsWithTile = function (region) {
|
|
return (tileBlockPoint.x < (region.x + 1) * 512)
|
|
&& (tileBlockPoint.x + tileBlockSize > region.x * 512)
|
|
&& (tileBlockPoint.z < (region.z + 1) * 512)
|
|
&& (tileBlockPoint.z + tileBlockSize > region.z * 512);
|
|
};
|
|
|
|
if (tileX >= minTileX
|
|
&& tileY >= minTileY
|
|
&& tileX <= maxTileX
|
|
&& tileY <= maxTileY
|
|
&& ((regions === undefined) || regions.some(intersectsWithTile))) {
|
|
return ('/static/unmined/tiles/zoom.{z}/{xd}/{yd}/tile.{x}.{y}.' + options.imageFormat)
|
|
.replace('{z}', zoom)
|
|
.replace('{yd}', '' + Math.floor(tileY / 10))
|
|
.replace('{xd}', '' + Math.floor(tileX / 10))
|
|
.replace('{y}', view.tile.row)
|
|
.replace('{x}', view.tile.column);
|
|
} else {
|
|
return "/static/empty.jpg";
|
|
}
|
|
},
|
|
{
|
|
detectRetina: false,
|
|
bounds: [[minMapX, minMapY], [minMapX + mapWidth, minMapY + mapHeight]]
|
|
});
|
|
|
|
let map = L.map(mapId, {
|
|
crs: L.CRS.Simple,
|
|
minZoom: options.minZoom + zoomOffset,
|
|
maxZoom: options.maxZoom + zoomOffset,
|
|
layers: [unminedLayer],
|
|
maxBoundsViscosity: 1.0
|
|
}).setView([0, 0], options.defaultZoom + zoomOffset);
|
|
|
|
let northWest = map.unproject([minMapX, minMapY], map.getMaxZoom());
|
|
let southEast = map.unproject([minMapX + mapWidth, minMapY + mapHeight], map.getMaxZoom());
|
|
map.setMaxBounds(new L.LatLngBounds(northWest, southEast));
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
} |