forked from kasprownik/electron-screencapture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (111 loc) · 3.35 KB
/
Copy pathindex.js
File metadata and controls
127 lines (111 loc) · 3.35 KB
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
import RecordRTC from "recordrtc";
import electron, {desktopCapturer} from "electron";
import find from "array-find";
import findIndex from "lodash.findindex"
/* This is NEEDED because RecordRTC is badly written */
global.html2canvas = (canvas, obj) => {
obj.onrendered(canvas);
};
const getStream = sourceId => {
return new Promise((resolve, reject) => {
desktopCapturer.getSources({types: ['screen']}, (error, sources) => {
if(error) {
reject(error);
return;
}
const display = getDisplay(sourceId);
const displayIndex = findIndex(electron.screen.getAllDisplays(), item => item.id === sourceId);
navigator.webkitGetUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: sources[displayIndex].id,
maxWidth: display.size.width,
maxHeight: display.size.height,
minWidth: display.size.width,
minHeight: display.size.height
}
}
}, resolve, reject);
});
});
};
const getVideo = stream => {
const video = document.createElement('video');
video.autoplay = true;
video.src = URL.createObjectURL(stream);
return new Promise(resolve => {
video.addEventListener('playing', () => {
resolve(video);
});
});
};
const getCanvas = (width, height) => {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
return canvas;
};
const drawFrame = ({ctx, video, x, y, width, height, availTop = screen.availTop}) => {
ctx.drawImage(video, x, y, width, height, 0, -availTop, width, height);
};
const getFrameImage = canvas => {
return canvas.toDataURL();
};
const getDisplay = id => {
return find(electron.screen.getAllDisplays(), item => item.id === id);
};
const getLoop = fn => {
let requestId;
const callFn = () => {
requestId = requestAnimationFrame(callFn);
fn();
};
callFn();
return () => {
cancelAnimationFrame(requestId);
};
};
const startRecording = ({canvas, video, x, y, width, height, availTop}) => {
const recorder = RecordRTC(canvas, {type: 'canvas'});
const ctx = canvas.getContext('2d');
const stopLoop = getLoop(() => drawFrame({ctx, video, x, y, width, height, availTop}));
recorder.startRecording();
return {
stop() {
return new Promise(resolve => {
stopLoop();
recorder.stopRecording(() => {
recorder.getDataURL(url => resolve({url, width, height}));
});
});
},
pause() {
recorder.pauseRecording();
},
resume() {
recorder.resumeRecording();
}
};
};
export const takeScreenshot = ({x, y, width, height, sourceId}) => {
const availTop = screen.availTop - getDisplay(sourceId).bounds.y;
return getStream(sourceId)
.then(getVideo)
.then(video => {
const canvas = getCanvas(width, height);
const ctx = canvas.getContext('2d');
drawFrame({ctx, video, x, y, width, height, availTop});
return getFrameImage(canvas);
});
};
export const captureVideo = ({x, y, width, height, sourceId}) => {
const availTop = screen.availTop - getDisplay(sourceId).bounds.y;
return getStream(sourceId)
.then(getVideo)
.then(video => {
const canvas = getCanvas(width, height);
return startRecording({canvas, video, x, y, width, height, availTop});
});
};