svg changes when converted to symbol

时间:2017-02-01 17:28:20

标签: svg gulp svg-sprite

I have this dummy svg showing a cirle with some grey figure inside

<svg viewBox="0 0 86 86" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
    <circle id="path-1" cx="43" cy="43" r="43"></circle>
</defs>
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
    <g transform="translate(-585.000000, -391.000000)">
        <g transform="translate(585.000000, 391.000000)">
            <mask id="mask-2" fill="white">
                <use xlink:href="#path-1"></use>
            </mask>
            <use id="Oval" fill="currentcolor" xlink:href="#path-1"></use>
            <g id="Group" mask="url(#mask-2)" fill="#b8b8b8">
                <g transform="translate(21.500000, 27.823529)" id="Page-1">
                    <path d="M0.5,0.176470588 L0.5,58.1764706 L15.5,58.1764706 L15.5,49.1764706 L29.5,49.1764706 L29.5,31.1764706 L7.5,31.1764706 L7.5,37.1764706 Z"></path>
                </g>
            </g>
        </g>
    </g>
</g>

when I convert it to a sprite using gulp-svg-sprite with mode symbol I get this result

<?xml version="1.0" encoding="UTF-8" ?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><symbol viewBox="0 0 86 86" id="company"><defs><circle id="aa" cx="43" cy="43" r="43"/></defs><g fill="none" fill-rule="evenodd"><mask id="ab" fill="#fff"><use xlink:href="#aa"/></mask><use fill="currentcolor" xlink:href="#aa"/><g mask="url(#ab)" fill="#b8b8b8"><path d="M22 28v58h15v-9h14V59H29v6z"/></g></g></symbol></svg>

Now the grey figure breaks out of the circle and this even happens if I copy root defs- and g-tags from my original svg directly into the symbol-tag. I have also tried inserting a clipPath in the symbol version but with no luck.

What am I missing here?

2 个答案:

答案 0 :(得分:0)

更新:简化你的svg可能有用......试试下面的例子,一个使用符号,一个没有(如果gulp sprite代码不能有嵌套符号)......这可能是defs区域的一个问题。

svg {
  width: 100px;
}
<svg viewBox="0 0 86 86" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <symbol id="c">
      <circle cx="43" cy="43" r="43" fill="currentColor" />    
    </symbol>
    <mask id="mask" color="#fff">
      <use xlink:href="#c" />
    </mask>
  </defs>
  <use xlink:href="#c" color="#666" />
  <path fill="#999" mask="url(#mask)" d="M10 0v60h30v-10h20v-20h-30v9z" />
</svg>

<svg viewBox="0 0 86 86" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <mask id="mask2" color="#fff">
      <circle cx="43" cy="43" r="43" fill="#FFF" />
    </mask>
  </defs>
  <circle cx="43" cy="43" r="43" fill="currentColor" />
  <path fill="#999" mask="url(#mask2)" d="M10 0v60h30v-10h20v-20h-30v9z" />
</svg>

答案 1 :(得分:0)

这可能不是一个完整的答案,但我设法让它发挥作用;

首先,我使用我的圆圈

使用clipPath扩展我的defs
<defs>
    <circle id="circle" cx="43" cy="43" r="43"></circle>
    <clipPath id="clippath"><use overflow="visible" xlink:href="#circle" /></clipPath>
</defs>

然后,在我的第一个组上使用clipPath并从gulp-svg-sprite切换到gulp-svgstore(将defs移动到符号上方的顶部)给出了我作为单个svg和svg的期望符号精灵。