Skip to content

Instantly share code, notes, and snippets.

@munaf-zz
Last active December 11, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save munaf-zz/4611003 to your computer and use it in GitHub Desktop.
Save munaf-zz/4611003 to your computer and use it in GitHub Desktop.
Flat World Tour
{"description":"Flat World Tour","endpoint":"","display":"svg","public":true,"require":[{"name":"D3 Geo Projection","url":"http://d3js.org/d3.geo.projection.v0.min.js"},{"name":"Topojson","url":"http://d3js.org/topojson.v0.min.js"},{"name":"World Topography","url":"https://gist.github.com/raw/4627768/353228ed4eac9f6adb422d069897c50196803638/world-topo.json"},{"name":"World Geo","url":"https://gist.github.com/raw/4630218/1b176e877db5a26d37b93b47154cd4bb5c368478/world-geo.js"}],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"usa.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"world.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01}
var width = 800, height = 500,
area = width * height,
centered;
var countries = world_geo.features;
var projection = d3.geo.kavrayskiy7()
.scale(width*0.23962)
.translate([0, 0]);
var path = d3.geo.path().projection(projection);
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", tween);
var g = svg.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
.append("g").attr("id", "states");
g.selectAll("path")
.data(countries)
.enter().append("path")
.attr("d", path)
.on("click", tween);
svg.append("text")
.attr("id", "country-name")
.attr("x", 100).attr("y", 100)
.text("");
var item = 0;
function tween() {
var x = 0, y = 0, k = 1,
country = countries[item];
var countryarea = path.area(country),
bounds = path.bounds(country),
countryheight = bounds[1][1] - bounds[0][1],
factor = countryheight / height;
if (country && centered !== country) {
var centroid = path.centroid(country);
x = -centroid[0];
y = -centroid[1];
k = 4;
centered = country;
} else { centered = null; }
g.selectAll("path")
.classed("active", centered && function(d) { return d === centered; });
g.transition().duration(2250)
.attrTween("transform", function(d, i, a) { return d3.interpolateTransform(a + "scale(1)", "scale(" + k + ")translate(" + x + "," + y + ")"); })
.style("stroke-width", 1.5 / k + "px");
d3.select("#country-name").transition().duration(500).text(centered.properties.name);
item++;
if (item === countries.length) item = 0;
/*
var b = path.bounds(country),
s = .95 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height),
t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2];
projection
.scale(s)
.translate(t);
svg.append("path")
.datum(states)
.attr("class", "feature")
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "mesh")
.attr("d", path);
svg.append("path")
.datum(state)
.attr("class", "outline")
.attr("d", path);*/
}
setInterval(tween, 3000);
svg {
margin-left: 100px;
margin-top: 100px;
border: 1px dashed gray;
background-color: #FFFFFF;
}
.background {
fill: none;
pointer-events: all;
}
#states {
fill: #E7E8EB;
stroke: #fff;
stroke-width: 1.5px;
}
#states .active {
fill: #1FB366;
}
.country {
fill: #ccc;
}
.country-boundary {
fill: none;
stroke: #fff;
stroke-width: .5px;
stroke-linejoin: round;
}
.graticule {
fill: none;
stroke: #000;
stroke-opacity: .3;
stroke-width: .5px;
}
.graticule.outline {
stroke: #333;
stroke-opacity: 1;
stroke-width: 1.5px;
}
#country-name {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-family: Georgia, serif;
/*font-weight: bold;*/
font-size: 40px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment