How to take screenshot of a div with JavaScript?

前端 未结 10 1502
误落风尘
误落风尘 2020-11-22 08:03

I am building something called the \"HTML Quiz\". It\'s completely ran on JavaScript and it\'s pretty cool.

At the end, a results box pops up that says \"Your Result

相关标签:
10条回答
  • 2020-11-22 08:30

    No, I don't know of a way to 'screenshot' an element, but what you could do, is draw the quiz results into a canvas element, then use the HTMLCanvasElement object's toDataURL function to get a data: URI with the image's contents.

    When the quiz is finished, do this:

    var c = document.getElementById('the_canvas_element_id');
    var t = c.getContext('2d');
    /* then use the canvas 2D drawing functions to add text, etc. for the result */
    

    When the user clicks "Capture", do this:

    window.open('', document.getElementById('the_canvas_element_id').toDataURL());
    

    This will open a new tab or window with the 'screenshot', allowing the user to save it. There is no way to invoke a 'save as' dialog of sorts, so this is the best you can do in my opinion.

    0 讨论(0)
  • 2020-11-22 08:32
    var shot1=imagify($('#widget')[0], (base64) => {
      $('img.screenshot').attr('src', base64);
    });
    

    Take a look at htmlshot package , then, check deeply the client side section:

    npm install htmlshot
    
    0 讨论(0)
  • 2020-11-22 08:38

    It's to simple you can use this code for capture the screenshot of particular area you have to define the div id in html2canvas. I'm using here 2 div-:

    div id="car"
    div id ="chartContainer"
    if you want to capture only cars then use car i'm capture here car only you can change chartContainer for capture the graph html2canvas($('#car') copy and paste this code

    <html>
        <head>
    
    
    <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>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
    <script>
        window.onload = function () {
        
        var chart = new CanvasJS.Chart("chartContainer", {
            animationEnabled: true,
            theme: "light2",
            title:{
                text: "Simple Line Chart"
            },
            axisY:{
                includeZero: false
            },
            data: [{        
                type: "line",       
                dataPoints: [
                    { y: 450 },
                    { y: 414},
                    { y: 520, indexLabel: "highest",markerColor: "red", markerType: "triangle" },
                    { y: 460 },
                    { y: 450 },
                    { y: 500 },
                    { y: 480 },
                    { y: 480 },
                    { y: 410 , indexLabel: "lowest",markerColor: "DarkSlateGrey", markerType: "cross" },
                    { y: 500 },
                    { y: 480 },
                    { y: 510 }
    
                ]
            }]
        });
        chart.render();
        
        }
    </script>
    </head>
    
    <body bgcolor="black">
    <div id="wholebody">  
    <a href="javascript:genScreenshotgraph()"><button style="background:aqua; cursor:pointer">Get Screenshot of Cars onl </button> </a>
    
    <div id="car" align="center">
        <i class="fa fa-car" style="font-size:70px;color:red;"></i>
        <i class="fa fa-car" style="font-size:60px;color:red;"></i>
        <i class="fa fa-car" style="font-size:50px;color:red;"></i>
        <i class="fa fa-car" style="font-size:20px;color:red;"></i>
        <i class="fa fa-car" style="font-size:50px;color:red;"></i>
        <i class="fa fa-car" style="font-size:60px;color:red;"></i>
        <i class="fa fa-car" style="font-size:70px;color:red;"></i>
    </div>
    <br>
    <div id="chartContainer" style="height: 370px; width: 100%;"></div>
    <script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
    
    <div id="box1">
    </div>
    </div>>
    </body>
    
    <script>
    
    function genScreenshotgraph() 
    {
        html2canvas($('#car'), {
            
          onrendered: function(canvas) {
    
            var imgData = canvas.toDataURL("image/jpeg");
            var pdf = new jsPDF();
            pdf.addImage(imgData, 'JPEG', 0, 0, -180, -180);
            pdf.save("download.pdf");
            
          
          
          }
         });
    
    }
    
    </script>
    
    </html>

    0 讨论(0)
  • 2020-11-22 08:39

    This is an expansion of @Dathan's answer, using html2canvas and FileSaver.js.

    $(function() { 
        $("#btnSave").click(function() { 
            html2canvas($("#widget"), {
                onrendered: function(canvas) {
                    theCanvas = canvas;
    
    
                    canvas.toBlob(function(blob) {
                        saveAs(blob, "Dashboard.png"); 
                    });
                }
            });
        });
    });
    

    This code block waits for the button with the id btnSave to be clicked. When it is, it converts the widget div to a canvas element and then uses the saveAs() FileSaver interface (via FileSaver.js in browsers that don't support it natively) to save the div as an image named "Dashboard.png".

    An example of this working is available at this fiddle.

    0 讨论(0)
提交回复
热议问题