Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x 428x | import { StatsPanel } from './StatsPanel'; import type { Panel, StatsInstance, PerformanceWithMemory } from './types'; import { PanelType } from './enums'; import { STATS_CONFIG, PANEL_CONFIGS, CONVERSION } from './constants'; /** * Singleton class for managing the stats overlay. * Provides FPS, MS, and memory usage monitoring. * Credits: https://github.com/mrdoob/stats.js/blob/master/LICENSE */ export class StatsOverlay implements StatsInstance { private static instance: StatsOverlay | null = null; public dom: HTMLDivElement | null = null; private currentMode = 0; private startTime: number = 0; private lastUpdateTime: number = 0; private frameCount = 0; private panels: Map<PanelType, Panel> = new Map(); private animationFrameId: number | null = null; private isSetup = false; private constructor() {} /** * Gets the singleton instance of StatsOverlay. */ public static getInstance(): StatsOverlay { Eif (!StatsOverlay.instance) { StatsOverlay.instance = new StatsOverlay(); } return StatsOverlay.instance; } /** * Sets up the stats overlay and starts the animation loop. */ public setup(): void { if (this.isSetup) { return; } try { // Initialize DOM and timing this.dom = this.createOverlayElement(); this.startTime = performance.now(); this.lastUpdateTime = this.startTime; // Initialize panels and show default this.initializePanels(); this.showPanel(PanelType.FPS); // Apply styles and add to DOM this.applyOverlayStyles(); document.body.appendChild(this.dom); this.startLoop(); this.isSetup = true; } catch (error) { console.warn('Failed to setup stats overlay:', error); } } /** * Cleans up the stats overlay by removing it from the DOM and stopping the animation loop. */ public cleanup(): void { this.stopLoop(); if (this.dom && this.dom.parentNode) { this.dom.parentNode.removeChild(this.dom); } this.dom = null; this.panels.clear(); this.isSetup = false; } /** * Shows a specific panel by its type. */ public showPanel(panelType: number): void { const children = Array.from(this.dom.children) as HTMLElement[]; children.forEach((child, index) => { child.style.display = index === panelType ? 'block' : 'none'; }); this.currentMode = panelType; } /** * Updates the stats display. */ public update(): void { this.startTime = this.updateStats(); } /** * Creates the overlay DOM element. */ private createOverlayElement(): HTMLDivElement { const element = document.createElement('div'); element.addEventListener('click', this.handleClick.bind(this), false); return element; } /** * Applies styles to the overlay element. */ private applyOverlayStyles(): void { Object.assign(this.dom.style, STATS_CONFIG.OVERLAY_STYLES); } /** * Handles click events on the overlay. */ private handleClick(event: MouseEvent): void { event.preventDefault(); const panelCount = this.dom.children.length; this.showPanel((this.currentMode + 1) % panelCount); } /** * Initializes all panels. */ private initializePanels(): void { // Always create FPS and MS panels const fpsPanel = new StatsPanel( PANEL_CONFIGS[PanelType.FPS].name, PANEL_CONFIGS[PanelType.FPS].foregroundColor, PANEL_CONFIGS[PanelType.FPS].backgroundColor ); this.addPanel(PanelType.FPS, fpsPanel); const msPanel = new StatsPanel( PANEL_CONFIGS[PanelType.MS].name, PANEL_CONFIGS[PanelType.MS].foregroundColor, PANEL_CONFIGS[PanelType.MS].backgroundColor ); this.addPanel(PanelType.MS, msPanel); // Only create memory panel if available if (this.isMemoryAvailable()) { const memPanel = new StatsPanel( PANEL_CONFIGS[PanelType.MEMORY].name, PANEL_CONFIGS[PanelType.MEMORY].foregroundColor, PANEL_CONFIGS[PanelType.MEMORY].backgroundColor ); this.addPanel(PanelType.MEMORY, memPanel); } } /** * Checks if memory monitoring is available. */ private isMemoryAvailable(): boolean { const perf = performance as PerformanceWithMemory; return perf.memory !== undefined; } /** * Adds a panel to the overlay. */ private addPanel(type: PanelType, panel: Panel): void { this.dom.appendChild(panel.dom); this.panels.set(type, panel); } /** * Starts the animation frame loop. */ private startLoop(): void { const loop = () => { this.update(); this.animationFrameId = requestAnimationFrame(loop); }; this.animationFrameId = requestAnimationFrame(loop); } /** * Stops the animation frame loop. */ private stopLoop(): void { if (this.animationFrameId !== null) { cancelAnimationFrame(this.animationFrameId); this.animationFrameId = null; } } /** * Updates all stats panels. */ private updateStats(): number { this.frameCount++; const currentTime = performance.now(); const deltaTime = currentTime - this.startTime; // Update MS panel const msPanel = this.panels.get(PanelType.MS); if (msPanel) { msPanel.update(deltaTime, STATS_CONFIG.MAX_MS_VALUE); } // Update FPS panel every second if (currentTime >= this.lastUpdateTime + STATS_CONFIG.UPDATE_INTERVAL) { const fps = (this.frameCount * CONVERSION.MS_PER_SECOND) / (currentTime - this.lastUpdateTime); const fpsPanel = this.panels.get(PanelType.FPS); if (fpsPanel) { fpsPanel.update(fps, STATS_CONFIG.MAX_FPS_VALUE); } this.lastUpdateTime = currentTime; this.frameCount = 0; // Update memory panel if available this.updateMemoryPanel(); } return currentTime; } /** * Updates the memory panel if available. */ private updateMemoryPanel(): void { const memPanel = this.panels.get(PanelType.MEMORY); if (!memPanel) { return; } const perf = performance as PerformanceWithMemory; if (perf.memory) { const memoryMB = perf.memory.usedJSHeapSize / CONVERSION.BYTES_TO_MB; const maxMemoryMB = perf.memory.jsHeapSizeLimit / CONVERSION.BYTES_TO_MB; memPanel.update(memoryMB, maxMemoryMB); } } } |