前言
- 前言
- 一、引入库
- 二、创建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下载