More actions
Try to fix it Tag: Reverted |
Rewrite from scratch |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
(function () { | |||
'use strict'; | |||
function resizeImageMap(container) { | |||
const img = container.querySelector('img'); | |||
const map = container.querySelector('map'); | |||
if (!img || !map) return; | |||
const originalWidth = parseInt(img.dataset.fileWidth, 10); | |||
const originalHeight = parseInt(img.dataset.fileHeight, 10); | |||
const currentWidth = img.clientWidth; | |||
const currentHeight = img.clientHeight; | |||
const scaleX = currentWidth / originalWidth; | |||
const scaleY = currentHeight / originalHeight; | |||
map.querySelectorAll('area').forEach(area => { | |||
const originalCoords = area.dataset.originalCoords; | |||
if (!originalCoords) return; | |||
const coords = originalCoords.split(',').map(Number); | |||
const newCoords = coords.map((value, index) => | |||
index % 2 === 0 | |||
? Math.round(value * scaleX) | |||
: Math.round(value * scaleY) | |||
); | |||
area.coords = newCoords.join(','); | |||
}); | |||
} | |||
function initResponsiveImageMaps() { | |||
document.querySelectorAll('.responsive-imagemap').forEach(container => { | |||
const map = container.querySelector('map'); | |||
if (!map) return; | |||
map.querySelectorAll('area').forEach(area => { | |||
if (!area.dataset.originalCoords) { | |||
area.dataset.originalCoords = area.coords; | |||
} | |||
}); | |||
const img = container.querySelector('img'); | |||
if (!img) return; | |||
// Resize on load and on resize | |||
img.addEventListener('load', () => resizeImageMap(container)); | |||
window.addEventListener('resize', () => resizeImageMap(container)); | |||
// Initial resize | |||
resizeImageMap(container); | |||
}); | |||
} | |||
// Wait for DOM and images to be ready | |||
if (document.readyState === 'complete') { | |||
initResponsiveImageMaps(); | |||
} else { | |||
window.addEventListener('load', initResponsiveImageMaps); | |||
} | |||
})(); | |||
})( | |||
Latest revision as of 19:48, 27 May 2025
(function () {
'use strict';
function resizeImageMap(container) {
const img = container.querySelector('img');
const map = container.querySelector('map');
if (!img || !map) return;
const originalWidth = parseInt(img.dataset.fileWidth, 10);
const originalHeight = parseInt(img.dataset.fileHeight, 10);
const currentWidth = img.clientWidth;
const currentHeight = img.clientHeight;
const scaleX = currentWidth / originalWidth;
const scaleY = currentHeight / originalHeight;
map.querySelectorAll('area').forEach(area => {
const originalCoords = area.dataset.originalCoords;
if (!originalCoords) return;
const coords = originalCoords.split(',').map(Number);
const newCoords = coords.map((value, index) =>
index % 2 === 0
? Math.round(value * scaleX)
: Math.round(value * scaleY)
);
area.coords = newCoords.join(',');
});
}
function initResponsiveImageMaps() {
document.querySelectorAll('.responsive-imagemap').forEach(container => {
const map = container.querySelector('map');
if (!map) return;
map.querySelectorAll('area').forEach(area => {
if (!area.dataset.originalCoords) {
area.dataset.originalCoords = area.coords;
}
});
const img = container.querySelector('img');
if (!img) return;
// Resize on load and on resize
img.addEventListener('load', () => resizeImageMap(container));
window.addEventListener('resize', () => resizeImageMap(container));
// Initial resize
resizeImageMap(container);
});
}
// Wait for DOM and images to be ready
if (document.readyState === 'complete') {
initResponsiveImageMaps();
} else {
window.addEventListener('load', initResponsiveImageMaps);
}
})();