簡體   English   中英

如何使用javascript在html中截取屏幕截圖?

[英]How to take a screenshot in html with javascript?

我是 html 和 javascript 的新手。 我正在嘗試截取我的 html 頁面的屏幕截圖並將其另存為jpgpng文件。

這是我的 html 圖像在此處輸入圖片說明

我想通過按圖像右上角的“圖像保存”按鈕,使用那些拖放divs截取右側(用灰色着色)的屏幕截圖。

如何使用 html 和 javascript 截屏? 我看到了一些 html2canvas 但這不是我想要的。 我認為..

有人對此有什么想法嗎?

謝謝,

ps如果你想要我的html代碼,我可以編輯

您只能使用 Canvas 將圖像或視頻幀捕獲為屏幕截圖。 但是,如果您想捕獲網頁上的特定元素,請嘗試使用以下庫: html2canvas

這是代碼:

注意:在 drawImage() 函數中仔細調整尺寸

 $(".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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM