Skip to content

Instantly share code, notes, and snippets.

@hugolpz
Forked from mjhoy/README.md
Last active August 29, 2015 14:01
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 hugolpz/c59f79a9f52e3897c129 to your computer and use it in GitHub Desktop.
Save hugolpz/c59f79a9f52e3897c129 to your computer and use it in GitHub Desktop.
Raster clipping via D3js
<!doctype html>
<meta charset='utf-8'>
<title>Lake Superior</title>
<style>
.lake {
stroke: none;
fill: none;
}
</style>
<svg></svg>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<script>
var width = 960,
height = 500,
svg = d3.select("svg").attr("width", width).attr("height", height);
var projection = d3.geo.albers()
.rotate([88.1, 0])
.center([0, 47.7])
.parallels([45.5, 49.8])
.scale(9600)
var path = d3.geo.path()
.projection(projection);
d3.json("lakes-topo.json", function(error, data) {
var lakes = topojson.object(data, data.objects.lakes);
svg.append("image")
.attr("xlink:href", "hill-relief.jpg")
.attr("width", width)
.attr("height", height)
.attr("class", "bg");
svg.selectAll(".lake")
.data(lakes.geometries)
.enter().append("path")
.attr("d", path)
.attr("class", "lake")
.attr("id", "superior"); // There is only one lake in this set.
svg.append("clipPath")
.attr("id", "clip")
.append("use")
.attr("xlink:href", "#superior");
svg.append("image")
.attr("clip-path", "url(#clip)")
.attr("xlink:href", "superior.jpg")
.attr("width", width)
.attr("height", height)
.attr("class", "bg");
});
</script>
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