-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
284 lines (220 loc) · 8.49 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>GeoJson - Three - Earth</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta content="Jane" name="author" />
<meta content="GeoJson - Three - Earth" name="description" />
<style>
html {
width: 100%;
height: 100%;
}
body {
width: 100%;
height: 100%;
color: #808080;
font-family: Monospace;
font-size: 13px;
text-align: center;
background-color: #242430;
margin: 0px;
overflow: hidden;
}
#container {
width: 100%;
height: 100%;
}
#info {
position: absolute;
width: 100px;
right: 0;
bottom: 20px;
font-size: 14px;
}
#info>a {
color: #eee;
text-decoration: none;
}
</style>
</head>
<body>
<div id="info">
<a href="https://github.com/jiangyuzhen">By - 姜姜姜</a>
</div>
<div id="container"></div>
<script src="./libs/three.min.js"></script>
<script src="./libs/OrbitControls.js"></script>
<script src="https://cdn.bootcss.com/echarts/3.8.5/echarts.min.js"></script>
<script src="./data/world.js"></script>
<script src="./data/china.js"></script>
<script>
var camera, controls, scene, renderer;
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var directionalLight;
var earth;
var mapSize = {
width: 4096,
height: 2048
};
var mapCanvas, mapTexture;
init();
animate();
// 初始化echarts地图
function initMap() {
mapCanvas = document.createElement('canvas');
mapCanvas.width = mapSize.width;
mapCanvas.height = mapSize.height;
mapTexture = new THREE.Texture(mapCanvas);
var chart = echarts.init(mapCanvas);
option = {
backgroundColor: '#404a59',
series: [
{
type: 'map',
mapType: 'world',
label: {
emphasis: {
color: '#fff',
fontSize: 14
}
},
itemStyle: {
normal: {
areaColor: '#0B1036',
borderColor: {
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [{
offset: 0, color: 'red' // 0% 处的颜色
}, {
offset: 1, color: 'blue' // 100% 处的颜色
}],
globalCoord: false // 缺省为 false
}
},
emphasis: {
areaColor: 'red'
}
},
boundingCoords: [
// 定位左上角经纬度
[-180, 90],
// 定位右下角经纬度
[180, -90]
]
},
{
type: 'map',
mapType: 'china',
label: {
emphasis: {
color: '#fff',
fontSize: 14
}
},
itemStyle: {
normal: {
areaColor: '#323c48',
borderColor: '#5FE6C9'
},
emphasis: {
areaColor: 'red'
}
},
boundingCoords: [
// 定位左上角经纬度
[-180, 90],
// 定位右下角经纬度
[180, -90]
]
}
]
};
chart.setOption(option);
mapTexture.needsUpdate = true;
// 选中或移出时才更新贴图
// 内存向显存上传数据很慢,应该尽量减少贴图更新
chart.on('mouseover', function () {
mapTexture.needsUpdate = true;
});
chart.on('mouseout', function () {
mapTexture.needsUpdate = true;
});
}
// 初始化three.js场景
function initScene() {
var container = document.getElementById('container');
// 场景
scene = new THREE.Scene();
// 相机
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = -600;
// 渲染器
renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0x242430);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
// 控制器
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.1;
controls.enablePan = false;
controls.rotateSpeed = 0.4;
// 平行光
directionalLight = new THREE.DirectionalLight(0xffffff, 0.9);
scene.add(directionalLight);
// 环境光
var light = new THREE.AmbientLight(0x202020);
scene.add(light);
// 地球
var earthGeometry = new THREE.SphereBufferGeometry(200, 36, 36);
var earthMaterial = new THREE.MeshLambertMaterial({ map: mapTexture, color: 0xffffff });
earth = new THREE.Mesh(earthGeometry, earthMaterial);
scene.add(earth);
}
function init() {
initMap();
initScene();
window.addEventListener('resize', onWindowResize, false);
container.addEventListener('mousemove', onMouseMove, false);
}
function onMouseMove(event) {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = - (event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera); // 通过鼠标坐标和相机位置构造射线
var intersected = raycaster.intersectObject(earth); // 获取射线和地球的相交点
if (intersected && intersected.length > 0) {
// 根据射线相交点的uv反算出在canvas上的坐标
var x = intersected[0].uv.x * mapSize.width;
var y = mapSize.height - intersected[0].uv.y * mapSize.height;
// 在mapCanvas上模拟鼠标事件,这里或许有更好的方法
var virtualEvent = document.createEvent('MouseEvents');
virtualEvent.initMouseEvent('mousemove', false, true, document.defaultView, 1, x, y, x, y, false, false, false, false, 0, null);
mapCanvas.dispatchEvent(virtualEvent);
}
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate); // 循环渲染
controls.update();
render();
}
function render() {
// 平行光始终从相机位置照向地球
directionalLight.position.copy(camera.position)
renderer.render(scene, camera);
}
</script>
</body>
</html>