Skip to content

Instantly share code, notes, and snippets.

Last active November 20, 2016 23:20
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Dynamic Simplification IV
license: gpl-3.0

This map dynamically simplifies the geometry in response to zooming, so that the smallest displayed detail is approximately one square pixel. Use the mousewheel, or pinch on touch devices, to zoom.

<!DOCTYPE html>
<canvas width="960" height="500"></canvas>
<script src=""></script>
<script src=""></script>
<script src=""></script>
var canvas ="canvas"),
context = canvas.node().getContext("2d"),
width ="width"),
height ="height");
var land,
var minZ, // minimum area threshold for simplification
transform = d3.geoIdentity().clipExtent([[0, 0], [width, height]]),
simplify = d3.geoTransform({point: function(x, y, z) { if (z >= minZ), y); }});
var zoom = d3.zoom()
.scaleExtent([1 / (1 << 5), 1 << 2])
.on("zoom", zoomed);
context.fillStyle = "#bbb";
context.strokeStyle = "#fff";
context.lineJoin = "round";
context.lineCap = "round";
var path = d3.geoPath()
.projection({stream: function(s) { return; }})
d3.json("us-states.json", function(error, us) {
if (error) throw error;
land = topojson.feature(us, us.objects.states);
borders = topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; });
// An arbitrary scale and center point to set the initial view.
// This projection is baked into the TopoJSON file,
// but is used here to compute the desired zoom translate.
var scale = 0.05, point = d3.geoMercator()
.translate([0, 0])
([-75.959, 38.250]);
.call(zoom.transform, d3.zoomIdentity
.translate(width / 2, height / 2)
.translate(-point[0], -point[1]));
function zoomed(d) {
var t = d3.event.transform;
minZ = 1 / (t.k * t.k);
transform.translate([t.x, t.y]).scale(t.k);
context.clearRect(0, 0, width, height);
Display the source blob
Display the rendered blob
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment