【问题标题】:How can I put random number values into "rgb()" for the svg "fill" attribute?如何将随机数值放入 svg“fill”属性的“rgb()”?
【发布时间】:2020-03-25 23:28:54
【问题描述】:

我的网站上有一个svg 元素,我想对其进行动画处理,使其从随机颜色值变为另一个随机颜色值。我现在的样子是这样的:

<?xml version="1.0" standalone="no"?>
<svg width="8cm" height="3cm" viewBox="0 0 800 300" xmlns="http://www.w3.org/2000/svg">
  <desc>Example anim01 - demonstrate animation elements</desc>
  <rect x="1" y="1" width="900" height="400"
        fill="none" stroke="none" stroke-width="2" />
  <!-- The following illustrates the use of the 'animate' element
        to animate a rectangles x, y, and width attributes so that
        the rectangle grows to ultimately fill the viewport. -->
  <rect id="RectElement" x="300" y="100" width="300" height="100"
        fill="rgb(0,255,0)"  >
    <animate attributeName="fill" begin="0" dur="1.3"
    fill="remove" from="rgb(0,255,0)" to="rgb(255,0,0)" repeatCount="indefinite"/>
    <animate attributeName="x" begin="0s" dur="1.3s"
             fill="freeze" from="300" to="0" repeatCount="indefinite"/>
    <animate attributeName="y" begin="0s" dur="1.3s"
             fill="freeze" from="100" to="0" repeatCount="indefinite"/>
    <animate attributeName="width" begin="0s" dur="1.3s"
             fill="freeze" from="300" to="800" repeatCount="indefinite"/>
    <animate attributeName="height" begin="0s" dur="1.3s"
             fill="freeze" from="100" to="300" repeatCount="indefinite"/>
    
    
  </rect>
  <!-- Set up a new user coordinate system so that
        the text string's origin is at (0,0), allowing
        rotation and scale relative to the new origin -->
  <g transform="translate(100,100)" >
    <!-- The following illustrates the use of the 'set', 'animateMotion',
         'animate' and 'animateTransform' elements. The 'text' element
         below starts off hidden (i.e., invisible). At 3 seconds, it:
           * becomes visible
           * continuously moves diagonally across the viewport
           * changes color from blue to dark red
           * rotates from -30 to zero degrees
           * scales by a factor of three. -->
    <text id="TextElement" x="0" y="0"
          font-family="Verdana" font-size="35.27" visibility="hidden"  >
      Buy our crap!
      <set attributeName="visibility" to="visible"
           begin="0s" dur="1.3s" fill="freeze" repeatCount="indefinite"/>
      <animateMotion path="M 0 0 L 100 100"
           begin="0s" dur="1.3s" fill="freeze" repeatCount="indefinite"/>
      
      <animateTransform attributeName="transform"
           type="rotate" from="-30" to="0"
           begin="0s" dur="1.3s" fill="freeze" repeatCount="indefinite"/>
      <animateTransform attributeName="transform"
           type="scale" from="0.5" to="2" additive="sum"
           begin="0s" dur="1.3s" fill="freeze" repeatCount="indefinite"/>
    </text>
  </g>
</svg>

但我想要的是为矩形分配一个随机颜色值(类似于“rgb(random #,random #,random #)”)。

我该如何做才能确保每一种颜色都是可能的结果?

【问题讨论】:

  • 您需要使用javascript random 函数来动态创建SMIL。 SMIL 没有天生的随机性能力。

标签: javascript html animation svg random


【解决方案1】:
let refreshRate = 1300;

function rand(max){
  return Math.floor(Math.random() * (max+1));
};

setInterval(function(){

document.getElementById("RectElement").setAttribute("fill","rgb("+rand(255)+", "+rand(255)+", "+rand(255)+")");

},refreshRate);

【讨论】:

  • jsfiddle.net/dxu3gzn6/3 是这样吗?我的理解是你想改变矩形颜色的每一种可能的颜色。
  • 这和我想象的有点不同。我只是希望它每次重新开始时都变成一种随机颜色,而不是一个循环中的所有颜色。不过我可能会使用它...
  • 只需更改 refreshRate=1300
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-10
  • 2021-05-30
  • 2019-06-03
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
相关资源
最近更新 更多