-
Notifications
You must be signed in to change notification settings - Fork 4
/
L.WindCanvas.js
238 lines (204 loc) · 8.6 KB
/
L.WindCanvas.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
/*
Extent from Canvas Layer by Stanislav Sumbera, 2016-2018, sumbera.com
Specific for windy
*/
// -- L.DomUtil.setTransform from leaflet 1.0.0 to work on 0.0.7
//------------------------------------------------------------------------------
L.DomUtil.setTransform = L.DomUtil.setTransform || function (el, offset, scale) {
var pos = offset || new L.Point(0, 0);
el.style[L.DomUtil.TRANSFORM] =
(L.Browser.ie3d ?
'translate(' + pos.x + 'px,' + pos.y + 'px)' :
'translate3d(' + pos.x + 'px,' + pos.y + 'px,0)') +
(scale ? ' scale(' + scale + ')' : '');
};
// -- support for both 0.0.7 and 1.0.0 rc2 leaflet
L.WindCanvas = (L.Layer ? L.Layer : L.Class).extend({
options: {
opacity: 1,
pane: 'overlayPane'
},
// -- initialized is called on prototype
initialize: function (options) {
this._map = null;
this._canvas = null;
this._frame = null;
this._delegate = null;
L.setOptions(this, options);
},
delegate :function(del){
this._delegate = del;
return this;
},
needRedraw: function () {
if (!this._frame) {
this._frame = L.Util.requestAnimFrame(this.drawLayer, this);
}
return this;
},
clear: function () {
this._canvas1.getContext('2d').clearRect(0, 0, 3000, 3000);
this._canvas2.getContext('2d').clearRect(0, 0, 3000, 3000);
},
//-------------------------------------------------------------
_onLayerDidResize: function (resizeEvent) {
if (resizeEvent) {
var go_hide_canvas = null
if (this._canvas == this._canvas2) {
this._canvas = this._canvas1;
L.DomUtil.removeClass(this._canvas1, "leaflet-layer-fade");
this._canvas1.style.opacity = this.options.opacity;
go_hide_canvas = this._canvas2;
}
else {
this._canvas = this._canvas2;
L.DomUtil.removeClass(this._canvas2, "leaflet-layer-fade");
this._canvas2.style.opacity = this.options.opacity;
go_hide_canvas = this._canvas1;
}
this._canvas1.width = resizeEvent.newSize.x;
this._canvas1.height = resizeEvent.newSize.y;
this._canvas2.width = resizeEvent.newSize.x;
this._canvas2.height = resizeEvent.newSize.y;
L.DomUtil.addClass(go_hide_canvas, "leaflet-layer-fade");
go_hide_canvas.style.opacity = 0;
}
},
//-------------------------------------------------------------
_onLayerDidMove: function () {
var go_hide_canvas = null;
if (this._canvas == this._canvas2) {
this._canvas = this._canvas1;
this._canvas.getContext('2d').clearRect(0, 0, 3000, 3000);
L.DomUtil.removeClass(this._canvas1, "leaflet-layer-fade");
this._canvas1.style.opacity = this.options.opacity;
go_hide_canvas = this._canvas2;
}
else {
this._canvas = this._canvas2;
this._canvas.getContext('2d').clearRect(0, 0, 3000, 3000);
L.DomUtil.removeClass(this._canvas2, "leaflet-layer-fade");
this._canvas2.style.opacity = this.options.opacity;
go_hide_canvas = this._canvas1;
}
var topLeft = this._map.containerPointToLayerPoint([0, 0]);
L.DomUtil.setPosition(this._canvas, topLeft);
this.drawLayer(true);
L.DomUtil.addClass(go_hide_canvas, "leaflet-layer-fade");
go_hide_canvas.style.opacity = 0;
},
_onLayerDidZoom: function() {
//pass
},
//-------------------------------------------------------------
getEvents: function () {
var events = {
resize: this._onLayerDidResize,
moveend: this._onLayerDidMove,
zoomend: this._onLayerDidZoom
};
if (this._map.options.zoomAnimation && L.Browser.any3d) {
events.zoomanim = this._animateZoom;
}
return events;
},
//-------------------------------------------------------------
onAdd: function (map) {
this._map = map;
this._canvas1 = L.DomUtil.create('canvas', 'leaflet-layer');
this._canvas2 = L.DomUtil.create('canvas', 'leaflet-layer');
if (typeof this.options.zIndex !== 'undefined') {
this._canvas1.style.zIndex = this.options.zIndex;
this._canvas2.style.zIndex = this.options.zIndex;
}
if (typeof this.options.className !== 'undefined') {
L.DomUtil.addClass(this._canvas1, this.options.className);
L.DomUtil.addClass(this._canvas2, this.options.className);
}
this._canvas1.style.opacity = this.options.opacity;
this._canvas2.style.opacity = 0;
this._canvas = this._canvas1;
var size = this._map.getSize();
this._canvas1.width = size.x;
this._canvas1.height = size.y;
this._canvas2.width = size.x;
this._canvas2.height = size.y;
var animated = this._map.options.zoomAnimation && L.Browser.any3d;
L.DomUtil.addClass(this._canvas1, 'leaflet-zoom-' + (animated ? 'animated' : 'hide'));
L.DomUtil.addClass(this._canvas2, 'leaflet-zoom-' + (animated ? 'animated' : 'hide'));
map._panes[this.options.pane].appendChild(this._canvas1);
map._panes[this.options.pane].appendChild(this._canvas2);
var topLeft = this._map.containerPointToLayerPoint([0, 0]);
L.DomUtil.setPosition(this._canvas1, topLeft);
L.DomUtil.setPosition(this._canvas2, topLeft);
map.on(this.getEvents(),this);
var del = this._delegate || this;
del.onLayerDidMount && del.onLayerDidMount(); // -- callback
this.needRedraw();
},
//-------------------------------------------------------------
onRemove: function (map) {
var del = this._delegate || this;
del.onLayerWillUnmount && del.onLayerWillUnmount(); // -- callback
if (this._frame) {
L.Util.cancelAnimFrame(this._frame);
}
map.getPanes().overlayPane.removeChild(this._canvas);
map.off(this.getEvents(),this);
this._canvas = null;
},
//------------------------------------------------------------
addTo: function (map) {
map.addLayer(this);
return this;
},
// --------------------------------------------------------------------------------
LatLonToMercator: function (latlon) {
return {
x: latlon.lng * 6378137 * Math.PI / 180,
y: Math.log(Math.tan((90 + latlon.lat) * Math.PI / 360)) * 6378137
};
},
//------------------------------------------------------------------------------
drawLayer: function (no_worker) {
// -- todo make the viewInfo properties flat objects.
var size = this._map.getSize();
var bounds = this._map.getBounds();
var zoom = this._map.getZoom();
var center = this.LatLonToMercator(this._map.getCenter());
var corner = this.LatLonToMercator(this._map.containerPointToLatLng(this._map.getSize()));
var del = this._delegate || this;
del.onDrawLayer && del.onDrawLayer({
layer : this,
canvas: this._canvas,
bounds: bounds,
size: size,
zoom: zoom,
center : center,
corner : corner,
no_worker: no_worker
});
this._frame = null;
},
// -- L.DomUtil.setTransform from leaflet 1.0.0 to work on 0.0.7
//------------------------------------------------------------------------------
_setTransform: function (el, offset, scale) {
var pos = offset || new L.Point(0, 0);
el.style[L.DomUtil.TRANSFORM] =
(L.Browser.ie3d ?
'translate(' + pos.x + 'px,' + pos.y + 'px)' :
'translate3d(' + pos.x + 'px,' + pos.y + 'px,0)') +
(scale ? ' scale(' + scale + ')' : '');
},
//------------------------------------------------------------------------------
_animateZoom: function (e) {
var scale = this._map.getZoomScale(e.zoom);
// -- different calc of animation zoom in leaflet 1.0.3 thanks @peterkarabinovic, @jduggan1
var offset = L.Layer ? this._map._latLngBoundsToNewLayerBounds(this._map.getBounds(), e.zoom, e.center).min :
this._map._getCenterOffset(e.center)._multiplyBy(-scale).subtract(this._map._getMapPanePos());
L.DomUtil.setTransform(this._canvas, offset, scale);
}
});
L.windCanvas = function (options) {
return new L.WindCanvas(options);
};