-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
112 lines (101 loc) · 3.96 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>请输入密码</title>
</head>
<body>
<div style="width: 100%; height: 100%; position: relative; top: 0; left: 0; z-index: 1; overflow-y: hidden;">
<video id="video" style="width: 1600px; height: 900px; position: absolute; top: 0; left: 0;" autoplay></video>
</div>
<div style="width: 100%; height: 100%; z-index: 2;">
<button id="screenshot">截图</button>
<button id="screenshot2">截图2</button>
<img id="showScreenShout" style="width: 320px; height: 200px;" />
<div id="needScreenShotArea" style="width: 320px; height: 200px; background-color: aqua;">
测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字
</div>
</div>
<!-- <div id="root"></div>-->
<!-- <script type="module" src="/src/main.tsx"></script>-->
<script src="./screenShotPlugin.umd.js"></script>
<script>
document.getElementById('screenshot').onclick = () => {
new screenShotPlugin({enableWebRtc: true, completeCallback: callback,closeCallback: closeFn});
}
// 截图确认按钮回调函数
const callback = (base64) =>{
console.log(base64);
}
// 截图取消时的回调函数
const closeFn = ()=>{
console.log("截图窗口关闭");
}
</script>
<script>
function init() {
if (window.screen) {
var video = document.getElementById('video');
video.style.width = window.screen.width + "px";
video.style.height = window.screen.height + "px";
}
}
// init();
const displayMediaOptions = {
video: {
cursor: "always"
},
audio: false
};
let videoElem = document.getElementById("video");
document.getElementById('screenshot2').onclick = () => {
screenshoutBtnClick()
}
function screenshoutBtnClick() {
var windowX = event.screenX - event.clientX;
var windowY = event.screenY - event.clientY;
console.log(navigator.mediaDevices);
navigator.mediaDevices.getDisplayMedia(displayMediaOptions).then(stream => {
videoElem.srcObject = stream;
setTimeout(() => {
var canvas = document.createElement("canvas");
canvas.width = videoElem.clientWidth;
canvas.height = videoElem.clientHeight;
var needScreenShotEl = document.getElementById('needScreenShotArea');
var pos = getPosition(needScreenShotEl);
var toolBarHeight = window.outerHeight - window.innerHeight;
canvas.getContext("2d").drawImage(videoElem, windowX + pos.left,
windowY + (document.fullscreenElement ? pos.top : pos.top),
needScreenShotEl.clientWidth, needScreenShotEl.clientHeight, 0, 0, canvas.width, canvas.height
);
var dataURL = canvas.toDataURL("image/png");
document.getElementById('showScreenShout').src = dataURL;
let tracks = videoElem.srcObject.getTracks();
tracks.forEach(track => track.stop());
videoElem.srcObject = null;
}, 200)
}).finally(() => {
});
}
function getPosition(node) {
//获取元素相对于其父元素的left值var left
var left = node.offsetLeft;
var top = node.offsetTop;
// 取得元素的offsetParent
current = node.offsetParent;
// 一直循环直到根元素
while (current != null) {
left += current.offsetLeft;
top += current.offsetTop;
current = current.offsetParent;
}
return {
"left": left,
"top": top
};
}
</script>
</body>
</html>