Reputation: 14689
I am trying to understand how to create a minimalistic example of a country map using d3.js and HTML5 canvas. I managed to implement the following code,
<html>
<head>
<title></title>
</head>
<body>
</body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script type="text/javascript">
// drawing a map in a canvas
var width = 960, height = 500;
var canvas = d3.select("body").append("canvas")
.attr("width", width)
.attr("height",height);
var context = canvas.node().getContext("2d");
var projection = d3.geo.mercator();
var path = d3.geo.path()
.projection(projection)
.context(context);
d3.json("tunisia.json", function(error, topology) {
canvas
.datum(topojson.object(topology, topology.objects.governorates))
.transition();
});
</script>
</html>
But, no results have shown in the browser and no error received in the console, could you please check. Also, is there any minimalistic example of US map with canvas in d3.js
Upvotes: 0
Views: 1464
Reputation: 993
You aren't drawing anything inside the canvas, that's why it fails.
The following code will work and will give the result in the image, and you can play with it at this jsbin:
<html>
<head>
<title></title>
</head>
<body>
</body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<script type="text/javascript">
// drawing a map in a canvas
var width = 960, height = 500;
var canvas = d3.select("body").append("canvas")
.attr("width", width)
.attr("height",height);
var context = canvas.node().getContext("2d");
var projection = d3.geo.mercator();
var path = d3.geo.path()
.projection(projection)
.context(context);
d3.json("https://cdn.rawgit.com/mbostock/4090846/raw/8a7f176072d508218e120773943b595c998991be/world-50m.json", function(error, data) {
var land = topojson.object(data, data.objects.land);
context.strokeStyle = '#000000';
context.fillStyle = '#aaaaaa';
context.beginPath();
path(land);
context.fill();
});
</script>
</html>
Let's see how does it work:
Upvotes: 1