【问题标题】:is it possible to make SVG circle fill color from bottom to top based on percentage? [duplicate]是否可以根据百分比从下到上制作 SVG 圆形填充颜色? [复制]
【发布时间】:2015-12-15 04:36:18
【问题描述】:
<svg viewbox="-20 -20 100 100">

  <circle r="15.29563" cx="0" stroke="#7dc4c2" fill="#5ea4a2">

</svg>

如何根据百分比填充如下圆圈!!

http://i.stack.imgur.com/gVAN5.png

提前致谢。

【问题讨论】:

    标签: svg geometry svg-filters svg-animate


    【解决方案1】:

    您可以使用具有停止不透明度的渐变来执行此操作。 您将分别添加两个不透明度为 0 和 1 的“中间”停靠点,并将两者的偏移量设置为您需要的百分比。

    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="200" height="200">
      <linearGradient id="lg" x1="0.5" y1="1" x2="0.5" y2="0">
          <stop offset="0%" stop-opacity="1" stop-color="royalblue"/>
          <stop offset="40%" stop-opacity="1" stop-color="royalblue"/>
          <stop offset="40%" stop-opacity="0" stop-color="royalblue"/>
          <stop offset="100%" stop-opacity="0" stop-color="royalblue"/>
      </linearGradient>
      <circle cx="50" cy="50" r="45" fill="url(#lg)" stroke="crimson" stroke-width="5"/>
    </svg>

    你甚至可以为它制作动画

    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="200" height="200">
          <linearGradient id="lg" x1="0.5" y1="1" x2="0.5" y2="0">
              <stop offset="0%" stop-opacity="1" stop-color="royalblue"/>
              <stop offset="40%" stop-opacity="1" stop-color="royalblue">
                <animate attributeName="offset" values="0;1;0" repeatCount="indefinite" dur="10s" begin="0s"/>
              </stop>
              <stop offset="40%" stop-opacity="0" stop-color="royalblue">
                <animate attributeName="offset" values="0;1;0" repeatCount="indefinite" dur="10s"  begin="0s"/>
              </stop>
              <stop offset="100%" stop-opacity="0" stop-color="royalblue"/>
          </linearGradient>
          <circle cx="50" cy="50" r="45" fill="url(#lg)" stroke="crimson" stroke-width="5"/>
    </svg>

    优点是它适用于任何形状和大小而不改变渐变

       <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 300" width="400" height="200">
          <linearGradient id="lg" x1="0.5" y1="1" x2="0.5" y2="0">
              <stop offset="0%" stop-opacity="1" stop-color="royalblue"/>
              <stop offset="40%" stop-opacity="1" stop-color="royalblue"/>
              <stop offset="40%" stop-opacity="0" stop-color="royalblue"/>
              <stop offset="100%" stop-opacity="0" stop-color="royalblue"/>
          </linearGradient>
          <circle cx="50" cy="50" r="45" fill="url(#lg)" stroke="crimson" stroke-width="5"/>
         <circle cx="250" cy="150" r="145" fill="url(#lg)" stroke="crimson" stroke-width="5"/>
         <rect x="400" y="20" width="100" height="100" fill="url(#lg)" stroke="crimson" stroke-width="5"/>
         <path d="M50 150L95 290 h-90z" fill="url(#lg)" stroke="crimson" stroke-width="5"/>
    
         <path d="M450 205 A45 45 0 0 1 450 295A100 100 0 0 0 450 205z" fill="url(#lg)" stroke="crimson" stroke-width="5"/>
       </svg>

    【讨论】:

    • 谢谢 Holger Will ...你是 svg 中的野兽...
    • 有没有办法用 css 做到这一点?我使用了你的例子并且喜欢它,但后来我发现 IE 不支持 SMIL
    • 想要动画到 60%。该怎么做?
    • 两个动画都使用 values="0;1;0" 而不是 values="0;0.6;0"...
    【解决方案2】:

    最简单的方法是创建一个带有圆形孔的蒙版,然后在其后面创建一个矩形动画。例如:

    <path fill="#fff" fill-rule="evenodd"
            d="M0 0 200 0 200 200 0 200ZM20 100 A80 80 0 0 0 180 100 80 80 0 0 0 20 100Z"/>
    

    这里的路径数据从一个 200 单位宽的方形框 (M0 0 200 0 200 200 0 200Z) 开始,然后使用两条圆弧在其中绘制一个直径为 80 单位的圆 (A80 80 0 0 0 180 100 80 80 0 0 0 20 100Z)。 evenodd 填充规则确保从正方形中切出圆形。

    如果您希望圆圈从下到上填充,则必须使用rotate 转换:

    <rect transform="rotate(180 100 100)" x="20" y="20" width="160" height="0" fill="#47f" id="fillup"/>
    

    这会围绕 SVG 图像的中间旋转坐标系,这样当你增加它的高度时,矩形会向上增长。在这里,当您将鼠标悬停在矩形上时,我使用 CSS 过渡来更改矩形的高度。但是您可以使用 Javascript 或 JQuery 将高度更改为您想要的任何值。

    这是一个工作示例:

    svg #fillup { height:0px; transition:height 0.5s; }
    svg:hover #fillup { height:160px; }
    <svg width="200" height="200" viewBox="0 0 200 200">
      <rect x="10" y="10" width="180" height="180" fill="#eee"/>
      <rect transform="rotate(180 100 100)" x="20" y="20"
            width="160" height="0" fill="#47f" id="fillup"/>
      <path fill="#fff" fill-rule="evenodd"
            d="M0 0 200 0 200 200 0 200ZM20 100 A80 80 0 0 0
               180 100 80 80 0 0 0 20 100Z"/>
      <circle cx="100" cy="100" r="90" fill="none" stroke="#888"
              stroke-width="20"/>
      <circle cx="100" cy="100" r="99" fill="none" stroke="#333"
              stroke-width="1"/>
      <circle cx="100" cy="100" r="80" fill="none" stroke="#333"
              stroke-width="1"/>
    </svg>

    【讨论】:

    • 感谢您回答 squeamish ossifrage。
    猜你喜欢
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    相关资源
    最近更新 更多