-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
396 lines (374 loc) · 14.8 KB
/
index.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import * as maptalks from 'maptalks';
const options = {
// snapTo threshold
tolerance: 15,
// filter geometries for get Adsorption reference object
filterGeometries: null
};
const TEMP_POINT = new maptalks.Point(0, 0);
function coordinateToContainerPoint(coordinate, map, out) {
return map.coordToContainerPoint(coordinate, null, out);
}
const TEMP_LINE = [[0, 0], [0, 0]], TEMP_MOUSEPOINT = [0, 0];
// code from https://zhuanlan.zhihu.com/p/408786267
function getShortestPointInLine(line, p) {
const p1 = line[0];
const p2 = line[1];
const dx = p2[0] - p1[0];
const dy = p2[1] - p1[1];
const cross = dx * (p[0] - p1[0]) + dy * (p[1] - p1[1]);
if (cross <= 0) {
TEMP_POINT.x = p1[0];
TEMP_POINT.y = p1[1];
return TEMP_POINT;
}
const d2 = dx * dx + dy * dy;
if (cross >= d2) {
TEMP_POINT.x = p2[0];
TEMP_POINT.y = p2[1];
return TEMP_POINT;
}
// 垂足
const u = cross / d2;
TEMP_POINT.x = (p1[0] + u * dx);
TEMP_POINT.y = (p1[1] + u * dy);
return TEMP_POINT;
}
const TEMP_POINT1 = new maptalks.Point(0, 0),
TEMP_POINT2 = new maptalks.Point(0, 0),
TEMP_POINT3 = new maptalks.Point(0, 0),
TEMP_POINT4 = new maptalks.Point(0, 0),
TEMP_EXTENT = new maptalks.Extent(0, 0, 0, 0),
TEMP_COORDINATE = new maptalks.Coordinate(0, 0);
function ringBBOX(ring, map, tolerance) {
let minx = Infinity, miny = Infinity, maxx = -Infinity, maxy = -Infinity;
for (let i = 0, len = ring.length; i < len; i++) {
const { x, y } = ring[i];
minx = Math.min(x, minx);
miny = Math.min(y, miny);
maxx = Math.max(x, maxx);
maxy = Math.max(y, maxy);
}
TEMP_COORDINATE.x = minx;
TEMP_COORDINATE.y = miny;
const p1 = coordinateToContainerPoint(TEMP_COORDINATE, map, TEMP_POINT1);
TEMP_COORDINATE.x = minx;
TEMP_COORDINATE.y = maxy;
const p2 = coordinateToContainerPoint(TEMP_COORDINATE, map, TEMP_POINT2);
TEMP_COORDINATE.x = maxx;
TEMP_COORDINATE.y = maxy;
const p3 = coordinateToContainerPoint(TEMP_COORDINATE, map, TEMP_POINT3);
TEMP_COORDINATE.x = maxx;
TEMP_COORDINATE.y = miny;
const p4 = coordinateToContainerPoint(TEMP_COORDINATE, map, TEMP_POINT4);
TEMP_EXTENT.xmin = Math.min(p1.x, p2.x, p3.x, p4.x) - tolerance;
TEMP_EXTENT.ymin = Math.min(p1.y, p2.y, p3.y, p4.y) - tolerance;
TEMP_EXTENT.xmax = Math.max(p1.x, p2.x, p3.x, p4.x) + tolerance;
TEMP_EXTENT.ymax = Math.max(p1.y, p2.y, p3.y, p4.y) + tolerance;
return TEMP_EXTENT;
}
// 确定点是否在线段上
function pointOnLine(point, startPoint, endPoint) {
const x = point.x;
const y = point.y;
const x1 = startPoint.x;
const y1 = startPoint.y;
const x2 = endPoint.x;
const y2 = endPoint.y;
const dxc = x - x1;
const dyc = y - y1;
const dxl = x2 - x1;
const dyl = y2 - y1;
const cross = dxc * dyl - dyc * dxl;
if (Math.abs(cross) > 0.1) {
return;
}
if (Math.abs(dxl) >= Math.abs(dyl)) {
if (dxl > 0 ? x1 < x && x < x2 : x2 < x && x < x1) {
return true;
}
} else if (dyl > 0 ? y1 < y && y < y2 : y2 < y && y < y1) {
return true;
}
}
// 确定点是否在环上,返回所在线段的 index 值
function pointOnRing(ring, point) {
if (!point) {
return;
}
if (point.x === ring[0].x && point.y === ring[0].y) {
return [0, 1];
}
for (let i = 0; i < ring.length - 1; i++) {
if (point.x === ring[i + 1].x && point.y === ring[i + 1].y) {
return [i + 1, i + 2];
}
if (pointOnLine(point, ring[i], ring[i + 1])) {
return [i + 1, i + 1];
}
}
}
export class Snap extends maptalks.Eventable(maptalks.Class) {
constructor(map, options) {
super(options);
this.map = map;
this._mousePoint = null;
this._geometries = [];
this.map.on('mousemove', this._mouseMove, this);
}
_mouseMove(e) {
this._mousePoint = e.containerPoint;
return this;
}
_validateMousePosition(point) {
return (this._mousePoint && this._mousePoint.distanceTo(point) <= this.options.tolerance + 5);
}
effectGeometry(geometry) {
if (this._geometries.indexOf(geometry) > -1) {
return this;
}
this._geometries.push(geometry);
const self = this;
const snapTo = function (handleConatainerPoint, lastContainerPoints) {
if (!handleConatainerPoint) {
return;
}
let geometries;
const filterGeometries = self.options.filterGeometries || self.options.fiterGeometries;
if (filterGeometries && maptalks.Util.isFunction(filterGeometries)) {
geometries = filterGeometries();
}
if (!geometries || !geometries.length) {
const layer = this.getLayer();
if (layer) {
geometries = layer.getGeometries();
}
}
return self._nearest(geometries, handleConatainerPoint, this, lastContainerPoints);
};
// bind adsort function
geometry.snapTo = snapTo;
return this;
}
unEffectGeometry(geometry) {
const index = this._geometries.indexOf(geometry);
if (index === -1) {
return this;
}
this._geometries.splice(index, 1);
delete geometry.snapTo;
return this;
}
dispose() {
this._geometries.forEach(geometry => {
this.unEffectGeometry(geometry);
});
this.map.off('mousemove', this._mouseMove, this);
delete this.map;
delete this._mousePoint;
delete this._geometries;
}
_nearest(geometries, handleConatainerPoint, currentGeometry, lastContainerPoints) {
// geometries = this._sortGeometries(geometries, handleConatainerPoint);
let point;
for (let i = 0, len = geometries.length; i < len; i++) {
const geometry = geometries[i];
if (geometry === currentGeometry) {
continue;
}
if (lastContainerPoints && lastContainerPoints.length) {
const nearestGeometry = this._nearestGeometry(geometries[i], handleConatainerPoint, lastContainerPoints);
// 在自动完成中间点模式下,第一个筛选到的元素可能不是上一个选中的点所在的元素
// 需要等到出现第一个可以补充中间点的元素出现,或者所有元素都无法补完时再返回结果
if (nearestGeometry) {
if (nearestGeometry.effectedVertex) {
point = nearestGeometry;
break;
} else if (!point) {
point = nearestGeometry;
}
}
} else {
point = this._nearestGeometry(geometries[i], handleConatainerPoint, lastContainerPoints);
if (point) {
break;
}
}
}
if (point) {
return point;
}
return this._mousePoint && this._mousePoint.copy();
}
_fireSnapEvent(point, geometry) {
point = point.point || point;
this.fire('snap', {
containerPoint: point.copy(),
geometry,
coordinate: this.map.containerPointToCoord(point)
});
return this;
}
_nearestGeometry(geometry, handleConatainerPoint, lastContainerPoints) {
// multi geometry
if (geometry.getGeometries) {
const geometries = geometry.getGeometries();
for (let i = 0, len = geometries.length; i < len; i++) {
const point = this._nearestGeometry(geometries[i], handleConatainerPoint, lastContainerPoints);
if (point) {
this._fireSnapEvent(point, geometry);
return point;
}
}
return;
}
const tolerance = Math.max(this.options.tolerance, 1);
const coordinates = geometry.getCoordinates();
const map = this.map;
const isNearest = (point) => {
return handleConatainerPoint.distanceTo(point) <= tolerance && this._validateMousePosition(point);
};
if (geometry instanceof maptalks.Marker) {
const point = coordinateToContainerPoint(coordinates, this.map, TEMP_POINT);
if (isNearest(point)) {
this._fireSnapEvent(point, geometry);
return point.copy();
}
return;
}
const nearestRing = (ring) => {
if (!ring || ring.length < 2) {
return;
}
const bbox = ringBBOX(ring, map, tolerance);
const x = handleConatainerPoint.x, y = handleConatainerPoint.y;
if (x < bbox.xmin || y < bbox.ymin || x > bbox.xmax || y > bbox.ymax) {
return;
}
const len = ring.length;
let i = 0;
let POINT;
for (; i < len - 1; i++) {
const coordinate = ring[i];
const point = POINT || coordinateToContainerPoint(coordinate, map, TEMP_POINT);
if (!POINT && isNearest(point)) {
return point.copy();
}
const coordinate1 = ring[i + 1];
const point1 = coordinateToContainerPoint(coordinate1, map, TEMP_POINT1);
if (isNearest(point1)) {
return point1.copy();
}
TEMP_LINE[0][0] = point.x;
TEMP_LINE[0][1] = point.y;
TEMP_LINE[1][0] = point1.x;
TEMP_LINE[1][1] = point1.y;
TEMP_MOUSEPOINT[0] = handleConatainerPoint.x;
TEMP_MOUSEPOINT[1] = handleConatainerPoint.y;
const linePoint = getShortestPointInLine(TEMP_LINE, TEMP_MOUSEPOINT);
if (isNearest(linePoint)) {
return linePoint.copy();
}
POINT = TEMP_POINT2;
POINT.x = point1.x;
POINT.y = point1.y;
}
};
// 获取环上在当前点击的点和前一个点之间的所有节点
const getEffectedVertexOnRing = (ring, oldPoints, newPoint, isRing) => {
const [oldPoint, beforeOldPoint] = oldPoints;
// 这里已经通过 nearestRing 校验过了环坐标的正确性,不需要再校验一次
const ringPoints = ring.map(point => coordinateToContainerPoint(point, map));
const oldIndex = pointOnRing(ringPoints, oldPoint);
if (!oldIndex) {
return;
}
const newIndex = pointOnRing(ringPoints, newPoint);
if (!newIndex) {
console.log('seems something wrong');
return;
}
if (oldIndex[0] === newIndex[0] && oldIndex[1] === newIndex[1]) {
return;
}
// 是多边形的环时,需要跨越首尾相接的节点;是线时则只截取中间的节点
if (isRing) {
let reverse = false;
// 使用再之前的一个点来判断自动完成的方向
const beforeOldIndex = pointOnRing(ringPoints, beforeOldPoint);
if (beforeOldIndex) {
if (beforeOldIndex[0] > oldIndex[0] || beforeOldIndex[1] > oldIndex[1]) {
reverse = true;
} else if (beforeOldIndex[0] === oldIndex[0] && beforeOldIndex[1] === oldIndex[1] && pointOnLine(beforeOldPoint, oldPoint, ringPoints[oldIndex[0]])) {
// 特殊情况,当两个点在同一线段上时,需要再单独计算一次位置
reverse = true;
}
}
if (oldIndex[0] < newIndex[0] || oldIndex[1] < newIndex[1]) {
if (reverse) {
return ringPoints.slice(1, oldIndex[1]).reverse().concat(ringPoints.slice(newIndex[1]).reverse());
} else {
return ringPoints.slice(oldIndex[1], newIndex[0]);
}
} else {
if (reverse) {
return ringPoints.slice(newIndex[1], oldIndex[0]).reverse();
} else {
return ringPoints.slice(oldIndex[1]).concat(ringPoints.slice(1, newIndex[1]));
}
}
}
if (oldIndex[0] < newIndex[0] || oldIndex[1] < newIndex[1]) {
return ringPoints.slice(oldIndex[1], newIndex[0]);
} else {
return ringPoints.slice(newIndex[1], oldIndex[0]).reverse();
}
};
if (geometry instanceof maptalks.LineString) {
const point = nearestRing(coordinates);
if (point) {
if (lastContainerPoints && lastContainerPoints.length) {
const effectedVertex = getEffectedVertexOnRing(coordinates, lastContainerPoints, point);
if (effectedVertex && effectedVertex.length) {
this._fireSnapEvent(point, geometry);
return { point: point.copy(), effectedVertex };
}
}
this._fireSnapEvent(point, geometry);
return point.copy();
}
return;
}
if (geometry instanceof maptalks.Polygon) {
for (let i = 0, len = coordinates.length; i < len; i++) {
const point = nearestRing(coordinates[i]);
if (point) {
if (lastContainerPoints && lastContainerPoints.length) {
const effectedVertex = getEffectedVertexOnRing(coordinates[i], lastContainerPoints, point, true);
if (effectedVertex && effectedVertex.length) {
this._fireSnapEvent(point, geometry);
return { point: point.copy(), effectedVertex };
}
}
this._fireSnapEvent(point, geometry);
return point.copy();
}
}
}
}
_sortGeometries(geometries, handleConatainerPoint) {
const map = this.map;
geometries.forEach(geo => {
const center = geo.getCenter();
if (!center) {
return;
}
const containerPoint = coordinateToContainerPoint(center, map);
geo._distance = handleConatainerPoint.distanceTo(containerPoint);
});
return geometries.sort((geo1, geo2) => {
return geo1._distance - geo2._distance;
});
}
}
Snap.mergeOptions(options);