stats-gl 2.4.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/panel.ts CHANGED
@@ -40,6 +40,7 @@ class Panel {
40
40
  this.canvas.style.cssText = 'width:90px;height:48px';
41
41
 
42
42
  this.context = this.canvas.getContext('2d');
43
+
43
44
  this.initializeCanvas();
44
45
  }
45
46
 
@@ -57,17 +58,17 @@ class Panel {
57
58
  const endColor: string = this.fg;
58
59
 
59
60
  switch (this.fg.toLowerCase()) {
60
- case '#0ff': // Cyan
61
- startColor = '#006666'; // Dark Cyan
61
+ case '#0ff':
62
+ startColor = '#006666';
62
63
  break;
63
- case '#0f0': // Green
64
- startColor = '#006600'; // Dark Green
64
+ case '#0f0':
65
+ startColor = '#006600';
65
66
  break;
66
- case '#ff0': // Yellow
67
- startColor = '#666600'; // Dark Yellow
67
+ case '#ff0':
68
+ startColor = '#666600';
68
69
  break;
69
- case '#e1e1e1': // Light Gray
70
- startColor = '#666666'; // Medium Gray
70
+ case '#e1e1e1':
71
+ startColor = '#666666';
71
72
  break;
72
73
  default:
73
74
  startColor = this.bg;
@@ -83,44 +84,35 @@ class Panel {
83
84
  private initializeCanvas() {
84
85
  if (!this.context) return;
85
86
 
87
+ this.context.imageSmoothingEnabled = false;
88
+
86
89
  this.context.font = 'bold ' + (9 * this.PR) + 'px Helvetica,Arial,sans-serif';
87
90
  this.context.textBaseline = 'top';
88
91
 
89
- // Create gradient
90
92
  this.gradient = this.createGradient();
91
93
 
92
- // Fill background
93
94
  this.context.fillStyle = this.bg;
94
95
  this.context.fillRect(0, 0, this.WIDTH, this.HEIGHT);
95
96
 
96
- // Draw text
97
97
  this.context.fillStyle = this.fg;
98
98
  this.context.fillText(this.name, this.TEXT_X, this.TEXT_Y);
99
99
 
100
- // Draw initial graph area
101
100
  this.context.fillStyle = this.fg;
102
101
  this.context.fillRect(this.GRAPH_X, this.GRAPH_Y, this.GRAPH_WIDTH, this.GRAPH_HEIGHT);
103
102
 
104
- // Apply semi-transparent background
105
103
  this.context.fillStyle = this.bg;
106
104
  this.context.globalAlpha = 0.9;
107
105
  this.context.fillRect(this.GRAPH_X, this.GRAPH_Y, this.GRAPH_WIDTH, this.GRAPH_HEIGHT);
108
106
  }
109
107
 
110
- update(
111
- value: number,
112
- valueGraph: number,
113
- maxValue: number,
114
- maxGraph: number,
115
- decimals = 0
116
- ) {
108
+ // Update only text portion
109
+ update(value: number, valueGraph: number, maxValue: number, maxGraph: number, decimals = 0) {
117
110
  if (!this.context || !this.gradient) return;
118
111
 
119
112
  const min = Math.min(Infinity, value);
120
113
  const max = Math.max(maxValue, value);
121
- maxGraph = Math.max(maxGraph, valueGraph);
122
114
 
123
- // Clear and redraw background
115
+ // Clear only the text area (from top to GRAPH_Y)
124
116
  this.context.globalAlpha = 1;
125
117
  this.context.fillStyle = this.bg;
126
118
  this.context.fillRect(0, 0, this.WIDTH, this.GRAPH_Y);
@@ -128,40 +120,74 @@ class Panel {
128
120
  // Draw text
129
121
  this.context.fillStyle = this.fg;
130
122
  this.context.fillText(
131
- `${value.toFixed(decimals)} ${this.name} (${min.toFixed(decimals)}-${parseFloat(
132
- max.toFixed(decimals)
133
- )})`,
123
+ `${value.toFixed(decimals)} ${this.name} (${min.toFixed(decimals)}-${parseFloat(max.toFixed(decimals))})`,
134
124
  this.TEXT_X,
135
125
  this.TEXT_Y
136
126
  );
127
+ }
128
+
129
+ // Update only graph portion
130
+ updateGraph(valueGraph: number, maxGraph: number) {
131
+ if (!this.context || !this.gradient) return;
132
+
133
+ // Handle zero values appropriately
134
+ if (valueGraph === 0 && maxGraph === 0) {
135
+ maxGraph = 1; // Prevent division by zero
136
+ }
137
+
138
+ // Ensure maxGraph is valid and values are positive
139
+ maxGraph = Math.max(maxGraph, valueGraph, 0.1);
140
+ valueGraph = Math.max(valueGraph, 0);
141
+
142
+ // Ensure all coordinates are rounded to avoid sub-pixel rendering
143
+ const graphX = Math.round(this.GRAPH_X);
144
+ const graphY = Math.round(this.GRAPH_Y);
145
+ const graphWidth = Math.round(this.GRAPH_WIDTH);
146
+ const graphHeight = Math.round(this.GRAPH_HEIGHT);
147
+ const pr = Math.round(this.PR);
137
148
 
138
149
  // Shift the graph left
139
150
  this.context.drawImage(
140
151
  this.canvas,
141
- this.GRAPH_X + this.PR,
142
- this.GRAPH_Y,
143
- this.GRAPH_WIDTH - this.PR,
144
- this.GRAPH_HEIGHT,
145
- this.GRAPH_X,
146
- this.GRAPH_Y,
147
- this.GRAPH_WIDTH - this.PR,
148
- this.GRAPH_HEIGHT
152
+ graphX + pr,
153
+ graphY,
154
+ graphWidth - pr,
155
+ graphHeight,
156
+ graphX,
157
+ graphY,
158
+ graphWidth - pr,
159
+ graphHeight
149
160
  );
150
161
 
151
- // Draw new column with gradient
152
- const columnHeight = this.GRAPH_HEIGHT - (1 - valueGraph / maxGraph) * this.GRAPH_HEIGHT;
162
+ // Clear only the new column area
163
+ this.context.fillStyle = this.bg;
164
+ this.context.fillRect(
165
+ graphX + graphWidth - pr,
166
+ graphY,
167
+ pr,
168
+ graphHeight
169
+ );
153
170
 
171
+ // Calculate column height
172
+ const columnHeight = Math.min(
173
+ graphHeight,
174
+ Math.round(valueGraph / maxGraph * graphHeight)
175
+ );
176
+
177
+ // Draw the gradient column
154
178
  if (columnHeight > 0) {
155
- this.context.globalAlpha = 1;
179
+ this.context.globalAlpha = 0.9;
156
180
  this.context.fillStyle = this.gradient;
157
181
  this.context.fillRect(
158
- this.GRAPH_X + this.GRAPH_WIDTH - this.PR,
159
- this.GRAPH_Y + this.GRAPH_HEIGHT - columnHeight,
160
- this.PR,
182
+ graphX + graphWidth - pr,
183
+ graphY + (graphHeight - columnHeight),
184
+ pr,
161
185
  columnHeight
162
186
  );
163
187
  }
188
+
189
+ this.context.globalAlpha = 1;
164
190
  }
165
191
  }
166
192
 
167
- export { Panel };
193
+ export { Panel };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stats-gl",
3
- "version": "2.4.1",
3
+ "version": "3.0.0",
4
4
  "type": "module",
5
5
  "author": "Renaud ROHLINGER (https://github.com/RenaudRohlinger)",
6
6
  "homepage": "https://github.com/RenaudRohlinger/stats-gl",