Skip to content

Instantly share code, notes, and snippets.

@hrdtbs
Created April 22, 2022 13:33
Show Gist options
  • Save hrdtbs/601bf4deccf2f88a203df1b7839a31eb to your computer and use it in GitHub Desktop.
Save hrdtbs/601bf4deccf2f88a203df1b7839a31eb to your computer and use it in GitHub Desktop.
MediaDevices.getDisplayMediaやImageCaptureを利用してスクリーンショットを撮るデモ、たぶんChromeぐらいでしか動かない
const mediaStream = await navigator.mediaDevices.getDisplayMedia({
preferCurrentTab: true,
});
const track = mediaStream.getVideoTracks()[0];
const imageCapture = new ImageCapture(track);
const imageBitmap = await imageCapture.grabFrame();
const settings = track.getSettings();
track.stop();
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("bitmaprenderer");
ctx.transferFromImageBitmap(imageBitmap);
const blob = await new Promise((resolve) => {
canvas.toBlob(resolve);
});
const objectUrl = URL.createObjectURL(blob);
console.log({
src: objectUrl,
width: settings.width,
height: settings.height,
aspectRatio: settings.aspectRatio,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment