forked from migurski/canvas-warp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
follower.js
94 lines (73 loc) · 2.52 KB
/
follower.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
// namespacing!
if (!com) {
var com = { };
if (!com.modestmaps) {
com.modestmaps = { };
}
}
com.modestmaps.Follower = function(map, location, content)
{
this.coord = map.provider.locationCoordinate(location);
this.offset = new com.modestmaps.Point(0, 0);
this.dimensions = new com.modestmaps.Point(100, 50);
this.padding = new com.modestmaps.Point(10, 10);
this.offset = new com.modestmaps.Point(0, -50);
var follower = this;
var zoomedCallback = function(m, a) { return follower.zoomed(m); };
map.addCallback('zoomed', zoomedCallback);
map.addCallback('centered', zoomedCallback);
map.addCallback('extentset', zoomedCallback);
map.addCallback('panned', function(m, a) { return follower.draw(m); });
this.div = document.createElement('div');
this.div.style.position = 'absolute';
this.div.style.width = this.dimensions.x + 'px';
this.div.style.height = this.dimensions.y + 'px';
this.div.style.backgroundColor = 'white';
this.div.style.border = 'solid black 1px';
this.div.innerHTML = content;
this.div.onmousedown = function(e) {
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
return false;
};
map.parent.appendChild(this.div);
this.draw(map);
}
com.modestmaps.Follower.prototype = {
div: null,
coord: null,
offset: null,
dimensions: null,
padding: null,
zoomed: function(map)
{
this.coord = this.coord.zoomTo(map.coordinate.zoom);
this.draw(map);
},
draw: function(map)
{
try {
var point = map.coordinatePoint(this.coord);
} catch(e) {
// too soon?
return;
}
if(point.x + this.dimensions.x + this.offset.x < 0) {
// too far left
this.div.style.display = 'none';
} else if(point.y + this.dimensions.y + this.offset.y < 0) {
// too far up
this.div.style.display = 'none';
} else if(point.x + this.offset.x > map.dimensions.x) {
// too far right
this.div.style.display = 'none';
} else if(point.y + this.offset.y > map.dimensions.y) {
// too far down
this.div.style.display = 'none';
} else {
this.div.style.display = 'block';
this.div.style.left = point.x + this.offset.x + 'px';
this.div.style.top = point.y + this.offset.y + 'px';
}
}
};