【问题标题】:Web Animations API - Setting Keyframes?Web Animations API - 设置关键帧?
【发布时间】:2019-11-27 15:00:54
【问题描述】:

我一直在使用 Web Animations API,发现它非常直观。
从我的角度来看,我想制作一个非常简单的动画,其中一个元素沿一个方向旋转,并且在按下按钮后沿另一个方向旋转。使用标准反向方法时,动画会因为时间用完而停止。

我已经尝试使用 setKeyframes 方法来改变变换方向,这在 Chrome 中似乎不起作用。在我的 Firefox 开发者版中,它工作得很好......

有人可以查看我的方法并告诉我是否遗漏了什么吗? 请记住,总的来说,我对 JS 很陌生。

提前致谢,
尼克拉斯

JSFiddle with my code

//This is what my JS Code looks like
//I have commented out the parts that throw a error inside Chrome
var fan = document.getElementById('fan');

var cw = new KeyframeEffect(
  fan, [{
      transform: "rotate(0)"
    },
    {
      transform: "rotate(360deg)",
    }
  ], {
    // timing options
    duration: 8000,
    iterations: Infinity,
  });

//creates new Animation object and starts it in clockwise rotation
var fananim = new Animation(cw, document.timeline);

//Stops Animation so it doesnt run automatically
fananim.pause();


var clockwise = function() {
 /* cw.setKeyframes(
    [{
        transform: "rotate(0)"
      },
      {
        transform: "rotate(360deg)",
      }
    ]
  ); */
  fananim.play();
}

var counterclockwise = function() {
  /* cw.setKeyframes(
    [{
        transform: "rotate(0)"
      },
      {
        transform: "rotate(-360deg)",
      }
    ]
  ); */
  fananim.play();

  //this leads to the time running out and the animation stopping
  //fananim.reverse();
}


var stop = function() {
  fananim.pause();
}

// Click Handlers for Buttons on Control Baord
var ela = document.getElementById("cw");
ela.addEventListener("click", clockwise, false);

var elb = document.getElementById("ccw");
elb.addEventListener("click", counterclockwise, false);

var elc = document.getElementById("stop");
elc.addEventListener("click", stop, false);

【问题讨论】:

    标签: javascript web-animations web-animations-api


    【解决方案1】:

    您是否在启用了实验性 Web 平台功能的 Chrome Canary 中进行测试?如果没有,setKeyframes 可能不可用。

    对于它的价值,动画似乎对我有用,只需进行一些调整:

    var fan = document.getElementById('fan');
    
    var cw = new KeyframeEffect(
      fan,
      [
        {
          transform: 'rotate(0)',
        },
        {
          transform: 'rotate(360deg)',
        },
      ],
      {
        // timing options
        duration: 8000,
        iterations: Infinity,
      }
    );
    
    // Creates new Animation object and starts it in clockwise rotation
    // NOTE(BB): No need for document.timeline, it's the default.
    var fananim = new Animation(cw /*, document.timeline*/);
    
    // Stops Animation so it doesnt run automatically
    // NOTE(BB): It only runs automatically if you use Element.animate()
    // fananim.pause();
    
    var clockwise = function() {
      cw.setKeyframes([
        {
          transform: 'rotate(0)',
        },
        {
          transform: 'rotate(360deg)',
        },
      ]);
      fananim.play();
    };
    
    var counterclockwise = function() {
      cw.setKeyframes([
        {
          transform: 'rotate(0)',
        },
        {
          transform: 'rotate(-360deg)',
        },
      ]);
      fananim.play();
    };
    
    var stop = function() {
      fananim.pause();
    };
    
    // Click Handlers for Buttons on Control Baord
    var ela = document.getElementById('cw');
    ela.addEventListener('click', clockwise, false);
    
    var elb = document.getElementById('ccw');
    elb.addEventListener('click', counterclockwise, false);
    
    var elc = document.getElementById('stop');
    elc.addEventListener('click', stop, false);
    

    JSFiddle

    但是,停止方法似乎不适用于 Chrome Canary。我会联系一些 Chrome 人员,看看发生了什么。

    关于我的调整的几点说明:

    • 不需要pause() 使用new Animation() 创建的动画。仅当您使用 elem.animate(...) 创建动画并自动播放时才需要它。

      [但是,调用pause() 会将其置于暂停状态而不是空闲状态,如果动画的第一帧与元素的现有状态不同,这可能很有用。]

    • 您无需在构造函数中将document.timeline 指定为Animation(),因为它是默认值。

    您还可以使用cw.updateTiming({ direction: 'reverse' }) 更改动画的方向。但是,这种方法和setKeyframes() 方法都会使动画跳转。我可能会尝试深入了解动画并使用reverse()。即使动画在另一个线程上运行,这也会产生平滑的更新,并且还具有在 Chrome 和 Firefox 的发布版本中提供 API 子集的优势。例如,

    const fan = document.getElementById('fan');
    const fananim = fan.animate(
      [
        {
          transform: 'rotate(0)',
        },
        {
          transform: 'rotate(360deg)',
        },
      ],
      {
        duration: 8000,
        iterations: Infinity,
      }
    );
    
    fananim.pause();
    fananim.startTime = -1000 * 1000;
    
    function clockwise() {
      if (fananim.playbackRate < 0) {
        fananim.reverse();
      }
      if (fananim.playState !== 'running') {
        fananim.play();
      }
    }
    
    function counterclockwise() {
      if (fananim.playbackRate > 0) {
        fananim.reverse();
      }
      if (fananim.playState !== 'running') {
        fananim.play();
      }
    }
    
    function stop() {
      fananim.pause();
    }
    
    const ela = document.getElementById('cw');
    ela.addEventListener('click', clockwise, false);
    
    const elb = document.getElementById('ccw');
    elb.addEventListener('click', counterclockwise, false);
    
    const elc = document.getElementById('stop');
    elc.addEventListener('click', stop, false);
    

    JSFiddle

    但是,我仍然在 Chrome Canary 中遇到了这个版本的一些问题。希望 Chrome 的人能弄清楚发生了什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多