@itwin/frontend-devtools
Version:
Debug menu and supporting UI widgets
57 lines • 2.03 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Widgets
*/
import { PerformanceMetrics } from "@itwin/core-frontend";
import { createCheckBox } from "../ui/CheckBox";
/** Displays average frames-per-second.
* NOTE: Enabling fps tracking causes a new frame to render on every tick of the render loop, which may negatively impact battery life.
* @beta
*/
export class FpsTracker {
_label;
_metrics;
_curIntervalId;
_vp;
constructor(parent, viewport) {
this._vp = viewport;
this._label = createCheckBox({
parent,
name: "Track FPS",
id: "fpsTracker_toggle",
handler: (cb) => this.toggle(cb.checked),
}).label;
}
[Symbol.dispose]() {
this.toggle(false);
}
clearInterval() {
if (undefined !== this._curIntervalId) {
window.clearInterval(this._curIntervalId);
this._curIntervalId = undefined;
}
}
toggle(enabled) {
this._vp.continuousRendering = enabled;
if (enabled) {
this._metrics = new PerformanceMetrics(false, true);
this._curIntervalId = window.setInterval(() => this.updateFPS(), 500);
this._label.innerText = "Tracking FPS...";
}
else {
this._metrics = undefined;
this.clearInterval();
this._label.innerText = "Track FPS";
}
this._vp.target.performanceMetrics = this._metrics;
}
updateFPS() {
const metrics = this._metrics;
const fps = (metrics.spfTimes.length / metrics.spfSum).toFixed(2);
this._label.innerText = `FPS: ${fps}`;
}
}
//# sourceMappingURL=FpsTracker.js.map