如何在 Class 对象中使用 requestAnimationFrame

ici*_*ing 4 javascript oop animation scope requestanimationframe

我有一个类需要一些坐标和持续时间数据。我想用它来为svg. 更明确地说,我想使用该数据svg在时间范围内更改属性。

我在课外使用一个step函数requestAnimationFrame

function step(timestamp) {
  if (!start) start = timestamp
  var progress =  timestamp - start;
  var currentX = parseInt(document.querySelector('#start').getAttribute('cx'));
  var moveX =  distancePerFrame(circleMove.totalFrames(), circleMove.xLine);

  document.querySelector('#start').setAttribute('cx', currentX + moveX);
  if (progress < circleMove.duration) {
    window.requestAnimationFrame(step);
  }
}

var circleMove = new SingleLineAnimation(3000, startXY, endXY)

var start = null

function runProgram() {
  window.requestAnimationFrame(step);
}
Run Code Online (Sandbox Code Playgroud)

我可以把它的方法,取代了circleLinethis。这对于第一次运行很好,但是当它this.step第二次调用回调时,好吧,我们处于回调黑洞中,并且对 的引用this被破坏了。做旧的self = this也行不通,一旦我们进入回调this是未定义的(我不知道为什么)。这是一种方法:

step(timestamp) {
  var self = this;

  if (!start) start = timestamp
  var progress =  timestamp - start;
  var currentX = parseInt(document.querySelector('#start').getAttribute('cx'));
  var moveX =  distancePerFrame(self.totalFrames(), self.xLine);

  document.querySelector('#start').setAttribute('cx', currentX + moveX);
  if (progress < self.duration) {
    window.requestAnimationFrame(self.step);
  }
}
Run Code Online (Sandbox Code Playgroud)

关于如何将“布线”保持在对象内部的任何想法?

这是或多或少与step类外部定义的函数一起使用的代码。

class SingleLineAnimation { 
  constructor(duration, startXY, endXY) {
    this.duration = duration;
    this.xLine = [ startXY[0], endXY[0] ];
    this.yLine = [ startXY[1], endXY[1] ];
  }

  totalFrames(framerate = 60) { // Default to 60htz ie, 60 frames per second
    return Math.floor(this.duration * framerate / 1000);
  } 

  frame(progress) {
    return this.totalFrames() - Math.floor((this.duration - progress) / 17 );
  } 
}
Run Code Online (Sandbox Code Playgroud)

这也将被插入到类中,现在它只是一个辅助函数:

function distancePerFrame(totalFrames, startEndPoints) {
  return totalFrames > 0 ? Math.floor(Math.abs(startEndPoints[0] - startEndPoints[1]) / totalFrames) : 0;
}
Run Code Online (Sandbox Code Playgroud)

然后单击一个按钮...

function runProgram() {
  window.requestAnimationFrame(step);
}
Run Code Online (Sandbox Code Playgroud)

Aln*_*tak 7

您需要将requestAnimationFrame回调函数绑定到上下文。这样做的规范方法是这样的:

window.requestAnimationFrame(this.step.bind(this))
Run Code Online (Sandbox Code Playgroud)

但这并不理想,因为您.bind每帧一次又一次地重复调用和创建新的函数引用。

如果你有一个本地范围的变量设置为this.step.bind(this)你可以传递它并避免持续重新绑定。

另一种选择是这样的:

function animate() {

    var start = performance.now();
    el = document.querySelector('#start');

    // use var self = this if you need to refer to `this` inside `frame()`

    function frame(timestamp) {

        var progress =  timestamp - start;
        var currentX = parseInt(el.getAttribute('cx'));
        var moveX =  distancePerFrame(circleMove.totalFrames(), circleMove.xLine);
        el.setAttribute('cx', currentX + moveX);

        if (progress < circleMove.duration) {
            window.requestAnimationFrame(frame);
        }
    }

    window.requestAnimationFrame(frame);
}
Run Code Online (Sandbox Code Playgroud)

即您正在设置初始状态,然后在一个纯本地范围的函数中执行动画,该函数被requestAnimationFrame.

注意:如果您无意中同时调用了另一个启动动画的函数,任一版本的代码都会产生不良交互。