-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTimeCaptions.tsx
150 lines (135 loc) · 4.39 KB
/
TimeCaptions.tsx
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { useCallback } from 'react';
import { useDraw } from '../hooks/useCanvas';
import { MINUTE } from '../lib/consts';
import { type DrawingFunction } from '../lib/types';
import { computeVisibleTimeMarkers, getCrispLineCoordinate } from '../utils/canvas';
const MINUTES_FORMATTER = (t: number) => `:${new Date(t).getMinutes().toString().padStart(2, '0')}`;
const HOURS_FORMATTER = (t: number, pixelsPerMinute: number) => {
const date = new Date(t);
if (pixelsPerMinute > 1) {
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
return `${hours}:${minutes}`;
} else {
return date.getHours().toString().padStart(2, '0');
}
};
export const CAPTION_SIZE = 33;
const RANGES_FORMATER: ((t: number, pixelsPerMinute: number) => string)[] = [
() => '',
() => '',
MINUTES_FORMATTER,
MINUTES_FORMATTER,
MINUTES_FORMATTER,
MINUTES_FORMATTER,
HOURS_FORMATTER,
HOURS_FORMATTER,
HOURS_FORMATTER,
HOURS_FORMATTER,
HOURS_FORMATTER,
];
const TimeCaptions = () => {
const drawingFunction = useCallback<DrawingFunction>(
(
ctx,
{
timeScale,
timeOrigin,
timePixelOffset,
getTimePixel,
swapAxis,
width,
height,
theme: {
background,
breakpoints,
timeRanges,
timeCaptionsPriorities,
timeCaptionsStyles,
timeGraduationsStyles,
},
showTicks,
}
) => {
const timeAxisSize = !swapAxis ? width : height;
const spaceAxisSize = !swapAxis ? height : width;
const minT = timeOrigin - timeScale * timePixelOffset;
const maxT = minT + timeScale * width;
// Find which styles to apply, relatively to the timescale (i.e. horizontal zoom level):
const pixelsPerMinute = (1 / timeScale) * MINUTE;
let labelLevels: number[] = [];
breakpoints.some((breakpoint, i) => {
if (pixelsPerMinute < breakpoint) {
labelLevels = timeCaptionsPriorities[i];
return true;
}
return false;
});
const labelMarkFormatter = (labelLevel: number, i: number) => ({
level: labelLevel,
rangeIndex: i,
});
const labelMarks = computeVisibleTimeMarkers(
minT,
maxT,
timeRanges,
labelLevels,
labelMarkFormatter
);
// Render caption background:
ctx.fillStyle = background;
if (!swapAxis) {
ctx.fillRect(0, spaceAxisSize - CAPTION_SIZE, timeAxisSize, CAPTION_SIZE);
} else {
ctx.fillRect(0, 0, CAPTION_SIZE, timeAxisSize);
}
// Render caption top border:
ctx.strokeStyle = timeGraduationsStyles[1].color;
ctx.lineWidth = timeGraduationsStyles[1].width;
if (!showTicks) {
ctx.beginPath();
if (!swapAxis) {
const y = getCrispLineCoordinate(spaceAxisSize - CAPTION_SIZE, ctx.lineWidth);
ctx.moveTo(0, y);
ctx.lineTo(timeAxisSize, y);
} else {
const x = getCrispLineCoordinate(CAPTION_SIZE, ctx.lineWidth);
ctx.moveTo(x, 0);
ctx.lineTo(x, timeAxisSize);
}
ctx.stroke();
}
// Render time captions:
for (const t in labelMarks) {
const { level, rangeIndex } = labelMarks[t];
const styles = timeCaptionsStyles[level];
const formatter = RANGES_FORMATER[rangeIndex];
const text = formatter(+t, pixelsPerMinute);
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.fillStyle = styles.color;
ctx.font = `${styles.fontWeight || 'normal'} ${styles.font}`;
const timePixel = getCrispLineCoordinate(getTimePixel(+t), ctx.lineWidth);
if (!swapAxis) {
if (showTicks) {
ctx.strokeStyle = timeCaptionsStyles[1].color;
ctx.moveTo(timePixel, spaceAxisSize - CAPTION_SIZE);
ctx.lineTo(timePixel, +t % 180000 === 0 ? 8 : 4);
ctx.stroke();
}
ctx.fillText(text, timePixel, spaceAxisSize - CAPTION_SIZE + (styles.topOffset || 0));
} else {
ctx.save();
ctx.translate(CAPTION_SIZE - (styles.topOffset || 0), timePixel);
ctx.rotate(Math.PI / 2);
ctx.fillText(text, 0, 0);
ctx.restore();
}
}
},
[]
);
useDraw('captions', drawingFunction);
return null;
};
export default TimeCaptions;