提交 14efaf36 编写于 作者: H hdx

AnimationFrame: 优化更新页面策略

上级 d8e6dace
......@@ -3,7 +3,7 @@
<page-head :title="title"></page-head>
<button @click="startRequestAnimationFrame">requestAnimationFrame</button>
<button @click="stopRequestAnimationFrame">cancelAnimationFrame</button>
<text class="frame-count">FPS: {{fps}} / {{formatFPSTime}}</text>
<text class="frame-count">FPS: {{FPSString}}</text>
<text class="frame-count">FrameCount: {{testFrameCount}}</text>
<text class="tips">提示: 在当前测试例子中,每增加一次调用 requestAnimationFrame 帧率翻倍,cancelAnimationFrame 后恢复</text>
</view>
......@@ -15,8 +15,7 @@
return {
title: 'AnimationFrame',
taskId: 0,
fps: 0,
formatFPSTime: '0ms',
FPSString: '- / -ms',
lastTime: 0,
frameCount: 0,
testFrameCount: 0
......@@ -30,24 +29,25 @@
methods: {
startRequestAnimationFrame() {
this.taskId = requestAnimationFrame((timestamp : number) => {
this.frameCount++
if (timestamp - this.lastTime >= 1000) {
this.formatFPSTime = (1000 / this.frameCount).toFixed(3) + 'ms'
this.fps = this.frameCount
this.frameCount = 0
this.lastTime = timestamp
}
this.updateFPS(timestamp)
this.testFrameCount++
this.startRequestAnimationFrame()
})
},
stopRequestAnimationFrame() {
this.fps = 0
this.formatFPSTime = '0ms'
cancelAnimationFrame(this.taskId)
this.lastTime = 0
this.frameCount = 0
cancelAnimationFrame(this.taskId)
this.FPSString = '- / -ms'
},
updateFPS(timestamp : number) {
this.frameCount++
if (timestamp - this.lastTime >= 1000) {
const timeOfFrame = (1000 / this.frameCount)
this.FPSString = `${this.frameCount} / ${timeOfFrame.toFixed(3)}ms`
this.frameCount = 0
this.lastTime = timestamp
}
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册