You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
939 B
TypeScript
36 lines
939 B
TypeScript
import {
|
|
DEFAULT_TOTAL_WIDTH,
|
|
MAJOR_TICK_GAP,
|
|
MID_TICK_GAP,
|
|
MINOR_TICK_GAP,
|
|
type Tick,
|
|
} from "../models/ruler";
|
|
import { normalizeTotalWidth } from "./segmentService";
|
|
|
|
const tickCache = new Map<number, Tick[]>();
|
|
|
|
/**
|
|
* 生成标尺刻度数据,并按 totalWidth 做缓存复用。
|
|
*/
|
|
export function buildTicks(totalWidth: number = DEFAULT_TOTAL_WIDTH): Tick[] {
|
|
const safeTotalWidth = normalizeTotalWidth(totalWidth);
|
|
const cachedTicks = tickCache.get(safeTotalWidth);
|
|
if (cachedTicks) {
|
|
return cachedTicks;
|
|
}
|
|
|
|
const ticks: Tick[] = [];
|
|
for (let x = 0; x <= safeTotalWidth; x += MINOR_TICK_GAP) {
|
|
if (x % MAJOR_TICK_GAP === 0) {
|
|
ticks.push({ x, type: "major", label: String(x) });
|
|
} else if (x % MAJOR_TICK_GAP === MID_TICK_GAP) {
|
|
ticks.push({ x, type: "mid" });
|
|
} else {
|
|
ticks.push({ x, type: "minor" });
|
|
}
|
|
}
|
|
|
|
tickCache.set(safeTotalWidth, ticks);
|
|
return ticks;
|
|
}
|