-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjquery.imageMap.js
More file actions
66 lines (57 loc) · 1.66 KB
/
jquery.imageMap.js
File metadata and controls
66 lines (57 loc) · 1.66 KB
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
/*
* jQuery imageMap plug-in 0.9
*
* Plugin HomePage http://tagnclick.com.br/blog/jquery-plugin-imageMap/
*
* Copyright (c) 2012 Hugo Campos
*
* Based on Jonas Raoni Soares Silva algorithm (http://jsfromhell.com/math/is-point-in-poly)
*
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
jQuery.fn.imageMap = function(options) {
var defaults = {
points: false,
names: false,
callback: false
};
var opts = jQuery.extend(defaults, options);
jQuery(this).bind('mouseover', function () {
var pos_x=0;
var pos_y=0;
var inside;
var element;
element = jQuery(this);
element.bind('mousemove', function() {
pos_x = event.offsetX?(event.offsetX):event.pageX-element.offsetLeft;
pos_y = event.offsetY?(event.offsetY):event.pageY-element.offsetTop;
newinside = isPointInPoly({x: pos_x, y: pos_y}, defaults.points);
if( inside != newinside ){
inside = newinside;
if(!defaults.names)
defaults.callback(inside);
else
if(inside==-1)
defaults.callback(-1);
else
defaults.callback(defaults.names[inside]);
}
});
}).mouseleave(function(){
element.unbind('mousemove');
});
}
function isPointInPoly(pt, polygons ){
for(var count in polygons){
poly = polygons[count];
for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
&& (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
&& (c = !c);
if(c) return count;
}
return -1;
}