Created
June 25, 2015 10:22
-
-
Save richardwestenra/96e20ca73b6e2ac35d82 to your computer and use it in GitHub Desktop.
d3.geo.path + canvas + pattern image fill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var projection = d3.geo.albers() | |
.scale(1000); | |
var canvas = d3.select("body").append("canvas") | |
.attr("width", width) | |
.attr("height", height); | |
var context = canvas.node().getContext("2d"); | |
var path = d3.geo.path() | |
.projection(projection) | |
.context(context); | |
d3.json("us.json", function(error, us) { | |
if (error) { throw error; } | |
var imageObj = new Image(); | |
imageObj.onload = function() { | |
var pattern = context.createPattern(imageObj, 'repeat'); | |
path(topojson.feature(us, us.objects.counties)); | |
context.fillStyle = pattern; | |
context.fill(); | |
}; | |
imageObj.src = 'http://lorempixel.com/output/abstract-q-c-640-480-6.jpg'; | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment