d3 / d3-shape

Graphical primitives for visualization, such as lines and areas.
https://d3js.org/d3-shape
ISC License
2.47k stars 305 forks source link

Coordinate precision controls? #139

Closed veltman closed 5 years ago

veltman commented 5 years ago

What are your thoughts on allowing user control over the precision of generated coordinates from methods like line() and area()?

I'm thinking of two reasons why this is potentially useful: 1) Any kind of non-browser static rendering where file size is a consideration. 2) I've run into a few browser-specific SVG rendering bugs that are precision-dependent, where coordinates with many significant digits can lead to weird problems (e.g. https://bugzilla.mozilla.org/show_bug.cgi?id=1507603).

2 has only come up on rare occasions for me, but 1 has come up more often.

I could imagine two versions:

Specify a number to round each coordinate to:

d3.line()
  .precision(1e-3);

Or instead update d3-path directly to take an optional precision argument that gets applied in the toString() method, and then someone could pass one as a custom context.

d3.line()
  .context(d3.path(1e-3));

// or...

d3.line()
  .context(d3.path().precision(1e-3));

Any thoughts on the merit of incorporating this? The latter approach would avoid cluttering the main API but still provide a low-level outlet for interested users.

veltman commented 5 years ago

Oops, I just realized this has already been discussed over at https://github.com/d3/d3-path/issues/10, I'll chime in there.