【问题标题】:How can I loop colors and maintain edge transparency with an SVG radial animation?如何使用 SVG 径向动画循环颜色并保持边缘透明度?
【发布时间】:2019-05-08 15:39:19
【问题描述】:

我有一个圆盘形 SVG,我想在其上应用一组颜色,这些颜色在循环中从中心向外辐射,同时还保持边缘(即停止不透明度)透明度,类似于如果对象存在的情况没有应用动画。

以下是动画版的主要障碍:

  1. 不保留边缘透明度。在某些情况下,我可能会通过覆盖类似的形状来解决这个问题,以适应边缘透明度,并使用与目标对象的背景颜色互补的颜色——但在这种情况下,对象位于渐变之上。

  2. 循环重置为配置值values="1%; ...",而不是平滑循环/循环颜色。不知道如何解决这个问题,或者是否可以使用径向动画。

提前感谢您的任何想法/建议!

<div style="background: linear-gradient(#231f20 0%, #231f20 21%, rgba(31, 28, 29, 0.89) 49%, rgba(20, 18, 18, 0.57) 74%, rgba(1, 1, 1, 0.02) 99%, rgba(0, 0, 0, 0) 100%);">
  <svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg"
       xmlns:xlink="http://www.w3.org/1999/xlink" width="300" style="display:inline-block;">
    <defs>
      <radialGradient id="gradient"> 
      <stop offset="0%" stop-color="#d6d2c4"/>
      <stop offset="24%" stop-color="#ffd600"/>
      <stop offset="42%" stop-color="#a6cd57"/>
      <stop offset="61%" stop-color="#f8971f"/>
      <stop offset="100%" stop-color="#f8971f" stop-opacity="0"/>
    </radialGradient>
    </defs>

    <!-- radial gradient animation -->
    <circle cx="5" cy="5" r="4" fill="url('#gradient')" />
  </svg>
  <svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg"
       xmlns:xlink="http://www.w3.org/1999/xlink" width="300" style="display:inline-block;">
    <defs>
      <radialGradient id="gradientAnimate"> 
  <stop offset="0%" stop-color="#d6d2c4"/>
  <stop offset="10%" stop-color="#ffd600"/>
  <stop offset="20%" stop-color="#a6cd57"/>
  <stop offset="30%" stop-color="#f8971f"/>
  <stop offset="40%" stop-color="#f8971f"/>
  <stop offset="50%" stop-color="#d6d2c4"/>
  <stop offset="70%" stop-color="#ffd600"/>
  <stop offset="80%" stop-color="#a6cd57"/>
  <stop offset="90%" stop-color="#f8971f"/>
  <stop offset="100%" stop-color="#f8971f" stop-opacity="0"/>
		<animate attributeName="r" dur="5s" repeatCount="indefinite"
           values="50%; 100%; 300%" />
</radialGradient>
    </defs>

    <!-- radial gradient animation -->
    <circle cx="5" cy="5" r="4" fill="url('#gradientAnimate')" />
  </svg>
</div>

【问题讨论】:

  • 1.动画中根本没有透明度。 2. 你可以有 values="24%; 100%; 24%;"这会给您带来脉动效果,否则您需要确保最终颜色与初始颜色相同。
  • 如果您想在边缘保持透明度,请单独为每个停止元素的不透明度设置动画,以便在任何给定时间始终是您想要的。

标签: animation svg alpha-transparency


【解决方案1】:

您的问题的解决方案是应用蒙版:一个从白色到黑色的径向渐变的圆圈:

<div style="background: linear-gradient(#231f20 0%, #231f20 21%, rgba(31, 28, 29, 0.89) 49%, rgba(20, 18, 18, 0.57) 74%, rgba(1, 1, 1, 0.02) 99%, rgba(0, 0, 0, 0) 100%);">
  <svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg"
       xmlns:xlink="http://www.w3.org/1999/xlink" width="300" style="display:inline-block;">
    <defs>
      <radialGradient id="gradient"> 
      <stop offset="0%" stop-color="#d6d2c4"/>
      <stop offset="24%" stop-color="#ffd600"/>
      <stop offset="42%" stop-color="#a6cd57"/>
      <stop offset="61%" stop-color="#f8971f"/>
      <stop offset="100%" stop-color="#f8971f" stop-opacity="0"/>
    </radialGradient>
    </defs>

    <!-- radial gradient animation -->
    <circle cx="5" cy="5" r="4" fill="url('#gradient')" />
  </svg>
  <svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg"
       xmlns:xlink="http://www.w3.org/1999/xlink" width="300" style="display:inline-block;">
    <defs>
  <radialGradient id="rg">
   <stop offset="50%" stop-color="#fff"></stop>
   <stop offset="100%" stop-color="#000"></stop>
  </radialGradient>
  <mask id="maskCirc">
   <circle cx="5" cy="5" r="4" fill="url(#rg)"></circle>
  </mask>
      
      <radialGradient id="gradientAnimate"> 
  <stop offset="0%" stop-color="#d6d2c4"/>
  <stop offset="10%" stop-color="#ffd600"/>
  <stop offset="20%" stop-color="#a6cd57"/>
  <stop offset="30%" stop-color="#f8971f"/>
  <stop offset="40%" stop-color="#f8971f"/>
  <stop offset="50%" stop-color="#d6d2c4"/>
  <stop offset="70%" stop-color="#ffd600"/>
  <stop offset="80%" stop-color="#a6cd57"/>
  <stop offset="90%" stop-color="#f8971f"/>
  <stop offset="100%" stop-color="#f8971f" stop-opacity="0"/>
		<animate attributeName="r" dur="5s" repeatCount="indefinite"
           values="50%; 100%; 300%" />
</radialGradient>
    </defs>

    <!-- radial gradient animation -->
    <circle cx="5" cy="5" r="4" fill="url('#gradientAnimate')" style="mask: url(#maskCirc);"/>
  </svg>
</div>

【讨论】:

    猜你喜欢
    • 2012-08-03
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 2012-01-21
    • 2017-03-09
    相关资源
    最近更新 更多