@rian8337/osu-strain-graph-generator 4.0.0-beta.102 → 4.0.0-beta.104

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/dist/index.js DELETED
@@ -1,452 +0,0 @@
1
- 'use strict';
2
-
3
- var osuBase = require('@rian8337/osu-base');
4
- var canvas = require('canvas');
5
-
6
- /******************************************************************************
7
- Copyright (c) Microsoft Corporation.
8
-
9
- Permission to use, copy, modify, and/or distribute this software for any
10
- purpose with or without fee is hereby granted.
11
-
12
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18
- PERFORMANCE OF THIS SOFTWARE.
19
- ***************************************************************************** */
20
- /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
21
-
22
-
23
- function __awaiter(thisArg, _arguments, P, generator) {
24
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
25
- return new (P || (P = Promise))(function (resolve, reject) {
26
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
27
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
28
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
29
- step((generator = generator.apply(thisArg, _arguments || [])).next());
30
- });
31
- }
32
-
33
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
34
- var e = new Error(message);
35
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
36
- };
37
-
38
- /**
39
- * Utility to draw a graph with only node-canvas.
40
- *
41
- * Used for creating strain graph of beatmaps.
42
- */
43
- class Chart {
44
- /**
45
- * @param values Initializer options for the graph.
46
- */
47
- constructor(values) {
48
- var _a;
49
- this.padding = 10;
50
- this.tickSize = 10;
51
- this.axisColor = "#424242";
52
- this.font = "12pt Calibri";
53
- this.axisLabelFont = "bold 11pt Calibri";
54
- this.fontHeight = 12;
55
- this.baseLabelOffset = 15;
56
- this.graphWidth = values.graphWidth;
57
- this.graphHeight = values.graphHeight;
58
- this.canvas = canvas.createCanvas(this.graphWidth, this.graphHeight);
59
- this.context = this.canvas.getContext("2d");
60
- this.minX = values.minX;
61
- this.minY = values.minY;
62
- this.maxX = values.maxX;
63
- this.maxY = values.maxY;
64
- this.unitsPerTickX = values.unitsPerTickX;
65
- this.unitsPerTickY = values.unitsPerTickY;
66
- this.background = values.background;
67
- this.xLabel = values.xLabel;
68
- this.yLabel = values.yLabel;
69
- this.xValueType = values.xValueType;
70
- this.yValueType = values.yValueType;
71
- this.pointRadius = Math.max(0, (_a = values.pointRadius) !== null && _a !== void 0 ? _a : 1);
72
- // Relationships
73
- this.rangeX = this.maxX - this.minX;
74
- this.rangeY = this.maxY - this.minY;
75
- this.numXTicks = Math.round(this.rangeX / this.unitsPerTickX);
76
- this.numYTicks = Math.round(this.rangeY / this.unitsPerTickY);
77
- this.x = this.getLongestValueWidth() + this.padding * 2;
78
- this.y = this.padding * 2;
79
- this.width = this.canvas.width - this.x - this.padding * 2;
80
- this.height =
81
- this.canvas.height - this.y - this.padding - this.fontHeight;
82
- this.scaleX =
83
- (this.width - (this.xLabel ? this.baseLabelOffset : 0)) /
84
- this.rangeX;
85
- this.scaleY =
86
- (this.height - (this.yLabel ? this.baseLabelOffset : 0)) /
87
- this.rangeY;
88
- // Draw background and X and Y axis tick marks
89
- this.setBackground();
90
- if (this.xLabel !== undefined) {
91
- this.drawXAxis();
92
- }
93
- if (this.yLabel !== undefined) {
94
- this.drawYAxis();
95
- }
96
- }
97
- /**
98
- * Draws a line graph with specified data, color, and line width.
99
- *
100
- * @param data The data to make the graph.
101
- * @param color The color of the line.
102
- * @param width The width of the line.
103
- */
104
- drawLine(data, color, width) {
105
- const c = this.context;
106
- c.save();
107
- this.transformContext();
108
- c.lineWidth = width;
109
- c.strokeStyle = c.fillStyle = color;
110
- c.beginPath();
111
- c.moveTo(data[0].x * this.scaleX, data[0].y * this.scaleY);
112
- for (const point of data) {
113
- // Data segment
114
- c.lineTo(point.x * this.scaleX, point.y * this.scaleY);
115
- c.stroke();
116
- c.closePath();
117
- if (this.pointRadius) {
118
- c.beginPath();
119
- c.arc(point.x * this.scaleX, point.y * this.scaleY, this.pointRadius, 0, 2 * Math.PI, false);
120
- c.fill();
121
- c.closePath();
122
- }
123
- // Position for next segment
124
- c.beginPath();
125
- c.moveTo(point.x * this.scaleX, point.y * this.scaleY);
126
- }
127
- c.restore();
128
- }
129
- /**
130
- * Draws an area graph with specified data and color.
131
- *
132
- * @param data The data to make the graph.
133
- * @param color The color of the area.
134
- */
135
- drawArea(data, color) {
136
- const c = this.context;
137
- c.save();
138
- this.transformContext();
139
- c.strokeStyle = c.fillStyle = color;
140
- c.beginPath();
141
- data.forEach((d) => {
142
- c.lineTo(d.x * this.scaleX, d.y * this.scaleY);
143
- });
144
- c.stroke();
145
- c.lineTo(data.at(-1).x * this.scaleX, 0);
146
- c.lineTo(0, 0);
147
- c.fill();
148
- c.restore();
149
- // Redraw axes since it gets overlapped by chart area.
150
- if (color !== this.axisColor) {
151
- if (this.xLabel !== undefined) {
152
- this.drawXAxis();
153
- }
154
- if (this.yLabel !== undefined) {
155
- this.drawYAxis();
156
- }
157
- }
158
- }
159
- /**
160
- * Returns a Buffer that represents the graph.
161
- */
162
- getBuffer() {
163
- return this.canvas.toBuffer();
164
- }
165
- /**
166
- * Draws the X axis of the graph.
167
- */
168
- drawXAxis() {
169
- const c = this.context;
170
- const labelOffset = this.xLabel ? this.baseLabelOffset : 0;
171
- const yLabelOffset = this.yLabel ? this.baseLabelOffset : 0;
172
- c.save();
173
- if (this.xLabel) {
174
- c.textAlign = "center";
175
- c.font = this.axisLabelFont;
176
- c.fillText(this.xLabel, this.x + this.width / 2, this.y + this.height + labelOffset);
177
- c.restore();
178
- }
179
- c.beginPath();
180
- c.moveTo(this.x + yLabelOffset, this.y + this.height - labelOffset);
181
- c.lineTo(this.x + this.width, this.y + this.height - labelOffset);
182
- c.strokeStyle = this.axisColor;
183
- c.lineWidth = 2;
184
- c.stroke();
185
- // Draw tick marks
186
- for (let n = 0; n <= this.numXTicks; ++n) {
187
- c.beginPath();
188
- c.moveTo((n * (this.width - yLabelOffset)) / this.numXTicks +
189
- this.x +
190
- yLabelOffset, this.y + this.height - labelOffset);
191
- c.lineTo((n * (this.width - yLabelOffset)) / this.numXTicks +
192
- this.x +
193
- yLabelOffset, this.y + this.height - labelOffset - this.tickSize);
194
- c.stroke();
195
- }
196
- // Draw labels
197
- c.font = this.font;
198
- c.fillStyle = "black";
199
- c.textAlign = "center";
200
- c.textBaseline = "middle";
201
- if (this.xLabel !== undefined) {
202
- for (let n = 0; n <= this.numXTicks; ++n) {
203
- const label = Math.round((n * this.maxX) / this.numXTicks);
204
- let stringLabel = label.toString();
205
- switch (this.xValueType) {
206
- case "time":
207
- stringLabel = this.timeString(label);
208
- break;
209
- }
210
- c.save();
211
- c.translate((n * (this.width - yLabelOffset)) / this.numXTicks +
212
- this.x +
213
- yLabelOffset, this.y + this.height + this.padding - labelOffset);
214
- c.fillText(stringLabel, 0, 0);
215
- c.restore();
216
- }
217
- }
218
- c.restore();
219
- }
220
- /**
221
- * Draws the Y axis of the graph.
222
- *
223
- * @param drawLabel Whether or not to draw the axis label.
224
- */
225
- drawYAxis() {
226
- const c = this.context;
227
- const labelOffset = this.yLabel ? this.baseLabelOffset : 0;
228
- const xLabelOffset = this.xLabel ? this.baseLabelOffset : 0;
229
- c.save();
230
- if (this.yLabel) {
231
- c.textAlign = "center";
232
- c.font = this.axisLabelFont;
233
- c.translate(0, this.graphHeight);
234
- c.rotate(-Math.PI / 2);
235
- c.fillText(this.yLabel, this.y + xLabelOffset + this.height / 2, this.x - labelOffset * 2.5);
236
- c.restore();
237
- }
238
- c.beginPath();
239
- c.moveTo(this.x + labelOffset, this.y);
240
- c.lineTo(this.x + labelOffset, this.y + this.height - xLabelOffset);
241
- c.strokeStyle = this.axisColor;
242
- c.lineWidth = 2;
243
- c.stroke();
244
- c.restore();
245
- // Draw tick marks
246
- if (this.yLabel !== undefined) {
247
- for (let n = 0; n < this.numYTicks; ++n) {
248
- c.beginPath();
249
- c.moveTo(this.x + labelOffset, (n * (this.height - xLabelOffset)) / this.numYTicks +
250
- this.y);
251
- c.lineTo(this.x + labelOffset + this.tickSize, (n * (this.height - xLabelOffset)) / this.numYTicks +
252
- this.y);
253
- c.stroke();
254
- }
255
- }
256
- // Draw values
257
- c.font = this.font;
258
- c.fillStyle = "black";
259
- c.textAlign = "right";
260
- c.textBaseline = "middle";
261
- for (let n = 0; n < this.numYTicks; ++n) {
262
- const value = Math.round(this.maxY - (n * this.maxY) / this.numYTicks);
263
- c.save();
264
- c.translate(this.x + labelOffset - this.padding, (n * (this.height - xLabelOffset)) / this.numYTicks + this.y);
265
- c.fillText(value.toString(), 0, 0);
266
- c.restore();
267
- }
268
- c.restore();
269
- }
270
- /**
271
- * Transforms the context and move it to the center of the graph.
272
- */
273
- transformContext() {
274
- const c = this.context;
275
- // Move context to point (0, 0) in graph
276
- c.translate(this.x + (this.yLabel ? this.baseLabelOffset : 0), this.y + this.height - (this.xLabel ? this.baseLabelOffset : 0));
277
- // Invert the Y scale so that it increments as we go upwards
278
- c.scale(1, -1);
279
- }
280
- /**
281
- * Gets the longest width from each label text in Y axis.
282
- */
283
- getLongestValueWidth() {
284
- if (this.yLabel === undefined) {
285
- return 0;
286
- }
287
- this.context.font = this.font;
288
- let longestValueWidth = 0;
289
- for (let n = 0; n < this.numYTicks; ++n) {
290
- const value = this.maxY - n * this.unitsPerTickY;
291
- let stringValue = value.toString();
292
- switch (this.yValueType) {
293
- case "time":
294
- stringValue = this.timeString(value);
295
- break;
296
- }
297
- longestValueWidth = Math.max(longestValueWidth, this.context.measureText(stringValue).width);
298
- }
299
- return longestValueWidth;
300
- }
301
- /**
302
- * Sets the background of the graph.
303
- */
304
- setBackground() {
305
- if (!this.background) {
306
- this.context.globalAlpha = 0.7;
307
- this.context.fillStyle = "#ffffff";
308
- this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
309
- this.context.fillStyle = "#000000";
310
- return;
311
- }
312
- this.context.globalAlpha = 1;
313
- this.context.drawImage(this.background, 0, 0, this.canvas.width, this.canvas.height);
314
- this.context.globalAlpha = 0.8;
315
- this.context.fillStyle = "#bbbbbb";
316
- this.context.fillRect(0, 0, 900, 250);
317
- this.context.globalAlpha = 1;
318
- this.context.fillStyle = "#000000";
319
- }
320
- /**
321
- * Time string parsing function for axis labels.
322
- */
323
- timeString(seconds) {
324
- seconds = Math.trunc(seconds);
325
- const days = Math.floor(seconds / 86400);
326
- seconds -= days * 86400;
327
- const hours = Math.floor(seconds / 3600);
328
- seconds -= hours * 3600;
329
- const minutes = Math.floor(seconds / 60);
330
- seconds -= minutes * 60;
331
- const final = [minutes.toString(), seconds.toString().padStart(2, "0")];
332
- if (hours > 0) {
333
- final.unshift(hours.toString());
334
- }
335
- if (days > 0) {
336
- final.unshift(days.toString());
337
- }
338
- return final.join(":");
339
- }
340
- }
341
-
342
- /**
343
- * Creates a sampler function that retrieves the value of the most recent peak at or before a given time.
344
- *
345
- * Peaks of different skills can represent sections of different (and possibly variable) lengths, or even
346
- * one peak per hitobject, so they cannot be assumed to share a common, fixed-width time grid. This walks
347
- * a single chronologically sorted `peaks` array forward as `time` increases, treating each peak as holding
348
- * its value until the next peak takes over.
349
- *
350
- * @param peaks The peaks to sample from, in chronological order.
351
- */
352
- function createSampler(peaks) {
353
- let index = -1;
354
- let value = 0;
355
- return (time) => {
356
- while (index + 1 < peaks.length && peaks[index + 1].time <= time) {
357
- ++index;
358
- value = peaks[index].value;
359
- }
360
- return value;
361
- };
362
- }
363
- /**
364
- * Generates the strain chart of a beatmap and returns the chart as a buffer.
365
- *
366
- * @param beatmap The beatmap to generate the strain graph for.
367
- * @param strainPeaks The strain peaks of the beatmap.
368
- * @param clockRate The clock rate of the beatmap.
369
- * @param options The options for the canvas.
370
- */
371
- function index (beatmap, strainPeaks, clockRate, options) {
372
- return __awaiter(this, void 0, void 0, function* () {
373
- var _a, _b, _c, _d, _e;
374
- const sectionLength = 400;
375
- const hasFlashlight = strainPeaks.flashlight.length > 0;
376
- // Each skill's peaks may be sectioned differently (fixed-width sections, variable-length sections,
377
- // or one peak per hitobject), so they are merged onto the union of their own timestamps rather than
378
- // a shared, assumed-uniform grid.
379
- const timestamps = new Set();
380
- for (const peak of strainPeaks.aimWithSliders) {
381
- timestamps.add(peak.time);
382
- }
383
- for (const peak of strainPeaks.speed) {
384
- timestamps.add(peak.time);
385
- }
386
- for (const peak of strainPeaks.flashlight) {
387
- timestamps.add(peak.time);
388
- }
389
- const sortedTimestamps = [...timestamps].sort((a, b) => a - b);
390
- const firstObjectStartTime = beatmap.hitObjects.objects[0].startTime / clockRate;
391
- const sampleAim = createSampler(strainPeaks.aimWithSliders);
392
- const sampleSpeed = createSampler(strainPeaks.speed);
393
- const sampleFlashlight = createSampler(strainPeaks.flashlight);
394
- const strainInformations = new Array(sortedTimestamps.length + 1);
395
- // Intentionally insert a 0 strain at 400ms less than the beginning
396
- // of the first object to smoothen the end curve.
397
- strainInformations[0] = {
398
- strain: 0,
399
- time: (firstObjectStartTime - sectionLength) / 1000,
400
- };
401
- for (let i = 0; i < sortedTimestamps.length; ++i) {
402
- const time = sortedTimestamps[i];
403
- const aimStrain = sampleAim(time);
404
- const speedStrain = sampleSpeed(time);
405
- const flashlightStrain = sampleFlashlight(time);
406
- strainInformations[i + 1] = {
407
- time: time / 1000,
408
- strain: hasFlashlight
409
- ? (aimStrain + speedStrain + flashlightStrain) / 3
410
- : (aimStrain + speedStrain) / 2,
411
- };
412
- }
413
- const maxTime = (_b = (_a = strainInformations.at(-1)) === null || _a === void 0 ? void 0 : _a.time) !== null && _b !== void 0 ? _b : beatmap.hitObjects.objects.at(-1).endTime / 1000 / clockRate;
414
- const maxStrain = Math.max(osuBase.MathUtils.max(strainInformations.map((v) => v.strain)), 1);
415
- const maxXUnits = 10;
416
- const maxYUnits = 10;
417
- const unitsPerTickX = Math.ceil(maxTime / maxXUnits / 10) * 10;
418
- const unitsPerTickY = maxStrain / maxYUnits / 20;
419
- const chart = new Chart({
420
- graphWidth: (_c = options === null || options === void 0 ? void 0 : options.width) !== null && _c !== void 0 ? _c : 600,
421
- graphHeight: (_d = options === null || options === void 0 ? void 0 : options.height) !== null && _d !== void 0 ? _d : 150,
422
- minX: 0,
423
- minY: 0,
424
- maxX: Math.ceil(maxTime / unitsPerTickX) * unitsPerTickX,
425
- maxY: (options === null || options === void 0 ? void 0 : options.drawStrainAxis)
426
- ? Math.ceil(maxStrain / Math.ceil(unitsPerTickY) / 20) *
427
- Math.ceil(unitsPerTickY) *
428
- 20
429
- : maxStrain,
430
- unitsPerTickX,
431
- unitsPerTickY: (options === null || options === void 0 ? void 0 : options.drawStrainAxis)
432
- ? Math.ceil(unitsPerTickY) * 20
433
- : unitsPerTickY,
434
- background: (options === null || options === void 0 ? void 0 : options.beatmapsetID) !== undefined
435
- ? yield canvas.loadImage(`https://assets.ppy.sh/beatmaps/${options.beatmapsetID.toString()}/covers/cover.jpg`).catch(() => undefined)
436
- : undefined,
437
- xLabel: (options === null || options === void 0 ? void 0 : options.showTimeLabel) ? "Time" : "",
438
- yLabel: (options === null || options === void 0 ? void 0 : options.drawStrainAxis)
439
- ? options.showStrainLabel
440
- ? "Strain"
441
- : ""
442
- : undefined,
443
- pointRadius: 0,
444
- xValueType: "time",
445
- });
446
- chart.drawArea(strainInformations.map((v) => new osuBase.Vector2(v.time, v.strain)), (_e = options === null || options === void 0 ? void 0 : options.color) !== null && _e !== void 0 ? _e : "#000000");
447
- return chart.getBuffer();
448
- });
449
- }
450
-
451
- module.exports = index;
452
- //# sourceMappingURL=index.js.map
@@ -1,49 +0,0 @@
1
- import { Beatmap } from '@rian8337/osu-base';
2
- import { StrainPeaks } from '@rian8337/osu-difficulty-calculator';
3
- import { StrainPeaks as StrainPeaks$1 } from '@rian8337/osu-rebalance-difficulty-calculator';
4
-
5
- /**
6
- * Options for initializing the canvas.
7
- */
8
- interface CanvasOptions {
9
- /**
10
- * The beatmapset ID to get background image from. If omitted, the background will be plain white.
11
- */
12
- readonly beatmapsetID?: number;
13
- /**
14
- * The width of the canvas. Defaults to 600.
15
- */
16
- readonly width?: number;
17
- /**
18
- * The height of the canvas. Defaults to 150.
19
- */
20
- readonly height?: number;
21
- /**
22
- * The color of the graph. Defaults to black.
23
- */
24
- readonly color?: string;
25
- /**
26
- * Whether to show the time label. Defaults to `false`.
27
- */
28
- readonly showTimeLabel?: boolean;
29
- /**
30
- * Whether to show the strain axis. Defaults to `false`.
31
- */
32
- readonly drawStrainAxis?: boolean;
33
- /**
34
- * Whether to show the strain label. Only active when `drawStrainAxis` is set to `true`. Defaults to `false`.
35
- */
36
- readonly showStrainLabel?: boolean;
37
- }
38
- /**
39
- * Generates the strain chart of a beatmap and returns the chart as a buffer.
40
- *
41
- * @param beatmap The beatmap to generate the strain graph for.
42
- * @param strainPeaks The strain peaks of the beatmap.
43
- * @param clockRate The clock rate of the beatmap.
44
- * @param options The options for the canvas.
45
- */
46
- declare function export_default(beatmap: Beatmap, strainPeaks: StrainPeaks | StrainPeaks$1, clockRate: number, options?: CanvasOptions): Promise<Buffer>;
47
-
48
- export { export_default as default };
49
- export type { CanvasOptions };