1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
// Rulers along the top and left edges of the view, and guide lines. Both are drawn
// over the view canvas in screen (CSS pixel) space, so toggling rulers never moves
// the canvas element.
export const RULER = 20;
const STEPS = [1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000, 10000, 20000];
let themeCache = null;
function themeColors() {
const theme = document.documentElement.dataset.theme;
if (themeCache?.theme === theme) return themeCache;
const cs = getComputedStyle(document.documentElement);
const v = (name, fb) => cs.getPropertyValue(name).trim() || fb;
themeCache = { theme, bg: v('--bg-1', '#232428'), line: v('--line', '#3a3c43'), text: v('--text-dim', '#9a9ca5'), accent: v('--accent', '#4c8dff') };
return themeCache;
}
/** Major tick spacing (doc px) so labels are at least ~56 screen px apart, plus minor subdivisions. */
function tickSteps(zoom) {
const major = STEPS.find((s) => s * zoom >= 56) ?? STEPS[STEPS.length - 1];
const sub = [10, 5, 2].find((n) => (major / n) * zoom >= 5 && Number.isInteger(major / n)) || 1;
return { major, minor: major / sub };
}
export function drawRulers(ctx, renderer, doc, pointer) {
const { zoom, panX, panY } = doc.view;
const W = renderer.width, H = renderer.height;
const c = themeColors();
const { major, minor } = tickSteps(zoom);
ctx.save();
ctx.fillStyle = c.bg;
ctx.fillRect(0, 0, W, RULER);
ctx.fillRect(0, 0, RULER, H);
ctx.strokeStyle = c.line;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(0, RULER - 0.5); ctx.lineTo(W, RULER - 0.5);
ctx.moveTo(RULER - 0.5, 0); ctx.lineTo(RULER - 0.5, H);
ctx.stroke();
// document extent highlight
ctx.fillStyle = 'rgba(128,128,128,0.12)';
ctx.fillRect(Math.max(RULER, panX), 0, Math.min(W, panX + doc.width * zoom) - Math.max(RULER, panX), RULER - 1);
ctx.fillRect(0, Math.max(RULER, panY), RULER - 1, Math.min(H, panY + doc.height * zoom) - Math.max(RULER, panY));
ctx.font = '10px system-ui, sans-serif';
ctx.fillStyle = c.text;
ctx.strokeStyle = c.text;
ctx.textBaseline = 'top';
ctx.beginPath();
// top ruler
const x0 = Math.floor((RULER - panX) / zoom / minor) * minor;
for (let d = x0; d * zoom + panX <= W; d += minor) {
const sx = Math.round(d * zoom + panX) + 0.5;
if (sx < RULER) continue;
const isMajor = Math.abs(d / major - Math.round(d / major)) < 1e-6;
const len = isMajor ? RULER - 1 : (Math.abs((d * 2) / major - Math.round((d * 2) / major)) < 1e-6 ? 7 : 4);
ctx.moveTo(sx, RULER - 1); ctx.lineTo(sx, RULER - 1 - len);
if (isMajor) ctx.fillText(String(Math.round(d)), sx + 3, 2);
}
// left ruler
const y0 = Math.floor((RULER - panY) / zoom / minor) * minor;
for (let d = y0; d * zoom + panY <= H; d += minor) {
const sy = Math.round(d * zoom + panY) + 0.5;
if (sy < RULER) continue;
const isMajor = Math.abs(d / major - Math.round(d / major)) < 1e-6;
const len = isMajor ? RULER - 1 : (Math.abs((d * 2) / major - Math.round((d * 2) / major)) < 1e-6 ? 7 : 4);
ctx.moveTo(RULER - 1, sy); ctx.lineTo(RULER - 1 - len, sy);
if (isMajor) {
ctx.save();
ctx.translate(2, sy + 3);
ctx.rotate(Math.PI / 2);
ctx.fillText(String(Math.round(d)), 0, -RULER + 4);
ctx.restore();
}
}
ctx.stroke();
// pointer position markers
if (pointer) {
ctx.strokeStyle = c.accent;
ctx.beginPath();
if (pointer.x > RULER) { ctx.moveTo(Math.round(pointer.x) + 0.5, 0); ctx.lineTo(Math.round(pointer.x) + 0.5, RULER - 1); }
if (pointer.y > RULER) { ctx.moveTo(0, Math.round(pointer.y) + 0.5); ctx.lineTo(RULER - 1, Math.round(pointer.y) + 0.5); }
ctx.stroke();
}
ctx.fillStyle = c.bg;
ctx.fillRect(0, 0, RULER - 1, RULER - 1);
ctx.restore();
}
export const GUIDE_COLOR = '#00b7ff';
export const SNAP_COLOR = '#ff2fd0';
/** Draw guide lines (doc space) across the whole view. `extra` is a guide being dragged. */
export function drawGuides(ctx, renderer, doc, guides, { color = GUIDE_COLOR, dash = null } = {}) {
const { zoom, panX, panY } = doc.view;
ctx.save();
ctx.strokeStyle = color;
ctx.lineWidth = 1;
if (dash) ctx.setLineDash(dash);
ctx.beginPath();
for (const g of guides) {
if (g.axis === 'x') {
const sx = Math.round(g.pos * zoom + panX) + 0.5;
ctx.moveTo(sx, 0); ctx.lineTo(sx, renderer.height);
} else {
const sy = Math.round(g.pos * zoom + panY) + 0.5;
ctx.moveTo(0, sy); ctx.lineTo(renderer.width, sy);
}
}
ctx.stroke();
ctx.restore();
}
|