MediaWiki:Common.js: Difference between revisions

From Rise of Agon Wiki
Admin (talk | contribs)
Clamp map to z6 only and downward-positive CRS
Admin (talk | contribs)
Revert to single image overlay (tiles problematic)
Line 1: Line 1:
// Custom Agon World Map with Leaflet (tiled)
// Custom Agon World Map with Leaflet (single image overlay)
(function() {
(function() {
     if (!document.getElementById('agon-map-container')) return;
     if (!document.getElementById('agon-map-container')) return;
Line 20: Line 20:
     var imageWidth = 16384;
     var imageWidth = 16384;
     var imageHeight = 16384;
     var imageHeight = 16384;
     var tileSize = 256;
      
     var zoomLevel = 6; // use only the most detailed level
     // Simple bounds matching image dimensions
 
     var bounds = [[0, 0], [imageHeight, imageWidth]];
    // CRS with downward-positive Y (pixel y grows down)
     var crs = L.extend({}, L.CRS.Simple, {
        transformation: new L.Transformation(1, 0, 1, 0)
    });
 
    // Bounds in lat/lng derived from pixel bounds at zoomLevel
    var southWest = crs.unproject(L.point(0, imageHeight), zoomLevel);
    var northEast = crs.unproject(L.point(imageWidth, 0), zoomLevel);
    var bounds = new L.LatLngBounds(southWest, northEast);


     var map = L.map('agon-map-container', {
     var map = L.map('agon-map-container', {
         crs: crs,
         crs: L.CRS.Simple,
         minZoom: zoomLevel,
         minZoom: -3,
         maxZoom: zoomLevel,
         maxZoom: 2,
         center: bounds.getCenter(),
         center: [imageHeight/2, imageWidth/2],
         zoom: zoomLevel,
         zoom: -2
        maxBounds: bounds,
        maxBoundsViscosity: 1.0
     });
     });


     var tileUrl = mw.config.get('wgScriptPath') + '/resources/assets/tiles/AgonBigMap/{z}/{x}/{y}.jpg';
    // Single image overlay
     L.tileLayer(tileUrl, {
     var imageUrl = mw.config.get('wgScriptPath') + '/resources/assets/AgonBigMap.png';
        tileSize: tileSize,
     L.imageOverlay(imageUrl, bounds).addTo(map);
        minZoom: zoomLevel,
   
        maxZoom: zoomLevel,
        maxNativeZoom: zoomLevel,
        bounds: bounds,
        noWrap: true,
        keepBuffer: 0
    }).addTo(map);
 
     map.fitBounds(bounds);
     map.fitBounds(bounds);
     L.control.scale().addTo(map);
     L.control.scale().addTo(map);


     // Click shows pixel coords (Y, X) at full detail
     // Click shows pixel coords (Y, X)
     map.on('click', function(e) {
     map.on('click', function(e) {
         var point = map.project(e.latlng, zoomLevel);
         var y = e.latlng.lat.toFixed(2);
        var y = point.y.toFixed(2);
         var x = e.latlng.lng.toFixed(2);
         var x = point.x.toFixed(2);
         var text = 'Coordinates: ' + y + ', ' + x;
         var text = 'Coordinates: ' + y + ', ' + x;
         console.log(text);
         console.log(text);
Line 71: Line 51:
     if (window.agonMapMarkers) {
     if (window.agonMapMarkers) {
         window.agonMapMarkers.forEach(function(marker) {
         window.agonMapMarkers.forEach(function(marker) {
             var coords = marker.coordinates.split(',').map(parseFloat); // [Y, X]
             var coords = marker.coordinates.split(',').map(parseFloat);
            var latlng = map.unproject([coords[1], coords[0]], zoomLevel); // [x, y]
             var icon = L.icon({
             var icon = L.icon({
                 iconUrl: marker.icon || mw.config.get('wgScriptPath') + '/resources/assets/marker-icon.png',
                 iconUrl: marker.icon || mw.config.get('wgScriptPath') + '/resources/assets/marker-icon.png',
Line 79: Line 58:
                 popupAnchor: [1, -34]
                 popupAnchor: [1, -34]
             });
             });
             L.marker(latlng, { icon: icon })
             L.marker([coords[0], coords[1]], { icon: icon })
                 .addTo(map)
                 .addTo(map)
                 .bindPopup('<b>' + marker.name + '</b><br>' + marker.description);
                 .bindPopup('<b>' + marker.name + '</b><br>' + marker.description);

Revision as of 09:12, 25 January 2026

// Custom Agon World Map with Leaflet (single image overlay)
(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;
    
    // Simple bounds matching image dimensions
    var bounds = [[0, 0], [imageHeight, imageWidth]];

    var map = L.map('agon-map-container', {
        crs: L.CRS.Simple,
        minZoom: -3,
        maxZoom: 2,
        center: [imageHeight/2, imageWidth/2],
        zoom: -2
    });

    // Single image overlay
    var imageUrl = mw.config.get('wgScriptPath') + '/resources/assets/AgonBigMap.png';
    L.imageOverlay(imageUrl, bounds).addTo(map);
    
    map.fitBounds(bounds);
    L.control.scale().addTo(map);

    // Click shows pixel coords (Y, X)
    map.on('click', function(e) {
        var y = e.latlng.lat.toFixed(2);
        var x = e.latlng.lng.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);
            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([coords[0], coords[1]], { icon: icon })
                .addTo(map)
                .bindPopup('<b>' + marker.name + '</b><br>' + marker.description);
        });
    }
}