-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcapture.js
87 lines (70 loc) · 2.21 KB
/
capture.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
import {resetAnimation, rows, cells, renderMessages, parseMessages, setStatus} from './script.js';
const startButton = document.getElementById('start');
const testButton = document.getElementById('test');
const content = document.getElementById('content');
const frameDelay = 200;
let gif = new GIF({
workers: 2,
quality: 10,
delay: frameDelay,
width: 738,
height: 136,
workerScript: 'gif.worker.js'
});
let recording = false;
let captureInterval;
function captureFrames() {
if (recording) {
html2canvas(content, {
backgroundColor: '#0d1117',
useCORS: true
}).then(canvas => {
gif.addFrame(canvas, { delay: frameDelay });
requestAnimationFrame(captureFrames);
}).catch(error => {
console.error('Error capturing canvas:', error);
});
}
}
startButton.addEventListener('click', async () => {
startButton.disabled = true;
testButton.disabled = true;
recording = true;
setStatus("Recording in progress. It wont come out glitchy. DO NOT CLOSE OR REFRESH THE PAGE.");
resetAnimation();
for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
for (let i = 0; i < 52; i++) {
cells[rowIndex][i].style.backgroundColor = "";
}
}
const captureFramesAsync = async () => {
requestAnimationFrame(captureFrames);
};
await Promise.all([
renderMessages(parseMessages()),
captureFramesAsync()
]);
stopRecording();
});
function stopRecording() {
clearInterval(captureInterval);
recording = false;
setStatus("Recording Finished... Rendering GIF. Please wait...");
gif.on('finished', (blob) => {
const url = URL.createObjectURL(blob);
const img = document.createElement('img');
img.src = url;
document.body.appendChild(img);
const link = document.createElement('a');
link.href = url;
link.download = 'recorded-content.gif';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
document.body.removeChild(img);
});
setTimeout(() => {
gif.render();
}, 100);
startButton.disabled = false;
}