globe-contour.html
6.0 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
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Globe - ECHARTS-GL</title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes"> <!-- Fullscreen Landscape on iOS -->
<link rel="stylesheet" href="./common.css">
</head>
<body>
<div id="main"></div>
<script src="https://unpkg.com/d3-contour@1"></script>
<script src="https://unpkg.com/d3-geo@1"></script>
<script src="https://unpkg.com/d3-timer@1"></script>
<script src="../node_modules/echarts/dist/echarts.js"></script>
<script src="https://cdn.jsdelivr.net/npm/echarts@4.9.0/map/js/world.js"></script>
<script src="../dist/echarts-gl.js"></script>
<script src="lib/jquery.min.js"></script>
<script src="lib/stackBlur.js"></script>
<script src="lib/dat.gui.js"></script>
<script src="js/commonUI.js"></script>
<script>
var config = {
color: '#c0101a',
levels: 1,
intensity: 8,
threshold: 0.01
}
var canvas = document.createElement('canvas');
canvas.width = 4096;
canvas.height = 2048;
context = canvas.getContext("2d");
context.lineWidth = 0.5;
context.strokeStyle = config.color;
context.fillStyle = config.color;
context.shadowColor = config.color;
var gui = new dat.GUI();
image("asset/bathymetry_bw_composite_4k.jpg").then(function(image) {
var m = image.height,
n = image.width,
values = new Array(n * m),
contours = d3.contours().size([n, m]).smooth(true),
projection = d3.geoIdentity().scale(canvas.width / n),
path = d3.geoPath(projection, context);
StackBlur.R(image, 5);
for (var j = 0, k = 0; j < m; ++j) {
for (var i = 0; i < n; ++i, ++k) {
values[k] = image.data[(k << 2)] / 255;
}
}
var option = {
image: canvas
}
var results = [];
function update(threshold, levels) {
context.clearRect(0, 0, canvas.width, canvas.height);
var thresholds = [];
for (var i = 0; i < levels; i++) {
thresholds.push((threshold + 1 / levels * i) % 1);
}
results = contours.thresholds(thresholds)(values);
redraw();
}
function redraw() {
results.forEach(function (d, idx) {
context.beginPath();
path(d);
context.globalAlpha = 1;
context.stroke();
if (idx > config.levels / 5 * 3) {
context.globalAlpha = 0.01;
context.fill();
}
});
option.onupdate();
}
d3.timer(function(t) {
var threshold = (t % 10000) / 10000;
update(threshold, 1);
});
initCharts(option);
update(config.threshold, config.levels);
// gui.add(config, 'levels', 0, 100).step(1).onFinishChange(function () {
// update(config.threshold, config.levels);
// });
// gui.add(config, 'threshold', 0, 1.00).onFinishChange(function () {
// update(config.threshold, config.levels);
// });
gui.addColor(config, 'color').onChange(function () {
context.strokeStyle = context.fillStyle = context.shadowColor = config.color;
redraw();
});
});
function image(url) {
return new Promise(function(resolve) {
var image = new Image;
image.src = url;
image.onload = function() {
var canvas = document.createElement("canvas");
canvas.width = image.width / 4;
canvas.height = image.height / 4;
var context = canvas.getContext("2d");
context.drawImage(image, 0, 0, canvas.width, canvas.height);
resolve(context.getImageData(0, 0, canvas.width, canvas.height));
};
});
}
var chart;
function initCharts(option) {
chart = echarts.init(document.getElementById('main'));
var contourChart = echarts.init(document.createElement('canvas'), null, {
width: 4096,
height: 2048
});
var img = new echarts.graphic.Image({
style: {
image: option.image,
x: -1,
y: -1,
width: option.image.width + 2,
height: option.image.height + 2
}
});
contourChart.getZr().add(img);
option.onupdate = function () {
img.dirty();
}
// https://www.behance.net/gallery/50293671/T-GLOBEhttps://bl.ocks.org/mbostock/818053c76d79d4841790c332656bf9da
chart.setOption({
globe: {
// environment: 'asset/starfield.jpg',
heightTexture: 'asset/bathymetry_bw_composite_4k.jpg',
displacementScale: 0.05,
displacementQuality: 'high',
baseColor: '#111',
shading: 'realistic',
realisticMaterial: {
roughness: 0.2,
metalness: 0
},
postEffect: {
enable: false,
depthOfField: {
enable: true
},
bloom: {
intensity: 0.05
}
},
temporalSuperSampling: {
enable: true
},
light: {
ambient: {
intensity: 0
},
main: {
intensity: 0.1,
shadow: false
},
ambientCubemap: {
texture: 'asset/lake.hdr',
exposure: 1,
diffuseIntensity: 0.5,
specularIntensity: 2
}
},
viewControl: {
autoRotate: false
},
layers: [{
type: 'blend',
blendTo: 'emission',
texture: contourChart,
intensity: config.intensity
}]
}
});
window.addEventListener('resize', function () {
chart.resize();
});
gui.add(config, 'intensity', 0, 10).onChange(function () {
chart.setOption({
globe: {
layers: [{
intensity: config.intensity
}]
}
})
});
}
</script>
</body>
</html>