normalize-svg-coords
normalize-svg-coords copied to clipboard
Wrong normalization case
let n = require('normalize-svg-coords')
n('M10,-20V20L-10,0Z', {viewBox: [-10, -20, 10, 20], min: -10, max: 10})
// 'M10.0000 -10.0000V20.0000L-10.0000 0.0000Z'
↑ we lost y-coord shift in M command, and result is skewed.
'M10,-20V20L-10,0Z':

'M10.0000 -10.0000V20.0000L-10.0000 0.0000Z':

We can use that as a test case.
Thank you for flagging this. I'll try and investigate and provide a fix soon
Can you confirm the expected result, please?
M5.0000 -10.0000V10.0000L-5.0000 0.0000Z
@lukem512 yes, looks right!
Thanks. I'm not yet sure what's causing this but I did find another bug whilst searching (see 0b165afa774027d967f34ca2e611ba165a0e5ec5)
The issue arises because the algorithm tries to scale x and y coordinates independently. After the corrections in the commit I mentioned above the output is M10.0000 -10.0000V10.0000L-10.0000 0.0000Z (see JSFiddle).
One possible solution is to always normalize on the greater of the x and y bounds. Using this solution results in this. Unfortunately this has the potential to go negative, for example the should return a normalized elliptic arcs test outputs M0.1474 0.1474A0.3064 0.3064 0 1 0 0.6244 -0.0116Z.
I'm sure a more elegant solution exists but I'm not yet able to visualise it.