This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimageMapResizer.js
116 lines (94 loc) · 3.77 KB
/
imageMapResizer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*! Image Map Resizer
* Desc: Resize HTML imageMap to scaled image.
* Copyright: (c) 2014 David J. Bradshaw - [email protected]
* License: MIT
*/
(function(){
'use strict';
function scaleImageMap(){
function resizeMap() {
function resizeAreaTag(cachedAreaCoords){
function scaleCoord(e){
return e * scallingFactor[(1===(isWidth = 1-isWidth) ? 'width' : 'height')];
}
var isWidth = 0;
return cachedAreaCoords.split(',').map(Number).map(scaleCoord).map(Math.floor).join(',');
}
var scallingFactor = {
width : displayedImage.width / sourceImage.width,
height : displayedImage.height / sourceImage.height
};
for (var i=0; i < areasLen ; i++) {
areas[i].coords = resizeAreaTag(cachedAreaCoordsArray[i]);
}
}
function start(){
//WebKit asyncs image loading, so we have to catch the load event.
sourceImage.onload = function sourceImageOnLoadF(){
if ((displayedImage.width !== sourceImage.width) || (displayedImage.height !== sourceImage.height)) {
resizeMap();
}
};
//Make copy of image, so we can get the actual size measurements
sourceImage.src = displayedImage.src;
}
function listenForResize(){
function debounce() {
clearTimeout(timer);
timer = setTimeout(resizeMap, 250);
}
if (window.addEventListener) { window.addEventListener('resize', debounce, false); }
else if (window.attachEvent) { window.attachEvent('onresize', debounce); }
}
function getCoords(e){
// normalize coord-string to csv format without any space chars
return e.coords.replace(/ *, */g,',').replace(/ +/g,',');
}
var
/*jshint validthis:true */
map = this,
areas = map.getElementsByTagName('area'),
areasLen = areas.length,
cachedAreaCoordsArray = Array.prototype.map.call(areas, getCoords),
displayedImage = document.querySelector('img[usemap="#'+map.name+'"]'),
sourceImage = new Image(),
timer = null;
start();
listenForResize();
}
function factory(){
function init(element){
if(!element.tagName) {
throw new TypeError('Object is not a valid DOM element');
} else if ('MAP' !== element.tagName.toUpperCase()) {
throw new TypeError('Expected <MAP> tag, found <'+element.tagName+'>.');
}
scaleImageMap.call(element);
}
return function imageMapResizeF(target){
switch (typeof(target)){
case 'undefined':
case 'string':
Array.prototype.forEach.call(document.querySelectorAll(target||'map'),init);
break;
case 'object':
init(target);
break;
default:
throw new TypeError('Unexpected data type ('+typeof(target)+').');
}
};
}
if (typeof define === 'function' && define.amd) {
define([],factory);
} else if (typeof exports === 'object') { //Node for browserfy
module.exports = factory();
} else {
window.imageMapResize = factory();
}
if('jQuery' in window) {
jQuery.fn.imageMapResize = function $imageMapResizeF(){
return this.filter('map').each(scaleImageMap).end();
};
}
})();