2016-09-22 104 views

回答

2

我離開你掛在您的評論的問題在你前面的問題所以這裏是你所追求的快速刺。最初的地圖繪製來自d3文檔示例here。繪製完成後,我添加一條線,其中包含從here借用的自定義弧線。然後,我使用attrTween"stroke-dasharray"來爲生成的線條生成動畫,並在該轉換結束時使用另一個轉換來增加該圓圈。

運行代碼:

<!DOCTYPE html> 
 
<meta charset="utf-8"> 
 
<style> 
 
    .stroke { 
 
    fill: none; 
 
    stroke: #000; 
 
    stroke-width: 3px; 
 
    } 
 
    
 
    .fill { 
 
    fill: #fff; 
 
    } 
 
    
 
    .graticule { 
 
    fill: none; 
 
    stroke: #777; 
 
    stroke-width: 0.5px; 
 
    stroke-opacity: 0.5; 
 
    } 
 
    
 
    .land { 
 
    fill: #222; 
 
    } 
 
    
 
    .boundary { 
 
    fill: none; 
 
    stroke: #fff; 
 
    stroke-width: 0.5px; 
 
    } 
 
</style> 
 
<svg width="960" height="960"></svg> 
 
<script src="//d3js.org/d3.v4.js"></script> 
 
<script src="//d3js.org/topojson.v1.min.js"></script> 
 
<script> 
 
    var svg = d3.select("svg"), 
 
    width = +svg.attr("width"), 
 
    height = +svg.attr("height"); 
 

 
    var projection = d3.geoMercator() 
 
    .scale((width - 3)/(2 * Math.PI)) 
 
    .translate([width/2, height/2]); 
 

 
    var path = d3.geoPath() 
 
    .projection(projection); 
 

 
    var graticule = d3.geoGraticule(); 
 

 
    svg.append("defs").append("path") 
 
    .datum({ 
 
     type: "Sphere" 
 
    }) 
 
    .attr("id", "sphere") 
 
    .attr("d", path); 
 

 
    svg.append("use") 
 
    .attr("class", "stroke") 
 
    .attr("xlink:href", "#sphere"); 
 

 
    svg.append("use") 
 
    .attr("class", "fill") 
 
    .attr("xlink:href", "#sphere"); 
 

 
    svg.append("path") 
 
    .datum(graticule) 
 
    .attr("class", "graticule") 
 
    .attr("d", path); 
 

 
    d3.json("https://rawgit.com/d3/d3-geo/master/test/data/world-50m.json", function(error, world) { 
 
    if (error) throw error; 
 

 
    svg.insert("path", ".graticule") 
 
     .datum(topojson.feature(world, world.objects.land)) 
 
     .attr("class", "land") 
 
     .attr("d", path); 
 

 
    svg.insert("path", ".graticule") 
 
     .datum(topojson.mesh(world, world.objects.countries, function(a, b) { 
 
     return a !== b; 
 
     })) 
 
     .attr("class", "boundary") 
 
     .attr("d", path); 
 

 

 
    var coordinates = [ 
 
     projection([-74, 40]), // new york 
 
     projection([-43, -22]) // rio 22.9068° S, 43.1729° W 
 
    ]; 
 

 
    var line = svg.append("path") 
 
     .datum(coordinates) 
 
     .attr("d", function(c) { 
 
     var d = { 
 
      source: c[0], 
 
      target: c[1] 
 
     }; 
 
     var dx = d.target[0] - d.source[0], 
 
      dy = d.target[1] - d.source[1], 
 
      dr = Math.sqrt(dx * dx + dy * dy); 
 
     return "M" + d.source[0] + "," + d.source[1] + "A" + dr + "," + dr + 
 
      " 0 0,1 " + d.target[0] + "," + d.target[1]; 
 
     }) 
 
     .style("stroke", "steelblue") 
 
     .style("stroke-width", 5) 
 
     .style("fill", "none") 
 
     .transition() 
 
     .duration(5000) 
 
     .attrTween("stroke-dasharray", function() { 
 
     var len = this.getTotalLength(); 
 
     return function(t) { 
 
      return (d3.interpolateString("0," + len, len + ",0"))(t) 
 
     }; 
 
     }) 
 
     .on('end', function(d) { 
 
     var c = coordinates[1]; 
 
     svg.append('circle') 
 
      .attr('cx', c[0]) 
 
      .attr('cy', c[1]) 
 
      .attr('r', 0) 
 
      .style('fill', 'red') 
 
      .style('fill-opacity', '0.5') 
 
      .transition() 
 
      .duration(2000) 
 
      .attr('r', 50) 
 
      .on('end', function(d) { 
 
      d3.select(this) 
 
       .transition() 
 
       .duration(2000) 
 
       .attr('r', 10); 
 
      }); 
 
     }); 
 
    }); 
 
</script>

+0

您好!你會知道這個答案:https://stackoverflow.com/questions/43696831/why-dont-the-flags-appear? – Coder1000

+0

該行之間不準確。赤道上的繪圖導致向北偏移 – jolly

+0

@pksingh,您需要更具體或提供示例。我更新了上面的代碼,從紐約運行到里約熱內盧(「赤道上空」)。看起來對我很準確。 – Mark

0

<!DOCTYPE html> 
 
<meta charset="utf-8"> 
 
<style> 
 
    .stroke { 
 
    fill: none; 
 
    stroke: #000; 
 
    stroke-width: 3px; 
 
    } 
 
    
 
    .fill { 
 
    fill: #fff; 
 
    } 
 
    
 
    .graticule { 
 
    fill: none; 
 
    stroke: #777; 
 
    stroke-width: 0.5px; 
 
    stroke-opacity: 0.5; 
 
    } 
 
    
 
    .land { 
 
    fill: #222; 
 
    } 
 
    
 
    .boundary { 
 
    fill: none; 
 
    stroke: #fff; 
 
    stroke-width: 0.5px; 
 
    } 
 
</style> 
 
<svg width="960" height="960"></svg> 
 
<script src="//d3js.org/d3.v4.js"></script> 
 
<script src="//d3js.org/topojson.v1.min.js"></script> 
 
<script> 
 
    var svg = d3.select("svg"), 
 
    width = +svg.attr("width"), 
 
    height = +svg.attr("height"); 
 

 
    var projection = d3.geoMercator() 
 
    .scale((width - 3)/(2 * Math.PI)) 
 
    .translate([width/2, height/2]); 
 

 
    var path = d3.geoPath() 
 
    .projection(projection); 
 

 
    var graticule = d3.geoGraticule(); 
 

 
    svg.append("defs").append("path") 
 
    .datum({ 
 
     type: "Sphere" 
 
    }) 
 
    .attr("id", "sphere") 
 
    .attr("d", path); 
 

 
    svg.append("use") 
 
    .attr("class", "stroke") 
 
    .attr("xlink:href", "#sphere"); 
 

 
    svg.append("use") 
 
    .attr("class", "fill") 
 
    .attr("xlink:href", "#sphere"); 
 

 
    svg.append("path") 
 
    .datum(graticule) 
 
    .attr("class", "graticule") 
 
    .attr("d", path); 
 

 
    d3.json("https://rawgit.com/mbostock/topojson/master/examples/world-50m.json", function(error, world) { 
 
    if (error) throw error; 
 

 
    svg.insert("path", ".graticule") 
 
     .datum(topojson.feature(world, world.objects.land)) 
 
     .attr("class", "land") 
 
     .attr("d", path); 
 

 
    svg.insert("path", ".graticule") 
 
     .datum(topojson.mesh(world, world.objects.countries, function(a, b) { 
 
     return a !== b; 
 
     })) 
 
     .attr("class", "boundary") 
 
     .attr("d", path); 
 

 

 
    var coordinates = [ 
 
     projection([-74, 40]), // new york 
 
     projection([37, 55]) // moscow 
 
    ]; 
 

 
    var line = svg.append("path") 
 
     .datum(coordinates) 
 
     .attr("d", function(c) { 
 
     var d = { 
 
      source: c[0], 
 
      target: c[1] 
 
     }; 
 
     var dx = d.target[0] - d.source[0], 
 
      dy = d.target[1] - d.source[1], 
 
      dr = Math.sqrt(dx * dx + dy * dy); 
 
     return "M" + d.source[0] + "," + d.source[1] + "A" + dr + "," + dr + 
 
      " 0 0,1 " + d.target[0] + "," + d.target[1]; 
 
     }) 
 
     .style("stroke", "steelblue") 
 
     .style("stroke-width", 5) 
 
     .style("fill", "none") 
 
     .transition() 
 
     .duration(5000) 
 
     .attrTween("stroke-dasharray", function() { 
 
     var len = this.getTotalLength(); 
 
     return function(t) { 
 
      return (d3.interpolateString("0," + len, len + ",0"))(t) 
 
     }; 
 
     }) 
 
     .on('end', function(d) { 
 
     var c = coordinates[1]; 
 
     svg.append('circle') 
 
      .attr('cx', c[0]) 
 
      .attr('cy', c[1]) 
 
      .attr('r', 0) 
 
      .style('fill', 'red') 
 
      .style('fill-opacity', '0.5') 
 
      .transition() 
 
      .duration(2000) 
 
      .attr('r', 50) 
 
      .on('end', function(d) { 
 
      d3.select(this) 
 
       .transition() 
 
       .duration(2000) 
 
       .attr('r', 10); 
 
      }); 
 
     }); 
 
    }); 
 
</script>