我有一个redux应用程序,它使用
在React开发模式下: 在React
以下是处理在每个tick上分派我的redux调用的动画代码。
什么原因会导致Chrome这样做,如何使每个渲染帧只有一个一致的
requestAnimationFrame
在每个TICK上呈现新状态,然而,Google Chrome似乎会为某些ticks调用requestAnimationFrame
多达3次(!!) ,而对于其他一些ticks则根本没有调用,导致动画效果不流畅。在React开发模式下: 在React
NODE_ENV=production
模式下:
已经以60fps的速度进行渲染,因此每16ms帧中多余的多个requestAnimationFrame
回调对CPU资源极为浪费。 没有tick的帧也会导致动画效果不流畅。 我的tick函数只需要4ms就可以计算出(远低于16ms的预算),且页面上只有一个回调在运行。以下是处理在每个tick上分派我的redux调用的动画代码。
class AnimationHandler {
constructor(store) {
this.store = store
this.animating = false
store.subscribe(::this.handleStateChange)
}
handleStateChange() {
if (!this.animating) {
this.tick()
}
}
tick(timestamp) {
this.animating = true
this.store.dispatch({
type: 'TICK',
current_timestamp: (new Date).getTime(),
// reducer handles updating rendered state to this point in time
})
window.requestAnimationFrame(::this.tick)
}
}
window.animations = new AnimationHandler(window.store) // redux store
什么原因会导致Chrome这样做,如何使每个渲染帧只有一个一致的
tick()
调用,不多也不少?
NODE_ENV=production
环境变量来编译React的生产模式时,情况有所改善。不幸的是,我从未找到根本原因。 - Nick Sweeting