【问题标题】:SVG image inside circle圆圈内的 SVG 图像
【发布时间】:2015-06-09 04:07:36
【问题描述】:

我想创建一个包含图像的圆圈,我已经尝试使用 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

【问题讨论】:

    标签: html svg


    【解决方案1】:

    试试这个,

    使用patternUnits="userSpaceOnUse" 并设置height="100%" width="100%"&lt;image&gt;

     <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

    【讨论】:

    • 将您的图片网址更改为我的,使其不起作用..有什么想法吗?
    【解决方案2】:

    模式会起作用。你只需要给&lt;image&gt; 一个大小。与 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>

    【讨论】:

      【解决方案3】:

      这是 SVG 的替代品,您实际上并不需要 SVG。您可以使用图像标签本身来实现您的目标。

      .avatar {
          vertical-align: middle;
          width: 20px;
          height: 20px;
          border-radius: 50%;
          border: solid 5px red;
      }
      
      <img src="https://connectoricons-prod.azureedge.net/kusto/icon_1.0.1027.1210.png" alt="Avatar" class="avatar">
      

      Refer here for live demo

      【讨论】:

      • 我在回答中已经明确提到了。这是 SVG 的替代方案。请查看现场演示链接。
      • 如果有人问你“如何修理宝马的发动机”,你建议买大众汽车吗?
      • 哈哈。那不是我的意图。这是一个更好的选择。我正在寻找与问题要求相同的期望输出。我发现这个答案很有用。
      • 这些问题没有说明任何关于 SVG 的内容;它要求它“创建一个包含图像的圆圈”。这是比 SVG 更好的方法
      猜你喜欢
      • 2023-03-18
      • 2015-09-03
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多