MediaWiki:Common.js: Difference between revisions

From Rise of Agon Wiki
Admin (talk | contribs)
Add Leaflet map integration
Admin (talk | contribs)
Use Leaflet.Zoomify plugin for proper tile loading
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
// Custom Agon World Map with Leaflet
// Custom Agon World Map with Leaflet + Zoomify tiles
(function() {
(function() {
    // Only run on map pages
     if (!document.getElementById('agon-map-container')) return;
     if (!document.getElementById('agon-map-container')) return;
   
 
    // Load Leaflet CSS
     var link = document.createElement('link');
     var link = document.createElement('link');
     link.rel = 'stylesheet';
     link.rel = 'stylesheet';
     link.href = 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css';
     link.href = 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css';
     document.head.appendChild(link);
     document.head.appendChild(link);
   
 
    // Load Leaflet JS
     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() {
     script.onload = function() {
         initAgonMap();
         // 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 22: Line 23:
     var mapContainer = document.getElementById('agon-map-container');
     var mapContainer = document.getElementById('agon-map-container');
     if (!mapContainer) return;
     if (!mapContainer) return;
   
 
    // Image dimensions
     var imageWidth = 16384;
     var imageWidth = 16384;
     var imageHeight = 16384;
     var imageHeight = 16384;
      
      
     // Create map with custom CRS for image coordinates
     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
     });
     });
    // 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);
      
      
    // Add the Agon map image as overlay
    var bounds = [[0, 0], [imageHeight, imageWidth]];
    L.imageOverlay(mw.config.get('wgScriptPath') + '/resources/assets/AgonBigMap.png', bounds).addTo(map);
   
    // Fit map to bounds
     map.fitBounds(bounds);
     map.fitBounds(bounds);
   
    // Add scale
     L.control.scale().addTo(map);
     L.control.scale().addTo(map);
   
 
     // Click to get coordinates
     // Click shows pixel coords (Y, X)
     map.on('click', function(e) {
     map.on('click', function(e) {
         var coord = e.latlng;
         var y = e.latlng.lat.toFixed(2);
         console.log('Clicked coordinates:', coord.lat.toFixed(2) + ', ' + coord.lng.toFixed(2));
         var x = e.latlng.lng.toFixed(2);
         L.popup()
        var text = 'Coordinates: ' + y + ', ' + x;
            .setLatLng(e.latlng)
        console.log(text);
            .setContent('Coordinates: ' + coord.lat.toFixed(2) + ', ' + coord.lng.toFixed(2))
         L.popup().setLatLng(e.latlng).setContent(text).openOn(map);
            .openOn(map);
     });
     });
   
 
     // Load markers from page data
     // Markers use stored pixel coords 'Y, X'
     if (window.agonMapMarkers) {
     if (window.agonMapMarkers) {
         window.agonMapMarkers.forEach(function(marker) {
         window.agonMapMarkers.forEach(function(marker) {
Line 66: Line 65:
                 popupAnchor: [1, -34]
                 popupAnchor: [1, -34]
             });
             });
           
             L.marker([coords[0], coords[1]], { 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);

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);
        });
    }
}