score:1
This happens because you aren't actually drawing the countries. You are drawing the mesh around the countries - and this means that your features aren't countries, but border (and coast) segments.
Normally when drawing countries, each feature is a whole country, and filling the path fills the feature, as you want. Each border between two countries is thus drawn twice. Topojson encodes topology, which breaks the countries shapes into line segments so that each segment is recorded once. The topojson mesh just produces these line segments. Filling a line essentially creates a polygon by linking the first point with the last point. In your map this is quite visible with the coasts of the continental US.
Try converting the topojson to geojson:
topojson.feature(topology, topology.objects.features)
As in the snippet below (using your code):
d3.json('https://unpkg.com/world-atlas@1/world/110m.json', (error, topology) => {
if (error) throw error;
let width = 960,
height = 500;
let projection = d3.geoMercator().scale('200').translate([width / 2, height / 1.4]);
let canvas = d3.select('body').append('canvas')
.attr('width', width)
.attr('height', height);
let context = canvas.node().getContext('2d'),
path = d3.geoPath()
.context(context)
.projection(projection);
context.fillStyle = '#d8d8d8';
context.strokeStyle = 'black';
context.beginPath();
path(topojson.feature(topology, topology.objects.countries));
context.fill();
context.stroke()
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
Source: stackoverflow.com
Related Query
- D3.js canvas context fill wrong
- D3 wrong fill color on start of a transition from line to area
- Map created from geojson file just fills the entire canvas with the color of the path fill
- Difference between svg and canvas in d3.js
- getBoundingClientRect returning wrong results
- HTML5 canvas alternatives for d3.js, graph visualization library
- Append SVG canvas to element other than body using D3
- right-click on a d3.js element: how to prevent browser context menu to appear
- D3js how to append 2 children at the same level in .enter context
- Fill rect with pattern
- SVG to Canvas with d3.js
- Fill SVG element with a repeating linear gradient color
- d3.js brush fill color histogram
- conditional fill in d3.js for area chart
- Big d3.js graph, canvas or server-side rendering?
- Random fill colors in Chart.js
- How to update the fill color on existing svg elements with d3.js?
- D3 fill shape with image using pattern
- How to fill inner area between multiple SVG paths?
- How to make a color scale in D3 JS to use in fill attribute?
- appending d3.js canvas to div
- D3 updating graph with new elements create edges with the wrong nodes
- D3.js scale returning wrong values from dataset
- Adding a horizontal line to d3 graph displays at the wrong value
- Typescript/D3 v4 - Context of this in d3.drag().on("end", this.dragended)
- D3 - Add background fill to rect
- C3.js SVG visualization to Canvas with canvg - Line charts filled with black rectangles , "ERROR: Element 'parsererror' not yet implemented"
- d3.js Sankey diagram: rectangles fill color
- Custom context menu in d3 and svg
- D3 - Correct number of elements on update, but wrong values
More Query from same tag
- NoReverseMatch at Error using Django and D3
- d3.js equivalent to $(this)
- D3 tooltip won't read my data
- d3.js simple link using source/targets
- D3 line chart doesn't do transition while appending data to chart
- display K and M in Angular nvd3 chart Yaxis
- hover effects with dagre / dagre-d3 / d3.js
- How to append data to a path or object in D3 and JavaScript?
- Wheel event is not fired on a SVG group element in Safari
- Convert D3 bar chart to React D3 bar chart
- How to update the index (i) on bound data in d3?
- How does d3's path.bounds work?
- D3 - Make sure size of bar width is consistent
- JS Equivalent of Linking to a Font File
- Dashboard supporting d3
- Is it possible to make the dots representing data points on a line graph have two colors (e.g. half red, half green, vertically split)?
- Javascript - JSON - Nesting and Grouping
- How to Remove all the click handlers in javascript
- d3 link static tooltip to points
- Appending an SVG graph to a node makes it crash, something basic missing
- Zooming in using D3
- d3.js doesn't work for some reason
- How can I remove non-integer ticks while zooming in with D3.js?
- The legend is not displayed correctly (text cut off) You can test it with jsbin
- SVG path element rendered inconsistently between different versions of Chrome
- How to have a definite boundary on a d3.js horizontal bar chart
- D3.js: Passing a parameter to event handler
- live streaming plot that accumulates
- d3 time format for YYYY-MM-DD ie. 2013-02-04
- chart is not generated