Resize image map on window resize

15,551

Solution 1

I wrote some simple function to rebuild all map points on every event. Try this

function mapRebuild(scaleValue) {
    var map = $("#imgmap"); // select your map or for all map you can choose $("map").each(function() { map = $(this);.....})
    map.find("area").each(function() { // select all areas
        var coords = $(this).attr("coords"); // extract coords
            coords = coords.split(","); // split to array
        var scaledCoords = "";
        for (var coord in coords) { // rebuild all coords with scaleValue
              scaledCoords += Math.floor(coords[coord] * scaleValue) + ",";
            }
        scaledCoords = scaledCoords.slice(0, -1); // last coma delete
        $(this).attr("coords", scaledCoords); // set new coords
        });
    }

scaleValue can be calculated as oldWindowWidth/newWindowWidth. Of course you need to keep the value of oldWindowWidth on window resize. Maybe my solution not on time, but i hope this is useful to someone

Solution 2

I think what you want is at http://home.comcast.net/~urbanjost/semaphore.html

where I show different examples of how to make your image map coordinates change when your image display size changes.

Solution 3

This is an old thread but for anyone looking for a solution for a similar or even identical problem, the ImageMapster jQuery plugin appears to provide the easiest solution. You can use its resize method (which can even animate the resizing if desired!) as follows to resize an image along with its image map:

$('img').mapster( 'resize', newWidth, newHeight, resizeTime);

You can find a link on ImageMapster's demo page to a jsFiddle that demonstrates resizing an image & its map in response to changing the browser window.

Share:
15,551
Kozy
Author by

Kozy

Updated on June 23, 2022

Comments

  • Kozy
    Kozy almost 2 years

    I'm trying to resize an image map on window resize event. The closest I've gotten is to use a mouseclick event, but it needs to be window resize for what I'm doing. I'm using Firefox 3.5.5

    I'm using jquery somewhat. Here's my example - the area button I want to resize on window resize is in the top left (click on it to resize map and area button):

    http://www.whitebrickstudios.com/foghornstour/imagemap3.html

    Any help would be appreciated! Thank you, Rich

  • Crescent Fresh
    Crescent Fresh over 14 years
    Although you speak truth, the OP is already doing this if you bothered to read the code.
  • Kozy
    Kozy over 14 years
    So it works, but, I have to resize firefox 6 times in order for it to work. IE is a mess. I made those changes. This is helpful, but, it's looking like Firefox and especially IE are having too much trouble resizing. I'm thinking now that I'm going to stick with my original plan and just use semi-accurately positioned divs to take the place of image map buttons. Damn. I'll be thinking about this though and if I ever come up with and answer I'll post it. Thanks for your help guys.
  • Kozy
    Kozy over 14 years
    I think you're missing the point danigb. I'm looking to resize an 'image map area', on window resize.
  • danigb
    danigb over 14 years
    Now I understand. In fact, i've opened your url and it didnt work for me (chrome). I had to do something similar and I ended using divs instead of imagemaps and setting the position: absolute; and top, left, width and height in % units. If you what i can explain a little bit more. cheers dani
  • Kozy
    Kozy over 14 years
    Hey Danigb, I ended up using divs as well to position images with %s. Though it'd still be cool to use image maps in a future version. This is what I have for image maps so far. home.comcast.net/~urbanjost/IMG/resizeimg4.html
  • Kozy
    Kozy over 14 years
    woops, meant to use this link: whitebrickstudios.com/foghornstour/imagemap3.html
  • Kozy
    Kozy over 14 years
    I actually incorporated some of the code in those examples. The problem I ran into though, was that though the image map would resize for events like 'click', it would not adjust on 'window resize' event.--Well it does, you just have to resize the browser 6 times to get it to work. I'm using FF 3.5 Sorry my link went broke, it's fixed now: whitebrickstudios.com/foghornstour/imagemap3.html I've opted to use divs positioned by %s but it'd be really cool to get this working one day.
  • Jordan Ryan Moore
    Jordan Ryan Moore over 14 years
    I had already "bothered" to read the code; however, the question itself didn't make it clear that this part of the solution wasn't the problem. I added the basic resize event handling for those who might find this later when searching for a solution. The second half of my question did address one of the problems with the code, so I'm unsure why I should receive a downvote.
  • Daniel Rikowski
    Daniel Rikowski over 10 years
    +1 I'm using this function, works like a charm. But I used img.width / img.naturalWidth instead of using the window width. That way I didn't need to take any additional borders, margins, paddings, etc. into account.
  • David Bradshaw
    David Bradshaw about 10 years
    I've expanded this out into a little lib, that keeps the map working as the image is resized and also copes with X and Y having different scaling. It also works out the scaling values for you. github.com/davidjbradshaw/imagemap-resizer