Variable-sized Image Map, version 0.01


/ Published in: JavaScript
Save to your folder(s)

Hey,

I just thought I would share something I made that I tested in IE6, IE8, Firefox 3.6, and Chrome 4. It allows you to resize a client-side image map based on the size of the image, so when your image resizes, your map still works how you want it to.

This is my first revision. There are a lot of easy improvements to be made. I'll post them sometime later. It should support "rect" but I only tested it with "poly". Might even work with "circle".


Copy this code and paste it in your HTML
  1. /*
  2. If you have an image map like this:
  3. <map id="_rightact" name="rightact">
  4. <area id="rightactarea" shape="poly" href="http://www.image-maps.com/" onmouseover="hideBox();" alt="" />
  5. </map>
  6.  
  7. Notice how I omit the coords in the <area> tag. This is because I will fill them in later.
  8.  
  9. You choose a base size, and go somewhere like image-maps.com and map your image out against that base size (which should be the initial size of the image, or maybe even a really high resolution if you want precision). THEN, save those coords it gives you and use them as your "base coordinates".
  10.  
  11. At the end of your body tag you should put something like this:
  12.  
  13. <script>
  14. area_resize('rightactarea', 1024, 768, '946,0,8,4,2,765,1022,740,1022,664,653,447,651,350,', $('#rightact').width(), $('#rightact').height());
  15. </script>
  16.  
  17. I put in the id of the particular 'area' I'm resizing, the base width, base height, set of base coordinates from image-maps.com, and then the width and height to which I want to resize the map (through jQuery it gets the pixel size of the source picture which was CSS-styled to be width:100%;height:100%).
  18.  
  19. Remember, call area_resize for every area inside your map.
  20.  
  21. Here is the area_resize function.
  22. It can be improved to support shape="rect" and shape="circle" (should be simple), and a map_resize function can be created to resize anything inside the area, too. Please refine, solidify, and test this to your heart's delight, and clean it up. =) I literally could find nothing like this anywhere so this is why I submitted this proof of concept.
  23.  
  24. -- SORRY, I DON'T KNOW HOW TO PUT CODE IN THE COMMENTS --
  25. */
  26.  
  27. function area_resize(areaId, baseWidth, baseHeight, baseCoords, newWidth, newHeight)
  28. {
  29. // supports "poly" right now, potentially others later
  30. // coords format: x1,y1,x2,y2,..,xn,yn
  31. //alert(newWidth + 'x' + newHeight);
  32.  
  33. var newCoords = "";
  34. var baseCoordsArray = String(baseCoords).split(",");
  35.  
  36. for (var i = 0; i < baseCoordsArray.length; i++)
  37. {
  38. if (String(baseCoordsArray[i]).length > 0)
  39. {
  40. if (i % 2 == 0)
  41. {
  42. // x coordinate
  43. newCoords += String(parseInt((newWidth / baseWidth) * parseInt(String(baseCoordsArray[i]))));
  44. }
  45. else
  46. {
  47. // y coordinate
  48. newCoords += String(parseInt((newHeight / baseHeight) * parseInt(String(baseCoordsArray[i]))));
  49. }
  50. }
  51. if (i < baseCoordsArray.length - 1) newCoords += ",";
  52. }
  53.  
  54. document.getElementById(areaId).coords = newCoords;
  55. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.