Skip to content

Instantly share code, notes, and snippets.

@kaezarrex
Last active November 26, 2019 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kaezarrex/11016980 to your computer and use it in GitHub Desktop.
Save kaezarrex/11016980 to your computer and use it in GitHub Desktop.
Park walk

Auto-centering and auto-scaling topojson data with an inkpen-style drawing animation.

<!DOCTYPE html>
<head>
<meta charset='utf-8'>
<style>
path {
fill: none;
stroke: #D7181E;
stroke-opacity: 0.8;
stroke-linecap: round;
stroke-linejoin: round;
}
</style>
</head>
<body>
<script src='http://d3js.org/d3.v3.min.js'></script>
<script src='http://d3js.org/topojson.v1.min.js'></script>
<script>
var width = 960;
var height = 500;
var margin = 20;
var pathWidths = [1, 2, 3, 4, 5];
var svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
width = width - 2*margin;
height = height - 2*margin;
d3.json('map.json', function(error, map) {
var feature = topojson.feature(map, map.objects.collection).features[0];
var center = d3.geo.centroid(feature);
var scale = 150;
var offset = [width/2, height/2];
var projection = d3.geo.mercator()
.scale(scale)
.center(center)
.translate(offset);
var path = d3.geo.path().projection(projection);
var bounds = path.bounds(feature);
var hscale = scale * width / (bounds[1][0] - bounds[0][0]);
var vscale = scale * height / (bounds[1][1] - bounds[0][1]);
var scale = (hscale < vscale) ? hscale : vscale;
projection = d3.geo.mercator()
.scale(scale)
.center(center);
path = d3.geo.path()
.projection(projection);
var group = svg.selectAll('g')
.data(topojson.feature(map, map.objects.collection).features)
group.enter().append('g')
.each(function(data) {
d3.select(this).selectAll('path')
.data(pathWidths).enter()
.append('path')
.attr('d', function() { return path(data); })
.style('stroke-width', function(d) { return d; })
.style('stroke-dasharray', '0,10000')
.transition()
.delay(function(d, i) { return i * 50 + 500; })
.duration(5000)
.ease('linear')
.style('stroke-dasharray', '2000,10000');
});
});
</script>
</body>
Display the source blob
Display the rendered blob
Raw
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