All files / core/src/RenderingEngine/helpers/stats StatsPanel.ts

0% Statements 0/64
0% Branches 0/4
0% Functions 0/12
0% Lines 0/64

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                                                                                                                                                                                                                                                                                                                                                                                                                                             
import type { Panel } from './types';
import { PANEL_CONFIG } from './constants';
 
/**
 * Individual panel for displaying stats (FPS, MS, MB).
 * Credits: https://github.com/mrdoob/stats.js/blob/master/LICENSE
 */
export class StatsPanel implements Panel {
  public dom: HTMLCanvasElement;
  private context: CanvasRenderingContext2D;
  private minValue = Infinity;
  private maxValue = 0;
 
  private readonly name: string;
  private readonly foregroundColor: string;
  private readonly backgroundColor: string;
  private readonly devicePixelRatio: number;
 
  // Calculated dimensions
  private readonly dimensions: {
    width: number;
    height: number;
    textX: number;
    textY: number;
    graphX: number;
    graphY: number;
    graphWidth: number;
    graphHeight: number;
  };
 
  constructor(name: string, foregroundColor: string, backgroundColor: string) {
    this.name = name;
    this.foregroundColor = foregroundColor;
    this.backgroundColor = backgroundColor;
    this.devicePixelRatio = Math.round(window.devicePixelRatio || 1);
 
    // Calculate dimensions based on device pixel ratio
    this.dimensions = this.calculateDimensions();
 
    // Initialize canvas
    this.dom = this.createCanvas();
    this.context = this.initializeContext();
 
    // Draw initial panel
    this.drawInitialPanel();
  }
 
  /**
   * Updates the panel with a new value.
   */
  public update(value: number, maxValue: number): void {
    this.updateMinMax(value);
    this.clearTextArea();
    this.drawText(value);
    this.scrollGraph();
    this.drawNewValue(value, maxValue);
  }
 
  /**
   * Calculates panel dimensions based on device pixel ratio.
   */
  private calculateDimensions() {
    const pr = this.devicePixelRatio;
    return {
      width: PANEL_CONFIG.WIDTH * pr,
      height: PANEL_CONFIG.HEIGHT * pr,
      textX: PANEL_CONFIG.TEXT_PADDING * pr,
      textY: PANEL_CONFIG.TEXT_Y_OFFSET * pr,
      graphX: PANEL_CONFIG.TEXT_PADDING * pr,
      graphY: PANEL_CONFIG.GRAPH_Y_OFFSET * pr,
      graphWidth: PANEL_CONFIG.GRAPH_WIDTH * pr,
      graphHeight: PANEL_CONFIG.GRAPH_HEIGHT * pr,
    };
  }
 
  /**
   * Creates the canvas element.
   */
  private createCanvas(): HTMLCanvasElement {
    const canvas = document.createElement('canvas');
    canvas.width = this.dimensions.width;
    canvas.height = this.dimensions.height;
    canvas.style.cssText = `width:${PANEL_CONFIG.WIDTH}px;height:${PANEL_CONFIG.HEIGHT}px`;
    return canvas;
  }
 
  /**
   * Initializes the canvas context.
   */
  private initializeContext(): CanvasRenderingContext2D {
    const ctx = this.dom.getContext('2d');
    if (!ctx) {
      throw new Error('Failed to get 2D context');
    }
 
    ctx.font = `bold ${PANEL_CONFIG.FONT_SIZE * this.devicePixelRatio}px ${
      PANEL_CONFIG.FONT_FAMILY
    }`;
    ctx.textBaseline = 'top';
 
    return ctx;
  }
 
  /**
   * Draws the initial panel background and text.
   */
  private drawInitialPanel(): void {
    const {
      width,
      height,
      textX,
      textY,
      graphX,
      graphY,
      graphWidth,
      graphHeight,
    } = this.dimensions;
 
    // Draw background
    this.context.fillStyle = this.backgroundColor;
    this.context.fillRect(0, 0, width, height);
 
    // Draw name
    this.context.fillStyle = this.foregroundColor;
    this.context.fillText(this.name, textX, textY);
 
    // Draw graph outline
    this.context.fillRect(graphX, graphY, graphWidth, graphHeight);
 
    // Draw graph background
    this.context.fillStyle = this.backgroundColor;
    this.context.globalAlpha = PANEL_CONFIG.GRAPH_ALPHA;
    this.context.fillRect(graphX, graphY, graphWidth, graphHeight);
    this.context.globalAlpha = 1;
  }
 
  /**
   * Updates min and max values.
   */
  private updateMinMax(value: number): void {
    this.minValue = Math.min(this.minValue, value);
    this.maxValue = Math.max(this.maxValue, value);
  }
 
  /**
   * Clears the text area for redrawing.
   */
  private clearTextArea(): void {
    const { width, graphY } = this.dimensions;
    this.context.fillStyle = this.backgroundColor;
    this.context.fillRect(0, 0, width, graphY);
  }
 
  /**
   * Draws the current value text.
   */
  private drawText(value: number): void {
    const { textX, textY } = this.dimensions;
    const text = this.formatText(value);
 
    this.context.fillStyle = this.foregroundColor;
    this.context.fillText(text, textX, textY);
  }
 
  /**
   * Formats the display text.
   */
  private formatText(value: number): string {
    const roundedValue = Math.round(value);
    const roundedMin = Math.round(this.minValue);
    const roundedMax = Math.round(this.maxValue);
    return `${roundedValue} ${this.name} (${roundedMin}-${roundedMax})`;
  }
 
  /**
   * Scrolls the graph to the left.
   */
  private scrollGraph(): void {
    const { graphX, graphY, graphWidth, graphHeight } = this.dimensions;
    const pr = this.devicePixelRatio;
 
    this.context.drawImage(
      this.dom,
      graphX + pr,
      graphY,
      graphWidth - pr,
      graphHeight,
      graphX,
      graphY,
      graphWidth - pr,
      graphHeight
    );
  }
 
  /**
   * Draws the new value on the graph.
   */
  private drawNewValue(value: number, maxValue: number): void {
    const { graphX, graphY, graphWidth, graphHeight } = this.dimensions;
    const pr = this.devicePixelRatio;
    const x = graphX + graphWidth - pr;
 
    // Draw full height bar in foreground color
    this.context.fillStyle = this.foregroundColor;
    this.context.fillRect(x, graphY, pr, graphHeight);
 
    // Draw partial height bar in background color
    const normalizedHeight = Math.round((1 - value / maxValue) * graphHeight);
    this.context.fillStyle = this.backgroundColor;
    this.context.globalAlpha = PANEL_CONFIG.GRAPH_ALPHA;
    this.context.fillRect(x, graphY, pr, normalizedHeight);
    this.context.globalAlpha = 1;
  }
}