2015-04-04 136 views
10

我想創建一個包含圖像的圓圈,我已經嘗試過使用patternfilter,但沒有一個給我預期的結果。下面是代碼:圓圈內的SVG圖像

<svg id="graph" width="100%" height="400px"> 
 

 
    <!-- filter --> 
 
    <filter id = "born1" x = "0%" y = "0%" width = "100%" height = "100%"> 
 
     <feImage xlink:href = "https://cdn3.iconfinder.com/data/icons/people-professions/512/Baby-512.png"/> 
 
    </filter> 
 
    <circle id = "born" class = "medium" cx = "5%" cy = "20%" r = "5%" fill = "white" stroke = "lightblue" stroke-width = "0.5%" filter = "url(#born1)"/> 
 
    
 
    <!-- pattern --> 
 
    <defs> 
 
    <pattern id="image" x="0" y="0" height="100%" width="100%"> 
 
     <image x="0" y="0" xlink:href="https://cdn3.iconfinder.com/data/icons/people-professions/512/Baby-512.png"></image> 
 
    </pattern> 
 
    </defs> 
 
    <circle id = "sd" class = "medium" cx = "5%" cy = "40%" r = "5%" fill = "white" stroke = "lightblue" stroke-width = "0.5%" fill="url(#image)"/> 
 
</svg>

我的目標是保護圈,讓背景圖片裏面,像CSS ATTR background-image

回答

16

模式才能正常工作。你只需要給<image>一個大小。與HTML不同,SVG圖像默認爲寬度和高度爲零。

此外,如果您想讓圖像與圓形一起縮放,那麼您應該爲圖案指定一個viewBox

<svg id="graph" width="100%" height="400px"> 
 

 
    <!-- pattern --> 
 
    <defs> 
 
    <pattern id="image" x="0%" y="0%" height="100%" width="100%" 
 
      viewBox="0 0 512 512"> 
 
     <image x="0%" y="0%" width="512" height="512" xlink:href="https://cdn3.iconfinder.com/data/icons/people-professions/512/Baby-512.png"></image> 
 
    </pattern> 
 
    </defs> 
 
    
 
    <circle id="sd" class="medium" cx="5%" cy="40%" r="5%" fill="url(#image)" stroke="lightblue" stroke-width="0.5%" /> 
 
</svg>

2

試試這個,

使用patternUnits="userSpaceOnUse"並設置height="100%" width="100%"<image>

<defs> 
    <pattern id="image" x="0" patternUnits="userSpaceOnUse" y="0" height="100%" width="100%"> 
     <image x="0" y="0" width="500" height="500" xlink:href="http://www.viralnovelty.net/wp-content/uploads/2014/07/121.jpg"></image> 
    </pattern> 
    </defs> 

Demo

+0

改變圖像的URL礦井,使其無法正常工作。任何想法? – 2015-04-04 05:16:32