Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inkscape: round coordinates

Tags:

inkscape

Is there a command in Inkscape to round coordinates of a path to the closest integer value.

For instance, to replace this path:

m 0,0 261.98828,-890.8828 -299.999999,-900 2593.486319,54.4063 -253.9941,850.09565 264.3594,870.83005 z

by this:

m 0,0 262,-891 -300,-900 2593,54 -254,850 264,871 z
like image 732
DevonDahon Avatar asked Apr 30 '18 09:04

DevonDahon


2 Answers

Yes, there is way to set coordinates precision.

  • File -> Save as...
  • Save it as "Optimized SVG"
  • In the first dialog setting, set the coordinate "precision" to 3 or whatever you want
like image 142
Rytis Guntulis Avatar answered Sep 20 '22 00:09

Rytis Guntulis


The example below shows 3 circles/points and a line connecting them. The points are rounded integers, whereas the line has trailing decimal values.

The javascript waits 2 seconds (so you can compare) before transforming all of the paths (just one) and aligning them to the nearest integer. This can be seen by the alignment of the path corners to the circles.

Load your SVG in a browser, run the JavaScript in the browser console, then export the DOM by right clicking on a node and copying the outerHTML. That should do.

setTimeout(() => {
  document.querySelectorAll('path').forEach((p) => {
    p.setAttribute(
      'd',
      p.getAttribute('d')
      .split(/(\d*\.?\d*)/g)
      .map((e) => {
        return isNaN(parseFloat(e)) ?
          e :
          Math.round(parseFloat(e), 0)
      }).join(''))
  })
}, 2000)
html,
body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
}
<svg viewBox="0 0 50 30" height="100%">
  <g fill="none">
    <circle cx="5" cy="21" r="1" stroke="green" stroke-width=".5" />
    <circle cx="43" cy="6" r="1" stroke="green" stroke-width=".5" />
    <circle cx="35" cy="24" r="1" stroke="green" stroke-width=".5" />
    <path
      stroke="red" 
      d="
       M 5.4131 20.555
       L 43.345 5.666
       L 34.822 24.22222
       Z"
     stroke-width=".5"
     />
  </g>
</svg>

It takes all the path elements and edits all the d attributes, rounding any numbers to the nearest integer.

like image 30
ADJenks Avatar answered Sep 21 '22 00:09

ADJenks