|
1 | | -window.getScreenshot = async (maxWidth = 1280, maxHeight = 1024) => { |
2 | | - if (window?.vtrack) { |
3 | | - const cap = new ImageCapture(window.vtrack); |
4 | | - const img = await cap.grabFrame(); |
5 | | - // rescale and convert to b64 |
6 | | - const scaleX = maxWidth / img.width; |
7 | | - const scaleY = maxHeight / img.height; |
8 | | - const scale = Math.min(scaleX, scaleY, 1); // don't upscale |
9 | | - const newWidth = Math.floor(img.width * scale); |
10 | | - const newHeight = Math.floor(img.height * scale); |
11 | | - const c = document.createElement('canvas'); |
12 | | - c.width = newWidth; c.height = newHeight; |
13 | | - c.getContext('2d').drawImage(img, 0, 0, newWidth, newHeight); |
14 | | - return c.toDataURL(); |
15 | | - } |
16 | | -} |
| 1 | +document.body.addEventListener('shareScreen', async e => |
| 2 | + window.vtrack = (await navigator.mediaDevices.getDisplayMedia()).getVideoTracks()[0] |
| 3 | +); |
17 | 4 |
|
18 | | -document.body.addEventListener('shareScreen', async (e) => { |
19 | | - vstream = await navigator.mediaDevices.getDisplayMedia(); |
20 | | - window.vtrack = vstream.getVideoTracks()[0]; |
21 | | -}); |
| 5 | +window.getScreenshot = async (mxw=1280, mxh=1024) => { |
| 6 | + if (!window?.vtrack) return; |
| 7 | + const img = await new ImageCapture(window.vtrack).grabFrame(); |
| 8 | + const scale = Math.min(mxw/img.width, mxh/img.height, 1); |
| 9 | + const c = document.createElement('canvas'); |
| 10 | + [c.width, c.height] = [img.width*scale, img.height*scale].map(Math.floor); |
| 11 | + c.getContext('2d').drawImage(img, 0, 0, c.width, c.height); |
| 12 | + return c.toDataURL(); |
| 13 | +} |
22 | 14 |
|
23 | | -document.body.addEventListener('captureScreen', async (e) => { |
24 | | - pushData(e.detail.idx, {img_data: await getScreenshot()}); |
25 | | -}); |
| 15 | +document.body.addEventListener('captureScreen', async e => |
| 16 | + pushData(e.detail.idx, {img_data: await getScreenshot()}) |
| 17 | +); |
26 | 18 |
|
0 commit comments