提交 c4bf00c1 编写于 作者: H hdx

AnimationFrame: 优化更新页面策略

上级 8adff6d4
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<page-head :title="title"></page-head> <page-head :title="title"></page-head>
<button @click="startRequestAnimationFrame">requestAnimationFrame</button> <button @click="startRequestAnimationFrame">requestAnimationFrame</button>
<button @click="stopRequestAnimationFrame">cancelAnimationFrame</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="frame-count">FrameCount: {{testFrameCount}}</text>
<text class="tips">提示: 在当前测试例子中,每增加一次调用 requestAnimationFrame 帧率翻倍,cancelAnimationFrame 后恢复</text> <text class="tips">提示: 在当前测试例子中,每增加一次调用 requestAnimationFrame 帧率翻倍,cancelAnimationFrame 后恢复</text>
</view> </view>
...@@ -14,9 +14,8 @@ ...@@ -14,9 +14,8 @@
data() { data() {
return { return {
title: 'AnimationFrame', title: 'AnimationFrame',
taskId: 0, taskId: 0,
fps: 0, FPSString: '- / -ms',
formatFPSTime: '0ms',
lastTime: 0, lastTime: 0,
frameCount: 0, frameCount: 0,
testFrameCount: 0 testFrameCount: 0
...@@ -30,24 +29,25 @@ ...@@ -30,24 +29,25 @@
methods: { methods: {
startRequestAnimationFrame() { startRequestAnimationFrame() {
this.taskId = requestAnimationFrame((timestamp : number) => { this.taskId = requestAnimationFrame((timestamp : number) => {
this.frameCount++ this.updateFPS(timestamp)
if (timestamp - this.lastTime >= 1000) {
this.formatFPSTime = (1000 / this.frameCount).toFixed(3) + 'ms'
this.fps = this.frameCount
this.frameCount = 0
this.lastTime = timestamp
}
this.testFrameCount++ this.testFrameCount++
this.startRequestAnimationFrame() this.startRequestAnimationFrame()
}) })
}, },
stopRequestAnimationFrame() { stopRequestAnimationFrame() {
this.fps = 0 cancelAnimationFrame(this.taskId)
this.formatFPSTime = '0ms'
this.lastTime = 0 this.lastTime = 0
this.frameCount = 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.
先完成此消息的编辑!
想要评论请 注册