It can be useful for normalizing svg files or for fixing this bug https://stackoverflow.com/questions/26263550/css-svg-background-strange-transparent-line-around
I'm afraid it has nothing to do with image optimization. It's a task for an editor.
In case like this it's better to use a little overlap like 1px to prevent such edge effects. There is a聽lot of such issues when page is zoomed in desktop browsers.
It's not just for that. preserveAspectRatio can be used to align the SVG content, regardless of how wide the outer canvas is. Let's say I have an SVG with content that's 64x64 pixels, but the outer element (in this case the <svg>-tag containing the <use>-tag) is 100px wide. I can then use preserveAspectRatio to align the SVG content top left, so I can resize the outer element (using CSS) without worring about flow. Right now, the content is centered no matter what I do, and this is not really great for my current use-case.
It's not just an editor-thing, because SVGO strips the tag off the incoming SVG-files, and there's currently no way to add them again (also, addAttributesToSVGElement doesn't seem to let me add values, and even if it did, I'd want to control this on a per-SVG basis instead of adding a blanket-statement, which would be a lot easier in the source file than in the setup for SVGO); nor is there a way to control this with styling from a browser.
So I would say this is a needed feature.
Turns out, effectively:
viewBox := fmt.Sprintf(`viewBox="%d %d %d %d"`,
vbMinX, vbMinY, (vbMaxX - vbMinX), (vbMaxY - vbMinY))
aspect := `preserveAspectRatio="none"`
canvas.Start(width, height, viewBox, aspect)
Most helpful comment
It's not just for that.
preserveAspectRatiocan be used to align the SVG content, regardless of how wide the outer canvas is. Let's say I have an SVG with content that's 64x64 pixels, but the outer element (in this case the<svg>-tag containing the<use>-tag) is 100px wide. I can then use preserveAspectRatio to align the SVG content top left, so I can resize the outer element (using CSS) without worring about flow. Right now, the content is centered no matter what I do, and this is not really great for my current use-case.It's not just an editor-thing, because SVGO strips the tag off the incoming SVG-files, and there's currently no way to add them again (also,
addAttributesToSVGElementdoesn't seem to let me add values, and even if it did, I'd want to control this on a per-SVG basis instead of adding a blanket-statement, which would be a lot easier in the source file than in the setup for SVGO); nor is there a way to control this with styling from a browser.So I would say this is a needed feature.