-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathScreenCapture.js
79 lines (79 loc) · 2.68 KB
/
ScreenCapture.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
const OFFSET = Symbol();
function isFullscreen(win) {
return win.fullScreen !== undefined
? win.fullScreen // firefox
: win.outerWidth >= win.screen.width - 16 &&
win.outerHeight >= win.screen.height - 16; // account for border
}
function isMaximized(win) {
return (
win.outerWidth >= win.screen.availWidth &&
win.outerHeight >= win.screen.availHeight
); // account for border
}
function getInnerScreenXY(win) {
if (win.mozInnerScreenX !== undefined)
return { x: win.mozInnerScreenX, y: win.mozInnerScreenY };
if (win[OFFSET] === undefined) {
// hack: use mouseevent screenXY
win[OFFSET] = { x: 0, y: 0 };
win.addEventListener(
"mousemove",
function (event) {
const { left, top } = getWindowCapturePadding(win);
win[OFFSET].x = event.screenX - event.clientX - (win.screenX - left);
win[OFFSET].y = event.screenY - event.clientY - (win.screenY - top);
},
{ capture: true, passive: true }
);
}
const { left, top } = getWindowCapturePadding(win);
return {
x: win[OFFSET].x + (win.screenX - left),
y: win[OFFSET].y + (win.screenY - top),
};
}
function getWindowCapturePadding(win) {
if (!/Windows/.test(navigator.userAgent))
return { left: 0, right: 0, top: 0, bottom: 0 };
const border = 8 / win.devicePixelRatio; // windows 8px border
if (win.mozInnerScreenX !== undefined)
// firefox
return isFullscreen(win)
? { left: 0, right: 0, top: 0, bottom: 0 }
: isMaximized(win)
? { left: 0, right: -border, top: 0, bottom: -border / 2 } // remove corner
: { left: 0, right: 0, top: 0, bottom: 0 };
// chrome, edge
else
return isFullscreen(win)
? { left: border, right: border, top: border, bottom: border } // add border
: isMaximized(win)
? { left: 0, right: 0, top: border, bottom: 0 } // add padding-top
: { left: -border, right: -border, top: 0, bottom: -border }; // add padding-top & remove border
}
export const getCaptureClientRect = {
window(win) {
const { x, y } = getInnerScreenXY(win);
const { left, right, top, bottom } = getWindowCapturePadding(win);
return {
x: win.screenX - left - x,
y: win.screenY - top - y,
width: win.outerWidth + left + right,
height: win.outerHeight + top + bottom,
};
},
monitor(win) {
const { x, y } = getInnerScreenXY(win);
return { x: -x, y: -y, width: win.screen.width, height: win.screen.height };
},
browser(win) {
return { x: 0, y: 0, width: win.innerWidth, height: win.innerHeight };
},
};
export function getDisplaySurface(track) {
return (
track.getSettings().displaySurface ||
(/monitor/i.test(track.label) ? "monitor" : "window")
);
}