Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide viewBox for svg wrapper element in stack mode #73

Closed
rqelibari opened this issue Apr 17, 2015 · 1 comment
Closed

Provide viewBox for svg wrapper element in stack mode #73

rqelibari opened this issue Apr 17, 2015 · 1 comment
Assignees

Comments

@rqelibari
Copy link

When using stack mode, the most outer svg tag in the result file does not contain a viewBox attribute.
This causes an undesired rendering in Safari (8.0.5), Chrome (42.0.2311.90) and Firefox (37.0.1) (I didn't test IE), when the svg file is included via fragment identifiers in an object tag:

<object data="/path/to/sprite.stack.svg#terminal" type="image/svg+xml"></object>

And here a screenshot of Safari:

problem

See that <object> does not fill whole <div> by width? Even style="width: 100% on <object>-Tag wouldn't change that.
The solution I've found out till now, is to include the viewBox attribute in the most outer svg of the file in the form:

<?xml ... ?><svg xmlns="..." xmlns:xlink:="..." viewBox="0 0 maxWidth maxHeight">...</svg>

maxWidth is hereby the maximal width of all included svgs. Same applies for maxHeight.
Again a screenshot in Safari after applying this rule:

solutionv1

I build a quick fix for that, modifying: lib/svg-sprite/mode/stack.js. Adding the following lines to the _buildSVG function will solve that problem:

    svg = new SVGSprite(...

    var maxWidth = 0, maxHeight=0;
    _.each(this.data.shapes, function(obj){
        if (obj.width.outer > maxWidth) maxWidth = obj.width.outer;
        if (obj.height.outer > maxHeight) maxHeight = obj.height.outer;
    });
    svg.rootAttributes.viewBox = "0 0 " + maxWidth + " " + maxHeight;

    return svg.toFile(...

Attention:
This is a quick fix. I didn't analyze the code base, so I don't know if this is the right place to put this or if there exists already an option for this. That's also the reason why I didn't provide a pull request.
However I can provide one as soon as I get feedback, if this is the right place and this fix is needed.

@jkphl
Copy link
Collaborator

jkphl commented Apr 18, 2015

Hey @rqelibari,

thanks for reporting this. To be honest, I probably never really checked using the stack sprite in conjunction with an <object> element. The only reason for skipping the viewBox attribute was to save some bytes — I can't think of any downsides. I'll incorporate it as you suggested. :)

Cheers,
Joschi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants