This demonstrates using topojson.merge, a new feature in TopoJSON 2.2, to merge multiple polygons into a single polygon whilst removing interior borders. (An alternative approach is to draw the polygons twice.)
Unlike mbostock's original example, this uses a pre-projected topojson file and a null projection.
forked from almccon's block: Merging TopoJSON states
xxxxxxxxxx
<meta charset="utf-8">
<style>
.state {
fill: #ccc;
}
.state-boundary {
fill: none;
stroke: #fff;
}
.state.selected {
fill: orange;
stroke: #000;
}
</style>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script>
var width = 1024,
height = 768;
var path = d3.geoPath()
.projection(null); // topojson file is already projected
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var selected = d3.set([
'AK', 'YT', 'BC', 'WA', 'OR', 'CA', 'NY'
]);
d3.json("https://gist.githubusercontent.com/almccon/fb125b016b5c9afad99b/raw/23f4acccdf42487c99a10f8fa21e8be70ae05979/ne_50m_usa_canada.topojson", function(error, us) {
console.log(us);
svg.append("path")
.datum(topojson.feature(us, us.objects.usa_canada))
.attr("class", "state")
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(us, us.objects.usa_canada, function(a, b) { return a !== b; }))
.attr("class", "state-boundary")
.attr("d", path);
svg.append("path")
.datum(topojson.merge(us, us.objects.usa_canada.geometries.filter(function(d) { return selected.has(d.id); })))
.attr("class", "state selected")
.attr("d", path);
});
d3.select(self.frameElement).style("height", height + "px");
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-93898246-1', 'auto');
ga('send', 'pageview');
</script>
</body>
https://d3js.org/d3.v4.min.js
https://d3js.org/topojson.v2.min.js