MediaWiki:Common.js
From Rise of Agon Wiki
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// Custom Agon World Map with Leaflet (tiled)
(function() {
if (!document.getElementById('agon-map-container')) return;
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css';
document.head.appendChild(link);
var script = document.createElement('script');
script.src = 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js';
script.onload = function() { initAgonMap(); };
document.head.appendChild(script);
})();
function initAgonMap() {
var mapContainer = document.getElementById('agon-map-container');
if (!mapContainer) return;
var imageWidth = 16384;
var imageHeight = 16384;
var tileSize = 256;
var maxZoom = 6; // tiles generated 0..6 (z=6 most detailed)
var scale = Math.pow(2, maxZoom);
// CRS.Simple default (y grows downward for tiles) with bounds sized to one tile at z=0
var bounds = L.latLngBounds([
[0, 0],
[imageHeight / scale, imageWidth / scale]
]);
var map = L.map('agon-map-container', {
crs: L.CRS.Simple,
minZoom: 0,
maxZoom: maxZoom,
center: bounds.getCenter(),
zoom: maxZoom, // start at full detail (tile z=6)
maxBounds: bounds,
maxBoundsViscosity: 1.0
});
var tileUrl = mw.config.get('wgScriptPath') + '/resources/assets/tiles/AgonBigMap/{z}/{x}/{y}.jpg';
L.tileLayer(tileUrl, {
tileSize: tileSize,
minZoom: 0,
maxZoom: maxZoom,
maxNativeZoom: maxZoom,
bounds: bounds,
noWrap: true
}).addTo(map);
map.fitBounds(bounds);
L.control.scale().addTo(map);
// Click shows pixel coords (Y, X) at max detail
map.on('click', function(e) {
var point = map.project(e.latlng, maxZoom);
var y = point.y.toFixed(2);
var x = point.x.toFixed(2);
var text = 'Coordinates: ' + y + ', ' + x;
console.log(text);
L.popup().setLatLng(e.latlng).setContent(text).openOn(map);
});
// Markers use stored pixel coords 'Y, X'
if (window.agonMapMarkers) {
window.agonMapMarkers.forEach(function(marker) {
var coords = marker.coordinates.split(',').map(parseFloat); // [Y, X]
var latlng = map.unproject([coords[1], coords[0]], maxZoom); // [x, y]
var icon = L.icon({
iconUrl: marker.icon || mw.config.get('wgScriptPath') + '/resources/assets/marker-icon.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34]
});
L.marker(latlng, { icon: icon })
.addTo(map)
.bindPopup('<b>' + marker.name + '</b><br>' + marker.description);
});
}
}
