-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
64 lines (53 loc) · 1.28 KB
/
ui.js
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
export class UI {
#painter
/**
* @param {Object} parameters
* @param {HTMLVideoElement} parameters.video
*/
constructor({video}) {
this.#painter = new Painter();
video.srcObject = this.#painter.stream;
}
/**
* @param {number} seconds
**/
setTime(seconds) {
this.#painter.paint(this.#formatTime(seconds));
}
/**
* @param {Number} seconds
* @returns {String} MM:SS
*/
#formatTime(seconds) {
return new Date(seconds * 1000).toISOString().substring(14, 19)
}
}
class Painter {
stream;
#canvas
#ctx
constructor() {
const canvas = document.createElement('canvas');
canvas.width = 3840;
canvas.height = 2160;
canvas.style.display = 'none';
this.#canvas = canvas;
this.#ctx = canvas.getContext('2d');
this.stream = canvas.captureStream();
}
/**
* @param {string} text
* @returns void
*/
paint(text) {
const ctx = this.#ctx;
const cv = this.#canvas;
ctx.clearRect(0, 0, cv.width, cv.height);
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, cv.width, cv.height);
ctx.fillStyle = "#FFFFFF";
ctx.font = '1000px sans-serif';
ctx.textAlign = 'center';
ctx.fillText(text, cv.width / 2, cv.height / 2 + ctx.measureText(text).actualBoundingBoxAscent / 2);
}
}