forked from maptalks/maptalks.three
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-circle.html
305 lines (263 loc) · 10.6 KB
/
custom-circle.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!DOCTYPE html>
<html>
<head>
<title>Customize your own components </title>
<script type="text/javascript" src="https://unpkg.com/[email protected]/build/dat.gui.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://unpkg.com/maptalks/dist/maptalks.css">
<script type="text/javascript" src="https://unpkg.com/maptalks/dist/maptalks.js"></script>
<script type="text/javascript" src="https://unpkg.com/@maptalks/gl/dist/maptalksgl.js"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/build/three.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/maptalks.three@latest/dist/maptalks.three.js"></script>
<style>
html,
body {
margin: 0px;
height: 100%;
width: 100%;
}
#map {
width: 100%;
height: 100%;
background-color: #000;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = new maptalks.Map("map", {
center: [13.416935229170008, 52.529564137540376],
zoom: 15,
pitch: 70,
bearing: 0,
centerCross: true,
doubleClickZoom: false,
baseLayer: new maptalks.TileLayer('tile', {
urlTemplate: 'https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png',
subdomains: ['a', 'b', 'c', 'd'],
attribution: '© <a href="http://osm.org">OpenStreetMap</a> contributors, © <a href="https://carto.com/">CARTO</a>'
})
});
var threeLayer = new maptalks.ThreeLayer('t', {
forceRenderOnMoving: true,
forceRenderOnRotating: true,
// animation: true
});
threeLayer.prepareToDraw = function (gl, scene, camera) {
var light = new THREE.DirectionalLight(0xffffff);
light.position.set(0, -10, 10).normalize();
scene.add(light);
addCircles();
threeLayer.config('animation', true);
};
const sceneConfig = {
postProcess: {
enable: true,
antialias: { enable: true }
}
};
const groupLayer = new maptalks.GroupGLLayer('group', [threeLayer], { sceneConfig });
groupLayer.addTo(map);
function getMaterial(fontSize, text, fillColor) {
var SIZE = 256;
var canvas = document.createElement('canvas');
canvas.width = canvas.height = SIZE;
var ctx = canvas.getContext('2d');
var gradient = ctx.createLinearGradient(0, 0, SIZE, 0);
// gradient.addColorStop("0", "#ffffff");
gradient.addColorStop("0.0", "#1a9bfc");
gradient.addColorStop("1.0", "#7049f0");
// gradient.addColorStop("0.66", "white");
// gradient.addColorStop("1.0", "red");
ctx.strokeStyle = gradient;
ctx.lineWidth = 40;
ctx.arc(SIZE / 2, SIZE / 2, SIZE / 2, 0, Math.PI * 2);
ctx.stroke();
ctx.fillStyle = fillColor;
ctx.font = `${fontSize}px Aria`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(text, SIZE / 2, SIZE / 2);
ctx.rect(0, 0, SIZE, SIZE);
var texture = new THREE.Texture(canvas);
texture.needsUpdate = true; //使用贴图时进行更新
var material = new THREE.MeshPhongMaterial({
map: texture,
// side: THREE.DoubleSide,
transparent: true
});
return material;
}
var circles;
function addCircles() {
var lnglats = [
[13.429362937522342, 52.518205849377495]
, [13.41688993786238, 52.52216099633924]
, [13.417991247928398, 52.53296954185342]
, [13.438154245439819, 52.533321196953096]
, [13.450418871799684, 52.52653968753597]
, [13.390340036780685, 52.51953598324846]
, [13.399921081391199, 52.50920191922407]
, [13.366122901455583, 52.50949703597493]
, [13.365784792637783, 52.51964629275582]
, [13.371429857108524, 52.528732386936014]
, [13.383686384074508, 52.53781463596616]
, [13.40395563186371, 52.540223413847315]
, [13.361485408920998, 52.53916869831616]
, [13.35373758485457, 52.52883597474849]
, [13.355233792792774, 52.519259850666316]
, [13.369548077301943, 52.506940362998336]
, [13.338732610093984, 52.50860998116909]
, [13.341879792058194, 52.52318729489704]
, [13.348448231846305, 52.537668773653735]
];
var text = Math.round(Math.random() * 10000);
var material = getMaterial(70, text, '#fff');
circles = lnglats.map(function (lnglat) {
var circle = new Circle(lnglat, {
radius: 200
}, material, threeLayer);
//tooltip test
circle.setToolTip('id:' + circle.getId(), {
showTimeout: 0,
eventsPropagation: true,
dx: 10
});
//infowindow test
circle.setInfoWindow({
content: 'id:' + circle.getId(),
title: 'message',
animationDuration: 0,
autoOpenOn: false
});
//event test
['click', 'mousemove', 'mouseout', 'mouseover', 'mousedown', 'mouseup', 'dblclick', 'contextmenu'].forEach(function (eventType) {
circle.on(eventType, function (e) {
console.log(e.type, e);
});
});
return circle;
});
threeLayer.addMesh(circles);
initGui();
}
function initGui() {
var params = {
add: true,
color: '#fff',
show: true,
opacity: 1,
altitude: 0,
animateShow: animateShow
};
var gui = new dat.GUI();
gui.add(params, 'add').onChange(function () {
if (params.add) {
threeLayer.addMesh(circles);
} else {
threeLayer.removeMesh(circles);
}
});
gui.addColor(params, 'color').name('circle color').onChange(function () {
var text = Math.round(Math.random() * 10000);
var material = getMaterial(70, text, params.color);
circles.forEach(function (mesh) {
mesh.setSymbol(material);
});
});
gui.add(params, 'opacity', 0, 1).onChange(function () {
circles.forEach(function (mesh) {
var material = mesh.getSymbol();
material.opacity = params.opacity;
mesh.setSymbol(material);
});
});
gui.add(params, 'show').onChange(function () {
circles.forEach(function (mesh) {
if (params.show) {
mesh.show();
} else {
mesh.hide();
}
});
});
gui.add(params, 'altitude', 0, 300).onChange(function () {
circles.forEach(function (mesh) {
mesh.setAltitude(params.altitude);
});
});
gui.add(params, 'animateShow');
}
function animateShow() {
circles.forEach(circle => {
circle.animateShow({
duration: 1000,
easing: 'upAndDown'
})
});
}
//default values
var OPTIONS = {
radius: 100,
altitude: 0
};
/**
* custom Circle component
*
* you can customize your own components
* */
class Circle extends maptalks.BaseObject {
constructor(coordinate, options, material, layer) {
options = maptalks.Util.extend({}, OPTIONS, options, { layer, coordinate });
super();
//Initialize internal configuration
// https://github.com/maptalks/maptalks.three/blob/1e45f5238f500225ada1deb09b8bab18c1b52cf2/src/BaseObject.js#L135
this._initOptions(options);
const { altitude, radius } = options;
//generate geometry
const r = layer.distanceToVector3(radius, radius).x
const geometry = new THREE.CircleBufferGeometry(r, 50);
//Initialize internal object3d
// https://github.com/maptalks/maptalks.three/blob/1e45f5238f500225ada1deb09b8bab18c1b52cf2/src/BaseObject.js#L140
this._createMesh(geometry, material);
//set object3d position
const z = layer.altitudeToVector3(altitude, altitude).x;
const position = layer.coordinateToVector3(coordinate, z);
this.getObject3d().position.copy(position);
// this.getObject3d().rotation.x = -Math.PI;
}
/**
* animateShow test
*
* */
animateShow(options = {}, cb) {
if (this._showPlayer) {
this._showPlayer.cancel();
}
if (maptalks.Util.isFunction(options)) {
options = {};
cb = options;
}
const duration = options['duration'] || 1000,
easing = options['easing'] || 'out';
const player = this._showPlayer = maptalks.animation.Animation.animate({
'scale': 1
}, {
'duration': duration,
'easing': easing
}, frame => {
const scale = frame.styles.scale;
if (scale > 0) {
this.getObject3d().scale.set(scale, scale, scale);
}
if (cb) {
cb(frame, scale);
}
});
player.play();
return player;
}
}
</script>
</body>
</html>