Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

getdisplaymedia screenshot

courtesy of @BobbyTables on GitHub

<canvas id="canvas"></canvas>

// Request media
navigator.mediaDevices.getDisplayMedia().then(stream => 
{
  // Grab frame from stream
  let track = stream.getVideoTracks()[0];
  let capture = new ImageCapture(track);
  capture.grabFrame().then(bitmap => 
  {
    // Stop sharing
    track.stop();
      
    // Draw the bitmap to canvas
    canvas.width = bitmap.width;
    canvas.height = bitmap.height;
    canvas.getContext('2d').drawImage(bitmap, 0, 0);
      
    // Grab blob from canvas
    canvas.toBlob(blob => {
        // Do things with blob here
      blob.name = `screenshot-${new Date().getTime()}`;
      // copy to clipboard
      let data = [new ClipboardItem({ [blob.type]: blob })];
      navigator.clipboard.write(data);
      console.log('output blob:', blob);
    });
  });
})
.catch(e => console.log(e));
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #getdisplaymedia #screenshot
ADD COMMENT
Topic
Name
7+8 =