-
Notifications
You must be signed in to change notification settings - Fork 1
/
Leaflet.LabelTextCollision.js
134 lines (102 loc) · 4.52 KB
/
Leaflet.LabelTextCollision.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
L.LabelTextCollision = L.Canvas
.extend({
options: {
/**
* Collision detection
*/
collisionFlg: true
},
initialize: function (options) {
options = L.Util.setOptions(this, options);
},
_updateTransform: function (center, zoom) {
L.Canvas.prototype._updateTransform.call(this, center, zoom);
var scale = this._map.getZoomScale(zoom, this._zoom), position = L.DomUtil
.getPosition(this._container), viewHalf = this._map
.getSize().multiplyBy(0.5 + this.options.padding), currentCenterPoint = this._map
.project(this._center, zoom), destCenterPoint = this._map
.project(center, zoom), centerOffset = destCenterPoint
.subtract(currentCenterPoint),
topLeftOffset = viewHalf.multiplyBy(-scale).add(position).add(
viewHalf).subtract(centerOffset);
if (L.Browser.any3d) {
L.DomUtil.setTransform(this._containerText, topLeftOffset,
scale);
} else {
L.DomUtil.setPosition(this._containerText, topLeftOffset);
}
},
_initContainer: function (options) {
L.Canvas.prototype._initContainer.call(this);
this._containerText = document.createElement('canvas');
L.DomEvent.on(this._containerText, 'mousemove',
L.Util.throttle(this._onMouseMove, 32, this), this).on(
this._containerText,
'click dblclick mousedown mouseup contextmenu',
this._onClick, this).on(this._containerText,
'mouseout', this._handleMouseOut, this);
this._ctxLabel = this._containerText.getContext('2d');
L.DomUtil
.addClass(this._containerText, 'leaflet-zoom-animated');
this.getPane().appendChild(this._containerText);
},
_update: function () {
// textList
this._textList = [];
L.Renderer.prototype._update.call(this);
var b = this._bounds, container = this._containerText, size = b
.getSize(), m = L.Browser.retina ? 2 : 1;
L.DomUtil.setPosition(container, b.min);
// set canvas size (also clearing it); use double size on retina
container.width = m * size.x;
container.height = m * size.y;
container.style.width = size.x + 'px';
container.style.height = size.y + 'px';
// display text on the whole surface
container.style.zIndex = '4';
this._container.style.zIndex = '3';
if (L.Browser.retina) {
this._ctxLabel.scale(2, 2);
}
// translate so we use the same path coordinates after canvas
// element moves
this._ctxLabel.translate(-b.min.x, -b.min.y);
L.Canvas.prototype._update.call(this);
},
_updatePoly: function (layer, closed) {
L.Canvas.prototype._updatePoly.call(this, layer, closed);
this._text(this._ctxLabel, layer);
},
_updateCircle: function (layer) {
L.Canvas.prototype._updateCircle.call(this, layer);
this._text(this._ctxLabel, layer);
},
_getCenter: function (points) {
var i, halfDist, segDist, dist, p1, p2, ratio, len = points.length;
if (!len) {
return null;
}
// polyline centroid algorithm; only uses the first ring if
// there are multiple
for (i = 0, halfDist = 0; i < len - 1; i++) {
halfDist += points[i].distanceTo(points[i + 1]) / 2;
}
// The line is so small in the current view that all points are
// on the same pixel.
if (halfDist === 0) {
return points[0];
}
for (i = 0, dist = 0; i < len - 1; i++) {
p1 = points[i];
p2 = points[i + 1];
segDist = p1.distanceTo(p2);
dist += segDist;
if (dist > halfDist) {
ratio = (dist - halfDist) / segDist;
var resutl = [p2.x - ratio * (p2.x - p1.x),
p2.y - ratio * (p2.y - p1.y)];
return L.point(resutl[0], resutl[1]);
}
}
},
});