Utility: Convert SVG path to all-relative or all-absolute commands

I like hand-editing my SVGs. Often I will create an initial version in Illustrator, and then export and continue with hand editing. Not only is it a bit of a meditative experience and it satisfies my obsessive-compulsive tendencies to clean up the code, it has actual practical benefits when you need to make certain changes […]


This content originally appeared on Lea Verou and was authored by Lea Verou

I like hand-editing my SVGs. Often I will create an initial version in Illustrator, and then export and continue with hand editing. Not only is it a bit of a meditative experience and it satisfies my obsessive-compulsive tendencies to clean up the code, it has actual practical benefits when you need to make certain changes or introduce animation. Some things are easier to do in a GUI, and others are easier to do in code, and I like having the flexibility to pick which one fits my use case best.

However, there was always a thing that was a PITA: modifying paths. Usually if I need anything more complicated than just moving them, I’d do it in Illustrator, but even moving them can be painful if they are not all relative (and no, I don’t like introducing pointless transforms for things that should really be in the d attribute).

For example, this was today’s result of trying to move an exported “a” glyph from Raleway Bold by modifying its first M command:

Trying to move a path by changing its first M command when not all of its commands are relative.

This happened because even though most commands were exported as relative, several were not and I had not noticed. I have no idea why some commands were exported as absolute, it seems kind of random.

When all commands are relative, moving a path is as simple as manipulating its initial M command and the rest just adapts, because that’s the whole point of relative commands. Same with manipulating every other part of the path, the rest of it just adapts. It’s beautiful. I honestly have no idea why anybody would favor absolute commands. And yet, googling “convert SVG path to relative” yields one result, whereas there are plenty of results about converting paths to absolute. No idea why that’s even desirable, ever (?).

I remembered I had come across that result before. Thankfully, there’s also a fiddle to go with it, which I had used in the past to convert my path. I love it, it uses this library called Snap.svg which supports converting paths to relative as a just-add-water utility method. However, that fiddle is a quick demo to answer a StackOverflow question, so the UI is not super pleasant to use (there is no UI: you just manipulate the path in the SVG and wait for the fiddle to run). This time around, I needed to convert multiple paths, so I needed a more efficient UI.

So I created this demo which is also based on Snap.svg, but has a slightly more efficient UI. You just paste your path in a textarea and it both displays it and instantly converts it to all-relative and all-absolute paths (also using Snap.svg). It also displays both your original path and the two converted ones, so you can make sure they still look the same. It even follows a pending-delete pattern so you can just focus on the output textarea and hit Cmd-C in one fell swoop.

I wasn’t sure about posting this or just tweeting it (it literally took less than 30 minutes — including this blog post — and I tend to only post small things like that on my twitter), but I thought it might be useful to others googling the same thing, so I may as well post it here for posterity. Enjoy!


This content originally appeared on Lea Verou and was authored by Lea Verou


Print Share Comment Cite Upload Translate Updates
APA

Lea Verou | Sciencx (2019-05-06T10:19:19+00:00) Utility: Convert SVG path to all-relative or all-absolute commands. Retrieved from https://www.scien.cx/2019/05/06/utility-convert-svg-path-to-all-relative-or-all-absolute-commands/

MLA
" » Utility: Convert SVG path to all-relative or all-absolute commands." Lea Verou | Sciencx - Monday May 6, 2019, https://www.scien.cx/2019/05/06/utility-convert-svg-path-to-all-relative-or-all-absolute-commands/
HARVARD
Lea Verou | Sciencx Monday May 6, 2019 » Utility: Convert SVG path to all-relative or all-absolute commands., viewed ,<https://www.scien.cx/2019/05/06/utility-convert-svg-path-to-all-relative-or-all-absolute-commands/>
VANCOUVER
Lea Verou | Sciencx - » Utility: Convert SVG path to all-relative or all-absolute commands. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2019/05/06/utility-convert-svg-path-to-all-relative-or-all-absolute-commands/
CHICAGO
" » Utility: Convert SVG path to all-relative or all-absolute commands." Lea Verou | Sciencx - Accessed . https://www.scien.cx/2019/05/06/utility-convert-svg-path-to-all-relative-or-all-absolute-commands/
IEEE
" » Utility: Convert SVG path to all-relative or all-absolute commands." Lea Verou | Sciencx [Online]. Available: https://www.scien.cx/2019/05/06/utility-convert-svg-path-to-all-relative-or-all-absolute-commands/. [Accessed: ]
rf:citation
» Utility: Convert SVG path to all-relative or all-absolute commands | Lea Verou | Sciencx | https://www.scien.cx/2019/05/06/utility-convert-svg-path-to-all-relative-or-all-absolute-commands/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.