Skip to content

Instantly share code, notes, and snippets.

@Andrew-Reid
Last active January 8, 2018 06:16
Show Gist options
  • Save Andrew-Reid/4f5d283cab8a4077e6831bceb65969aa to your computer and use it in GitHub Desktop.
Save Andrew-Reid/4f5d283cab8a4077e6831bceb65969aa to your computer and use it in GitHub Desktop.
World Map and Pattern Transitions
<!DOCTYPE html>
<meta charset="utf-8">
<svg width="960" height="960"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/topojson-client@3"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var pattern = svg.append("pattern")
pattern.attr("width",20)
.attr("height",20)
.attr("patternUnits","userSpaceOnUse")
.attr("patternTransform","rotate(45)")
.attr("id","testPattern");
pattern.append("rect")
.attr("x",0)
.attr("y",0)
.attr("width",0)
.attr("height",0)
.attr("fill","orange")
var projection = d3.geoMercator()
.scale((width - 3) / (2 * Math.PI))
.translate([width / 2, height / 2]);
var path = d3.geoPath()
.projection(projection);
d3.json("https://unpkg.com/world-atlas@1/world/50m.json", function(error, world) {
if (error) throw error;
svg.selectAll("path")
.data(topojson.feature(world, world.objects.countries).features)
.enter()
.append("path")
.attr("class", "boundary")
.attr("d", path)
.attr("fill","url(#testPattern)");
function toggle() {
pattern.select("rect")
.attr("height",0)
.attr("width",0)
.attr("fill","orange")
.transition()
.attr("height",20)
.attr("width",20)
.attr("fill","white")
.ease(d3.easeQuadOut)
.duration(2000)
.on("end",toggle);
}
toggle();
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment