stats-gl 2.4.2 → 3.0.1

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,33 @@ 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
- this.context.fillStyle = this.fg;
102
- this.context.fillRect(this.GRAPH_X, this.GRAPH_Y, this.GRAPH_WIDTH, this.GRAPH_HEIGHT);
103
100
 
104
- // Apply semi-transparent background
105
101
  this.context.fillStyle = this.bg;
106
102
  this.context.globalAlpha = 0.9;
107
103
  this.context.fillRect(this.GRAPH_X, this.GRAPH_Y, this.GRAPH_WIDTH, this.GRAPH_HEIGHT);
108
104
  }
109
105
 
110
- update(
111
- value: number,
112
- valueGraph: number,
113
- maxValue: number,
114
- maxGraph: number,
115
- decimals = 0
116
- ) {
106
+ // Update only text portion
107
+ update(value: number, valueGraph: number, maxValue: number, maxGraph: number, decimals = 0) {
117
108
  if (!this.context || !this.gradient) return;
118
109
 
119
110
  const min = Math.min(Infinity, value);
120
111
  const max = Math.max(maxValue, value);
121
- maxGraph = Math.max(maxGraph, valueGraph);
122
112
 
123
- // Clear and redraw background
113
+ // Clear only the text area (from top to GRAPH_Y)
124
114
  this.context.globalAlpha = 1;
125
115
  this.context.fillStyle = this.bg;
126
116
  this.context.fillRect(0, 0, this.WIDTH, this.GRAPH_Y);
@@ -128,40 +118,74 @@ class Panel {
128
118
  // Draw text
129
119
  this.context.fillStyle = this.fg;
130
120
  this.context.fillText(
131
- `${value.toFixed(decimals)} ${this.name} (${min.toFixed(decimals)}-${parseFloat(
132
- max.toFixed(decimals)
133
- )})`,
121
+ `${value.toFixed(decimals)} ${this.name} (${min.toFixed(decimals)}-${parseFloat(max.toFixed(decimals))})`,
134
122
  this.TEXT_X,
135
123
  this.TEXT_Y
136
124
  );
125
+ }
126
+
127
+ // Update only graph portion
128
+ updateGraph(valueGraph: number, maxGraph: number) {
129
+ if (!this.context || !this.gradient) return;
130
+
131
+ // Handle zero values appropriately
132
+ if (valueGraph === 0 && maxGraph === 0) {
133
+ maxGraph = 1; // Prevent division by zero
134
+ }
135
+
136
+ // Ensure maxGraph is valid and values are positive
137
+ maxGraph = Math.max(maxGraph, valueGraph, 0.1);
138
+ valueGraph = Math.max(valueGraph, 0);
139
+
140
+ // Ensure all coordinates are rounded to avoid sub-pixel rendering
141
+ const graphX = Math.round(this.GRAPH_X);
142
+ const graphY = Math.round(this.GRAPH_Y);
143
+ const graphWidth = Math.round(this.GRAPH_WIDTH);
144
+ const graphHeight = Math.round(this.GRAPH_HEIGHT);
145
+ const pr = Math.round(this.PR);
137
146
 
138
147
  // Shift the graph left
139
148
  this.context.drawImage(
140
149
  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
150
+ graphX + pr,
151
+ graphY,
152
+ graphWidth - pr,
153
+ graphHeight,
154
+ graphX,
155
+ graphY,
156
+ graphWidth - pr,
157
+ graphHeight
158
+ );
159
+
160
+ // Clear only the new column area
161
+ this.context.fillStyle = this.bg;
162
+ this.context.fillRect(
163
+ graphX + graphWidth - pr,
164
+ graphY,
165
+ pr,
166
+ graphHeight
149
167
  );
150
168
 
151
- // Draw new column with gradient
152
- const columnHeight = this.GRAPH_HEIGHT - (1 - valueGraph / maxGraph) * this.GRAPH_HEIGHT;
169
+ // Calculate column height
170
+ const columnHeight = Math.min(
171
+ graphHeight,
172
+ Math.round(valueGraph / maxGraph * graphHeight)
173
+ );
153
174
 
175
+ // Draw the gradient column
154
176
  if (columnHeight > 0) {
155
- this.context.globalAlpha = 1;
177
+ this.context.globalAlpha = 0.9;
156
178
  this.context.fillStyle = this.gradient;
157
179
  this.context.fillRect(
158
- this.GRAPH_X + this.GRAPH_WIDTH - this.PR,
159
- this.GRAPH_Y + this.GRAPH_HEIGHT - columnHeight,
160
- this.PR,
180
+ graphX + graphWidth - pr,
181
+ graphY + (graphHeight - columnHeight),
182
+ pr,
161
183
  columnHeight
162
184
  );
163
185
  }
186
+
187
+ this.context.globalAlpha = 1;
164
188
  }
165
189
  }
166
190
 
167
- export { Panel };
191
+ export { Panel };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stats-gl",
3
- "version": "2.4.2",
3
+ "version": "3.0.1",
4
4
  "type": "module",
5
5
  "author": "Renaud ROHLINGER (https://github.com/RenaudRohlinger)",
6
6
  "homepage": "https://github.com/RenaudRohlinger/stats-gl",