-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdrawTrack.ts
84 lines (70 loc) · 1.74 KB
/
drawTrack.ts
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
import { sum } from 'lodash';
import { TRACK_HEIGHT_CONTAINER, COLORS, TICKS_PATTERN } from '../../consts';
import { getTickPattern } from '../../utils';
const { WHITE_50, GREY_20, RAIL_TICK } = COLORS;
const drawRails = ({
xStart,
yStart,
width,
stroke = GREY_20,
ctx,
}: {
xStart: number;
yStart: number;
width: number;
stroke?: string;
ctx: CanvasRenderingContext2D;
}) => {
ctx.clearRect(xStart, yStart, width, 9);
ctx.fillStyle = WHITE_50;
ctx.strokeStyle = stroke;
ctx.beginPath();
ctx.rect(xStart, yStart, width, 8);
ctx.fill();
ctx.stroke();
};
const drawTick = ({
ctx,
xStart,
yStart,
ticks,
stroke,
}: {
ctx: CanvasRenderingContext2D;
xStart: number;
yStart: number;
ticks: number[];
stroke: string;
}) => {
const sumTicks = sum(ticks) / 2;
ctx.strokeStyle = stroke;
ctx.beginPath();
ctx.setLineDash(ticks);
ctx.moveTo(xStart, yStart - sumTicks);
ctx.lineTo(xStart, yStart + sumTicks);
ctx.stroke();
};
type DrawTrackProps = {
ctx: CanvasRenderingContext2D;
width: number;
getTimePixel: (time: number) => number;
labelMarks: Record<number, { level: number; rangeIndex: number }>;
};
export const drawTrack = ({ ctx, width, getTimePixel, labelMarks }: DrawTrackProps) => {
ctx.fillStyle = WHITE_50;
ctx.save();
drawRails({ xStart: -1, yStart: TRACK_HEIGHT_CONTAINER / 2 - 4, width: width + 1, ctx });
for (const t in labelMarks) {
const date = new Date(+t);
const minutes = date.getMinutes().toString().padStart(2, '0');
const tickPattern = getTickPattern(minutes);
drawTick({
ctx,
xStart: getTimePixel(+t),
yStart: TRACK_HEIGHT_CONTAINER / 2,
ticks: TICKS_PATTERN[tickPattern],
stroke: RAIL_TICK,
});
}
ctx.restore();
};