JavaScript Pills - 4. Take screenshots programmaticallysteemCreated with Sketch.

in #coding6 years ago (edited)

How can we take screenshots using JavaScript code?

Here is an example using getUserMedia:

   <div>
     <video autoplay id="video" >
     <canvas id="canvas" style="display:none">
     <img id="img" width="400" height="400">
   </div>
   <iframe style="display:none" onload="
     const video = document.getElementById('video');
     const canvas = document.getElementById('canvas');
     // The following instruction will first ask the user the permission to
     // turn the camera on, unless it was given in the past to the same
     // website (and the latter runs on https).
     navigator.mediaDevices.getUserMedia(
       { video: {width: {min: 1280}, height: {min: 720}}}
     ).then((s) => video.srcObject = s);
       setTimeout(() => {
         if (confirm('Take picture?')) {
           canvas.width = video.videoWidth;
           canvas.height = video.videoHeight;
           canvas.getContext('2d').drawImage(video, 0, 0);
           document.getElementById('img').src =
             canvas.toDataURL('image/webp');
         }
       }, 3000);
">

In alternative, instead of filling the image with the frame taken from the video element we could:

  • Download the picture to the local File System (see my other article about downloading files programmatically);
  • Send the picture as payload of an HTTP request to store it in a DB.
    eg. https://jsonbin.io -> pretty cool storage service - free & no registration required

That's all folks! :D

OTHER JAVASCRIPT ARTICLES:


Taking a nap
Scraping an email provider
Download files programmatically + XSS
Take screenshots programmatically

Sort:  

Congratulations @gaottantacinque! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

You published 4 posts in one day

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:
SteemitBoard World Cup Contest - Brazil vs Belgium


Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes


Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Thank you for using our FREE ReSteem Service. We hope to see you again soon.


Upvote this comment to support our mission to promote minnows ♡



Check out this link and learn how we make Steem Power FAST! → https://steemit.com/steemit/@kayyam09/how-to-get-free-delegated-steem-power-and-rise-quickly-on-steemit

You have been upvoted by @rentmoney because your post was nominated in our Followers Appreciation Promotion

Coin Marketplace

STEEM 0.20
TRX 0.12
JST 0.028
BTC 64294.64
ETH 3491.72
USDT 1.00
SBD 2.53