Skip to content

Commit d4f9e61

Browse files
committed
reduce II
1 parent bbf1d86 commit d4f9e61

File tree

1 file changed

+31
-43
lines changed

1 file changed

+31
-43
lines changed

src/js/tabs/osd.js

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,15 +2469,11 @@ function drawHorizontalAxis(ctx, params, axis) {
24692469
}
24702470
}
24712471
}
2472-
OSD._drawTopAxis = function (ctx, params) {
2473-
drawHorizontalAxis(ctx, params, "top");
2474-
};
2475-
OSD._drawBottomAxis = function (ctx, params) {
2476-
drawHorizontalAxis(ctx, params, "bottom");
2477-
};
2478-
OSD._drawLeftAxis = function (ctx, params) {
2479-
const { rowsCount, cy, left, ch, signPad, rows, containerRect, config } = params;
2480-
ctx.textAlign = "right";
2472+
2473+
// Shared vertical axis drawing helper for left/right rulers
2474+
function drawVerticalAxis(ctx, params, axis) {
2475+
const { rowsCount, cy, left, right, ch, cw, signPad, rows, containerRect, config } = params;
2476+
ctx.textAlign = axis === "left" ? "right" : "left";
24812477
for (let i = 0; i < rowsCount; i++) {
24822478
const y = OSD._rowCenterY(i, containerRect, rows);
24832479
const offset = i - cy;
@@ -2487,8 +2483,14 @@ OSD._drawLeftAxis = function (ctx, params) {
24872483
ctx.strokeStyle = isCenter ? config.colorCenter : isMajor ? config.colorMajor : config.colorMinor;
24882484
ctx.lineWidth = 1;
24892485
ctx.beginPath();
2490-
const x0 = left - 1;
2491-
const x1 = Math.max(0, left - tick);
2486+
let x0, x1, labelX;
2487+
if (axis === "left") {
2488+
x0 = left - 1;
2489+
x1 = Math.max(0, left - tick);
2490+
} else {
2491+
x0 = Math.min(cw - 1, right + 1);
2492+
x1 = Math.min(cw - 1, right + tick);
2493+
}
24922494
ctx.moveTo(x0 + 0.5, y + 0.5);
24932495
ctx.lineTo(x1 + 0.5, y + 0.5);
24942496
ctx.stroke();
@@ -2497,47 +2499,33 @@ OSD._drawLeftAxis = function (ctx, params) {
24972499
const text = offset.toString();
24982500
const textWidth = ctx.measureText(text).width;
24992501
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+
if (axis === "left") {
2503+
const desired = x1 - (isMajor ? config.sideLabelOffsetMajor : config.sideLabelOffset) - extra;
2504+
labelX = Math.max(config.minEdgePadding + textWidth, desired);
2505+
} else {
2506+
const desired = x1 + (isMajor ? config.sideLabelOffsetMajor : config.sideLabelOffset) + extra;
2507+
labelX = Math.min(cw - config.minEdgePadding - textWidth, desired);
2508+
}
25022509
ctx.lineWidth = 3;
25032510
ctx.strokeStyle = "rgba(0,0,0,0.6)";
25042511
const yLabel = Math.max(config.minEdgePadding, Math.min(ch - config.minEdgePadding, y + 0.5));
25052512
ctx.strokeText(text, labelX, yLabel);
25062513
ctx.fillText(text, labelX, yLabel);
25072514
}
25082515
}
2516+
}
2517+
2518+
OSD._drawTopAxis = function (ctx, params) {
2519+
drawHorizontalAxis(ctx, params, "top");
2520+
};
2521+
OSD._drawBottomAxis = function (ctx, params) {
2522+
drawHorizontalAxis(ctx, params, "bottom");
2523+
};
2524+
OSD._drawLeftAxis = function (ctx, params) {
2525+
drawVerticalAxis(ctx, params, "left");
25092526
};
25102527
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-
}
2528+
drawVerticalAxis(ctx, params, "right");
25412529
};
25422530

25432531
OSD.drawRulers = function () {

0 commit comments

Comments
 (0)