-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.ts
196 lines (182 loc) · 7.2 KB
/
index.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { CanvasCapture } from '../../';
// Initialize and pass in canvas.
const canvas = document.getElementById('my-canvas') as HTMLCanvasElement;
CanvasCapture.init(canvas, {
showRecDot: true,
showAlerts: true,
showDialogs: true,
verbose: false,
ffmpegCorePath: '../ffmpeg/ffmpeg-core.js',
});
// Bind key presses to begin/end recordings.
const MP4_OPTIONS = {
name: 'demo-mp4',
format: CanvasCapture.MP4,
quality: 1,
fps: 60,
onExportProgress: (progress: number) => console.log(`MP4 export progress: ${progress}.`),
onExportFinish: () => console.log(`Finished MP4 export.`),
} as CanvasCapture.MP4_OPTIONS;
CanvasCapture.bindKeyToVideoRecord('v', MP4_OPTIONS);
const WEBM_OPTIONS = {
name: 'demo-webm',
format: CanvasCapture.WEBM,
quality: 1,
fps: 60,
onExportProgress: (progress: number) => console.log(`WEBM export progress: ${progress}.`),
onExportFinish: () => console.log(`Finished WEBM export.`),
} as CanvasCapture.WEBM_OPTIONS;
CanvasCapture.bindKeyToVideoRecord('w', WEBM_OPTIONS);
const GIF_OPTIONS = {
name: 'demo-gif',
quality: 1,
fps: 60,
onExportProgress: (progress: number) => console.log(`GIF export progress: ${progress}.`),
onExportFinish: () => console.log(`Finished GIF export.`),
} as CanvasCapture.GIF_OPTIONS;
CanvasCapture.bindKeyToGIFRecord('g', GIF_OPTIONS);
// These take a single snapshot.
const PNG_OPTIONS = {
name: 'demo-png',
dpi: 72,
onExportProgress: (progress: number) => console.log(`PNG frames export progress: ${progress}.`),
onExportFinish: () => console.log(`Finished PNG frames zip.`),
} as CanvasCapture.PNG_OPTIONS;
CanvasCapture.bindKeyToPNGSnapshot('p', PNG_OPTIONS);
CanvasCapture.bindKeyToPNGFramesRecord('o', PNG_OPTIONS);
const JPEG_OPTIONS = {
name: 'demo-jpg',
quality: 1,
dpi: 72,
onExportProgress: (progress: number) => console.log(`JPEG frames export progress: ${progress}.`),
onExportFinish: () => console.log(`Finished JPEG frames zip.`),
} as CanvasCapture.JPEG_OPTIONS;
CanvasCapture.bindKeyToJPEGSnapshot('j', JPEG_OPTIONS);
CanvasCapture.bindKeyToJPEGFramesRecord('h', JPEG_OPTIONS);
// Simple canvas draw setup.
const context = canvas.getContext("2d")!;
let angle = 0;
const image = document.createElement("img");
image.src = 'monalisa.png';
function loop() {
requestAnimationFrame(loop);
// Wait until is loaded.
if (image.width) {
// Draw black background
context.beginPath();
context.fillStyle = 'black';
context.fillRect(0, 0, canvas.width, canvas.height);
// Renter rotated image.
context.save();
context.translate(canvas.width / 2, canvas.height / 2);
context.rotate(angle);
context.drawImage(image, -image.width / 2, -image.height / 2);
context.restore();
// Increase rotation.
angle += 0.02;
}
// Tell CanvasCapture where in the animation loop
// to check for hotkey presses.
CanvasCapture.checkHotkeys();
// You need to do this only if you are recording a video or gif.
if (CanvasCapture.isRecording()) CanvasCapture.recordFrame();
}
// Start animation loop.
loop();
// Wire up ui.
document.getElementById("savePNG")!.addEventListener('click', (e) => {
e.preventDefault();
CanvasCapture.takePNGSnapshot(PNG_OPTIONS);
});
const startRecordPNGFrames = document.getElementById('startPNG')!;
let pngFramesCapture: CanvasCapture.ACTIVE_CAPTURE | undefined;
startRecordPNGFrames.addEventListener('click', (e) => {
e.preventDefault();
pngFramesCapture = CanvasCapture.beginPNGFramesRecord(PNG_OPTIONS);
startRecordPNGFrames.style.display = pngFramesCapture ? 'none' : 'inline';
stopRecordPNGFrames.style.display = pngFramesCapture ? 'inline' : 'none';
});
const stopRecordPNGFrames = document.getElementById('stopPNG')!;
stopRecordPNGFrames.addEventListener('click', (e) => {
e.preventDefault();
CanvasCapture.stopRecord(pngFramesCapture);
pngFramesCapture = undefined;
stopRecordPNGFrames.style.display = 'none';
startRecordPNGFrames.style.display = 'inline';
});
stopRecordPNGFrames.style.display = 'none';
document.getElementById("saveJPG")!.addEventListener('click', (e) => {
e.preventDefault();
CanvasCapture.takeJPEGSnapshot(JPEG_OPTIONS);
});
const startRecordJPGFrames = document.getElementById('startJPG')!;
let jpgFramesCapture: CanvasCapture.ACTIVE_CAPTURE | undefined;
startRecordJPGFrames.addEventListener('click', (e) => {
e.preventDefault();
jpgFramesCapture = CanvasCapture.beginJPEGFramesRecord(JPEG_OPTIONS);
startRecordJPGFrames.style.display = jpgFramesCapture ? 'none' : 'inline';
stopRecordJPGFrames.style.display = jpgFramesCapture ? 'inline' : 'none';
});
const stopRecordJPGFrames = document.getElementById('stopJPG')!;
stopRecordJPGFrames.addEventListener('click', (e) => {
e.preventDefault();
CanvasCapture.stopRecord(jpgFramesCapture);
pngFramesCapture = undefined;
stopRecordJPGFrames.style.display = 'none';
startRecordJPGFrames.style.display = 'inline';
});
stopRecordJPGFrames.style.display = 'none';
const startRecordMP4 = document.getElementById('startMP4')!;
let mp4Capture: CanvasCapture.ACTIVE_CAPTURE | undefined;
startRecordMP4.addEventListener('click', (e) => {
e.preventDefault();
mp4Capture = CanvasCapture.beginVideoRecord(MP4_OPTIONS);
startRecordMP4.style.display = mp4Capture ? 'none' : 'inline';
stopRecordMP4.style.display = mp4Capture ? 'inline' : 'none';
});
const stopRecordMP4 = document.getElementById('stopMP4')!;
stopRecordMP4.addEventListener('click', (e) => {
e.preventDefault();
CanvasCapture.stopRecord(mp4Capture);
mp4Capture = undefined;
stopRecordMP4.style.display = 'none';
startRecordMP4.style.display = 'inline';
});
stopRecordMP4.style.display = 'none';
const startRecordWEBM = document.getElementById('startWEBM')!;
let webmCapture: CanvasCapture.ACTIVE_CAPTURE | undefined;
startRecordWEBM.addEventListener('click', (e) => {
e.preventDefault();
webmCapture = CanvasCapture.beginVideoRecord(WEBM_OPTIONS);
startRecordWEBM.style.display = webmCapture ? 'none' : 'inline';
stopRecordWEBM.style.display = webmCapture ? 'inline' : 'none';
});
const stopRecordWEBM = document.getElementById('stopWEBM')!;
stopRecordWEBM.addEventListener('click', (e) => {
e.preventDefault();
CanvasCapture.stopRecord(webmCapture);
webmCapture = undefined;
stopRecordWEBM.style.display = 'none';
startRecordWEBM.style.display = 'inline';
});
stopRecordWEBM.style.display = 'none';
const startRecordGIF = document.getElementById('startGIF')!;
let gifCapture: CanvasCapture.ACTIVE_CAPTURE | undefined;
startRecordGIF.addEventListener('click', (e) => {
e.preventDefault();
gifCapture = CanvasCapture.beginGIFRecord(GIF_OPTIONS);
startRecordGIF.style.display = gifCapture ? 'none' : 'inline';
stopRecordGIF.style.display = gifCapture ? 'inline' : 'none';
});
const stopRecordGIF = document.getElementById('stopGIF')!;
stopRecordGIF.addEventListener('click', (e) => {
e.preventDefault();
CanvasCapture.stopRecord(gifCapture);
gifCapture = undefined;
stopRecordGIF.style.display = 'none';
startRecordGIF.style.display = 'inline';
});
stopRecordGIF.style.display = 'none';
document.getElementById('WEBM-support')!.innerHTML = `(supported by this browser: ${CanvasCapture.browserSupportsWEBM()})`;
document.getElementById('MP4-support')!.innerHTML = `(supported by this browser: ${CanvasCapture.browserSupportsMP4()})`;
document.getElementById('GIF-support')!.innerHTML = `(supported by this browser: ${CanvasCapture.browserSupportsGIF()})`;