将背景图像(.png)添加到SVG圆形

Tej*_*tha 43 html css svg

这可能吗?以下是我尝试过但它完全用黑色填充圆圈.

<svg id='vizMenu' width="700" height="660">
    <defs>
        <filter id="dropshadow" height="130%">
            <feGaussianBlur in="SourceAlpha" stdDeviation="2"/> 
            <feOffset dx="0.5" dy="0.8" result="offsetblur"/> 
            <feMerge>
                <feMergeNode/>
                <feMergeNode in="SourceGraphic"/>
            </feMerge>
        </filter>
    </defs>
    <circle id='top' filter="url(#dropshadow)" cx="180" cy="120" r="80" stroke="#2E2E2E" stroke-width="2" fill="url('images/word-cloud.png')"/>
    <circle id='bottom' filter="url(#dropshadow)" cx="500" cy="300" r="80" stroke="#2E2E2E" stroke-width="2" fill="url('images/word-cloud.png')"/>
    <circle id='extra' filter="url(#dropshadow)" cx="180" cy="560" r="80" stroke="#2E2E2E" stroke-width="2" fill="#ffffff"/>
</svg>
Run Code Online (Sandbox Code Playgroud)

met*_*ion 38

通过SVG Patterns实现svg元素的图像填充...

<svg width="700" height="660">
  <defs>
    <pattern id="image" x="0" y="0" patternUnits="userSpaceOnUse" height="1" width="1">
      <image x="0" y="0" xlink:href="url.png"></image>
    </pattern>
  </defs>
  <circle id='top' cx="180" cy="120" r="80" fill="url(#image)"/>
</svg>
Run Code Online (Sandbox Code Playgroud)

  • 我们可以使用fill停止在后台复制png显示.在这种情况下,它将继续在背景中复制图像.是否可以只显示一次图案?我的意思是没有平铺... (5认同)
  • 关闭defs标签丢失 (3认同)
  • 有没有办法阻止模式重复,让它缩放以适应? (3认同)
  • 这种技术根本不起作用.您必须使用[Teo Inke]中的方法(http://stackoverflow.com/users/2105657/teo-inke)[answer](http://stackoverflow.com/questions/11496734/add-a-background-图像PNG到一个-SVG-圆形状#答案-24128191).即便如此,当你尝试以任何方式进行转换时(即缩放等),它会中断. (2认同)

Teo*_*nke 25

好吧,我无法使用已接受的答案.这就是我最终做到的方式:

<svg width="100" height="100">
  <defs>
    <pattern id="image" patternUnits="userSpaceOnUse" height="100" width="100">
      <image x="0" y="0" height="100" width="100" xlink:href="http://i.imgur.com/7Nlcay7.jpg"></image>
    </pattern>
  </defs>
  <circle id='top' cx="50" cy="50" r="50" fill="url(#image)"/>
</svg>
Run Code Online (Sandbox Code Playgroud)

如果要自定义大小,请将其用作比例参考:

x = yourPreferredSize

<svg width=">2x" height=">2x">
  <defs>
    <pattern id="image" patternUnits="userSpaceOnUse" height=">2x" width=">2x">
      <image x="0" y="0" height="2x" width="2x" xlink:href="http://i.imgur.com/7Nlcay7.jpg"></image>
    </pattern>
  </defs>
  <circle id='top' cx="x" cy="x" r="x" fill="url(#image)"/>
</svg>
Run Code Online (Sandbox Code Playgroud)

(此比例适用于平方图像)

  • 如何阻止它重复?它也不是正确的 (4认同)
  • 即使我无法使用已接受的答案.感谢您的回答. (3认同)

Ari*_*osh 13

我知道这是一个老问题,但我使用过滤器覆盖图像.上面的解决方案对我来说不起作用,因为缩放并且看起来图像是平铺的.我用它代替,我希望它也能帮助别人.

<svg width="700" height="660">
    <filter id="this_image" x="0%" y="0%" width="100%" height="100%">
        <feImage xlink:href="test_image.png"/>
    </filter>
    <circle filter="url(#this_image)" cx="180" cy="120" r="80" />
</svg>
Run Code Online (Sandbox Code Playgroud)

  • 这有效,但图像是以矩形形式出现的吗?圆圈应该完全用bg图像填充. (4认同)

Pho*_*han 9

正确解释图像重复问题(感谢AmeliaBR)

TL; DR:概念objectBoundingBoxpreserveAspectRatio使用!

<svg height = "10%" width = "10%">
  <defs>
    <pattern id = "attachedImage" height = "100%" width = "100%"            
                   patternContentUnits = "objectBoundingBox">
       <image xlink:href = "url.png" preserveAspectRatio = "none" 
              width = "1" height = "1"/>
    </pattern>
  </defs>
<circle cx = "50%" cy = "50%" r = "35%" fill = "url(#attachedImage)"/>
</svg>
Run Code Online (Sandbox Code Playgroud)