Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take a screenshot in html with javascript?

I'm a newbie in html and javascript. And I'm trying to take a screenshot of my page of html and save it as jpg or png file.

Here is my html image enter image description here

I want to take a screenshot of right side(colored with gray) with those drag and drop divs by pressing Image Save button at the right corner from image.

How can I take a screen shot with html and javascript? I saw some of html2canvas but that is not what I want. I think..

Does anybody have an idea for this?

Thanks,

p.s. if you want the code of my html, I can EDIT

like image 377
paulc1111 Avatar asked Mar 16 '17 07:03

paulc1111


People also ask

How do I take a screenshot using Javascript?

A screenshot of any element in JavaScript can be taken using the html2canvas library. This library can be downloaded from its official website.

How do I take a screenshot in HTML?

Please click on the toolbar button (or press Alt+Shift+D combination) to capture the screenshot. You can adjust the screenshot image format from the options page.

Can Javascript detect a screenshot?

So they can trigger an event that records when somebody presses the print screen button on a page of their website. This type of detection is accomplished by using javascript to detect if you are pressing the PrintScreen button or other screenshot shortcut button combinations on your keyboard.

How do I capture a screenshot of a Web page?

Also, you can press Ctrl+Shift+P on Windows or Command+Shift+P on Mac. Type screenshot into the search box. Select Capture full-size screenshot. Once Chrome takes the screenshot, it should save it into your Downloads folder.


1 Answers

You can only capture images or video frames as a screenshot using Canvas. But if you want to capture particular element on a web page try some library like this: html2canvas

Here is the code:

Note: Adjust dimensions carefully in drawImage() function

$(".drag").draggable();
$(".drag").droppable();
var takeScreenShot = function() {
    html2canvas(document.getElementById("container"), {
        onrendered: function (canvas) {
            var tempcanvas=document.createElement('canvas');
            tempcanvas.width=350;
            tempcanvas.height=350;
            var context=tempcanvas.getContext('2d');
            context.drawImage(canvas,112,0,288,200,0,0,350,350);
            var link=document.createElement("a");
            link.href=tempcanvas.toDataURL('image/jpg');   //function blocks CORS
            link.download = 'screenshot.jpg';
            link.click();
        }
    });
}
#container{
    width:400px;
    height:200px;
}
#rightcontainer{
    width:300px;
    height:200px;
    background:gray;
    color:#fff;
    margin-left:110px;
    padding:10px;
}
#leftmenu{
    color:#fff;
    background-color:green;
    width:100px;
    height:200px;
    position:fixed;
    left:0;
    padding:10px;
}

button{
    display:block;
    height:20px;
    margin-top:10px;
    margin-bottom:10px;
}
.drag{
  width:40px;
  height:20px;
  background-color:blue;
  z-index:100000;
  margin-top:10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
  
  
<button onclick="takeScreenShot()">Snapshot</button>
<div id="container">
    <div id="leftmenu">
      Left Side
      <div class="drag">
      </div>
      <div class="drag">
      </div>
      <div class="drag">
      </div>
      Drag----------->
            &
      Click Snapshot
    </div>
    <div id="rightcontainer">
        Right Side
    </div>
</div>
like image 108
Gaurav Chaudhary Avatar answered Nov 15 '22 03:11

Gaurav Chaudhary