MinedMap/viewer/MinedMap.js

253 lines
5.4 KiB
JavaScript
Raw Normal View History

2015-02-02 09:08:36 +01:00
var MinedMapLayer = L.GridLayer.extend({
initialize: function (mipmaps, layer) {
this.mipmaps = mipmaps;
this.layer = layer;
this.zoomOffset = L.Browser.retina ? 1 : 0;
2015-02-02 09:08:36 +01:00
2020-06-20 14:04:59 +02:00
this.options.tileSize = L.Browser.retina ? 256 : 512;
2018-07-26 19:12:08 +02:00
this.options.attribution = 'Generated by <a href="https://github.com/NeoRaider/MinedMap">MinedMap</a>';
2015-02-02 09:34:38 +01:00
2020-06-20 14:04:59 +02:00
this.options.minZoom = -(mipmaps.length-1 + this.zoomOffset);
this.options.maxNativeZoom = -this.zoomOffset;
2020-06-20 14:04:59 +02:00
// for https://github.com/Leaflet/Leaflet/issues/137
2015-02-02 09:08:36 +01:00
if (!L.Browser.android) {
this.on('tileunload', this._onTileRemove);
}
},
createTile: function (coords, done) {
var tile = document.createElement('img');
tile.onload = L.bind(this._tileOnLoad, this, done, tile);
tile.onerror = L.bind(this._tileOnError, this, done, tile);
2020-06-20 14:04:59 +02:00
/*
Alt tag is set to empty string to keep screen readers from reading URL and for compliance reasons
http://www.w3.org/TR/WCAG20-TECHS/H67
*/
2015-02-02 09:08:36 +01:00
tile.alt = '';
2020-06-20 14:04:59 +02:00
/*
Set role="presentation" to force screen readers to ignore this
https://www.w3.org/TR/wai-aria/roles#textalternativecomputation
*/
tile.setAttribute('role', 'presentation');
var z = -(coords.z + this.zoomOffset);
if (z < 0)
z = 0;
var mipmap = this.mipmaps[z];
if (coords.x >= mipmap.info.minX && coords.x <= mipmap.info.maxX &&
coords.y >= mipmap.info.minZ && coords.y <= mipmap.info.maxZ &&
mipmap.regions[coords.y-mipmap.info.minZ][coords.x-mipmap.info.minX])
tile.src = 'data/'+this.layer+'/'+z+'/r.'+coords.x+'.'+coords.y+'.png';
if (z == 0)
L.DomUtil.addClass(tile, 'overzoomed');
2015-02-02 09:08:36 +01:00
return tile;
},
_tileOnLoad: function (done, tile) {
2020-06-20 14:04:59 +02:00
if (L.Browser.ielt9)
setTimeout(Util.bind(done, this, null, tile), 0);
else
done(null, tile);
2015-02-02 09:08:36 +01:00
},
_tileOnError: function (done, tile, e) {
done(e, tile);
},
_onTileRemove: function (e) {
e.tile.onload = null;
},
_abortLoading: function () {
var i, tile;
for (i in this._tiles) {
2020-06-20 14:04:59 +02:00
if (this._tiles[i].coords.z !== this._tileZoom) {
tile = this._tiles[i].el;
2015-02-02 09:08:36 +01:00
2020-06-20 14:04:59 +02:00
tile.onload = L.Util.falseFn;
tile.onerror = L.Util.falseFn;
2015-02-02 09:08:36 +01:00
2020-06-20 14:04:59 +02:00
if (!tile.complete) {
tile.src = L.Util.emptyImageUrl;
L.DomUtil.remove(tile);
}
2015-02-02 09:08:36 +01:00
}
}
2020-06-20 14:04:59 +02:00
},
_removeTile: function (key) {
var tile = this._tiles[key];
if (!tile) { return; }
// Cancels any pending http requests associated with the tile
// unless we're on Android's stock browser,
// see https://github.com/Leaflet/Leaflet/issues/137
if (!L.Browser.androidStock) {
tile.el.setAttribute('src', L.Util.emptyImageUrl);
}
return L.GridLayer.prototype._removeTile.call(this, key);
},
2015-02-02 09:08:36 +01:00
});
2015-02-04 07:22:49 +01:00
var CoordControl = L.Control.extend({
initialize: function () {
this.options.position = 'bottomleft';
},
onAdd: function (map) {
this._container = L.DomUtil.create('div', 'leaflet-control-attribution');
return this._container;
},
update: function (x, z) {
if (!this._map) { return; }
this._container.innerHTML = 'X: ' + x + '&nbsp;&nbsp;&nbsp;Z: ' + z;
}
});
2015-02-04 17:00:53 +01:00
var parseHash = function () {
var args = {};
if (window.location.hash) {
var parts = window.location.hash.substr(1).split('&');
for (var i = 0; i < parts.length; i++) {
var key_value = parts[i].split('=');
var key = key_value[0], value = key_value.slice(1).join('=');
args[key] = value;
}
}
return args;
}
2015-02-02 09:08:36 +01:00
window.createMap = function () {
var xhr = new XMLHttpRequest();
xhr.onload = function () {
2015-02-02 18:39:44 +01:00
var res = JSON.parse(this.responseText),
mipmaps = res.mipmaps,
2015-02-02 18:39:44 +01:00
spawn = res.spawn;
2015-02-02 09:08:36 +01:00
2015-02-04 22:01:14 +01:00
var x, z, zoom, light;
var updateParams = function () {
var args = parseHash();
zoom = parseInt(args['zoom']);
x = parseFloat(args['x']);
z = parseFloat(args['z']);
light = parseInt(args['light']);
if (isNaN(zoom))
zoom = 0;
if (isNaN(x))
x = spawn.x;
if (isNaN(z))
z = spawn.z;
};
updateParams();
2015-02-04 17:00:53 +01:00
2015-02-02 09:08:36 +01:00
var map = L.map('map', {
2015-02-04 17:00:53 +01:00
center: [-z, x],
zoom: zoom,
minZoom: -(mipmaps.length-1),
2015-02-02 14:18:04 +01:00
maxZoom: 3,
2015-02-02 09:08:36 +01:00
crs: L.CRS.Simple,
maxBounds: [
[-512*(mipmaps[0].info.maxZ+1), 512*mipmaps[0].info.minX],
[-512*mipmaps[0].info.minZ, 512*(mipmaps[0].info.maxX+1)],
2015-02-02 09:08:36 +01:00
],
});
var mapLayer = new MinedMapLayer(mipmaps, 'map');
var lightLayer = new MinedMapLayer(mipmaps, 'light');
2015-02-03 13:17:36 +01:00
mapLayer.addTo(map);
2015-02-04 17:00:53 +01:00
if (light)
map.addLayer(lightLayer);
2015-02-03 13:17:36 +01:00
var overlayMaps = {
"Illumination": lightLayer,
};
L.control.layers({}, overlayMaps).addTo(map);
2015-02-04 07:22:49 +01:00
var coordControl = new CoordControl();
coordControl.addTo(map);
map.on('mousemove', function(e) {
coordControl.update(Math.round(e.latlng.lng), Math.round(-e.latlng.lat));
});
2015-02-04 17:00:53 +01:00
var makeHash = function () {
2015-02-04 22:01:14 +01:00
var ret = '#x='+x+'&z='+z;
if (zoom != 0)
ret += '&zoom='+zoom;
2015-02-04 17:00:53 +01:00
if (map.hasLayer(lightLayer))
2015-02-04 22:01:14 +01:00
ret += '&light=1';
2015-02-04 17:00:53 +01:00
return ret;
};
var updateHash = function () {
window.location.hash = makeHash();
};
2015-02-04 22:01:14 +01:00
var refreshHash = function () {
zoom = map.getZoom();
center = map.getCenter();
x = Math.round(center.lng);
z = Math.round(-center.lat);
updateHash();
}
2015-02-04 17:19:56 +01:00
updateHash();
2015-02-04 22:01:14 +01:00
map.on('moveend', refreshHash);
map.on('zoomend', refreshHash);
map.on('layeradd', refreshHash);
map.on('layerremove', refreshHash);
2015-02-04 17:00:53 +01:00
window.onhashchange = function () {
if (window.location.hash == makeHash())
return;
2015-02-04 22:01:14 +01:00
updateParams();
2015-02-04 17:00:53 +01:00
map.setView([-z, x], zoom);
if (light)
map.addLayer(lightLayer);
else
map.removeLayer(lightLayer);
2015-02-04 22:01:14 +01:00
updateHash();
2015-02-04 17:00:53 +01:00
};
2015-02-02 09:08:36 +01:00
};
xhr.open('GET', 'data/info.json', true);
xhr.send();
}