normalize-svg-coords icon indicating copy to clipboard operation
normalize-svg-coords copied to clipboard

Wrong normalization case

Open dy opened this issue 8 years ago • 5 comments

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': image

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

We can use that as a test case.

dy avatar Aug 23 '17 21:08 dy

Thank you for flagging this. I'll try and investigate and provide a fix soon

lukem512 avatar Aug 29 '17 14:08 lukem512

Can you confirm the expected result, please?

M5.0000 -10.0000V10.0000L-5.0000 0.0000Z

lukem512 avatar Aug 30 '17 09:08 lukem512

@lukem512 yes, looks right!

dy avatar Aug 30 '17 14:08 dy

Thanks. I'm not yet sure what's causing this but I did find another bug whilst searching (see 0b165afa774027d967f34ca2e611ba165a0e5ec5)

lukem512 avatar Aug 30 '17 16:08 lukem512

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.

lukem512 avatar Sep 01 '17 09:09 lukem512