Вычислить FPS в Canvas с помощью requestAnimationFrame

Как я могу рассчитать FPS для игрового приложения на холсте? Я видел несколько примеров, но ни один из них не использует requestAnimationFrame, и я не уверен, как применить там свои решения. Это мой код:

 (function (window, document, undefined) {var canvas = document.getElementById ("mycanvas"), context = canvas.getContext ("2d"), width = canvas.width, height = canvas .height, fps = 0, game_running = true, show_fps = true; function showFPS () {context.fillStyle = "Black"; context.font = "normal 16pt Arial"; context.fillText (fps + "fps", 10, 26);} function gameLoop () {// Очистить контекст экрана .clearRect (0, 0, width, height); if (show_fps) showFPS (); if (game_running) requestAnimationFrame (gameLoop);} gameLoop ();} ( this.document)) 
 canvas {border: 3px solid # fd3300; } 
   

Кстати, есть ли какая-нибудь библиотека, которую я мог бы добавить для повышения производительности?

33
задан colxi 12 April 2019 в 03:53
поделиться