-
Notifications
You must be signed in to change notification settings - Fork 5
/
AnimationEngineGRIB.js
740 lines (589 loc) · 24.8 KB
/
AnimationEngineGRIB.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// Classes defined here:
// AnimationEngineGRIB
// SourceData
// ParticleSystem
// Particle
class AnimationEngineGRIB {
// Static variables
static numActiveAnimations = 0;
// Variables
prevTime = 0;
canvasParticles = undefined;
source = undefined;
particles = undefined;
frameTime = 0;
FRAMERATE = 40; // in ms
isDestroyed = false;
// Constructor for data-based animation
// dataMatrix has the size of longPoints * latPoints * 2 values (east-north or value-angle)
// dataFormat is "east_north" || "value_angle"
// animType is "velocity" || "wave" || "whiteWave"
constructor(inCanvas, inMap, dataMatrix, minLat, minLong, maxLat, maxLong, dataFormat, animType) {
AnimationEngineGRIB.numActiveAnimations += 1; // Count the active animations
this.canvasParticles = inCanvas; // Canvas
this.map = inMap; // OL map
// Set height and width of the canvas
this.canvasParticles.width = this.map.getViewport().offsetWidth;
this.canvasParticles.height = this.map.getViewport().offsetHeight;
// Set up variable for when map is moving
this.mapIsMoving = false;
// https://stackoverflow.com/questions/11565471/removing-event-listener-which-was-added-with-bind
// Bind the event listeners
this.onMapMoveStart = this.onMapMoveStart.bind(this);
this.onMapMoveEnd = this.onMapMoveEnd.bind(this);
// Define animation format (for particle system)
let animation = {
type: animType, //"velocity" || "wave" || "whiteWave";
format: dataFormat, //"east_north" || "value_angle";
}
// Create new source
this.source = new SourceData(dataMatrix, minLat, minLong, maxLat, maxLong, animation);
// Create particle system
this.particles = new ParticleSystem(this.canvasParticles, this.source, this.map);
this.particles.clear();
// Reposition particles
this.particles.repositionParticles();
// Start drawing loop (only once)
this.update();
}
destroyer() {
AnimationEngineGRIB.numActiveAnimations += -1;
this.isDestroyed = true;
}
// Update the animation
update() {
// Check if it is deleted before anything else, to stop de loop
if (this.canvasParticles.parentElement == null) {
console.log("destroyed")
return;
}
if (this.isDestroyed) {
console.log("destroyed by destroyer");
return;
}
// Update timer
let timeNow = performance.now();
let dt = (timeNow - this.prevTime) / 1000; // in seconds;
this.frameTime = dt;
this.prevTime = timeNow;
// If data is loaded and layer is visible
if (this.source) {
//console.log(this.source.wmsURL);
if (this.source.isReady)
if (!this.mapIsMoving)
this.particles.draw(dt);
}
// Loop
//var that = this;
//setTimeout(function() {that.update()}, that.FRAMERATE); // Frame rate in milliseconds
setTimeout(() => this.update(), this.FRAMERATE);
}
// Callback when source is loaded
onSourceLoad() {
console.log("Source loaded!");
// Reposition particles when data is loaded
this.particles.repositionParticles();
}
resizeCanvas() {
// Resize animation canvas
this.canvasParticles.width = this.map.getViewport().offsetWidth;
this.canvasParticles.height = this.map.getViewport().offsetHeight;
// Resize num of particles
this.particles.resizeNumParticles();
}
// Callback when map size changes
onMapMoveEnd() {
this.resizeCanvas();
this.mapIsMoving = false;
if (this.source) {
if (this.source.isReady) {
this.particles.repositionParticles();
}
}
}
onMapMoveStart() {
if (this.particles)
this.particles.clear();
this.mapIsMoving = true;
}
// PUBLIC METHOD
static getNumActiveAnimations() {
return AnimationEngineGRIB.numActiveAnimations;
}
}
// Class that defines the source from a single WMS image
// class SourceWMS {
// // Variables
// imgDataEast;
// imgDataNorth;
// canvasLongLatStep = 0.02;
// colorrange;
// //medBBOX = [-19, 36, 31, 45]; // LONG, LAT -18.12, 36.3, 45.98, 30.18 from https://resources.marine.copernicus.eu/product-detail/MEDSEA_ANALYSISFORECAST_WAV_006_017/INFORMATION
// // WMS service does not always provide the BBOX given, be careful. Check with the URL
// medBBOX = [-17, 30, 30, 45];
// catseaBBOX = [-1, 36, 9, 44]; // LONG, LAT
// // Constructor
// constructor(animation) {
// this.isReady = false;
// this.animation = animation;
// }
// // When all tiles from layer are loaded, call a function
// defineOnLoadCallback(callbackOnLoad) {
// this.callbackFunc = callbackOnLoad; // Defined in Animation Engine
// }
// // Update/Change the WMS Source
// updateWMSSource(wmsURL, animation) {
// // Keep track of images loaded
// this.isReady = false;
// this.loaded = 0;
// // Store animation information defined in ForecastBar.vue
// this.animation = animation;
// this.wmsURL = wmsURL; // Only for debuggin;
// // Define WMS image url with a standard size
// // SIZE TODO: could be random size or according to lat-long extension?
// wmsURL = SourceWMS.setWMSParameter(wmsURL, 'WIDTH', '2048');
// wmsURL = SourceWMS.setWMSParameter(wmsURL, 'HEIGHT', '1024');
// // BBOX
// this.bbox = this.medBBOX;//this.catseaBBOX;
// wmsURL = SourceWMS.setWMSParameter(wmsURL, 'BBOX', JSON.stringify(this.bbox).replace('[', '').replace(']', ''));
// // STYLE gray
// let style = 'boxfill/greyscale';
// wmsURL = SourceWMS.setWMSParameter(wmsURL, 'STYLES', style);
// // PROJECTION EPSG:4326 (lat and long are equally distributed in pixels)
// wmsURL = SourceWMS.setWMSParameter(wmsURL, 'PROJECTION', 'EPSG:4326');
// // OUT OF RANGE PIXELS &ABOVEMAXCOLOR=extend&BELOWMINCOLOR=extend
// wmsURL = SourceWMS.setWMSParameter(wmsURL, 'ABOVEMAXCOLOR', 'extend');
// wmsURL = SourceWMS.setWMSParameter(wmsURL, 'BELOWMINCOLOR', 'extend');
// // COLORRANGE
// this.colorrange = SourceWMS.getWMSParameter(wmsURL, 'COLORSCALERANGE').split('%2C');
// this.colorrange = this.colorrange.map((e) => parseFloat(e));
// // Paint this image streched in a canvas of width and height related to the bounding box
// // Each pixel corresponds to a lat-long
// this.longExtension = Math.abs(this.bbox[0] - this.bbox[1]);
// this.latExtension = Math.abs(this.bbox[2] - this.bbox[3]);
// // Make canvas lat-long relationship
// let canvas = document.createElement('canvas');
// canvas.width = this.longExtension / this.canvasLongLatStep;
// canvas.height = this.latExtension / this.canvasLongLatStep;
// let ctx = canvas.getContext('2d');
// //document.body.append(canvas);
// // WMS data layers
// if (animation.layerNames.length == 2) {
// for (let i = 0; i < 2; i++) {
// // LAYER east and north or intensity and angle
// wmsURL = SourceWMS.setWMSParameter(wmsURL, 'LAYERS', animation.layerNames[i]);
// // For data stored in intensity and angle, the colorrange of the angle should go from 0 to 360
// if (animation.format == 'value_angle' && i == 1)
// wmsURL = SourceWMS.setWMSParameter(wmsURL, 'COLORSCALERANGE', '0,360');
// // For data stored in east-north, there range should have negative values too
// else if (animation.format == 'east_north') {
// this.colorrange[0] = -this.colorrange[1];
// wmsURL = SourceWMS.setWMSParameter(wmsURL, 'COLORSCALERANGE', this.colorrange.toString());
// }
// // Get WMS image data
// console.log("Loading data source WMS images...");
// let img = new Image();
// img.crossOrigin = "Anonymous";
// // Image is loaded, paint it in the canvas and get the data
// img.onload = () => {
// // Paint streched WMS image in canvas
// // For more details: https://developer.mozilla.org/es/docs/Web/CSS/image-rendering
// ctx.clearRect(img, 0, 0, canvas.width, canvas.height);
// ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
// if (i == 0)
// this.imgDataEast = ctx.getImageData(0, 0, canvas.width, canvas.height); // Store image data
// else
// this.imgDataNorth = ctx.getImageData(0, 0, canvas.width, canvas.height); // Store image data
// this.loaded++;
// // Both layers are loaded
// if (this.loaded == 2) {
// this.isReady = true;
// //document.body.append(canvas);
// // Callback
// if (this.callbackFunc)
// this.callbackFunc();
// }
// };
// // Start loading image
// img.src = wmsURL;
// console.log(wmsURL);
// }
// }
// }
// // Set WMS parameter
// static setWMSParameter(wmsURL, paramName, paramContent) {
// // If parameter does not exist
// if (wmsURL.indexOf(paramName + "=") == -1) {
// console.log("Parameter ", paramName, " does not exist in WMS URL");
// return wmsURL + '&' + paramName + '=' + paramContent;
// }
// let currentContent = SourceWMS.getWMSParameter(wmsURL, paramName);
// return wmsURL.replace(currentContent, paramContent);
// }
// // Get WMS parameter
// static getWMSParameter(wmsURL, paramName) {
// // If parameter does not exist
// if (wmsURL.indexOf(paramName + "=") == -1) {
// console.log("Parameter ", paramName, " does not exist in WMS URL");
// return '';
// }
// let tmpSTR = wmsURL.substr(wmsURL.indexOf(paramName + "="));
// let endOfContent = tmpSTR.indexOf('&') == -1 ? tmpSTR.length : tmpSTR.indexOf('&');
// return tmpSTR.substring(paramName.length + 1, endOfContent);
// }
// // Get value at Long Lat
// getValueAtLongLat(long, lat, value) {
// // If is outside bbox
// let minLong = Math.min(this.bbox[0], this.bbox[1]);
// let minLat = Math.min(this.bbox[2], this.bbox[3]);
// if (lat < minLat || lat > Math.max(this.bbox[2], this.bbox[3]) || // lat
// long < minLong || long > Math.max(this.bbox[0], this.bbox[1])) { // long
// value[0] = undefined; value[1] = undefined; // Reset value
// return value;
// }
// // Get closest pixel to long lat (nearest-neighbour interpolation)
// let colPixelPos = Math.round((long - minLong) / this.canvasLongLatStep);
// let rowPixelPos = Math.round((lat - minLat) / this.canvasLongLatStep);
// // Invert row pixel position (mirror data on the horizontal axis)
// rowPixelPos = this.imgDataEast.height - rowPixelPos;
// // Index
// let index = rowPixelPos * this.imgDataEast.width + colPixelPos;
// // Get RGB values (grayscale)
// let redE = this.imgDataEast.data[index * 4] / 255; //R east
// let redN = this.imgDataNorth.data[index * 4] / 255; //R north
// let alphaE = this.imgDataEast.data[index * 4 + 3] / 255; // Alpha
// // Low alpha
// if (alphaE < 0.1) {
// value[0] = undefined; value[1] = undefined; // Reset value
// return value;
// }
// // Rescale RGB value to real unit
// // Images store East and North intensity
// if (this.animation.format == 'east_north') {
// value[0] = redE * (this.colorrange[1] - this.colorrange[0]) + this.colorrange[0]; // lat
// value[1] = redN * (this.colorrange[1] - this.colorrange[0]) + this.colorrange[0]; // long
// }
// // Images store Intensity and Angle in degrees
// else if (this.animation.format == 'value_angle') {
// let intensity = redE * (this.colorrange[1] - this.colorrange[0]) + this.colorrange[0];
// let angle = redN * 360;
// value[1] = - 0.1 * Math.cos(angle * Math.PI / 180) * intensity;
// value[0] = - 0.1 * Math.sin(angle * Math.PI / 180) * intensity;
// }
// //console.log(long + ", " + lat + ", " + value);
// return value;
// }
// /*getValueAtPixel(pixel, value) {
// let index = pixel[0] * this.imgDataEast.width + pixel[1];
// if (index > this.imgDataEast.data.length)
// return [0, 0];
// // Get RGB values (grayscale)
// let redE = this.imgDataEast.data[index * 4] / 255; //R east
// let redN = this.imgDataNorth.data[index * 4] / 255; //R north
// // Rescale RGB value to real unit
// value[0] = redE * (this.colorrange[1] - this.colorrange[0]) + this.colorrange[0]; // lat
// value[1] = redN * (this.colorrange[1] - this.colorrange[0]) + this.colorrange[0]; // long
// return value
// }*/
// }
class SourceData {
constructor(dataMatrix, minLat, minLong, maxLat, maxLong, animation) {
this.dataMatrix = dataMatrix;
this.minLat = minLat;
this.minLong = minLong;
this.maxLat = maxLat;
this.maxLong = maxLong;
this.animation = animation;
// Get max and min values
// WHY IS THIS NECESSARY? FOR ANIMATION PATH?
this.maxValue = -Infinity;
for (let i = 0; i < dataMatrix.length; i++){
for (let j = 0; j < dataMatrix[0].length; j++){
let valueU = dataMatrix[i][j][0];
let valueV = dataMatrix[i][j][1];
if (!isNaN(valueU) && !isNaN(valueV)){
this.maxValue = Math.max(this.maxValue, Math.abs(valueU));
this.maxValue = Math.max(this.maxValue, Math.abs(valueV));
}
}
}
this.isReady = true;
}
getValueAtLongLat = function (long, lat, value) {
if ((lat < this.minLat || lat > this.maxLat) || // lat
(long < this.minLong || long > this.maxLong)) { // long
value[0] = undefined; value[1] = undefined; // Reset value
return value;
}
let numRows = this.dataMatrix.length;
let numCols = this.dataMatrix[0].length;
let iIndex = Math.floor(numRows * (lat - this.minLat) / (this.maxLat - this.minLat));
let jIndex = Math.floor(numCols * (long - this.minLong) / (this.maxLong - this.minLong));
iIndex = -iIndex + numRows - 1; // Mirror
value[0] = this.dataMatrix[iIndex][jIndex][0];
value[1] = this.dataMatrix[iIndex][jIndex][1];
value[0] = (value[0] === null || isNaN(value[0])) ? undefined : value[0];
value[1] = (value[1] === null || isNaN(value[1])) ? undefined : value[1];
// Find location
return value;
}
}
// Class that manages the particle system
class ParticleSystem {
// Variables
fullScreenNumParticles = 10000;
speedFactor = 0.7;
fullScreenPixels = 1920 * 1080;
// Constructor
constructor(canvas, source, olMap) {
// Canvas
this.canvas = canvas;
this.ctx = canvas.getContext("2d");
// Source
this.source = source;
// Map
this.map = olMap;
// Number of particles
this.resizeNumParticles();
// Create particles
this.particles = [];
for (let i = 0; i < this.fullScreenNumParticles; i++)
this.particles[i] = new Particle(this);
}
// Functions
// Set num particles according to the number of pixels
resizeNumParticles() {
let numPixels = this.canvas.width * this.canvas.height;
let numParticlesFactor = numPixels / this.fullScreenPixels;
// Active number of animations
numParticlesFactor /= AnimationEngineGRIB.getNumActiveAnimations();
// Define number of particles
this.numParticles = Math.min(Math.round(numParticlesFactor * this.fullScreenNumParticles), this.fullScreenNumParticles);
}
// Reposition particles
repositionParticles() {
for (let i = 0; i < this.numParticles; i++)
this.particles[i].repositionParticle();
}
// Clear canvas and reset particles. Important when the map moves, because some undesired lines appear
clear() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
draw(dt) {
// Trail effect
// https://codepen.io/Tyriar/pen/BfizE
this.ctx.fillStyle = 'rgba(255, 255, 255, .9)';
this.ctx.globalCompositeOperation = "destination-in";
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
this.ctx.globalCompositeOperation = "source-over";
// Trail color
this.ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
// Line style
this.ctx.strokeStyle = 'rgba(0, 0, 0, 0.7)';
this.ctx.lineWidth = 1.5;
this.ctx.beginPath();
for (let i = 0; i < this.numParticles; i++)
this.particles[i].draw(dt);
this.ctx.stroke();
}
}
// Particle class
class Particle {
// Variables
numVerticesPath = 20;
stepInPixels = 20; // Step (ideally in lat, long, not in pixels)
color = [0, 0, 0];
// Constructor
constructor(particleSystem) {
this.particleSystem = particleSystem;
this.life = Math.random();
// Variable for optimization
this.valueVec2 = [0, 0];
this.pointVec2 = [0, 0];
// Variables for drawing
this.prevPos = [0, 0];
this.currentPos = [0, 0];
// Drawing function
if (particleSystem.source.animation.type == 'velocity') {
this.draw = this.drawVelocity;
this.numVerticesPath = 20;
this.stepInPixels = 20;
}
else if (particleSystem.source.animation.type == 'wind') {
this.draw = this.drawVelocity;
this.numVerticesPath = 20;
this.stepInPixels = 1;
this.particleSystem.speedFactor = 0.01;
}
else if (particleSystem.source.animation.type == 'wave') {
this.draw = this.drawWaves;
this.numVerticesPath = 8;
this.stepInPixels = 20;
}
else if (particleSystem.source.animation.type == 'whiteWave') {
this.draw = this.drawWaves;
this.numVerticesPath = 4;
this.stepInPixels = 20;
this.color = [255, 255, 255];
this.particleSystem.speedFactor = 6;
}
this.vertices = new Float32Array(this.numVerticesPath * 2); // XY values
this.verticesValue = new Float32Array(this.numVerticesPath); // Wind/Current/Wave
}
// Functions
// Reposition particle
repositionParticle() {
// Reset previous position for painting path
this.prevPos[0] = undefined;
this.prevPos[1] = undefined;
// Generate starting vertex with initial value
this.generatePoint(this.pointVec2, this.valueVec2);
// Assign initial position
this.vertices[0] = this.pointVec2[0];
this.vertices[1] = this.pointVec2[1];
// Create vertices path
for (var i = 1; i < this.numVerticesPath; i++) {
// Make step
// North is inverted because of pixels (less pixels, more nord)
this.pointVec2[0] += this.valueVec2[0] * this.stepInPixels || 0; // 0 if there is no data
this.pointVec2[1] -= this.valueVec2[1] * this.stepInPixels || 0; // 0 if there is no data
// Assign positions to vertices array
this.vertices[i * 2] = this.pointVec2[0];
this.vertices[i * 2 + 1] = this.pointVec2[1];
// Get new value according to point
//this.valueVec2 = this.particleSystem.source.getValueAtPixel(this.pointVec2, this.valueVec2); // Is rounding the pixel movement, could be done with floats
// Transform pixel to long lat
let coord = this.particleSystem.map.getCoordinateFromPixel(this.pointVec2);
if (coord == null)
return;
//coord = ol.proj.transform(coord, 'EPSG:3857', 'EPSG:4326');
// Get new value according to longitude and latitude
this.valueVec2 = this.particleSystem.source.getValueAtLongLat(coord[0], coord[1], this.valueVec2);
// Assign values
if (this.valueVec2[0] !== undefined)
this.verticesValue[i] = Math.sqrt(this.valueVec2[0] * this.valueVec2[0] + this.valueVec2[1] * this.valueVec2[1]);
}
}
// Generate new point
// Could be done more intelligent, like taking the extent of the layer from openlayers or the WMS service
generatePoint(point, value, callStackNum) {
callStackNum = callStackNum || 1;
// Generate random X,Y number
// TODO: random should be within lat-long limits
let source = this.particleSystem.source;
let map = this.particleSystem.map;
let ppWidth = this.particleSystem.canvas.width;
let ppHeight = this.particleSystem.canvas.height;
let ppMin = map.getPixelFromCoordinate([source.minLong, source.minLat]);
let ppMax = map.getPixelFromCoordinate([source.maxLong, source.maxLat]);
if (ppMin == null)
return;
// Limit bbox
ppMin[0] = ppMin[0] < 0 ? 0 : ppMin[0] > ppWidth ? ppWidth : ppMin[0];
ppMin[1] = ppMin[1] < 0 ? 0 : ppMin[1] > ppHeight ? ppWidth : ppMin[1];
ppMax[0] = ppMax[0] < 0 ? 0 : ppMax[0] > ppWidth ? ppWidth : ppMax[0];
ppMax[1] = ppMax[1] < 0 ? 0 : ppMax[1] > ppHeight ? ppWidth : ppMax[1];
point[0] = Math.random() * (ppMax[0] - ppMin[0]) + ppMin[0];//Math.random() * this.particleSystem.canvas.width;
point[1] = Math.random() * (ppMin[1] - ppMax[1]) + ppMax[1];//Math.random() * this.particleSystem.canvas.height;
// Check if it has data
if (point[0] == undefined || isNaN(point[0]) || point[0] == null)
console.error(point);
// Get value at pixel
// Transform pixel to long lat
let coord = this.particleSystem.map.getCoordinateFromPixel(point); // returns [long,lat]?
if (coord == null && callStackNum < 20) { // Library is not loaded yet?
callStackNum++;
this.generatePoint(point, value, callStackNum);
}
else if (callStackNum >= 20){ // Library is not yet loaded
return;
}
//coord = ol.proj.transform(coord, 'EPSG:3857', 'EPSG:4326'); // HACK: I don't know why in this map it is already in 4326
// Get value at long lat from source
//value = this.particleSystem.source.getValueAtPixel(point, value);
value = this.particleSystem.source.getValueAtLongLat(coord[0], coord[1], value);
// If pixel does not contain data, throw it again at least 20 times
if (value[0] == undefined && callStackNum < 20) {
this.generatePoint(point, value, callStackNum + 1); // Recursive function
}
}
// Draw / Update
drawVelocity(dt) {
// Update life
this.life += 0.005 + this.particleSystem.speedFactor * 0.1 * this.verticesValue[Math.round(this.life * this.numVerticesPath)];
// Reset life
if (this.life > 1 || isNaN(this.life)) {
this.life = Math.random();
// Start of vertices path
this.prevPos[0] = undefined;
this.prevPos[1] = undefined;
}
// Get exact position
let prevVertPath = Math.floor(this.life * (this.numVerticesPath - 1)); // From 0 to numVerticesPath
let nextVertPath = Math.ceil(this.life * (this.numVerticesPath - 1)); // From 0 to numVerticesPath
// Interpolation value
let interpCoeff = (nextVertPath - this.life * (this.numVerticesPath - 1));
this.currentPos[0] = interpCoeff * this.vertices[prevVertPath * 2] +
(1 - interpCoeff) * this.vertices[nextVertPath * 2];
this.currentPos[1] = interpCoeff * this.vertices[prevVertPath * 2 + 1] +
(1 - interpCoeff) * this.vertices[nextVertPath * 2 + 1];
// When prevPos is not valid (map moved, source changed, etc)
if (this.prevPos[0] == undefined) {
this.prevPos[0] = this.currentPos[0];
this.prevPos[1] = this.currentPos[1];
return;
}
// Draw in canvas
let ctx = this.particleSystem.ctx;
ctx.moveTo(this.prevPos[0], this.prevPos[1])
ctx.lineTo(this.currentPos[0], this.currentPos[1]);
// Assign prevPos
this.prevPos[0] = this.currentPos[0];
this.prevPos[1] = this.currentPos[1];
}
// Draw waves
drawWaves(dt) {
// Value
let value = this.verticesValue[Math.round(this.life * this.numVerticesPath)];
// Update life
this.life += 0.01 + this.particleSystem.speedFactor * 0.01;
// Reset life
if (this.life > 1 || isNaN(this.life)) {
this.life = Math.random();
// Start of vertices path
this.prevPos[0] = undefined;
this.prevPos[1] = undefined;
}
// Get exact position
let prevVertPath = Math.floor(this.life * (this.numVerticesPath - 1)); // From 0 to numVerticesPath
let nextVertPath = Math.ceil(this.life * (this.numVerticesPath - 1)); // From 0 to numVerticesPath
// Interpolation value
let interpCoeff = (nextVertPath - this.life * (this.numVerticesPath - 1));
this.currentPos[0] = interpCoeff * this.vertices[prevVertPath * 2] +
(1 - interpCoeff) * this.vertices[nextVertPath * 2];
this.currentPos[1] = interpCoeff * this.vertices[prevVertPath * 2 + 1] +
(1 - interpCoeff) * this.vertices[nextVertPath * 2 + 1];
// When prevPos is not valid (map moved, source changed, etc)
if (this.prevPos[0] == undefined) {
this.prevPos[0] = this.currentPos[0];
this.prevPos[1] = this.currentPos[1];
return;
}
// Varying alpha (strongest alpha in the middle of the path)
let alphaFactor = 16 * Math.pow((1 - this.life) * (this.life), 2);
// Draw in canvas
let ctx = this.particleSystem.ctx;
// Change linewidth according to value
ctx.stroke();
ctx.beginPath();
ctx.lineWidth = Math.max(value * 15, 4);
//ctx.fillStyle = 'rgba(0, 0, 0, ', alphaFactor*0.0, ')';
let colorStr = 'rgba(' + this.color[0] + ',' + this.color[1] + ',' + this.color[2] + ', ' + alphaFactor * 0.5 + ')'
ctx.strokeStyle = colorStr; // Makes the app go slow, consider something different
ctx.moveTo(this.prevPos[0], this.prevPos[1])
ctx.lineTo(this.currentPos[0], this.currentPos[1]);
// Assign prevPos
this.prevPos[0] = this.currentPos[0];
this.prevPos[1] = this.currentPos[1];
}
}