How to capture screenshot of parts of the client “desktop” using HTML/JavaScript ?

两盒软妹~` 提交于 2019-12-01 01:33:36

Yes, it is possible!
But as far as I know only for Firefox and Chrome (I used Chrome). Thanks to Screen Capturing and WebRTC. More info about WebRTC

I used a library called RTCMultiConnection which is very easy to use, but you should be able to do that also without any use of a library.

Here, just to give you a startingpoint:

// 1. Create the connection Objekt
var connection = new RTCMultiConnection();

// 2. Activate screen, which is the whole monitor, not only the browser window!
connection.session = {
    screen: true,
    data: false,
    oneway: true
};

// 3. Create the callback for the stream
connection.onstream = function(event) {
  // Make something with the event
  // event.stream contains the stream, event.mediaElement the media
  // I used event.mediaElement as parameter to draw the frage into an canvas; via context2d.drawImage(event.mediaElement, ...)
  // Then I create an base64 String via canvas.toDataURL("image/png") and 
  // Don't forget to stop the stream if you just want to have one single image
};

// 4. Start Desktop Sharing
connection.open({
  // you could register a onMediaCaptured callback here
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!