a concret sample to recalculate coords of image map when window is loaded or resized:
this image is 1930 * 3360 :
<div>
<img id = "alhambra" src="filename.png" usemap="#image-map" width=100%>
<map name="image-map">
<area target="" alt="home" id = "home" title="home" href="" coords="1905,307,35,12" shape="rect">
<area target="" alt="vaciado" id = "vaciado" title="vaciado" href="" coords="141,367,1783,631" shape="rect">
<area target="" alt="tienda" id = "tienda" title="tienda" href="" coords="282,1408,278" shape="circle">
<area target="" alt="stocks" id = "stocks" title="stocks" href="" coords="1300,2968,722,2699" shape="rect">
<area target="" alt="whatsapp" id = "whatsapp" title="whatsapp" href="" coords="506,2980,1788,3193" shape="rect">
<area target="" alt="direccion" id = "direccion" title="direccion" href="" coords="43,3215,1883,3324" shape="rect">
</map>
</div>
and add the script after body as:
</body>
<script>
let text_home_coord = document.getElementById('home').coords;
let text_vaciado_coord = document.getElementById('vaciado').coords;
let text_tienda_coord = document.getElementById('tienda').coords;
let text_stocks_coord = document.getElementById('stocks').coords;
let text_whatsapp_coord = document.getElementById('whatsapp').coords;
let text_direccion_coord = document.getElementById('direccion').coords;
function img_map_response(){
// get width and height in pixel
var width_100_in_px = document.getElementById('alhambra').offsetWidth;
var height_100_in_px = document.getElementById('alhambra').offsetHeight;
// recalculate coords of image map
function get_coor_resp(nombre_coords){
// real width and height of image map
var img_real_width="1930";
var img_real_height="3360";
// convert string coords to array
text_array = nombre_coords.split(',');
// rect
if (text_array.length == 4) {
// convert strig to integer
x1 = parseInt(parseInt(text_array[0])*parseInt(width_100_in_px)/parseInt(img_real_width));
y1 = parseInt(parseInt(text_array[1])*parseInt(height_100_in_px)/parseInt(img_real_height));
x2 = parseInt(parseInt(text_array[2])*parseInt(width_100_in_px)/parseInt(img_real_width));
y2 = parseInt(parseInt(text_array[3])*parseInt(height_100_in_px)/parseInt(img_real_height));
// result converted in array of strings
array_txt =[x1.toString(), y1.toString(), x2.toString(), y2.toString()]
console.log("array_txt",array_txt)
return array_txt.join(',')
// circle
} else {
// convert strig to integer
x1 = parseInt(parseInt(text_array[0])*parseInt(width_100_in_px)/parseInt(img_real_width));
y1 = parseInt(parseInt(text_array[1])*parseInt(height_100_in_px)/parseInt(img_real_height));
r = parseInt(parseInt(text_array[2])*parseInt(width_100_in_px)/parseInt(img_real_width));
// result converted in array of strings
array_txt =[x1.toString(), y1.toString(), r.toString()]
return array_txt.join(',')
}
}
// set coords by recalculate coords (converted in string)
document.getElementById('home').coords=get_coor_resp(text_home_coord);
document.getElementById('vaciado').coords=get_coor_resp(text_vaciado_coord);
document.getElementById('tienda').coords=get_coor_resp(text_tienda_coord);
document.getElementById('stocks').coords=get_coor_resp(text_stocks_coord);
document.getElementById('whatsapp').coords=get_coor_resp(text_whatsapp_coord);
document.getElementById('direccion').coords=get_coor_resp(text_direccion_coord);
}
// add events load and resize for recalculate coords
window.addEventListener('load', img_map_response);
window.addEventListener('resize', img_map_response);
</script>
</html>
imageMapResize
maybe. You sayscaleXY('theMap'
when I think you meantscaleXY('myimage'
? – Neufer