$('body').before('<div id="fps" style="z-index:10000;position:fixed;top:3;left:3;font-weight:bold;"></div>');
var showFPS = (function () {
var requestAnimationFrame =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
var e, pe, pid, fps, last, offset, step, appendFps;
fps = 0;
last = Date.now();
step = function () {
offset = Date.now() - last;
fps += 1;
if (offset >= 1000) {
last += offset;
appendFps(fps);
fps = 0;
}
requestAnimationFrame(step);
};
appendFps = function (fps) {
console.log(fps + 'FPS');
$('#fps').html(fps + 'FPS');
};
step();
})();
在你的项目中直接加入这个代码就可以在你的页面之中添加上FPS显示,如果想在vue或者react中使用这个FPS你可以将以上代码做成一个npm包或者上传至npmjs服务器,就可以直接npm i 进行本地安装使用