Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active February 9, 2016 02:02

Revisions

  1. mbostock revised this gist Feb 9, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions .block
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    license: gpl-3.0
  2. mbostock revised this gist Oct 31, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    <!DOCTYPE html>
    <meta charset="utf-8">
    <body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script>
    <script src="//d3js.org/d3.v3.min.js"></script>
    <script src="//d3js.org/topojson.v1.min.js"></script>
    <script>

    var width = 960,
  3. mbostock revised this gist Jun 11, 2015. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    <!DOCTYPE html>
    <meta charset="utf-8">
    <body>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="http://d3js.org/topojson.v1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script>
    <script>

    var width = 960,
    @@ -45,6 +45,8 @@
    .context(context);

    d3.json("us.json", function(error, json) {
    if (error) throw error;

    canvas
    .datum(topojson.feature(topojson.presimplify(json), json.objects.land))
    .call(zoomTo(chesapeake, 0.05).event)
  4. mbostock revised this gist Aug 22, 2013. 1 changed file with 1 addition and 7 deletions.
    8 changes: 1 addition & 7 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -23,12 +23,6 @@
    }
    });

    var simplifyThenClip = {
    stream: function(stream) {
    return simplify.stream(clip.stream(stream));
    }
    };

    var zoom = d3.behavior.zoom()
    .size([width, height])
    .on("zoom", zoomed);
    @@ -47,7 +41,7 @@
    var context = canvas.node().getContext("2d");

    var path = d3.geo.path()
    .projection(simplifyThenClip)
    .projection({stream: function(s) { return simplify.stream(clip.stream(s)); }})
    .context(context);

    d3.json("us.json", function(error, json) {
  5. mbostock revised this gist Aug 22, 2013. 2 changed files with 1 addition and 6 deletions.
    5 changes: 0 additions & 5 deletions d3.min.js
    0 additions, 5 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <!DOCTYPE html>
    <meta charset="utf-8">
    <body>
    <script src="d3.min.js"></script>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="http://d3js.org/topojson.v1.min.js"></script>
    <script>

  6. mbostock revised this gist Aug 22, 2013. No changes.
  7. mbostock revised this gist Aug 20, 2013. 1 changed file with 0 additions and 0 deletions.
    Binary file added thumbnail.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  8. mbostock created this gist Aug 20, 2013.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    A variation of the [dynamic simplification demonstration](/mbostock/6252418) using a filled polygon rather than a stroked mesh. As a result, the effect of simplification is almost imperceptible during zooming. Aside from the fluid framerate achieved by faster rendering, of course!
    5 changes: 5 additions & 0 deletions d3.min.js
    5 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    90 changes: 90 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    <!DOCTYPE html>
    <meta charset="utf-8">
    <body>
    <script src="d3.min.js"></script>
    <script src="http://d3js.org/topojson.v1.min.js"></script>
    <script>

    var width = 960,
    height = 500;

    var chesapeake = [-75.959, 38.250];

    var scale,
    translate,
    area; // minimum area threshold for simplification

    var clip = d3.geo.clipExtent()
    .extent([[0, 0], [width, height]]);

    var simplify = d3.geo.transform({
    point: function(x, y, z) {
    if (z >= area) this.stream.point(x * scale + translate[0], y * scale + translate[1]);
    }
    });

    var simplifyThenClip = {
    stream: function(stream) {
    return simplify.stream(clip.stream(stream));
    }
    };

    var zoom = d3.behavior.zoom()
    .size([width, height])
    .on("zoom", zoomed);

    // This projection is baked into the TopoJSON file,
    // but is used here to compute the desired zoom translate.
    var projection = d3.geo.mercator()
    .translate([0, 0])
    .scale(4000)
    .precision(0);

    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(simplifyThenClip)
    .context(context);

    d3.json("us.json", function(error, json) {
    canvas
    .datum(topojson.feature(topojson.presimplify(json), json.objects.land))
    .call(zoomTo(chesapeake, 0.05).event)
    .transition()
    .duration(5000)
    .each(jump);
    });

    function zoomTo(location, scale) {
    var point = projection(location);
    return zoom
    .translate([width / 2 - point[0] * scale, height / 2 - point[1] * scale])
    .scale(scale);
    }

    function zoomed(d) {
    translate = zoom.translate();
    scale = zoom.scale();
    area = 1 / scale / scale;
    context.clearRect(0, 0, width, height);
    context.beginPath();
    path(d);
    context.fill();
    }

    function jump() {
    var t = d3.select(this);
    (function repeat() {
    t = t.transition()
    .call(zoomTo(chesapeake, 1.25).event)
    .transition()
    .call(zoomTo(chesapeake, 0.05).event)
    .each("end", repeat);
    })();
    }

    </script>
    1 change: 1 addition & 0 deletions us.json
    1 addition, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.