MediaWiki:Common.js: Difference between revisions

From Rise of Agon Wiki
Admin (talk | contribs)
Revert to single image overlay (tiles problematic)
Admin (talk | contribs)
Use Leaflet.Zoomify plugin for proper tile loading
 
Line 1: Line 1:
// Custom Agon World Map with Leaflet (single image overlay)
// Custom Agon World Map with Leaflet + Zoomify tiles
(function() {
(function() {
     if (!document.getElementById('agon-map-container')) return;
     if (!document.getElementById('agon-map-container')) return;
Line 10: Line 10:
     var script = document.createElement('script');
     var script = document.createElement('script');
     script.src = 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js';
     script.src = 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js';
     script.onload = function() { initAgonMap(); };
     script.onload = function() {
        // Load Leaflet.Zoomify plugin
        var zoomifyScript = document.createElement('script');
        zoomifyScript.src = 'https://cdn.jsdelivr.net/npm/leaflet-zoomify@1.0.6/src/L.TileLayer.Zoomify.js';
        zoomifyScript.onload = function() { initAgonMap(); };
        document.head.appendChild(zoomifyScript);
    };
     document.head.appendChild(script);
     document.head.appendChild(script);
})();
})();
Line 21: Line 27:
     var imageHeight = 16384;
     var imageHeight = 16384;
      
      
    // Simple bounds matching image dimensions
     var bounds = [[0, 0], [imageHeight, imageWidth]];
     var bounds = [[0, 0], [imageHeight, imageWidth]];


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


     // Single image overlay
     // Zoomify tile layer
     var imageUrl = mw.config.get('wgScriptPath') + '/resources/assets/AgonBigMap.png';
     var zoomifyUrl = mw.config.get('wgScriptPath') + '/resources/assets/tiles/AgonBigMap/';
     L.imageOverlay(imageUrl, bounds).addTo(map);
     L.tileLayer.zoomify(zoomifyUrl, {
        width: imageWidth,
        height: imageHeight,
        bounds: bounds
    }).addTo(map);
      
      
     map.fitBounds(bounds);
     map.fitBounds(bounds);

Latest revision as of 09:14, 25 January 2026

// Custom Agon World Map with Leaflet + Zoomify tiles
(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() {
        // Load Leaflet.Zoomify plugin
        var zoomifyScript = document.createElement('script');
        zoomifyScript.src = 'https://cdn.jsdelivr.net/npm/leaflet-zoomify@1.0.6/src/L.TileLayer.Zoomify.js';
        zoomifyScript.onload = function() { initAgonMap(); };
        document.head.appendChild(zoomifyScript);
    };
    document.head.appendChild(script);
})();

function initAgonMap() {
    var mapContainer = document.getElementById('agon-map-container');
    if (!mapContainer) return;

    var imageWidth = 16384;
    var imageHeight = 16384;
    
    var bounds = [[0, 0], [imageHeight, imageWidth]];

    var map = L.map('agon-map-container', {
        crs: L.CRS.Simple,
        minZoom: 0,
        maxZoom: 6
    });

    // Zoomify tile layer
    var zoomifyUrl = mw.config.get('wgScriptPath') + '/resources/assets/tiles/AgonBigMap/';
    L.tileLayer.zoomify(zoomifyUrl, {
        width: imageWidth,
        height: imageHeight,
        bounds: 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);
        });
    }
}