-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecording.js
235 lines (215 loc) · 7.02 KB
/
recording.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const VideoStreamMerger = require('video-stream-merger');
const html2canvas = require('html2canvas');
const path = require('path');
const fs = require('fs');
const mimeTypes = require('mime-types');
let record = null;
let saveCallback = null;
function save(fileName, data) {
const reader = new FileReader();
reader.onload = function() {
const buffer = new Buffer.from(reader.result);
const pathName = path.resolve(__dirname, './.tmp');
if (!fs.existsSync(pathName)) {
fs.mkdirSync(pathName);
}
const destName = path.join(pathName, fileName);
fs.writeFile(destName, buffer, {}, (err, _) => {
if (err) {
return;
}
_;
});
};
reader.readAsArrayBuffer(data);
}
/**
* Generates a canvas element for wrapping the screen recording
* for aesthetic purposes
*
* @param {Number} width The width of the canvas to frame
* @param {Number} height The height of the canvas to frame
* @param {Number} padding The border padding
* @param {Number} frameSize Size of title bar of the frame
* @param {Number} frameRadius Radius (in pixels) of the frame corners
*/
function generateFrame(width, height, padding, frameSize, frameRadius) {
const { backgroundColor } = window.store.getState().ui;
/* Import frame markup and hyper logo */
const frameMarkup = fs.readFileSync(
path.join(__dirname, 'frame-markup.html'),
'utf-8',
);
const hyperLogoSVG = fs.readFileSync(
path.join(__dirname, 'hyper-logo.svg'),
'utf-8',
);
/* Create Frame & Mount to DOM */
const frame = document.createElement('div');
frame.id = 'orama-frame';
frame.style = `
position: fixed;
z-index: -99999;
top: -99999px;
height: ${height + padding * 2}px;
width: ${width + padding * 2}px;
padding: ${padding}px;
`;
frame.innerHTML = `
<style>
:root {
--orama-size: ${frameSize}px;
--orama-button-size: calc(var(--orama-size) / 3.5);
--orama-title-size: var(--orama-size);
--orama-window-height: ${height}px;
--orama-window-width: ${width}px;
--orama-title-radius: ${frameRadius}px;
--orama-window-color: ${backgroundColor};
--orama-close-color: rgb(255, 95, 86);
--orama-max-color: rgb(255, 189, 46);
--orama-min-color: rgb(39, 201, 63);
}
</style>
${frameMarkup}`;
document.body.appendChild(frame);
// Create Canvas for drawing
const baseCanvas = document.createElement('canvas');
baseCanvas.height = height + frameSize + padding * 2;
baseCanvas.width = width + padding * 2;
/* Create shadow behind terminal window */
const baseCtx = baseCanvas.getContext('2d');
baseCtx.fillStyle = 'white';
baseCtx.fillRect(0, 0, baseCanvas.width, baseCanvas.height);
baseCtx.shadowColor = 'rgba(0, 0, 0, 0.25)';
baseCtx.shadowBlur = width / 10;
baseCtx.fillStyle = backgroundColor;
baseCtx.fillRect(padding, padding + frameSize, width, height - frameRadius);
/* Paint Frame onto Canvas */
return html2canvas(document.getElementById('orama-frame'), {
backgroundColor: null,
height: height + frameSize + padding * 2,
width: width + padding * 2,
allowTaint: true,
logging: false,
scale: 1,
}).then(htmlCanvas => {
/* Draw frame onto base canvas */
baseCtx.drawImage(htmlCanvas, 0, frameSize);
/* Return promise to canvas with Hyper logo painted */
return new Promise((resolve, reject) => {
const hyperLogo = new Image();
hyperLogo.onload = function() {
const [logo_w, logo_h] = [frameSize / 2.5, (frameSize * 14) / 47];
const [logo_x, logo_y] = [
width / 2 + padding - logo_w / 2,
padding + frameSize / 2 - logo_h / 2,
];
baseCtx.shadowBlur = 0;
baseCtx.drawImage(hyperLogo, logo_x, logo_y, logo_w, logo_h);
resolve(baseCanvas);
};
hyperLogo.onerror = function() {
reject(undefined);
};
hyperLogo.src = `data:image/svg+xml,${encodeURI(hyperLogoSVG).replace(
/#/g,
'%23',
)}`;
});
});
}
function _getCanvas(canvases, className) {
for (var canvas of canvases) {
if (canvas.className === className) {
return canvas;
}
}
return null;
}
exports.startRecording = function(canvases, fileName) {
const getMax = param => {
return canvases.reduce((max, val) =>
Math.max(max[param], val[param]) ? max : val,
)[param];
};
/* Determine video information */
const fps = 10;
const height = getMax('height');
const width = getMax('width');
/* Setup recording frame */
const { fontSize } = window.store.getState().ui;
const frameSize = fontSize * 6;
const frameRadius = fontSize / 1.5;
const framePadding = 150;
generateFrame(width, height, framePadding - 50, frameSize, frameRadius).then(
frameCanvas => {
const frameStream = frameCanvas.captureStream(fps);
const textStream = (
_getCanvas(canvases, 'xterm-text-layer') || _getCanvas(canvases, '')
).captureStream(fps);
const cursorStream = _getCanvas(
canvases,
'xterm-cursor-layer',
).captureStream(fps);
/* Merge tracks into a single stream */
let stream;
const merger = new VideoStreamMerger({
width: width + framePadding * 2,
height: height + frameSize + framePadding * 2,
fps,
clearRect: false,
});
merger.addStream(frameStream, {
x: 0,
y: 0,
width: width + framePadding * 2,
height: height + frameSize + framePadding * 2,
mute: true,
});
merger.addStream(textStream, {
x: framePadding,
y: framePadding + frameSize / 1.25,
width,
height,
mute: true,
});
merger.addStream(cursorStream, {
x: framePadding,
y: framePadding + frameSize / 1.25,
width,
height,
mute: true,
});
/* Begin the video streams */
merger.start();
stream = merger.result;
let mimeType = 'video/webm';
if (MediaRecorder.isTypeSupported('video/mp4')) {
mimeType = 'video/mp4';
}
record = new MediaRecorder(stream, { mimeType });
record.start();
/* Handle data from streams */
const chunks = new Array();
record.ondataavailable = chunk => {
chunks.push(chunk.data);
};
/* Teardown */
record.onstop = () => {
merger.destroy();
const blob = new Blob(chunks, { type: mimeType });
var fullName = `${fileName}.${mimeTypes.extension(mimeType)}`;
save(fullName, blob);
saveCallback(fullName);
saveCallback = null;
};
},
);
};
exports.stopRecording = function(callback) {
saveCallback = callback;
record.stop();
/* Remove recording frame */
const frame = document.getElementById('orama-frame');
frame.parentNode.removeChild(frame);
};