更改每次循环迭代的参数

时间:2021-06-13 07:42:38

标签: javascript loops iteration anime.js

尝试一个简单的循环动画,在 animejs 循环中为每次迭代更改其参数。此代码更改了变量值,但未应用它们。值通过迭代保持不变。我怎样才能做到这一点?


  let
    R = Math.floor((Math.random() * 255)),
    G = Math.floor((Math.random() * 255)),
    B = Math.floor((Math.random() * 255)),
    dur = Math.floor((Math.random() * 4000));

      anime({ // <------------------------------------- Anime JS function
        targets: '.element',
        color: `rgb(${R}, ${G}, ${B})`, // <----------- Values I would like to change on each iteration
        duration: dur,  // <--------------------------- Values I would like to change on each iteration
        loop: true,
        easing: 'linear',
        loopComplete: function (anim) { // <----------- Loop iteration callback
          R = Math.floor((Math.random() * 255));
          G = Math.floor((Math.random() * 255));
          B = Math.floor((Math.random() * 255));
          dur = Math.floor((Math.random() * 2000));
          console.log(R + ', ' + G + ', ' + B); // <---- Checked. RGB values are changing on each iteration.
        },
      });

let
  R = Math.floor((Math.random() * 255)),
  G = Math.floor((Math.random() * 255)),
  B = Math.floor((Math.random() * 255)),
  dur = Math.floor((Math.random() * 4000));

anime({
  targets: '#square',
  color: `rgb(${R}, ${G}, ${B})`,
  duration: dur,
  loop: true,
  easing: 'linear',
  loopBegin: function (anim) {
    R = Math.floor((Math.random() * 255));
    G = Math.floor((Math.random() * 255));
    B = Math.floor((Math.random() * 255));
    dur = Math.floor((Math.random() * 2000));
    console.log(R + ', ' + G + ', ' + B);
  },
});
#square {
    font-size: 100px
  }
<script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>


<div id="square">square</div>

0 个答案:

没有答案
相关问题