|
1 require("../../test/env"); |
|
2 require("../../d3"); |
|
3 require("../../d3.geo"); |
|
4 |
|
5 var fs = require("fs"), |
|
6 util = require("util"), |
|
7 Canvas = require("canvas"); |
|
8 |
|
9 var w = 1920, |
|
10 h = 1080; |
|
11 |
|
12 var projection = d3.geo.albersUsa() |
|
13 .scale(2000) |
|
14 .translate([w / 2, h / 2]); |
|
15 |
|
16 var path = d3.geo.path() |
|
17 .projection(projection); |
|
18 |
|
19 var canvas = new Canvas(w, h), |
|
20 context = canvas.getContext("2d"); |
|
21 |
|
22 context.antialias = "none"; |
|
23 context.lineWidth = 8; |
|
24 context.lineJoin = "round"; |
|
25 |
|
26 d3.json(__dirname + "/../data/us-counties.json", function(collection) { |
|
27 renderAll("stroke"); |
|
28 renderAll("fill"); |
|
29 fs.writeFile("us-counties.png", canvas.toBuffer()); |
|
30 |
|
31 function renderAll(action) { |
|
32 collection.features.forEach(function(feature) { |
|
33 var re = /[MLZ]/g, |
|
34 d = path(feature), |
|
35 i = 0, |
|
36 m; |
|
37 |
|
38 context[action + "Style"] = "#" + pad((+feature.id).toString(16)); |
|
39 context.beginPath(); |
|
40 re.lastIndex = 1; |
|
41 while (m = re.exec(d)) render(m.index); |
|
42 render(); |
|
43 context[action](); |
|
44 |
|
45 function render(j) { |
|
46 switch (d.charAt(i)) { |
|
47 case "M": { |
|
48 var p = d.substring(i + 1, j).split(",").map(Number); |
|
49 context.moveTo(p[0], p[1]); |
|
50 break; |
|
51 } |
|
52 case "L": { |
|
53 var p = d.substring(i + 1, j).split(",").map(Number); |
|
54 context.lineTo(p[0], p[1]); |
|
55 break; |
|
56 } |
|
57 case "Z": { |
|
58 context.closePath(); |
|
59 break; |
|
60 } |
|
61 } |
|
62 i = j; |
|
63 } |
|
64 }); |
|
65 } |
|
66 }); |
|
67 |
|
68 function pad(s) { |
|
69 return s.length < 6 ? new Array(7 - s.length).join("0") + s : s; |
|
70 } |