前言
- 前言
- 一、引入庫
- 二、建立3d氣泡專題圖
-
- 定義一個專題圖對象
- 建構資料
- 添加圖層
- 總結
前言
在研究webGL和Three.js之前,曾經看過mapbox-gl源碼的一個分支,在mapbox主分支不支援 symbol 類型 layer 的3D效果(也就是Z軸傳值)時,根據源碼内容建立一個demo研究此類型的3d效果,現在我把成果分享出來!
一、引入庫
需要引入 jquery、mapbox-gl分支代碼和自己定義的測試資料json.js
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="mapbox-gl-dev.js"></script>
二、建立3d氣泡專題圖
定義一個專題圖對象
// 預設氣泡大小
var size = 150;
// ES5:canvas氣泡對象構造函數 ES6可用class構造
var symbolObject = function (){
return {
width: size,
height: size,
color: 'red',
data: new Uint8Array(size * size * 4),
// 當map加載後直接自動調用建立canvas氣泡
onAdd: function () {
var canvas = document.createElement('canvas');
canvas.width = this.width;
canvas.height = this.height;
//畫筆
this.context = canvas.getContext('2d');
},
// 修改顔色屬性傳回對象
setColor: function(colors){
this.color = colors;
return this;
},
//重複調用繪制
render: function () {
var duration = 1000;
var t = (performance.now() % duration) / duration;
//氣泡整體半徑 (必須小于size大小,否則超出邊框部分不顯示)
var radius = (size / 2) * 0.3;
//外圈光暈大小
var outerRadius = (size / 2) * 0.55 * t + radius;
var context = this.context;
// 繪制外圈光暈
context.clearRect(0, 0, this.width, this.height);
context.beginPath();
context.arc(
this.width / 2,
this.height / 2,
outerRadius,
0,
Math.PI * 2
);
//外圈光暈顔色 + 透明度
context.fillStyle = 'rgba(130, 130, 130,' + (1 - t) + ')';
context.fill();
// 繪制内圈實心圓
context.beginPath();
context.arc(
this.width / 2,
this.height / 2,
radius,
0,
Math.PI * 2
);
//添加文字
context.font = "20px 黑體";
context.textAlign = "bottom";
context.strokeText("200萬", 100, 100);
//實心圓顔色
context.fillStyle = this.color;
//實心圓邊框顔色
context.strokeStyle = this.color;
//實心圓邊框寬度
context.lineWidth = 2 + 4 * (1 - t);
context.fill();
//context.stroke();
// 更新canvas高度、寬度資料
this.data = context.getImageData(
0,
0,
this.width,
this.height
).data;
// 重複繪制每一幀,動畫效果
map.triggerRepaint();
// 繪制完成後傳回true
return true;
},
}
};
建構資料
var json1 = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [104.042723,30.667576]
},
"properties":{
"id": 1,
"name": "p1",
'color':'yellow'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [104.061695,30.590509]
},
"properties":{
"id": 1,
"name": "p1",
'color':'yellow'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [104.165755,30.65515]
},
"properties":{
"id": 1,
"name": "p1",
'color':'yellow'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [104.171504,30.707824]
},
"properties":{
"id": 1,
"name": "p1",
'color':'yellow'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [104.069744,30.760966]
},
"properties":{
"id": 1,
"name": "p1",
'color':'yellow'
}
}
]
};
//....... json2、json3資料
添加圖層
map.on('load', function () {
map.addImage('symbolObject1', new symbolObject().setColor("#ccc"), { pixelRatio: 2 });
map.addImage('symbolObject2', new symbolObject().setColor('rgba(255, 100, 100, 1)'), { pixelRatio: 1.5 });
map.addImage('symbolObject3', new symbolObject().setColor('#69b779'), { pixelRatio: 2.5 });
map.addSource('points1', {
'type': 'geojson',
'data': json1
});
map.addSource('points2', {
'type': 'geojson',
'data': json2
});
map.addSource('points3', {
'type': 'geojson',
'data': json3
});
map.addLayer({
'id': 'points1',
'type': 'symbol',
'source': 'points1',
'layout': {
'icon-image': 'symbolObject1',
"symbol-z-offset": 5000.0 //mapbox-gl主分支代碼沒有這個屬性,這是源碼分支裡一個屬性
}
});
map.addLayer({
'id': 'points2',
'type': 'symbol',
'source': 'points2',
'layout': {
'icon-image': 'symbolObject2',
"symbol-z-offset": 9000.0
}
});
map.addLayer({
'id': 'points3',
'type': 'symbol',
'source': 'points3',
'layout': {
'icon-image': 'symbolObject3',
"symbol-z-offset": 3000.0
}
});
});
總結
提示:
效果如下
mapbox-gl分支代碼連結
demo下載下傳