-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathindex.js
More file actions
103 lines (90 loc) · 2.94 KB
/
index.js
File metadata and controls
103 lines (90 loc) · 2.94 KB
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
// using var to work around a WebKit bug
var canvas = document.getElementById('canvas'); // eslint-disable-line
const pxRatio = Math.max(Math.floor(window.devicePixelRatio) || 1, 2);
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
const gl = canvas.getContext('webgl', {antialiasing: false});
const wind = window.wind = new WindGL(gl);
wind.numParticles = 65536;
function frame() {
if (wind.windData) {
wind.draw();
}
requestAnimationFrame(frame);
}
frame();
const gui = new dat.GUI();
gui.add(wind, 'numParticles', 1024, 589824);
gui.add(wind, 'fadeOpacity', 0.96, 0.999).step(0.001).updateDisplay();
gui.add(wind, 'speedFactor', 0.05, 1.0);
gui.add(wind, 'dropRate', 0, 0.1);
gui.add(wind, 'dropRateBump', 0, 0.2);
const windFiles = {
0: '2021062700',
24: '2021062800',
48: '2021062900',
72: '2021063000',
96: '2021070100'
};
const meta = {
'2021-06-27+h': 0,
'retina resolution': true,
'github.com/mapbox/webgl-wind': function () {
window.location = 'https://github.com/mapbox/webgl-wind';
}
};
gui.add(meta, '2021-06-27+h', 0, 96, 24).onFinishChange(updateWind);
if (pxRatio !== 1) {
gui.add(meta, 'retina resolution').onFinishChange(updateRetina);
}
gui.add(meta, 'github.com/mapbox/webgl-wind');
updateWind(0);
updateRetina();
function updateRetina() {
const ratio = meta['retina resolution'] ? pxRatio : 1;
canvas.width = canvas.clientWidth * ratio;
canvas.height = canvas.clientHeight * ratio;
wind.resize();
}
getJSON('https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_110m_coastline.geojson', function (data) {
const canvas = document.getElementById('coastline');
canvas.width = canvas.clientWidth * pxRatio;
canvas.height = canvas.clientHeight * pxRatio;
const ctx = canvas.getContext('2d');
ctx.lineWidth = pxRatio;
ctx.lineJoin = ctx.lineCap = 'round';
ctx.strokeStyle = 'white';
ctx.beginPath();
for (let i = 0; i < data.features.length; i++) {
const line = data.features[i].geometry.coordinates;
for (let j = 0; j < line.length; j++) {
ctx[j ? 'lineTo' : 'moveTo'](
(line[j][0] + 180) * canvas.width / 360,
(-line[j][1] + 90) * canvas.height / 180);
}
}
ctx.stroke();
});
function updateWind(name) {
getJSON('wind/' + windFiles[name] + '.json', function (windData) {
const windImage = new Image();
windData.image = windImage;
windImage.src = 'wind/' + windFiles[name] + '.png';
windImage.onload = function () {
wind.setWind(windData);
};
});
}
function getJSON(url, callback) {
const xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open('get', url, true);
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
callback(xhr.response);
} else {
throw new Error(xhr.statusText);
}
};
xhr.send();
}