Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resizing an image that is not at its natural size originally #69

Open
kevin-rowe opened this issue Aug 31, 2018 · 4 comments
Open

resizing an image that is not at its natural size originally #69

kevin-rowe opened this issue Aug 31, 2018 · 4 comments

Comments

@kevin-rowe
Copy link

After a few tweaks, this script works perfectly.

I have made some changes to my copy of the code to allow an image that is not displayed at natural dimensions to have its map resized.

        var scalingFactor = {
            //                width  : image.width  / image.naturalWidth,
            //                height : image.height / image.naturalHeight
            width: window.innerWidth / origwindowwidth,
            height: window.innerHeight / origwindowheight
        };

...
var
/*jshint validthis:true */
map = this,
areas = null, cachedAreaCoordsArray = null, image = null, timer = null,
origwindowwidth = window.innerWidth,
origwindowheight = window.innerHeight;

This works for me.
You could probably refine this to work individual images with origimageheight as a variable in the map and use the image.height on startup if you had multiple maps that could be independently resized.

Thought I'd share.

Kevin.

@zdenek-horak
Copy link

I had a similar issue with SVG image where image map has been targeted for different-than-natural resolution and the approach above could not work. I went with a custom attribute and a minor modification:

            var scalingFactor = {
                width: image.width / (image.hasAttribute("defaultWidth") ? parseInt(image.getAttribute("defaultWidth")) : image.naturalWidth),
                height: image.height / (image.hasAttribute("defaultHeight") ? parseInt(image.getAttribute("defaultHeight")) : image.naturalHeight)
            };

@tiger-five-in-lb
Copy link

where would you stick this code in? thanks!

@thomas-tnt23
Copy link

thomas-tnt23 commented Mar 24, 2021

For me this won't work in Safari, so this is my solution...

var imageClone = document.createElement('img');
imageClone.src = image.src; 
document.body.appendChild(imageClone);
var naturalWidth = imageClone.width;
var naturalHeight = imageClone.height;
imageClone.remove();

var scalingFactor =
{
    width  : image.width  / naturalWidth,
    height : image.height / naturalHeight 
};

in the end i got a BrowserCache-Issue, which is solved quick and dirty... :P

 function setup() {
                    areas = map.getElementsByTagName('area')
                    cachedAreaCoordsArray = Array.prototype.map.call(areas, getCoords)
                    image = getImg('#' + map.name) || getImg(map.name)
                    image.src = image.src + "?" + new Date().getTime(); //<<<------------SOLUTION
                    map._resize = resizeMap //Bind resize method to HTML map element
                }

@KyleDave
Copy link

KyleDave commented Aug 1, 2021

Seems very odd to me that the imagemap resizer doesn't run when the page opens (in Firefox v.88). If I did anything to resize the window however, the map was fixed so I knew it was working. All I did was add this event:
window.dispatchEvent(new Event('readystatechange'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants