MediaWiki:Common.js: Difference between revisions

From Rise of Agon Wiki
Admin (talk | contribs)
Fix tile coords with unproject/project
Admin (talk | contribs)
Use Leaflet.Zoomify plugin for proper tile loading
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
// Custom Agon World Map with Leaflet (tiled)
// 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';
Line 10: Line 8:
     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 23: Line 24:
     if (!mapContainer) return;
     if (!mapContainer) return;


    // Image dimensions and tiling
     var imageWidth = 16384;
     var imageWidth = 16384;
     var imageHeight = 16384;
     var imageHeight = 16384;
     var tileSize = 256;
      
     var maxZoom = 6; // 16384 / 256 = 64 => log2(64) = 6
     var bounds = [[0, 0], [imageHeight, imageWidth]];


    // Create map with custom CRS for image coordinates
     var map = L.map('agon-map-container', {
     var map = L.map('agon-map-container', {
         crs: L.CRS.Simple,
         crs: L.CRS.Simple,
         minZoom: 0,
         minZoom: 0,
         maxZoom: maxZoom,
         maxZoom: 6
        zoom: 0
     });
     });


     // Convert pixel bounds to lat/lng bounds using unproject at max zoom
     // Zoomify tile layer
    var southWest = map.unproject([0, imageHeight], maxZoom);
     var zoomifyUrl = mw.config.get('wgScriptPath') + '/resources/assets/tiles/AgonBigMap/';
    var northEast = map.unproject([imageWidth, 0], maxZoom);
     L.tileLayer.zoomify(zoomifyUrl, {
    var bounds = new L.LatLngBounds(southWest, northEast);
         width: imageWidth,
 
         height: imageHeight,
    // Tile layer using generated pyramid under /resources/assets/tiles/AgonBigMap/{z}/{x}/{y}.jpg
         bounds: bounds
     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);
     }).addTo(map);
 
      
     // Fit map to bounds and keep panning constrained
     map.fitBounds(bounds);
     map.fitBounds(bounds);
    map.setMaxBounds(bounds);
    // Add scale
     L.control.scale().addTo(map);
     L.control.scale().addTo(map);


     // Click to get coordinates (report image pixel coords Y, X)
     // Click shows pixel coords (Y, X)
     map.on('click', function(e) {
     map.on('click', function(e) {
         var point = map.project(e.latlng, maxZoom); // pixel space
         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);
         L.popup()
         L.popup().setLatLng(e.latlng).setContent(text).openOn(map);
            .setLatLng(e.latlng)
            .setContent(text)
            .openOn(map);
     });
     });


     // Load markers from page data; stored as 'Y, X' pixel coordinates
     // Markers use stored pixel coords 'Y, X'
     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]], maxZoom); // [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 84: Line 65:
                 popupAnchor: [1, -34]
                 popupAnchor: [1, -34]
             });
             });
 
             L.marker([coords[0], coords[1]], { icon: icon })
             L.marker(latlng, { 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);
        });
    }
}