-
Notifications
You must be signed in to change notification settings - Fork 789
/
Copy pathLayer.js
263 lines (211 loc) · 7.54 KB
/
Layer.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
/**
* @author kyle / http://nikai.us/
*/
import BaseLayer from "../BaseLayer";
import CanvasLayer from "./CanvasLayer";
import clear from "../../canvas/clear";
import DataSet from "../../data/DataSet";
import TWEEN from "../../utils/Tween";
class Layer extends BaseLayer{
constructor(map, dataSet, options) {
super(map, dataSet, options);
var self = this;
var data = null;
options = options || {};
this.clickEvent = this.clickEvent.bind(this);
this.mousemoveEvent = this.mousemoveEvent.bind(this);
self.init(options);
self.argCheck(options);
self.transferToMercator();
var canvasLayer = this.canvasLayer = new CanvasLayer({
map: map,
context: this.context,
paneName: options.paneName,
mixBlendMode: options.mixBlendMode,
enableMassClear: options.enableMassClear,
zIndex: options.zIndex,
update: function() {
self._canvasUpdate();
}
});
dataSet.on('change', function() {
self.transferToMercator();
canvasLayer.draw();
});
}
throttle(fun, ctx, pixel, e) {
clearTimeout(fun.tId)
fun.tId = setTimeout(function(){
fun.call(ctx, pixel, e);
}, 300);
}
clickEvent(e) {
var pixel = e.pixel;
super.clickEvent(pixel, e);
}
mousemoveEvent(e) {
var pixel = e.pixel;
this.throttle(super.mousemoveEvent, this, pixel, e);
// super.mousemoveEvent(pixel, e);
}
bindEvent(e) {
this.unbindEvent();
var map = this.map;
if (this.options.methods) {
if (this.options.methods.click) {
map.setDefaultCursor("default");
map.addEventListener('click', this.clickEvent);
}
if (this.options.methods.mousemove) {
map.addEventListener('mousemove', this.mousemoveEvent);
}
}
}
unbindEvent(e) {
var map = this.map;
if (this.options.methods) {
if (this.options.methods.click) {
map.removeEventListener('click', this.clickEvent);
}
if (this.options.methods.mousemove) {
map.removeEventListener('mousemove', this.mousemoveEvent);
}
}
}
// 经纬度左边转换为墨卡托坐标
transferToMercator() {
var projection = this.map.getMapType().getProjection();
if (this.options.coordType !== 'bd09mc') {
var data = this.dataSet.get();
data = this.dataSet.transferCoordinate(data, function(coordinates) {
if (coordinates[0] < -180 || coordinates[0] > 180 || coordinates[1] < -90 || coordinates[1] > 90) {
return coordinates;
} else {
var pixel = projection.lngLatToPoint({
lng: coordinates[0],
lat: coordinates[1]
});
return [pixel.x, pixel.y];
}
}, 'coordinates', 'coordinates_mercator');
this.dataSet._set(data);
}
}
getContext() {
return this.canvasLayer.canvas.getContext(this.context);
}
_canvasUpdate(time) {
if (!this.canvasLayer) {
return;
}
var self = this;
var animationOptions = self.options.animation;
var map = this.canvasLayer._map;
var zoomUnit = Math.pow(2, 18 - map.getZoom());
var projection = map.getMapType().getProjection();
var mcCenter = projection.lngLatToPoint(map.getCenter());
var nwMc = new BMap.Pixel(mcCenter.x - (map.getSize().width / 2) * zoomUnit, mcCenter.y + (map.getSize().height / 2) * zoomUnit); //左上角墨卡托坐标
var context = this.getContext();
if (self.isEnabledTime()) {
if (time === undefined) {
clear(context);
return;
}
if (this.context == '2d') {
context.save();
context.globalCompositeOperation = 'destination-out';
context.fillStyle = 'rgba(0, 0, 0, .1)';
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
context.restore();
}
} else {
clear(context);
}
if (this.context == '2d') {
for (var key in self.options) {
context[key] = self.options[key];
}
} else {
context.clear(context.COLOR_BUFFER_BIT);
}
if (self.options.minZoom && map.getZoom() < self.options.minZoom || self.options.maxZoom && map.getZoom() > self.options.maxZoom) {
return;
}
var scale = 1;
if (this.context != '2d') {
scale = this.canvasLayer.devicePixelRatio;
}
var dataGetOptions = {
fromColumn: self.options.coordType == 'bd09mc' ? 'coordinates' : 'coordinates_mercator',
transferCoordinate: function(coordinate) {
var x = (coordinate[0] - nwMc.x) / zoomUnit * scale;
var y = (nwMc.y - coordinate[1]) / zoomUnit * scale;
return [x, y];
}
}
if (time !== undefined) {
dataGetOptions.filter = function(item) {
var trails = animationOptions.trails || 10;
if (time && item.time > (time - trails) && item.time < time) {
return true;
} else {
return false;
}
}
}
// get data from data set
var data = self.dataSet.get(dataGetOptions);
this.processData(data);
var nwPixel = map.pointToPixel(new BMap.Point(0, 0));
if (self.options.unit == 'm') {
if (self.options.size) {
self.options._size = self.options.size / zoomUnit;
}
if (self.options.width) {
self.options._width = self.options.width / zoomUnit;
}
if (self.options.height) {
self.options._height = self.options.height / zoomUnit;
}
} else {
self.options._size = self.options.size;
self.options._height = self.options.height;
self.options._width = self.options.width;
}
this.drawContext(context, data, self.options, nwPixel);
//console.timeEnd('draw');
//console.timeEnd('update')
self.options.updateCallback && self.options.updateCallback(time);
}
init(options) {
var self = this;
self.options = options;
this.initDataRange(options);
this.context = self.options.context || '2d';
if (self.options.zIndex) {
this.canvasLayer && this.canvasLayer.setZIndex(self.options.zIndex);
}
if (self.options.max) {
this.intensity.setMax(self.options.max);
}
if (self.options.min) {
this.intensity.setMin(self.options.min);
}
this.initAnimator();
this.bindEvent();
}
addAnimatorEvent() {
this.map.addEventListener('movestart', this.animatorMovestartEvent.bind(this));
this.map.addEventListener('moveend', this.animatorMoveendEvent.bind(this));
}
show() {
this.map.addOverlay(this.canvasLayer);
}
hide() {
this.map.removeOverlay(this.canvasLayer);
}
draw() {
this.canvasLayer.draw();
}
}
export default Layer;