Is there a way to make a screenshot using javascript?

5

I would like to know if there is any way to do a screenshot using javascript , I have found how to make a screenshot of a url but what I need is to capture the screen that the user is viewing.

Thanks in advance.

    
asked by ftorres 01.01.2017 в 22:19
source

1 answer

9

Making screenshot is a native behavior of the operating system, therefore, from your browser you can not access it. What you can do is transform the HTML document to canvas and later to a URL by means of the toDataURL() function.

The following example makes use of the library html2canvas .

html2canvas(document.body, {
  onrendered (canvas) {
    var link = document.getElementById('download');;
    var image = canvas.toDataURL();
    link.href = image;
    link.download = 'screenshot.png';
  }
 });
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  Hola, éste es un test para tomar una screenshot y descargarla.
  <br/><br/>
  <a id="download">Tomar screenshot y descargar</a>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
</body>
</html>

If you want to be downloaded automatically just call the function click on the link ( link.click() ).

    
answered by 01.01.2017 / 22:55
source