html - 圆圈内的 SVG 图像

标签 html svg

我想创建一个包含图像的圆圈,我已经尝试使用 patternfilter 但它们都没有给我预期的结果。下面是代码:

<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

最佳答案

一个模式会起作用。你只需要给 <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>

关于html - 圆圈内的 SVG 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29442833/

相关文章:

svg - 将 userSpaceOnUse 坐标转换为 objectBoundingBox 坐标的算法?

css - 在 HTML/CSS 中堆叠图像

javascript - 下拉列表菜单 - 不起作用

html - 指向 anchor 的链接不会跳到页面顶部

html - 如何剪辑 SVG 图?

javascript - SVG:根据值定位RECT

javascript - 拉斐尔透明元素onclick

html - Font Awesome 5 - 如何将图标放在圆圈内?

javascript - 删除html元素文本中的重复单词?

html - 尽管类不同,动画只发生在一个复选框上