-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
114 lines (95 loc) · 3.4 KB
/
app.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
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
const script = document.createElement("script");
script.src = "https://rawgit.com/spite/ccapture.js/master/build/CCapture.all.min.js";
script.addEventListener("load", function () {
const timeInput = document.getElementById("time");
const startTimeInput = document.getElementById("startTime");
const fontInput = document.getElementById("font");
const sizeInput = document.getElementById("size");
const bgColorInput = document.getElementById("bgColor");
const fontColorInput = document.getElementById("fontColor");
const startBtn = document.getElementById("startBtn");
const timer = document.getElementById("timer");
function updateTimerStyle() {
timer.style.fontFamily = fontInput.value || "Arial";
timer.style.fontSize = `${sizeInput.value || 24}px`;
timer.style.backgroundColor = bgColorInput.value;
timer.style.color = fontColorInput.value || "#FFFFFF";
}
function generateFileName(startTime, endTime) {
const startSeconds = formatTime(startTime, "seconds");
const endSeconds = formatTime(endTime, "seconds");
return `timer-video-from${startSeconds}-to${endSeconds}.webm`;
}
function startTimer() {
updateTimerStyle();
const maxTime = parseInt(timeInput.value, 10);
let currentTime = parseInt(startTimeInput.value, 10);
timer.textContent = formatTime(currentTime);
const intervalId = setInterval(() => {
currentTime++;
timer.textContent = formatTime(currentTime);
if (currentTime >= maxTime) {
clearInterval(intervalId);
}
}, 1000);
}
function formatTime(time) {
const minutes = Math.floor(time / 60);
const seconds = time % 60;
return `${minutes
.toString()
.padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
}
startBtn.addEventListener("click", startTimer);
const downloadBtn = document.getElementById("downloadBtn");
let capturer;
let canvas;
function setupCanvas() {
canvas = document.createElement("canvas");
canvas.width = 400;
canvas.height = 200;
document.body.appendChild(canvas);
}
function startCapture() {
capturer = new CCapture({
format: "webm",
framerate: 30,
name: generateFileName(parseInt(startTimeInput.value, 10), parseInt(timeInput.value, 10)),
timeLimit: parseInt(timeInput.value, 10),
verbose: true,
display: true,
});
capturer.start();
}
function drawTimer() {
const ctx = canvas.getContext("2d");
ctx.font = `${sizeInput.value || 24}px ${fontInput.value || "Arial"}`;
ctx.fillStyle = bgColorInput.value;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = fontColorInput.value;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(timer.textContent, canvas.width / 2, canvas.height / 2);
}
function endCapture() {
capturer.stop();
capturer.save();
const downloadBtn = document.getElementById("downloadBtn");
downloadBtn.removeAttribute("disabled");
document.body.removeChild(canvas);
}
startBtn.addEventListener("click", () => {
startTimer();
startCapture();
const downloadBtn = document.getElementById("downloadBtn");
downloadBtn.setAttribute("disabled", true);
});
downloadBtn.addEventListener("click", endCapture);
function captureFrame() {
drawTimer();
capturer?.capture(canvas);
}
setupCanvas();
setInterval(captureFrame, 1000 / 30);
});
document.head.appendChild(script);