Closed jbiermann closed 1 year ago
round
takes a second parameter map
which defines which attributes should be rounded. By default it rounds nothing. However, the round method has no concept of rounding a delimited string like the points
attribute has. It just converts every string to number and rounds it. If you want to round points, you need to do it yourself:
var codeRounded = item.svg(function(node) {
const round = (n) => Math.round(n * 1000)/1000
if (node.type.startsWith('poly')) {
node.attr('points', node.array().map((p) => p.map(round)))
}
});
thank you, that does work. I did not note that it does not treat delimited strings. The accidentality deleting of points / attributes of the build in round function is already fixed in the master trunk.
Maybe your comment is a good candidate for adding this to the documentation?
Yeah maybe :D
Bug report
The round export modifier function kills arbitrary attributes (3.0.16) or polygon/polyline points (3.0.16) on master it does not work on polygone/polyline shapes. I am referencing to the round example from the documentation.
Fiddle
See this fiddle. Here I load an existing SVG shape from DOM, do some manipulation with it and export it a) svg() and b with svg(function(node) { ...} ) with the round export modifier. I set the output to the textarea box.
Explanation
That the round export modifier function rounds the points to the desired accuracy on all shape types and keeps every other attribute (e.g. ID) as they have been set before.
What is happening instead?
On 3.0.16: It kills the ID attribute and removes every point of the polygon/polyline, the shape is broken. The ID is missing on all shape types.
On master: The ID attribute stays intact, but the round export modifier function does nothing on the polygon/polyline shapes. On other shapes like circles it rounds to the desired places.
What error message are you getting?
None, the output of the function is wrong.