Skip to content

Instantly share code, notes, and snippets.

Forked from mbostock/.block
Last active February 9, 2016 02:02
  • Star 1 You must be signed in to star a gist
  • Fork 1 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?
Map Zooming III
license: gpl-3.0

This variation of the map zooming example uses interaction to control a smooth zooming interpolator. Mouse over the map to zoom between San Francisco, New Orleans and New York.

<!DOCTYPE html>
<meta charset="utf-8">
canvas {
-webkit-tap-highlight-color: rgba(0,0,0,0);
<script src="//"></script>
<script src="//"></script>
var width = 960,
height = 500;
// 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])
var zoom = d3.scale.linear()
.domain([0, width / 2, width])
projection([-122.417, 37.775]).concat(width / 6),
projection([-90.0500, 29.9667]).concat(width / 3),
projection([-74.0064, 40.7142]).concat(width / 4)
var scale,
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) * scale + translate[0], y * scale + translate[1]);
var canvas ="body").append("canvas")
.attr("width", width)
.attr("height", height)
.on("mousemove", mousemoved)
.on("touchmove", touchmoved);
var context = canvas.node().getContext("2d");
var path = d3.geo.path()
.projection({stream: function(s) { return; }})
d3.json("us-states.json", function(error, json) {
if (error) throw error;
function touchmoved() {
function mousemoved() {
function render(x) {
var point = zoom(x);
scale = width / point[2];
translate = [width / 2 - point[0] * scale, height / 2 - point[1] * scale];
area = 1 / scale / scale;
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