-
Notifications
You must be signed in to change notification settings - Fork 8
/
FullPageCaptureWithVideo.js
191 lines (169 loc) · 6.08 KB
/
FullPageCaptureWithVideo.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
/**
* 全网页截图,就算网页包含了视频标签,也截
*
* @example
*
* // 全网页截图
* FullPageCaptureWithVideo.capture(null, function (data) {
* // 新窗口打开图片
* window.open().document.write("<img src=" + data + " />");
* // 当然,你也可以直接上传保存图片
* // Upload(data)
* });
*
*
* // 只截取指定节点
* FullPageCaptureWithVideo.capture({
* dom: document.getElementById('video_chat_container'),
* h2cUrl: 'your html2canvas.min.js absolute url here...'
* }, function (data) {
* // 新窗口打开图片
* window.open().document.write("<img src=" + data + " />");
* // 当然,你也可以直接上传保存图片
* // Upload(data)
* });
*
* // 设置水印
* FullPageCaptureWithVideo.capture({ waterMark: {
text:'抓取于:' + new Date().toLocaleString(),
font: "20px Arial",
color: '#f00',
position: {
x: 20,
y: 20
} }
}, function (data) {
// 新窗口打开图片
window.open().document.write("<img src=" + data + " />");
// 当然,你也可以直接上传保存图片
// Upload(data)
});
*
*
* @github https://github.com/zxlie/FullPageCaptureWithVideo
* @date 2018-10-24 (程序猿节)
* @author zhaoxianlie (阿烈叔)
*/
var FullPageCaptureWithVideo = (function () {
/**
* 获取节点offset
* @param el
* @param rootEl
* @returns {{top: number, left: number}}
*/
var getOffset = function (el, rootEl) {
var top = 0,
left = 0;
while (el && el != rootEl && el.offsetParent) {
top += el.offsetTop;
left += el.offsetLeft;
el = el.offsetParent
}
return {
top: top,
left: left
};
};
/**
* 安装 html2canvas.js
* @param h2cUrl html2canvas.js的URL地址
* @param resolve
*/
var installHtml2Canvas = function (h2cUrl,resolve) {
if (typeof html2canvas === 'undefined') {
console.log('即将安装 html2canvas ...');
if(!h2cUrl || !/^http(s)?:\/\/[^\s]+$/.test(h2cUrl)) {
h2cUrl = 'https://html2canvas.hertzen.com/dist/html2canvas.min.js';
console.log('传入的html2canvas.js地址不合法 或 未指定 ,将采用官方地址替换');
}
var script = document.createElement('script');
script.setAttribute('src', h2cUrl);
document.head.appendChild(script);
var intervalId = window.setInterval(function () {
if (typeof html2canvas === 'function') {
console.log('html2canvas安装成功!');
window.clearInterval(intervalId);
resolve && resolve();
}
}, 50);
} else {
resolve && resolve();
}
};
/**
* 给canvas打上水印
* @param canvas
* @param options
* @config text 需要水印的文字
* @config font 字体设置
* @config color 水印颜色
* @config position 位置{x,y}
* @returns {*}
*/
var waterMark = function(canvas,options){
var ctx = canvas.getContext("2d");
ctx.font = options.font || "12px";
ctx.fillStyle = options.color || "rgba(255,0,0,0.8)";
ctx.fillText(options.text, options.position && options.position.x || 10, options.position && options.position.y || 10);
return canvas;
};
/**
* 抓屏,有video标签都自动带上
* @param options 配置参数
* @config dom 需要截取的DOM节点,默认是document.body
* @config h2cUrl html2canvas.js的URL地址
* @config waterMark 水印
* @c-config text 需要水印的文字
* @c-config font 字体设置
* @c-config color 水印颜色
* @c-config position 位置{x,y}
* @param resolve 抓图回调
*/
var capture = function (options, resolve) {
options = options || {};
if (typeof html2canvas === 'function') {
try {
var dom = options.dom || document.body;
html2canvas(dom || document.body, {
useCORS : true,
foreignObjectRendering : true,
allowTaint :true,
logging:false
}).then(function (canvas) {
console.log('屏幕截取即将开始 ...');
// 将 视频区域还原回去
var context = canvas.getContext('2d');
var videos = Array.prototype.slice.call(document.getElementsByTagName('video'), 0);
if (videos.length) {
videos.forEach(function (video, index) {
var offset = getOffset(video, dom);
context.drawImage(video, offset.left, offset.top, video.offsetWidth, video.offsetHeight);
if (index === videos.length - 1) {
console.log('屏幕截取成功!');
if(options.waterMark) {
canvas = waterMark(canvas,options.waterMark);
}
resolve && resolve(canvas.toDataURL('image/png'));
}
});
} else {
console.log('屏幕截取成功!');
if(options.waterMark) {
canvas = waterMark(canvas,options.waterMark);
}
resolve && resolve(canvas.toDataURL('image/png'));
}
});
} catch (e) {
console.log('sth happened : ', e)
}
} else {
installHtml2Canvas(options.h2cUrl,function () {
capture(options, resolve);
});
}
};
return {
capture: capture
}
})();