Created
April 3, 2025 18:45
-
-
Save JamieCurnow/bc467f8ab2a310f1a001784ea2676c90 to your computer and use it in GitHub Desktop.
Screenshot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const videoElem = document.createElement('video') | |
const displayMediaOptions = { | |
video: { | |
displaySurface: 'window' | |
}, | |
audio: { | |
echoCancellation: true, | |
noiseSuppression: true, | |
sampleRate: 44100, | |
suppressLocalAudioPlayback: true | |
}, | |
surfaceSwitching: 'include', | |
selfBrowserSurface: 'exclude', | |
systemAudio: 'exclude' | |
} | |
videoElem.srcObject = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions) | |
// Wait for the video element to load metadata before drawing it onto the canvas. | |
await new Promise((resolve) => { | |
videoElem.addEventListener('loadedmetadata', () => { | |
videoElem.play() | |
resolve(null) | |
}) | |
}) | |
const canvas = document.createElement('canvas') | |
canvas.width = 1920 | |
canvas.height = 1080 | |
const ctx = canvas.getContext('2d') | |
if (!ctx) { | |
throw new Error('Failed to get canvas context') | |
} | |
ctx.drawImage(videoElem, 0, 0, canvas.width, canvas.height) | |
let image = canvas.toDataURL('image/jpeg') | |
// download the image | |
const a = document.createElement('a') | |
a.href = image | |
a.download = 'screenshot.jpg' | |
a.click() | |
a.remove() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment