@rian8337/osu-strain-graph-generator 2.2.0 → 2.4.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/README.md CHANGED
@@ -41,7 +41,7 @@ import { default as getStrainChart } from "@rian8337/osu-strain-graph-generator"
41
41
  }
42
42
 
43
43
  const rating = new OsuStarRating().calculate({
44
- map: beatmapInfo.map,
44
+ map: beatmapInfo.beatmap,
45
45
  });
46
46
 
47
47
  const chart = await getStrainChart(rating);
package/dist/index.js CHANGED
@@ -3,374 +3,374 @@
3
3
  var osuBase = require('@rian8337/osu-base');
4
4
  var canvas = require('canvas');
5
5
 
6
- /**
7
- * Utility to draw a graph with only node-canvas.
8
- *
9
- * Used for creating strain graph of beatmaps.
10
- */
11
- class Chart {
12
- /**
13
- * The canvas instance of this chart.
14
- */
15
- canvas;
16
- /**
17
- * The 2D rendering surface for the drawing surface of this chart.
18
- */
19
- context;
20
- graphWidth;
21
- graphHeight;
22
- minX;
23
- minY;
24
- maxX;
25
- maxY;
26
- unitsPerTickX;
27
- unitsPerTickY;
28
- background;
29
- xLabel;
30
- yLabel;
31
- xValueType;
32
- yValueType;
33
- pointRadius;
34
- padding = 10;
35
- tickSize = 10;
36
- axisColor = "#555";
37
- font = "12pt Calibri";
38
- axisLabelFont = "bold 11pt Calibri";
39
- fontHeight = 12;
40
- baseLabelOffset = 15;
41
- rangeX;
42
- rangeY;
43
- numXTicks;
44
- numYTicks;
45
- x;
46
- y;
47
- width;
48
- height;
49
- scaleX;
50
- scaleY;
51
- /**
52
- * @param values Initializer options for the graph.
53
- */
54
- constructor(values) {
55
- this.graphWidth = values.graphWidth;
56
- this.graphHeight = values.graphHeight;
57
- this.canvas = canvas.createCanvas(this.graphWidth, this.graphHeight);
58
- this.context = this.canvas.getContext("2d");
59
- this.minX = values.minX;
60
- this.minY = values.minY;
61
- this.maxX = values.maxX;
62
- this.maxY = values.maxY;
63
- this.unitsPerTickX = values.unitsPerTickX;
64
- this.unitsPerTickY = values.unitsPerTickY;
65
- this.background = values.background;
66
- this.xLabel = values.xLabel;
67
- this.yLabel = values.yLabel;
68
- this.xValueType = values.xValueType;
69
- this.yValueType = values.yValueType;
70
- this.pointRadius = Math.max(0, values.pointRadius ?? 1);
71
- // Relationships
72
- this.rangeX = this.maxX - this.minX;
73
- this.rangeY = this.maxY - this.minY;
74
- this.numXTicks = Math.round(this.rangeX / this.unitsPerTickX);
75
- this.numYTicks = Math.round(this.rangeY / this.unitsPerTickY);
76
- this.x = this.getLongestValueWidth() + this.padding * 2;
77
- this.y = this.padding * 2;
78
- this.width = this.canvas.width - this.x - this.padding * 2;
79
- this.height =
80
- this.canvas.height - this.y - this.padding - this.fontHeight;
81
- this.scaleX =
82
- (this.width - (this.xLabel ? this.baseLabelOffset : 0)) /
83
- this.rangeX;
84
- this.scaleY =
85
- (this.height - (this.yLabel ? this.baseLabelOffset : 0)) /
86
- this.rangeY;
87
- // Draw background and X and Y axis tick marks
88
- this.setBackground();
89
- this.drawXAxis(true);
90
- this.drawYAxis(true);
91
- }
92
- /**
93
- * Draws a line graph with specified data, color, and line width.
94
- *
95
- * @param data The data to make the graph.
96
- * @param color The color of the line.
97
- * @param width The width of the line.
98
- */
99
- drawLine(data, color, width) {
100
- const c = this.context;
101
- c.save();
102
- this.transformContext();
103
- c.lineWidth = width;
104
- c.strokeStyle = c.fillStyle = color;
105
- c.beginPath();
106
- c.moveTo(data[0].x * this.scaleX, data[0].y * this.scaleY);
107
- for (let n = 0; n < data.length; ++n) {
108
- const point = data[n];
109
- // Data segment
110
- c.lineTo(point.x * this.scaleX, point.y * this.scaleY);
111
- c.stroke();
112
- c.closePath();
113
- if (this.pointRadius) {
114
- c.beginPath();
115
- c.arc(point.x * this.scaleX, point.y * this.scaleY, this.pointRadius, 0, 2 * Math.PI, false);
116
- c.fill();
117
- c.closePath();
118
- }
119
- // Position for next segment
120
- c.beginPath();
121
- c.moveTo(point.x * this.scaleX, point.y * this.scaleY);
122
- }
123
- c.restore();
124
- }
125
- /**
126
- * Draws an area graph with specified data and color.
127
- *
128
- * @param data The data to make the graph.
129
- * @param color The color of the area.
130
- */
131
- drawArea(data, color) {
132
- const c = this.context;
133
- c.save();
134
- this.transformContext();
135
- c.strokeStyle = c.fillStyle = color;
136
- c.beginPath();
137
- data.forEach((d) => c.lineTo(d.x * this.scaleX, d.y * this.scaleY));
138
- c.stroke();
139
- c.lineTo(data.at(-1).x * this.scaleX, 0);
140
- c.lineTo(0, 0);
141
- c.fill();
142
- c.restore();
143
- // Redraw axes since it gets
144
- // overlapped by chart area
145
- if (color !== this.axisColor) {
146
- this.drawXAxis();
147
- this.drawYAxis();
148
- }
149
- }
150
- /**
151
- * Returns a Buffer that represents the graph.
152
- */
153
- getBuffer() {
154
- return this.canvas.toBuffer();
155
- }
156
- /**
157
- * Draws the X axis of the graph.
158
- *
159
- * @param drawLabel Whether or not to draw the axis label.
160
- */
161
- drawXAxis(drawLabel) {
162
- const c = this.context;
163
- const labelOffset = this.xLabel ? this.baseLabelOffset : 0;
164
- const yLabelOffset = this.yLabel ? this.baseLabelOffset : 0;
165
- c.save();
166
- if (this.xLabel && drawLabel) {
167
- c.textAlign = "center";
168
- c.font = this.axisLabelFont;
169
- c.fillText(this.xLabel, this.x + this.width / 2, this.y + this.height + labelOffset);
170
- c.restore();
171
- }
172
- c.beginPath();
173
- c.moveTo(this.x + yLabelOffset, this.y + this.height - labelOffset);
174
- c.lineTo(this.x + this.width, this.y + this.height - labelOffset);
175
- c.strokeStyle = this.axisColor;
176
- c.lineWidth = 2;
177
- c.stroke();
178
- // Draw tick marks
179
- for (let n = 0; n < this.numXTicks; ++n) {
180
- c.beginPath();
181
- c.moveTo(((n + 1) * (this.width - yLabelOffset)) / this.numXTicks +
182
- this.x +
183
- yLabelOffset, this.y + this.height - labelOffset);
184
- c.lineTo(((n + 1) * (this.width - yLabelOffset)) / this.numXTicks +
185
- this.x +
186
- yLabelOffset, this.y + this.height - labelOffset - this.tickSize);
187
- c.stroke();
188
- }
189
- // Draw labels
190
- c.font = this.font;
191
- c.fillStyle = "black";
192
- c.textAlign = "center";
193
- c.textBaseline = "middle";
194
- for (let n = 0; n < this.numXTicks; ++n) {
195
- const label = Math.round(((n + 1) * this.maxX) / this.numXTicks);
196
- let stringLabel = label.toString();
197
- switch (this.xValueType) {
198
- case "time":
199
- stringLabel = this.timeString(label);
200
- break;
201
- }
202
- c.save();
203
- c.translate(((n + 1) * (this.width - yLabelOffset)) / this.numXTicks +
204
- this.x +
205
- yLabelOffset, this.y + this.height + this.padding - labelOffset);
206
- c.fillText(stringLabel, 0, 0);
207
- c.restore();
208
- }
209
- c.restore();
210
- }
211
- /**
212
- * Draws the Y axis of the graph.
213
- *
214
- * @param drawLabel Whether or not to draw the axis label.
215
- */
216
- drawYAxis(drawLabel) {
217
- const c = this.context;
218
- const labelOffset = this.yLabel ? this.baseLabelOffset : 0;
219
- const xLabelOffset = this.xLabel ? this.baseLabelOffset : 0;
220
- c.save();
221
- if (this.yLabel && drawLabel) {
222
- c.textAlign = "center";
223
- c.font = this.axisLabelFont;
224
- c.translate(0, this.graphHeight);
225
- c.rotate(-Math.PI / 2);
226
- c.fillText(this.yLabel, this.y + xLabelOffset + this.height / 2, this.x - labelOffset * 2.5);
227
- c.restore();
228
- }
229
- c.beginPath();
230
- c.moveTo(this.x + labelOffset, this.y);
231
- c.lineTo(this.x + labelOffset, this.y + this.height - xLabelOffset);
232
- c.strokeStyle = this.axisColor;
233
- c.lineWidth = 2;
234
- c.stroke();
235
- c.restore();
236
- // Draw tick marks
237
- for (let n = 0; n < this.numYTicks; ++n) {
238
- c.beginPath();
239
- c.moveTo(this.x + labelOffset, (n * (this.height - xLabelOffset)) / this.numYTicks + this.y);
240
- c.lineTo(this.x + labelOffset + this.tickSize, (n * (this.height - xLabelOffset)) / this.numYTicks + this.y);
241
- c.stroke();
242
- }
243
- // Draw values
244
- c.font = this.font;
245
- c.fillStyle = "black";
246
- c.textAlign = "right";
247
- c.textBaseline = "middle";
248
- for (let n = 0; n < this.numYTicks; ++n) {
249
- const value = Math.round(this.maxY - (n * this.maxY) / this.numYTicks);
250
- c.save();
251
- c.translate(this.x + labelOffset - this.padding, (n * (this.height - xLabelOffset)) / this.numYTicks + this.y);
252
- c.fillText(value.toString(), 0, 0);
253
- c.restore();
254
- }
255
- c.restore();
256
- }
257
- /**
258
- * Transforms the context and move it to the center of the graph.
259
- */
260
- transformContext() {
261
- const c = this.context;
262
- // Move context to point (0, 0) in graph
263
- c.translate(this.x + (this.yLabel ? this.baseLabelOffset : 0), this.y + this.height - (this.xLabel ? this.baseLabelOffset : 0));
264
- // Invert the Y scale so that it
265
- // increments as we go upwards
266
- c.scale(1, -1);
267
- }
268
- /**
269
- * Gets the longest width from each label text in Y axis.
270
- */
271
- getLongestValueWidth() {
272
- this.context.font = this.font;
273
- let longestValueWidth = 0;
274
- for (let n = 0; n < this.numYTicks; ++n) {
275
- const value = this.maxY - n * this.unitsPerTickY;
276
- let stringValue = value.toString();
277
- switch (this.yValueType) {
278
- case "time":
279
- stringValue = this.timeString(value);
280
- break;
281
- }
282
- longestValueWidth = Math.max(longestValueWidth, this.context.measureText(stringValue).width);
283
- }
284
- return longestValueWidth;
285
- }
286
- /**
287
- * Sets the background of the graph.
288
- */
289
- setBackground() {
290
- if (!this.background) {
291
- this.context.globalAlpha = 0.7;
292
- this.context.fillStyle = "#ffffff";
293
- this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
294
- this.context.fillStyle = "#000000";
295
- return;
296
- }
297
- this.context.globalAlpha = 1;
298
- this.context.drawImage(this.background, 0, 0, this.canvas.width, this.canvas.height);
299
- this.context.globalAlpha = 0.8;
300
- this.context.fillStyle = "#bbbbbb";
301
- this.context.fillRect(0, 0, 900, 250);
302
- this.context.globalAlpha = 1;
303
- this.context.fillStyle = "#000000";
304
- }
305
- /**
306
- * Time string parsing function for axis labels.
307
- */
308
- timeString(second) {
309
- return new Date(1000 * Math.ceil(second))
310
- .toISOString()
311
- .substr(11, 8)
312
- .replace(/^[0:]+/, "");
313
- }
6
+ /**
7
+ * Utility to draw a graph with only node-canvas.
8
+ *
9
+ * Used for creating strain graph of beatmaps.
10
+ */
11
+ class Chart {
12
+ /**
13
+ * The canvas instance of this chart.
14
+ */
15
+ canvas;
16
+ /**
17
+ * The 2D rendering surface for the drawing surface of this chart.
18
+ */
19
+ context;
20
+ graphWidth;
21
+ graphHeight;
22
+ minX;
23
+ minY;
24
+ maxX;
25
+ maxY;
26
+ unitsPerTickX;
27
+ unitsPerTickY;
28
+ background;
29
+ xLabel;
30
+ yLabel;
31
+ xValueType;
32
+ yValueType;
33
+ pointRadius;
34
+ padding = 10;
35
+ tickSize = 10;
36
+ axisColor = "#555";
37
+ font = "12pt Calibri";
38
+ axisLabelFont = "bold 11pt Calibri";
39
+ fontHeight = 12;
40
+ baseLabelOffset = 15;
41
+ rangeX;
42
+ rangeY;
43
+ numXTicks;
44
+ numYTicks;
45
+ x;
46
+ y;
47
+ width;
48
+ height;
49
+ scaleX;
50
+ scaleY;
51
+ /**
52
+ * @param values Initializer options for the graph.
53
+ */
54
+ constructor(values) {
55
+ this.graphWidth = values.graphWidth;
56
+ this.graphHeight = values.graphHeight;
57
+ this.canvas = canvas.createCanvas(this.graphWidth, this.graphHeight);
58
+ this.context = this.canvas.getContext("2d");
59
+ this.minX = values.minX;
60
+ this.minY = values.minY;
61
+ this.maxX = values.maxX;
62
+ this.maxY = values.maxY;
63
+ this.unitsPerTickX = values.unitsPerTickX;
64
+ this.unitsPerTickY = values.unitsPerTickY;
65
+ this.background = values.background;
66
+ this.xLabel = values.xLabel;
67
+ this.yLabel = values.yLabel;
68
+ this.xValueType = values.xValueType;
69
+ this.yValueType = values.yValueType;
70
+ this.pointRadius = Math.max(0, values.pointRadius ?? 1);
71
+ // Relationships
72
+ this.rangeX = this.maxX - this.minX;
73
+ this.rangeY = this.maxY - this.minY;
74
+ this.numXTicks = Math.round(this.rangeX / this.unitsPerTickX);
75
+ this.numYTicks = Math.round(this.rangeY / this.unitsPerTickY);
76
+ this.x = this.getLongestValueWidth() + this.padding * 2;
77
+ this.y = this.padding * 2;
78
+ this.width = this.canvas.width - this.x - this.padding * 2;
79
+ this.height =
80
+ this.canvas.height - this.y - this.padding - this.fontHeight;
81
+ this.scaleX =
82
+ (this.width - (this.xLabel ? this.baseLabelOffset : 0)) /
83
+ this.rangeX;
84
+ this.scaleY =
85
+ (this.height - (this.yLabel ? this.baseLabelOffset : 0)) /
86
+ this.rangeY;
87
+ // Draw background and X and Y axis tick marks
88
+ this.setBackground();
89
+ this.drawXAxis(true);
90
+ this.drawYAxis(true);
91
+ }
92
+ /**
93
+ * Draws a line graph with specified data, color, and line width.
94
+ *
95
+ * @param data The data to make the graph.
96
+ * @param color The color of the line.
97
+ * @param width The width of the line.
98
+ */
99
+ drawLine(data, color, width) {
100
+ const c = this.context;
101
+ c.save();
102
+ this.transformContext();
103
+ c.lineWidth = width;
104
+ c.strokeStyle = c.fillStyle = color;
105
+ c.beginPath();
106
+ c.moveTo(data[0].x * this.scaleX, data[0].y * this.scaleY);
107
+ for (let n = 0; n < data.length; ++n) {
108
+ const point = data[n];
109
+ // Data segment
110
+ c.lineTo(point.x * this.scaleX, point.y * this.scaleY);
111
+ c.stroke();
112
+ c.closePath();
113
+ if (this.pointRadius) {
114
+ c.beginPath();
115
+ c.arc(point.x * this.scaleX, point.y * this.scaleY, this.pointRadius, 0, 2 * Math.PI, false);
116
+ c.fill();
117
+ c.closePath();
118
+ }
119
+ // Position for next segment
120
+ c.beginPath();
121
+ c.moveTo(point.x * this.scaleX, point.y * this.scaleY);
122
+ }
123
+ c.restore();
124
+ }
125
+ /**
126
+ * Draws an area graph with specified data and color.
127
+ *
128
+ * @param data The data to make the graph.
129
+ * @param color The color of the area.
130
+ */
131
+ drawArea(data, color) {
132
+ const c = this.context;
133
+ c.save();
134
+ this.transformContext();
135
+ c.strokeStyle = c.fillStyle = color;
136
+ c.beginPath();
137
+ data.forEach((d) => c.lineTo(d.x * this.scaleX, d.y * this.scaleY));
138
+ c.stroke();
139
+ c.lineTo(data.at(-1).x * this.scaleX, 0);
140
+ c.lineTo(0, 0);
141
+ c.fill();
142
+ c.restore();
143
+ // Redraw axes since it gets
144
+ // overlapped by chart area
145
+ if (color !== this.axisColor) {
146
+ this.drawXAxis();
147
+ this.drawYAxis();
148
+ }
149
+ }
150
+ /**
151
+ * Returns a Buffer that represents the graph.
152
+ */
153
+ getBuffer() {
154
+ return this.canvas.toBuffer();
155
+ }
156
+ /**
157
+ * Draws the X axis of the graph.
158
+ *
159
+ * @param drawLabel Whether or not to draw the axis label.
160
+ */
161
+ drawXAxis(drawLabel) {
162
+ const c = this.context;
163
+ const labelOffset = this.xLabel ? this.baseLabelOffset : 0;
164
+ const yLabelOffset = this.yLabel ? this.baseLabelOffset : 0;
165
+ c.save();
166
+ if (this.xLabel && drawLabel) {
167
+ c.textAlign = "center";
168
+ c.font = this.axisLabelFont;
169
+ c.fillText(this.xLabel, this.x + this.width / 2, this.y + this.height + labelOffset);
170
+ c.restore();
171
+ }
172
+ c.beginPath();
173
+ c.moveTo(this.x + yLabelOffset, this.y + this.height - labelOffset);
174
+ c.lineTo(this.x + this.width, this.y + this.height - labelOffset);
175
+ c.strokeStyle = this.axisColor;
176
+ c.lineWidth = 2;
177
+ c.stroke();
178
+ // Draw tick marks
179
+ for (let n = 0; n < this.numXTicks; ++n) {
180
+ c.beginPath();
181
+ c.moveTo(((n + 1) * (this.width - yLabelOffset)) / this.numXTicks +
182
+ this.x +
183
+ yLabelOffset, this.y + this.height - labelOffset);
184
+ c.lineTo(((n + 1) * (this.width - yLabelOffset)) / this.numXTicks +
185
+ this.x +
186
+ yLabelOffset, this.y + this.height - labelOffset - this.tickSize);
187
+ c.stroke();
188
+ }
189
+ // Draw labels
190
+ c.font = this.font;
191
+ c.fillStyle = "black";
192
+ c.textAlign = "center";
193
+ c.textBaseline = "middle";
194
+ for (let n = 0; n < this.numXTicks; ++n) {
195
+ const label = Math.round(((n + 1) * this.maxX) / this.numXTicks);
196
+ let stringLabel = label.toString();
197
+ switch (this.xValueType) {
198
+ case "time":
199
+ stringLabel = this.timeString(label);
200
+ break;
201
+ }
202
+ c.save();
203
+ c.translate(((n + 1) * (this.width - yLabelOffset)) / this.numXTicks +
204
+ this.x +
205
+ yLabelOffset, this.y + this.height + this.padding - labelOffset);
206
+ c.fillText(stringLabel, 0, 0);
207
+ c.restore();
208
+ }
209
+ c.restore();
210
+ }
211
+ /**
212
+ * Draws the Y axis of the graph.
213
+ *
214
+ * @param drawLabel Whether or not to draw the axis label.
215
+ */
216
+ drawYAxis(drawLabel) {
217
+ const c = this.context;
218
+ const labelOffset = this.yLabel ? this.baseLabelOffset : 0;
219
+ const xLabelOffset = this.xLabel ? this.baseLabelOffset : 0;
220
+ c.save();
221
+ if (this.yLabel && drawLabel) {
222
+ c.textAlign = "center";
223
+ c.font = this.axisLabelFont;
224
+ c.translate(0, this.graphHeight);
225
+ c.rotate(-Math.PI / 2);
226
+ c.fillText(this.yLabel, this.y + xLabelOffset + this.height / 2, this.x - labelOffset * 2.5);
227
+ c.restore();
228
+ }
229
+ c.beginPath();
230
+ c.moveTo(this.x + labelOffset, this.y);
231
+ c.lineTo(this.x + labelOffset, this.y + this.height - xLabelOffset);
232
+ c.strokeStyle = this.axisColor;
233
+ c.lineWidth = 2;
234
+ c.stroke();
235
+ c.restore();
236
+ // Draw tick marks
237
+ for (let n = 0; n < this.numYTicks; ++n) {
238
+ c.beginPath();
239
+ c.moveTo(this.x + labelOffset, (n * (this.height - xLabelOffset)) / this.numYTicks + this.y);
240
+ c.lineTo(this.x + labelOffset + this.tickSize, (n * (this.height - xLabelOffset)) / this.numYTicks + this.y);
241
+ c.stroke();
242
+ }
243
+ // Draw values
244
+ c.font = this.font;
245
+ c.fillStyle = "black";
246
+ c.textAlign = "right";
247
+ c.textBaseline = "middle";
248
+ for (let n = 0; n < this.numYTicks; ++n) {
249
+ const value = Math.round(this.maxY - (n * this.maxY) / this.numYTicks);
250
+ c.save();
251
+ c.translate(this.x + labelOffset - this.padding, (n * (this.height - xLabelOffset)) / this.numYTicks + this.y);
252
+ c.fillText(value.toString(), 0, 0);
253
+ c.restore();
254
+ }
255
+ c.restore();
256
+ }
257
+ /**
258
+ * Transforms the context and move it to the center of the graph.
259
+ */
260
+ transformContext() {
261
+ const c = this.context;
262
+ // Move context to point (0, 0) in graph
263
+ c.translate(this.x + (this.yLabel ? this.baseLabelOffset : 0), this.y + this.height - (this.xLabel ? this.baseLabelOffset : 0));
264
+ // Invert the Y scale so that it
265
+ // increments as we go upwards
266
+ c.scale(1, -1);
267
+ }
268
+ /**
269
+ * Gets the longest width from each label text in Y axis.
270
+ */
271
+ getLongestValueWidth() {
272
+ this.context.font = this.font;
273
+ let longestValueWidth = 0;
274
+ for (let n = 0; n < this.numYTicks; ++n) {
275
+ const value = this.maxY - n * this.unitsPerTickY;
276
+ let stringValue = value.toString();
277
+ switch (this.yValueType) {
278
+ case "time":
279
+ stringValue = this.timeString(value);
280
+ break;
281
+ }
282
+ longestValueWidth = Math.max(longestValueWidth, this.context.measureText(stringValue).width);
283
+ }
284
+ return longestValueWidth;
285
+ }
286
+ /**
287
+ * Sets the background of the graph.
288
+ */
289
+ setBackground() {
290
+ if (!this.background) {
291
+ this.context.globalAlpha = 0.7;
292
+ this.context.fillStyle = "#ffffff";
293
+ this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
294
+ this.context.fillStyle = "#000000";
295
+ return;
296
+ }
297
+ this.context.globalAlpha = 1;
298
+ this.context.drawImage(this.background, 0, 0, this.canvas.width, this.canvas.height);
299
+ this.context.globalAlpha = 0.8;
300
+ this.context.fillStyle = "#bbbbbb";
301
+ this.context.fillRect(0, 0, 900, 250);
302
+ this.context.globalAlpha = 1;
303
+ this.context.fillStyle = "#000000";
304
+ }
305
+ /**
306
+ * Time string parsing function for axis labels.
307
+ */
308
+ timeString(second) {
309
+ return new Date(1000 * Math.ceil(second))
310
+ .toISOString()
311
+ .substr(11, 8)
312
+ .replace(/^[0:]+/, "");
313
+ }
314
314
  }
315
315
 
316
- /**
317
- * Generates the strain chart of a difficulty calculator and returns the chart as a buffer.
318
- *
319
- * @param calculator The difficulty calculator to generate the strain graph for.
320
- * @param beatmapsetID The beatmapset ID to get background image from. If omitted, the background will be plain white.
321
- * @param color The color of the graph.
322
- */
323
- async function getStrainChart(calculator, beatmapsetID, color = "#000000") {
324
- if ([
325
- calculator.strainPeaks.aimWithSliders.length,
326
- calculator.strainPeaks.aimWithoutSliders.length,
327
- calculator.strainPeaks.speed.length,
328
- calculator.strainPeaks.flashlight.length,
329
- ].some((v) => v === 0)) {
330
- return null;
331
- }
332
- const sectionLength = 400;
333
- const currentSectionEnd = Math.ceil(calculator.beatmap.hitObjects.objects[0].startTime / sectionLength) * sectionLength;
334
- const strainInformations = new Array(Math.max(calculator.strainPeaks.aimWithSliders.length, calculator.strainPeaks.speed.length, calculator.strainPeaks.flashlight.length));
335
- for (let i = 0; i < strainInformations.length; ++i) {
336
- const aimStrain = calculator.strainPeaks.aimWithSliders[i] ?? 0;
337
- const speedStrain = calculator.strainPeaks.speed[i] ?? 0;
338
- const flashlightStrain = calculator.strainPeaks.flashlight[i] ?? 0;
339
- strainInformations[i] = {
340
- time: (currentSectionEnd + sectionLength * i) / 1000,
341
- strain: calculator.mods.some((m) => m instanceof osuBase.ModFlashlight)
342
- ? (aimStrain + speedStrain + flashlightStrain) / 3
343
- : (aimStrain + speedStrain) / 2,
344
- };
345
- }
346
- const maxTime = strainInformations.at(-1).time ??
347
- calculator.objects.at(-1).object.endTime / 1000;
348
- const maxStrain = Math.max(...strainInformations.map((v) => {
349
- return v.strain;
350
- }), 1);
351
- const maxXUnits = 10;
352
- const maxYUnits = 10;
353
- const unitsPerTickX = Math.ceil(maxTime / maxXUnits / 10) * 10;
354
- const unitsPerTickY = Math.ceil(maxStrain / maxYUnits / 20) * 20;
355
- const chart = new Chart({
356
- graphWidth: 900,
357
- graphHeight: 250,
358
- minX: 0,
359
- minY: 0,
360
- maxX: Math.ceil(maxTime / unitsPerTickX) * unitsPerTickX,
361
- maxY: Math.ceil(maxStrain / unitsPerTickY) * unitsPerTickY,
362
- unitsPerTickX,
363
- unitsPerTickY,
364
- background: await canvas.loadImage(`https://assets.ppy.sh/beatmaps/${beatmapsetID}/covers/cover.jpg`).catch(() => {
365
- return undefined;
366
- }),
367
- xLabel: "Time",
368
- yLabel: "Strain",
369
- pointRadius: 0,
370
- xValueType: "time",
371
- });
372
- chart.drawArea(strainInformations.map((v) => new osuBase.Vector2(v.time, v.strain)), color);
373
- return chart.getBuffer();
316
+ /**
317
+ * Generates the strain chart of a difficulty calculator and returns the chart as a buffer.
318
+ *
319
+ * @param calculator The difficulty calculator to generate the strain graph for.
320
+ * @param beatmapsetID The beatmapset ID to get background image from. If omitted, the background will be plain white.
321
+ * @param color The color of the graph.
322
+ */
323
+ async function getStrainChart(calculator, beatmapsetID, color = "#000000") {
324
+ if ([
325
+ calculator.strainPeaks.aimWithSliders.length,
326
+ calculator.strainPeaks.aimWithoutSliders.length,
327
+ calculator.strainPeaks.speed.length,
328
+ calculator.strainPeaks.flashlight.length,
329
+ ].some((v) => v === 0)) {
330
+ return null;
331
+ }
332
+ const sectionLength = 400;
333
+ const currentSectionEnd = Math.ceil(calculator.beatmap.hitObjects.objects[0].startTime / sectionLength) * sectionLength;
334
+ const strainInformations = new Array(Math.max(calculator.strainPeaks.aimWithSliders.length, calculator.strainPeaks.speed.length, calculator.strainPeaks.flashlight.length));
335
+ for (let i = 0; i < strainInformations.length; ++i) {
336
+ const aimStrain = calculator.strainPeaks.aimWithSliders[i] ?? 0;
337
+ const speedStrain = calculator.strainPeaks.speed[i] ?? 0;
338
+ const flashlightStrain = calculator.strainPeaks.flashlight[i] ?? 0;
339
+ strainInformations[i] = {
340
+ time: (currentSectionEnd + sectionLength * i) / 1000,
341
+ strain: calculator.mods.some((m) => m instanceof osuBase.ModFlashlight)
342
+ ? (aimStrain + speedStrain + flashlightStrain) / 3
343
+ : (aimStrain + speedStrain) / 2,
344
+ };
345
+ }
346
+ const maxTime = strainInformations.at(-1).time ??
347
+ calculator.objects.at(-1).object.endTime / 1000;
348
+ const maxStrain = Math.max(...strainInformations.map((v) => {
349
+ return v.strain;
350
+ }), 1);
351
+ const maxXUnits = 10;
352
+ const maxYUnits = 10;
353
+ const unitsPerTickX = Math.ceil(maxTime / maxXUnits / 10) * 10;
354
+ const unitsPerTickY = Math.ceil(maxStrain / maxYUnits / 20) * 20;
355
+ const chart = new Chart({
356
+ graphWidth: 900,
357
+ graphHeight: 250,
358
+ minX: 0,
359
+ minY: 0,
360
+ maxX: Math.ceil(maxTime / unitsPerTickX) * unitsPerTickX,
361
+ maxY: Math.ceil(maxStrain / unitsPerTickY) * unitsPerTickY,
362
+ unitsPerTickX,
363
+ unitsPerTickY,
364
+ background: await canvas.loadImage(`https://assets.ppy.sh/beatmaps/${beatmapsetID}/covers/cover.jpg`).catch(() => {
365
+ return undefined;
366
+ }),
367
+ xLabel: "Time",
368
+ yLabel: "Strain",
369
+ pointRadius: 0,
370
+ xValueType: "time",
371
+ });
372
+ chart.drawArea(strainInformations.map((v) => new osuBase.Vector2(v.time, v.strain)), color);
373
+ return chart.getBuffer();
374
374
  }
375
375
 
376
376
  module.exports = getStrainChart;
package/package.json CHANGED
@@ -1,42 +1,42 @@
1
1
  {
2
- "name": "@rian8337/osu-strain-graph-generator",
3
- "version": "2.2.0",
4
- "description": "A module for generating strain graph of an osu!standard beatmap.",
5
- "keywords": [
6
- "osu",
7
- "osu-strain-graph"
8
- ],
9
- "author": "Rian8337 <52914632+Rian8337@users.noreply.github.com>",
10
- "homepage": "https://github.com/Rian8337/osu-droid-module#readme",
11
- "license": "MIT",
12
- "main": "dist/index.js",
13
- "types": "typings/index.d.ts",
14
- "typedocMain": "src/index.ts",
15
- "files": [
16
- "dist/**",
17
- "typings/**"
18
- ],
19
- "repository": {
20
- "type": "git",
21
- "url": "git+https://github.com/Rian8337/osu-droid-module.git"
22
- },
23
- "scripts": {
24
- "build": "rollup -c ../../rollup.config.js",
25
- "lint": "eslint --ext ts",
26
- "prepare": "npm run build",
27
- "test": "echo \"No tests for this module\""
28
- },
29
- "bugs": {
30
- "url": "https://github.com/Rian8337/osu-droid-module/issues"
31
- },
32
- "dependencies": {
33
- "@rian8337/osu-base": "^2.2.0",
34
- "@rian8337/osu-difficulty-calculator": "^2.2.0",
35
- "@rian8337/osu-rebalance-difficulty-calculator": "^2.2.0",
36
- "canvas": "^2.9.0"
37
- },
38
- "publishConfig": {
39
- "access": "public"
40
- },
41
- "gitHead": "e8ab54978ffed744317da3dd0073bd4df4d97319"
2
+ "name": "@rian8337/osu-strain-graph-generator",
3
+ "version": "2.4.0",
4
+ "description": "A module for generating strain graph of an osu!standard beatmap.",
5
+ "keywords": [
6
+ "osu",
7
+ "osu-strain-graph"
8
+ ],
9
+ "author": "Rian8337 <52914632+Rian8337@users.noreply.github.com>",
10
+ "homepage": "https://github.com/Rian8337/osu-droid-module#readme",
11
+ "license": "MIT",
12
+ "main": "dist/index.js",
13
+ "types": "typings/index.d.ts",
14
+ "typedocMain": "src/index.ts",
15
+ "files": [
16
+ "dist/**",
17
+ "typings/**"
18
+ ],
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/Rian8337/osu-droid-module.git"
22
+ },
23
+ "scripts": {
24
+ "build": "rollup -c ../../rollup.config.js",
25
+ "lint": "eslint --ext ts",
26
+ "prepare": "npm run build",
27
+ "test": "echo \"No tests for this module\""
28
+ },
29
+ "bugs": {
30
+ "url": "https://github.com/Rian8337/osu-droid-module/issues"
31
+ },
32
+ "dependencies": {
33
+ "@rian8337/osu-base": "^2.3.0",
34
+ "@rian8337/osu-difficulty-calculator": "^2.4.0",
35
+ "@rian8337/osu-rebalance-difficulty-calculator": "^2.4.0",
36
+ "canvas": "^2.10.2"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "gitHead": "b19b520a3fa4244ef96ff3da36ab6d1b3126c89d"
42
42
  }
@@ -1,13 +1,13 @@
1
1
  import { DifficultyCalculator } from '@rian8337/osu-difficulty-calculator';
2
2
  import { DifficultyCalculator as DifficultyCalculator$1 } from '@rian8337/osu-rebalance-difficulty-calculator';
3
3
 
4
- /**
5
- * Generates the strain chart of a difficulty calculator and returns the chart as a buffer.
6
- *
7
- * @param calculator The difficulty calculator to generate the strain graph for.
8
- * @param beatmapsetID The beatmapset ID to get background image from. If omitted, the background will be plain white.
9
- * @param color The color of the graph.
10
- */
4
+ /**
5
+ * Generates the strain chart of a difficulty calculator and returns the chart as a buffer.
6
+ *
7
+ * @param calculator The difficulty calculator to generate the strain graph for.
8
+ * @param beatmapsetID The beatmapset ID to get background image from. If omitted, the background will be plain white.
9
+ * @param color The color of the graph.
10
+ */
11
11
  declare function getStrainChart(calculator: DifficultyCalculator | DifficultyCalculator$1, beatmapsetID?: number, color?: string): Promise<Buffer | null>;
12
12
 
13
13
  export { getStrainChart as default };