【问题标题】:How can I chain animations in anime.js?如何在anime.js中链接动画?
【发布时间】:2019-09-28 15:13:39
【问题描述】:

有没有什么方法可以在anime.js 中链接动画或有队列/动画组,我可以等待以便继续其他动画?

【问题讨论】:

    标签: javascript anime.js


    【解决方案1】:

    每个带有anime 的动画都会返回一个promise,因此您可以将async/awaitPromise.all 结合使用,不过请记住,Promise.all 可以让所有动画同时运行。例如,假设您要同时运行 3 个动画,然后在该组完成后,再制作一个动画:

    async function animateLockAndBackground() {
      const bigLockAnimation = anime({
        targets: '#big-lock',
        strokeDashoffset: [0, 5],
        easing: 'easeInOutSine',
        duration: 250,
        easing: 'easeInSine'
      }).finished;
    
      const lockLineAnimation = anime({
        targets: '#lock-line',
        strokeDashoffset: [0, 3],
        translateY: [{
            value: '-2px',
            duration: 350,
            easing: 'easeOutExpo'
          },
          {
            value: '2px',
            duration: 350,
            easing: 'easeOutExpo'
          },
          {
            value: '-2px',
            duration: 350,
            easing: 'easeOutExpo'
          },
        ],
      }).finished;
    
      const innerCircleAnimation = anime({
        targets: '#inner-circle',
        translateY: [{
            value: '-1px',
            duration: 250,
            easing: 'easeOutExpo'
          },
          {
            value: '1px',
            duration: 250,
            easing: 'easeOutExpo'
          },
          {
            value: '-1px',
            duration: 250,
            easing: 'easeOutExpo'
          },
          {
            value: 0,
            duration: 250,
            easing: 'easeOutExpo'
          },
        ],
      }).finished;
    
      await Promise.all([bigLockAnimation, lockLineAnimation, innerCircleAnimation]);
    }
    
    animateLockAndBackground().then(() => {
      console.log('First animation finished.');
      anime({
        targets: '.plugins-not-installed-text',
        translateY: [{
          value: '10px',
          duration: 750
        }]
      });
      anime({
        targets: '#lock-wrapper',
        translateY: [{
          value: '-10px',
          duration: 750
        }]
      });
      anime({
        targets: '#plugins-not-installed-screen',
        opacity: 0,
        duration: 500,
        easing: 'linear'
      }).finished.then(() => {
        console.log('Second animation finished.');
      });
    });
    #plugins-not-installed-screen {
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
    }
    
    #plugins-not-installed-screen .upper {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        z-index: 2;
        padding: 24px 48px;
        background-image: url('../Images/component.png');
        background-repeat: repeat;
    }
    
    .plugins-not-installed-text {
      font-size: 15px;
      text-align: center;
    }
    
    #lock {
      display: block;
      width: 50px;
      height: 65px;
    }
    
    #plugins-not-installed-screen #lock {}
    
    #plugins-not-installed-screen #big-lock {
      stroke-dasharray: 61 62;
      stroke-dashoffset: 5;
      /* go to 5 */
    }
    
    #plugins-not-installed-screen #lock-line {
      stroke-dasharray: 31 33;
      stroke-dashoffset: 0;
      /* go to 3 */
    }
    
    #components-to-install-list {
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      width: 100%;
    }
    
    .install-component-individual {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      padding: 12px 0;
      border-bottom: 1px solid rgba(0, 0, 0, 0.1);
      width: 100%;
    }
    <script src="https://cdn.jsdelivr.net/npm/animejs@3.1.0/lib/anime.min.js"></script>
    <div id="plugins-not-installed-screen" class="">
      <div class="upper">
        <div id="lock-wrapper">
          <svg version="1.1" id="lock" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 25 35" style="enable-background:new 0 0 25 35;" xml:space="preserve">
              <style type="text/css">
                  #big-lock{fill:none;stroke:#686868;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;}
                  #inner-circle{fill:none;stroke:#686868;stroke-width:2;stroke-linecap:round;stroke-miterlimit:10;}
                  #lock-line{fill:none;stroke:#686868;stroke-width:2;stroke-linecap:round;stroke-miterlimit:10;}
              </style>
              <path id="big-lock" d="M4.4,13.5c-1.2,0.8-2,2.1-2,3.6v4c0,2.8,1.1,5.4,3.1,7.4c1.9,1.9,4.5,2.9,7.2,2.9
                  c0.1,0,0.2,0,0.3,0c5.5-0.1,10-4.9,10-10.5v-3.8c0.1-1.8-0.9-3.3-2.4-4l-6.5-2.7c-0.8-0.3-1.8-0.4-2.6,0L10.1,11"/>
              <circle id="inner-circle" cx="12.7" cy="21.9" r="2.9"/>
              <path id="lock-line" d="M7.1,15.1V9.9c0-3.1,2.5-5.6,5.6-5.6h0c3.1,0,5.6,2.5,5.6,5.6v8"/>
          </svg>
        </div>
        <h5 class="plugins-not-installed-text">Plugins not installed.</h5>
      </div>
    </div>

    那么,如果您想在该函数中让 lock-line 动画同时其他 2 个元素正在动画,那么您只有 2 个动画时间轴而不是 3 个动画时间轴?这里:

    async function animateLockAndBackground() {
        const bigLockAnimation = anime({
            targets: '#big-lock',
            strokeDashoffset: [0, 5],
            easing: 'easeInOutSine',
            duration: 250,
            easing: 'easeInSine'
        }).finished;
    
        const innerCircleAnimation = anime({
            targets: '#inner-circle',
            translateY: [
                {value: '-1px', duration: 250, easing: 'easeOutExpo'},
                {value: '1px', duration: 250, easing: 'easeOutExpo'},
                {value: '-1px', duration: 250, easing: 'easeOutExpo'},
                {value: 0, duration: 250, easing: 'easeOutExpo'},
            ],
        }).finished;
    
        await Promise.all([bigLockAnimation, innerCircleAnimation]);
    }
    
    animateLockAndBackground().then(() => {
        return anime({
            targets: '#lock-line',
            strokeDashoffset: [0, 3],
            translateY: [
                {value: '-2px', duration: 350, easing: 'easeOutExpo'},
                {value: '2px', duration: 350, easing: 'easeOutExpo'},
                {value: '-2px', duration: 350, easing: 'easeOutExpo'},
            ],
        }).finished;
    }).then(() => {
        anime({
            targets: '.plugins-not-installed-text',
            translateY: [
                {value: '10px', duration: 750}
            ]
        });
        anime({
            targets: '#lock-wrapper',
            translateY: [
                {value: '-10px', duration: 750}
            ]
        });
        anime({
            targets: '#plugins-not-installed-screen',
            opacity: 0,
            duration: 500,
            easing: 'linear'
        });
    });
    

    我们将 lock-line 动画移到原始组之外,让它等待该组,然后在 lock-line 之后出现的任何其他动画。

    您应该将动画视为可以链接的简单承诺。

    【讨论】:

      【解决方案2】:

      试试时间线功能:https://animejs.com/documentation/#timelineBasics 如果你愿意,你可以自己做所有的承诺,但动漫已经处理了它,所以它真的没有任何意义。制作时间线函数将动画链接在一起,您甚至可以通过在 .add 函数 .add({ //your animation }, time) 或相对偏移量 .add({ //your animation }, -/+ = time) 中添加时间来强制它在前一个动画完成之前完成(*加或减不是两者)

      【讨论】:

      • 我觉得这是这个问题的现代解决方案。
      猜你喜欢
      • 2021-10-08
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      相关资源
      最近更新 更多