Skip to content

Commit d9eb503

Browse files
committed
reduce II
1 parent bbf1d86 commit d9eb503

File tree

1 file changed

+46
-60
lines changed

1 file changed

+46
-60
lines changed

src/js/tabs/osd.js

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
1+
// Shared vertical axis drawing helper for left/right rulers
2+
function drawVerticalAxis(ctx, params, axis) {
3+
const { rowsCount, cy, left, right, ch, cw, signPad, rows, containerRect, config } = params;
4+
ctx.textAlign = axis === "left" ? "right" : "left";
5+
for (let i = 0; i < rowsCount; i++) {
6+
const y = OSD._rowCenterY(i, containerRect, rows);
7+
const offset = i - cy;
8+
const isCenter = i === cy;
9+
const isMajor = Math.abs(offset) % config.verticalLabelStep === 0 || i === 0 || i === rowsCount - 1 || isCenter;
10+
const tick = isMajor ? config.vertTickMajor : config.tickMinor;
11+
ctx.strokeStyle = isCenter ? config.colorCenter : isMajor ? config.colorMajor : config.colorMinor;
12+
ctx.lineWidth = 1;
13+
ctx.beginPath();
14+
let x0, x1, labelX;
15+
if (axis === "left") {
16+
x0 = left - 1;
17+
x1 = Math.max(0, left - tick);
18+
} else {
19+
x0 = Math.min(cw - 1, right + 1);
20+
x1 = Math.min(cw - 1, right + tick);
21+
}
22+
ctx.moveTo(x0 + 0.5, y + 0.5);
23+
ctx.lineTo(x1 + 0.5, y + 0.5);
24+
ctx.stroke();
25+
if (isMajor) {
26+
ctx.fillStyle = isCenter ? config.colorCenter : "#ffffff";
27+
const text = offset.toString();
28+
const textWidth = ctx.measureText(text).width;
29+
const extra = text.startsWith("-") ? signPad : 0;
30+
if (axis === "left") {
31+
const desired = x1 - (isMajor ? config.sideLabelOffsetMajor : config.sideLabelOffset) - extra;
32+
labelX = Math.max(config.minEdgePadding + textWidth, desired);
33+
} else {
34+
const desired = x1 + (isMajor ? config.sideLabelOffsetMajor : config.sideLabelOffset) + extra;
35+
labelX = Math.min(cw - config.minEdgePadding - textWidth, desired);
36+
}
37+
ctx.lineWidth = 3;
38+
ctx.strokeStyle = "rgba(0,0,0,0.6)";
39+
const yLabel = Math.max(config.minEdgePadding, Math.min(ch - config.minEdgePadding, y + 0.5));
40+
ctx.strokeText(text, labelX, yLabel);
41+
ctx.fillText(text, labelX, yLabel);
42+
}
43+
}
44+
}
145
import { i18n } from "../localization";
246
import GUI, { TABS } from "../gui";
347
import { tracking } from "../Analytics";
@@ -2476,68 +2520,10 @@ OSD._drawBottomAxis = function (ctx, params) {
24762520
drawHorizontalAxis(ctx, params, "bottom");
24772521
};
24782522
OSD._drawLeftAxis = function (ctx, params) {
2479-
const { rowsCount, cy, left, ch, signPad, rows, containerRect, config } = params;
2480-
ctx.textAlign = "right";
2481-
for (let i = 0; i < rowsCount; i++) {
2482-
const y = OSD._rowCenterY(i, containerRect, rows);
2483-
const offset = i - cy;
2484-
const isCenter = i === cy;
2485-
const isMajor = Math.abs(offset) % config.verticalLabelStep === 0 || i === 0 || i === rowsCount - 1 || isCenter;
2486-
const tick = isMajor ? config.vertTickMajor : config.tickMinor;
2487-
ctx.strokeStyle = isCenter ? config.colorCenter : isMajor ? config.colorMajor : config.colorMinor;
2488-
ctx.lineWidth = 1;
2489-
ctx.beginPath();
2490-
const x0 = left - 1;
2491-
const x1 = Math.max(0, left - tick);
2492-
ctx.moveTo(x0 + 0.5, y + 0.5);
2493-
ctx.lineTo(x1 + 0.5, y + 0.5);
2494-
ctx.stroke();
2495-
if (isMajor) {
2496-
ctx.fillStyle = isCenter ? config.colorCenter : "#ffffff";
2497-
const text = offset.toString();
2498-
const textWidth = ctx.measureText(text).width;
2499-
const extra = text.startsWith("-") ? signPad : 0;
2500-
const desired = x1 - (isMajor ? config.sideLabelOffsetMajor : config.sideLabelOffset) - extra;
2501-
const labelX = Math.max(config.minEdgePadding + textWidth, desired);
2502-
ctx.lineWidth = 3;
2503-
ctx.strokeStyle = "rgba(0,0,0,0.6)";
2504-
const yLabel = Math.max(config.minEdgePadding, Math.min(ch - config.minEdgePadding, y + 0.5));
2505-
ctx.strokeText(text, labelX, yLabel);
2506-
ctx.fillText(text, labelX, yLabel);
2507-
}
2508-
}
2523+
drawVerticalAxis(ctx, params, "left");
25092524
};
25102525
OSD._drawRightAxis = function (ctx, params) {
2511-
const { rowsCount, cy, right, cw, signPad, rows, containerRect, config } = params;
2512-
ctx.textAlign = "left";
2513-
for (let i = 0; i < rowsCount; i++) {
2514-
const y = OSD._rowCenterY(i, containerRect, rows);
2515-
const offset = i - cy;
2516-
const isCenter = i === cy;
2517-
const isMajor = Math.abs(offset) % config.verticalLabelStep === 0 || i === 0 || i === rowsCount - 1 || isCenter;
2518-
const tick = isMajor ? config.vertTickMajor : config.tickMinor;
2519-
ctx.strokeStyle = isCenter ? config.colorCenter : isMajor ? config.colorMajor : config.colorMinor;
2520-
ctx.lineWidth = 1;
2521-
ctx.beginPath();
2522-
const x0 = Math.min(cw - 1, right + 1);
2523-
const x1 = Math.min(cw - 1, right + tick);
2524-
ctx.moveTo(x0 + 0.5, y + 0.5);
2525-
ctx.lineTo(x1 + 0.5, y + 0.5);
2526-
ctx.stroke();
2527-
if (isMajor) {
2528-
ctx.fillStyle = isCenter ? config.colorCenter : "#ffffff";
2529-
const text = offset.toString();
2530-
const textWidth = ctx.measureText(text).width;
2531-
const extra = text.startsWith("-") ? signPad : 0;
2532-
const desired = x1 + (isMajor ? config.sideLabelOffsetMajor : config.sideLabelOffset) + extra;
2533-
const labelX = Math.min(cw - config.minEdgePadding - textWidth, desired);
2534-
ctx.lineWidth = 3;
2535-
ctx.strokeStyle = "rgba(0,0,0,0.6)";
2536-
const yLabel = Math.max(config.minEdgePadding, Math.min(params.ch - config.minEdgePadding, y + 0.5));
2537-
ctx.strokeText(text, labelX, yLabel);
2538-
ctx.fillText(text, labelX, yLabel);
2539-
}
2540-
}
2526+
drawVerticalAxis(ctx, params, "right");
25412527
};
25422528

25432529
OSD.drawRulers = function () {

0 commit comments

Comments
 (0)