More actions
Different fix Tag: Reverted |
Fix 3 Tag: Reverted |
||
| Line 12: | Line 12: | ||
// Handle WebKit image load issues by using onload | // Handle WebKit image load issues by using onload | ||
$('<img />').on('load', function() { | var $realImage = $('<img />'); | ||
$realImage.on('load', function() { | |||
var attrW = 'width', attrH = 'height'; | var attrW = 'width', attrH = 'height'; | ||
var w = $that.attr(attrW), h = $that.attr(attrH); | var w = $that.attr(attrW), h = $that.attr(attrH); | ||
| Line 40: | Line 41: | ||
for (var i = 0; i < coords.length; ++i) { | for (var i = 0; i < coords.length; ++i) { | ||
// Recalculate each coordinate based on image scaling | // Recalculate each coordinate based on image scaling | ||
if (i % 2 === 0) | if (i % 2 === 0) { | ||
// Adjust the x coordinate | |||
coordsPercent[i] = parseInt(((coords[i] / w) * 100) * wPercent); | coordsPercent[i] = parseInt(((coords[i] / w) * 100) * wPercent); | ||
else | } else { | ||
// Adjust the y coordinate | |||
coordsPercent[i] = parseInt(((coords[i] / h) * 100) * hPercent); | coordsPercent[i] = parseInt(((coords[i] / h) * 100) * hPercent); | ||
} | |||
} | } | ||
// Log out the adjusted coordinates for debugging | |||
console.log("Updated coordinates for area:", coordsPercent); | |||
// Update the area element's coords with recalculated coordinates | // Update the area element's coords with recalculated coordinates | ||
$this.attr(c, coordsPercent.join(',')); | $this.attr(c, coordsPercent.join(',')); | ||
Revision as of 18:35, 27 May 2025
;(function(window, $, mw) {
if (window.responsiveImageMapLoaded) return;
window.responsiveImageMapLoaded = true;
function rwdImageMap($img) {
$img.each(function() {
var that = this;
var $that = $(that);
// Only proceed if the image has a usemap attribute
if (typeof($(this).attr('usemap')) === 'undefined') return;
// Handle WebKit image load issues by using onload
var $realImage = $('<img />');
$realImage.on('load', function() {
var attrW = 'width', attrH = 'height';
var w = $that.attr(attrW), h = $that.attr(attrH);
// Fallback if width/height attributes are missing
if (!w || !h) {
var temp = new Image();
temp.src = $that.attr('src');
if (!w) w = temp.width;
if (!h) h = temp.height;
}
var wPercent = $that.width() / 100,
hPercent = $that.height() / 100,
map = $that.attr('usemap').replace('#', ''),
c = 'coords';
// Update each area element's coordinates within the map
$that.siblings('map[name="' + map + '"]').find('area').each(function() {
var $this = $(this);
if (!$this.data(c))
$this.data(c, $this.attr(c));
var coords = $this.data(c).split(','),
coordsPercent = [];
for (var i = 0; i < coords.length; ++i) {
// Recalculate each coordinate based on image scaling
if (i % 2 === 0) {
// Adjust the x coordinate
coordsPercent[i] = parseInt(((coords[i] / w) * 100) * wPercent);
} else {
// Adjust the y coordinate
coordsPercent[i] = parseInt(((coords[i] / h) * 100) * hPercent);
}
}
// Log out the adjusted coordinates for debugging
console.log("Updated coordinates for area:", coordsPercent);
// Update the area element's coords with recalculated coordinates
$this.attr(c, coordsPercent.join(','));
});
}).attr('src', $that.attr('src')); // Trigger image load
});
}
// Trigger on page load
mw.hook('wikipage.content').add(function($e) {
var $img = $e.find('.responsive-imagemap .noresize:not(.made-responsive)')
.css({'width': '', 'height': ''})
.addClass('made-responsive')
.find('img[usemap]');
rwdImageMap($img);
});
// Trigger on window resize
$(window).resize(function() {
rwdImageMap($('.responsive-imagemap .made-responsive img[usemap]'));
});
})(this, jQuery, mediaWiki);