How to run Mike Bostock's D3 examples?

前端 未结 2 1579
独厮守ぢ
独厮守ぢ 2021-02-10 03:01

I\'ve been trying to run Mike Bostock\'s See-Through Globe example, but the references to his json files are incorrect if you try to reproduce it locally.

The issue comes

2条回答
  •  有刺的猬
    2021-02-10 03:46

    You can access the data directly here: http://bl.ocks.org/mbostock/raw/4090846/world-110m.json

    To make it work, you'll need to create a new variable in your script and assign the json to it directly.

    Code from the fiddle you provided:

    var topo = // Paste data from provided link here. It will be one big object literal.
    
    var width = 960,
        height = 960,
        speed = -1e-2,
        start = Date.now();
    
    var sphere = {type: "Sphere"};
    
    var projection = d3.geo.orthographic()
        .scale(width / 2.1)
        .translate([width / 2, height / 2])
        .precision(.5);
    
    var graticule = d3.geo.graticule();
    
    var canvas = d3.select("body").append("canvas")
        .attr("width", width)
        .attr("height", height);
    
    var context = canvas.node().getContext("2d");
    
    var path = d3.geo.path()
        .projection(projection)
        .context(context);
    
    // d3.json("https://render.github.com/mbostock/topojson/master/examples/world-110m.json", function(error, topo) {
      var land = topojson.feature(topo, topo.objects.land), // topo var is now provided by pasted-in data instead of fetched json.
          grid = graticule();
    
      d3.timer(function() {
        context.clearRect(0, 0, width, height);
    
        projection.rotate([speed * (Date.now() - start), -15]).clipAngle(90);
    
        context.beginPath();
        path(sphere);
        context.lineWidth = 3;
        context.strokeStyle = "#000";
        context.stroke();
        context.fillStyle = "#fff";
        context.fill();
    
        projection.clipAngle(180);
    
        context.beginPath();
        path(land);
        context.fillStyle = "#dadac4";
        context.fill();
    
        context.beginPath();
        path(grid);
        context.lineWidth = .5;
        context.strokeStyle = "rgba(119,119,119,.5)";
        context.stroke();
    
        projection.clipAngle(90);
    
        context.beginPath();
        path(land);
        context.fillStyle = "#737368";
        context.fill();
        context.lineWidth = .5;
        context.strokeStyle = "#000";
        context.stroke();
      });
    // });
    
    d3.select(self.frameElement).style("height", height + "px");
    

    I would have modified the fiddle directly, but the json data file is large enough that jsfiddle chokes when I try to paste it in.

    Hope this helps.

提交回复
热议问题