【问题标题】:how to make an svg colour change with time如何使 svg 颜色随时间变化
【发布时间】:2021-05-13 21:52:51
【问题描述】:

我在下面有这条 SVG 路径线,它有两个圆圈以特定的持续时间从一端移动到另一端。是否可以更改蓝色圆圈的颜色,使其在开始时为蓝色,20 秒后开始变为白色,然后在还有 20 秒的时间到达结束,而红色圆圈会根据圆圈的颜色改变自己的颜色,然后在当前蓝色移动圆圈后面添加一个发光,这将是当前蓝色圆圈的颜色?

这是否意味着必须使用 javascript 制作整体,或者我必须如何修改 HTML 以使其按照描述的方式运行?

			<svg width="450" height="450">
               <path id="motionPath2"
					d="M 50 200 L 400 200 "
					stroke="
					none" fill="transparent" />
					         <circle class="circle" r=20 fill=#ff0000 z-index=55>
             <animateMotion dur="100s" repeatCount="indefinite"
					rotate="auto">
                 <mpath href="#motionPath2" />
             </animateMotion>
         </circle>
         <path id="motionPath"
					d="M 50 200 L 400 200 "
					stroke="
					black" fill="transparent" />
					         <circle class="circle" r=5 fill=#45b6fe z-index=55>
             <animateMotion dur="100s" repeatCount="indefinite"
					rotate="auto">
                 <mpath href="#motionPath" />
             </animateMotion>
         </circle>
      </svg>

【问题讨论】:

    标签: html css svg


    【解决方案1】:

    您可以引入 CSS 动画,以便轻松处理着色。

    这里有一个基本的例子来说明这个想法:

    @keyframes color {
      from {
        fill: red;
      }
      to {
        fill: green;
      }
    }
    
    #big {
      animation: color 20s linear infinite;
    }
    <svg width="450" height="450">
                   <path id="motionPath2"
    					d="M 50 200 L 400 200 "
    					stroke="
    					none" fill="transparent" />
    					         <circle class="circle" id="big" r=20 fill=#ff0000 z-index=55>
                 <animateMotion dur="20s" repeatCount="indefinite"
    					rotate="auto">
                     <mpath href="#motionPath2" />
                 </animateMotion>
             </circle>
             <path id="motionPath"
    					d="M 50 200 L 400 200 "
    					stroke="
    					black" fill="transparent" />
    					         <circle class="circle" r=5 fill=#45b6fe z-index=55>
                 <animateMotion dur="20s" repeatCount="indefinite"
    					rotate="auto">
                     <mpath href="#motionPath" />
                 </animateMotion>
             </circle>
          </svg>

    因为它是关于一个简单的几何图形,你可以很容易地使用没有 SVG 的 CSS 来做到这一点

    .box {
     margin:100px;
     height:2px;
     background:green;
     position:relative;
    }
    .box:before,
    .box:after{
      content:"";
      position:absolute;
      width:80px;
      height:80px;
      border-radius:50%;
      left:0;
      top:0;
      transform:translate(-50%,-50%);
      background:red;
      animation:move 10s linear infinite alternate;
      animation-name:move,color,glow;
    }
    .box:after {
      width:20px;
      height:20px;
      background:blue;
    }
    @keyframes move {
      to {
        left:100%;
      }
    }
    
    @keyframes color {
      to {
        background:yellow;
      }
    }
    @keyframes glow {
      to {
        box-shadow:0 0 30px yellow;
      }
    }
    <div class="box">
    
    </div>

    【讨论】:

    • 该信息很有帮助。我还更新了原始的 sn-p 以显示完整的想法。另外,是否可以将发光添加到#big,因为它已经有一个动画,根据link,发光也必须是一个动画,但我不相信同一个 ID 中可以有两个单独的动画盒子。此外,当尝试使用 box-shadow 时,它会创建一个发光的盒子而不是圆形。
    • @john_snow5214 不要用我所做的来编辑您的问题,否则我的回答将无关紧要:) 您可以在一个元素上添加任意数量的动画:stackoverflow.com/q/26986129/8620333
    • 我改回来了
    • 添加此更改:
      并将该链接中的发光代码链接到微调器,但它不起作用。
    • @john_snow5214 检查更新,添加了一个更易于处理的 CSS 解决方案,您可以在其中更改颜色和发光效果,只需根据需要进行调整
    【解决方案2】:

    这是对您问题的部分回答,因为我只理解部分内容。我希望在看到答案后您能够提出一个更好的问题。

    我所做的更改:

    您正在为每个圆圈使用动画,但由于路径相同且动画相同,我已将两个圆圈放在一个组中,并为圆圈设置动画。

    另外,由于路径是一条线,我可以使用 animateTransform ... type="translate" 代替。但是,您可能希望将路径更改为更复杂的路径,所以我坚持使用 animateMotion

    要为颜色设置动画,您可以使用&lt;animate&gt; 为填充设置动画,并为颜色使用values 属性:values="red;white;orange"

    因为你提到最后一种颜色应该在动画结束前 20 秒出现,所以我使用keyTimes= "0; 0.8; 1"。请注意,keyTimes 属性与values 属性具有相同数量的值,并表示用于控制动画节奏的时间值列表。

    <svg width="450" height="450">
    <path id="motionPath2" d="M 50 200 L 400 200 "
    					stroke="black" fill="transparent" />
    <g>  
      <circle class="circle" r="20" fill="#ff0000"></circle>
    <circle class="circle" r=5 fill="#45b6fe" >
         <animate 
           attributeName="fill"
           attributeType="XML"
           values="#45b6fe;white;orange"
           keyTimes= "0; 0.8; 1"
           dur="100s"
           repeatCount="indefinite"/>
      </circle>
       <animateMotion dur="100s" repeatCount="indefinite">
           <mpath href="#motionPath2" />
       </animateMotion>
      </g>       
     </svg>

    【讨论】:

    • 它正在工作。我最初想到的问题是,当时间为0时,圆圈是蓝色的,然后在20秒内变为白色,保持白色60秒,然后在最后20秒内变为橙色。
    • 我添加了 values="#45b6fe;white;white;orange" 和 keyTimes="0; 0.2;0.8; 1"
    • 另外,我在哪里可以找到解释 svg 功能的此类 svg 文档?
    • @john_snow5214 herehere
    • 感谢您提供链接
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签