提交 cf185672 编写于 作者: H hdx

cancelAnimationFrame: 增加帧率计算并显示

上级 283a36b9
...@@ -3,7 +3,9 @@ ...@@ -3,7 +3,9 @@
<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">{{testFrameCount}}</text> <text class="frame-count">FPS: {{fps}}</text>
<text class="frame-count">FrameCount: {{testFrameCount}}</text>
<text class="tips">提示: 在当前测试例子中,调用一次 requestAnimationFrame 帧率翻倍,cancelAnimationFrame 后恢复</text>
</view> </view>
</template> </template>
...@@ -13,6 +15,9 @@ ...@@ -13,6 +15,9 @@
return { return {
title: 'cancelAnimationFrame', title: 'cancelAnimationFrame',
taskId: 0, taskId: 0,
lastTime: 0,
frameCount: 0,
fps: 0,
testFrameCount: 0 testFrameCount: 0
} }
}, },
...@@ -23,8 +28,14 @@ ...@@ -23,8 +28,14 @@
}, },
methods: { methods: {
startRequestAnimationFrame() { startRequestAnimationFrame() {
this.taskId = requestAnimationFrame((_ : number) => { this.taskId = requestAnimationFrame((timestamp : number) => {
this.testFrameCount++ this.testFrameCount++
this.frameCount++
if (timestamp - this.lastTime >= 1000) {
this.fps = this.frameCount
this.frameCount = 0
this.lastTime = timestamp
}
this.startRequestAnimationFrame() this.startRequestAnimationFrame()
}) })
}, },
...@@ -42,5 +53,11 @@ ...@@ -42,5 +53,11 @@
.frame-count { .frame-count {
margin-top: 15px; margin-top: 15px;
}
.tips {
font-size: 12px;
margin-top: 30px;
opacity: 0.7;
} }
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册