7

D3.js地图打点射线动效

在项目中,通常使用打点和射线来实现区域的攻防、传达、运动等需求,

最终效果

http://en.jsrun.net/ZbiKp/emb...

代码解析

首先引入d3V3和snap

导入地图数据

{"type":"FeatureCollection",
 "bbox": [ -180.0, -89.99893, 180.0, 83.59961 ],
  "features":[
{"type":"Feature","properties":{"name":"Afghanistan"},"geometry":{"type":"Polygon","coordinates":[[[61.210817,35.650072],[62.230651,35.270664],[62.984662,35.404041],[63.193538,35.857166],[63.982896,36.007957],[64.546479,36.312073],[64.746105,37.111818],[65.588948,37.305217],[65.745631,37.661164],[66.217385,37.39379],[66.518607,37.362784],[67.075782,37.356144],[67.83,37.144994],[68.135562,37.023115],[68.859446,37.344336],[69.196273,37.151144],[69.518785,37.608997],[70.116578,37.588223],[70.270574,37.735165],[70.376304,38.138396],[70.806821,38.486282],[71.348131,38.258905],[71.239404,37.953265],[71.541918,37.905774],[71.448693,37.065645],[71.844638,36.738171],[72.193041,36.948288],[72.63689,37.047558],[73.260056,37.495257],[73.948696,37.421566],[74.980002,37.41999],[75.158028,37.133031],[74.575893,37.020841],[74.067552,36.836176],[72.920025,36.720007],[71.846292,36.509942],[71.262348,36.074388],[71.498768,35.650563],[71.613076,35.153203],[71.115019,34.733126],[71.156773,34.348911],[70.881803,33.988856],[69.930543,34.02012],[70.323594,33.358533],[69.687147,33.105499],[69.262522,32.501944],[69.317764,31.901412],[68.926677,31.620189],[68.556932,31.71331],[67.792689,31.58293],[67.683394,31.303154],[66.938891,31.304911],[66.381458,30.738899],[66.346473,29.887943],[65.046862,29.472181],[64.350419,29.560031],[64.148002,29.340819],[63.550261,29.468331],[62.549857,29.318572],[60.874248,29.829239],[61.781222,30.73585],[61.699314,31.379506],[60.941945,31.548075],[60.863655,32.18292],[60.536078,32.981269],[60.9637,33.528832],[60.52843,33.676446],[60.803193,34.404102],[61.210817,35.650072]]]},"id":"AFG"},
{"type":"Feature","properties":{"name":"Angola"},"geometry":{"type":"MultiPolygon","coordinates":[[[[16.326528,-5.87747],[16.57318,-6.622645],[16.860191,-7.222298],[17.089996,-7.545689],
//....................

因为数据很长,只贴上了一部分,完整的地图代码请在案例中提取。

绘制地图

var width = 800;
var height = 500;
var svg = d3.select("#map").append('svg')
                    .attr('id',"mapSvg")
                    .attr('width',width)
                    .attr('height',height);
var projection = d3.geo.mercator() //geo坐标和浏览器坐标的换算
                        .center([5, 32])
                        .scale(140)
                        .translate([width / 2, height / 2]);
        var path = d3.geo.path()
                    .projection(projection);

                var mapG = svg.append('g')
                    .attr('id',"mapG");

                    mapG.selectAll("path")
                        .data(root.features)
                        .enter()
                        .append("path")
                        .attr("class","map-path")
                        .attr("stroke", "#212121")
                        .attr("stroke-width", 1)
                        .attr("fill", "#808080")
                        .attr("style","display:block")
                        .attr("d", path);
                        mapG.append("g").attr("id","pointG")
                        pushData();//传入源和目标的坐标并触发绘制点和线
                       

上面的代码中将地图数据转换成了浏览器坐标,这样才可以绘制出svg的路径。

传入打点数据

function pushData(){
                var data = [
                    {
                    from:[161.71533203124997,-10.387304687499991],
                    to:[117.1115,40.7071]
                     },
                    {
                        from:[117.5744140625001,4.17060546875004],
                        to:[115.2686,30.6628]
                    },
                    {
                        from:[90.31328125000007,47.676171874999994],
                        to:[112.6758,30.9979]
                    },
                    {
                        from:[129.71972656249997,42.47500000000005],
                        to:[121.01931115058903, 23.436683457875347]
                    },
                    {
                        from:[-85.73383789062493,68.630126953125],
                        to:[91.1865,30.1465]
                    },
                    {
                        from:[-63.938574218750006,-12.529687499999994],
                        to:[115.2686,30.6628]
                    },
                    {
                        from:[15.000683593750011,46.6259765625],
                        to:[99.613,24.0546]
                    },
                    {
                        from:[106.75341796875003,20.73505859375004],
                        to:[117.1115,40.7071]
                    },
                ];


                //每一个动画独立互不影响,所以采取分层的原理
               var index = 1;
               setInterval(function(){
                        var n = data.length * Math.random();
                        n = parseInt(n);
                        if(n>7){
                            n = 7
                        }
                
                 var p = d3.select('#pointG').append('svg').attr('id','paper'+index);
                        runAttack('paper'+index,data[n]);
                 index++
                },200);


}

使用定时器触发绘制打点。

绘制打点及连线

function runAttack(id,data){
               
                
                var height =500;
                var width = 800;
                              var s = Snap('#'+id);
                var projection = d3.geo.mercator()
                        .center([5, 32])
                        .scale(140)
                        .translate([width / 2, height / 2]);
                function makePro(arr){
                    var centroid = projection(arr),
                        x = centroid[0],
                        y = centroid[1];
                    return [x,y]
                }
                var circleF = s.circle(makePro(data.from)[0],makePro(data.from)[1],0);
                var circleT = s.circle(makePro(data.to)[0],makePro(data.to)[1],0);
                var lineL = s.line(makePro(data.from)[0],makePro(data.from)[1],makePro(data.from)[0],makePro(data.from)[1]);
                
                circleF.attr({
                    fill: "rgba(0,0,0,0)",
                    stroke:"r()rgba(24,255,253,0.5)-#34A1FF",
                    'stroke-width':"5px"
                });
                circleT.attr({
                    fill:"#18FFFD",
                    stroke:"r()#34A1FF-rgba(24,255,253,0.5)",
                    'stroke-width':"8px"
                });
                lineL.attr({
                    stroke:"L(" + makePro(data.to)[0] + "," + makePro(data.to)[1] + "," + makePro(data.from)[0] + "," + makePro(data.from)[1] + ")#18FFFD-rgba(0,225,132,0.1)",
                    'stroke-width':"1px",
                    fill:"rgba(0,0,0,0)"
                });

                circleF.animate({r:20,'stroke-width':"1px"},600,function(){
                    circleF.remove();
                });
                lineL.animate({x2:makePro(data.to)[0],y2:makePro(data.to)[1]},500,mina.easeinout,function(){
                    lineL.animate({x1:makePro(data.to)[0],y1:makePro(data.to)[1],'stroke-width':'0'},500,mina.easein,function(){
                      lineL.remove();
                    })
                
                    circleT.animate({r:10},1000,function(){
                      circleT.remove();
                      s.remove();
                    })
                });

            }

这里涉及了snap的一些基本操作,详细教程请在官网学习,这里不重复赘述。
snap的动画还是比较容易上手的,原理和jquery的animate差不多。只是snap是针对于svg的动画框架,在赋予属性中
非常灵活。在绘制节点前,同样需要把节点的geo坐标转换成浏览器坐标。
绘制射线其实就是头宽尾细的尾部逐渐消失的一条直线。

snap绘制射线虽然可以无缝和d3的地图结合,但是很难做到短的连续的射线,今后将会使用tween完成另一种射线形式。

--修复了defs中渐变定义未清除的问题。


dlwbill
97 声望9 粉丝

web前端 数据可视化 D3 echarts Vue