首頁 web前端 html教學 html5瀏覽器截圖的範例

html5瀏覽器截圖的範例

Sep 02, 2017 am 09:56 AM
firefox html5 webkit

這篇文章主要介紹了html5使用html2canvas實現瀏覽器截圖的範例,非常具有實用價值,需要的朋友可以參考下

最近做專案為了解決全域異常資訊記錄,研究了一下瀏覽器全螢幕截圖功能,方便使用者發現異常時能夠快速截圖發給管理員。最終記錄的異常資訊如下,上面的【截圖報告管理員】就是使用html2canvas前端外掛程式實現的。

html2canvas介紹

#以前我們只能透過其他的截圖工具來截取圖片。現代瀏覽器的功能已經越來越強,隨著H5的逐漸普及,瀏覽器本身就可以截圖啦。 html2canvas就是這樣一款前端插件,它的原理是將Dom節點在Canvas裡邊畫出來。雖然很方便,但有以下限制:

  • 不支援iframe

  • 不支援跨域圖片

  • 無法在瀏覽器外掛程式中使用

  • 部分瀏覽器上不支援SVG圖片

  • ##不支援Flash

  • 不支援古代瀏覽器和IE,如果你想確認是否支援某個瀏覽器,可以用它來訪問http://deerface.sinaapp.com/ 試試看:)

由於我的使用場景很簡單,記錄一下異常訊息,並且異常頁面也是由自己定義的,那麼html2canvas 就足夠使用了。

使用實例

引用jquery,html2canvas即可,使用程式碼也很簡單。我這裡使用的是 html2canvas 0.5.0 版本


 html2canvas($("#tbl_exception"), {
         onrendered: function (canvas) {
             var url = canvas.toDataURL();
              //以下代码为下载此图片功能
             var triggerDownload = $("<a>").attr("href", url).attr("download", getNowFormatDate()+"异常信息.png").appendTo("body");
               triggerDownload[0].click();
               triggerDownload.remove();
           }
   });
登入後複製

第一個參數是要截圖的Dom對象,第二個參數時渲染完成後回呼的canvas物件。

NameTypeDefaultDescriptionallowTaintbooleanfalseWhether to allow cross-origin images to taint the canvas#background string#fffCanvas background color, if none is specified in DOM. Set undefined for transparentheightnumbernullDefine the heigt of the canvas in pixels. If null, renders with full height of the window. #letterRenderingbooleanfalseWhether to render each letter seperately. Necessary ifletter-spacing is used.proxy
##booleanfalseWhether to log events in the console.
#string undefinedUrl to the proxy which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded.



##taintTest

boolean

true######Whether to test each image if it taints the canvas before drawing them############timeout# #####number######0######Timeout for loading images, in milliseconds. Setting it to 0 will result in no timeout.############width ######number######null######Define the width of the canvas in pixels. If null, renders with full width of the window.########### ##useCORS######boolean######false######Whether to attempt to load cross-origin images as CORS served, before reverting back to proxy######### #########問題分析#########介紹完使用之後,說說自己使用中遇到的問題,截圖只能截取目前螢幕內的內容。在查看插件源碼,進行調試之後找到了解決方案。下面貼出原始碼和修改後的程式碼######原始碼:############
 return renderDocument(node.ownerDocument, options, node.ownerDocument.defaultView.innerWidth, node.ownerDocument.defaultView.innerHeight, index).then(function(canvas) {
        if (typeof(options.onrendered) === "function") {
            log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas");
            options.onrendered(canvas);
        }
        return canvas;
    });
登入後複製
###修改程式碼:###########
   //2016-02-18修改源码,解决BUG 对于部分不能截屏不能全屏添加自定义宽高的参数以支持
    var width = options.width != null ? options.width : node.ownerDocument.defaultView.innerWidth;
    var height = options.height != null ? options.height : node.ownerDocument.defaultView.innerHeight;
    return renderDocument(node.ownerDocument, options, width, height, index).then(function (canvas) {
        if (typeof(options.onrendered) === "function") {
            log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas");
            options.onrendered(canvas);
        }
        return canvas;
    });
登入後複製
# ##主要是讓使用者在呼叫時能夠自訂需要截取Dom物件的寬與高,現在呼叫方式如下############
            $("#btn_screen").on("click", function () {               
                html2canvas($("#tbl_exception"), {
                    height: $("#tbl_exception").outerHeight() + 20,
                    onrendered: function (canvas) {
                        var url = canvas.toDataURL();
                        //以下代码为下载此图片功能
                        var triggerDownload = $("<a>").attr("href", url).attr("download", getNowFormatDate()+"异常信息.png").appendTo("body");
                        triggerDownload[0].click();
                        triggerDownload.remove();
                    }
                });
            });
登入後複製
### ####

以上是html5瀏覽器截圖的範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

HTML 中的表格邊框 HTML 中的表格邊框 Sep 04, 2024 pm 04:49 PM

HTML 表格邊框指南。在這裡,我們以 HTML 中的表格邊框為例,討論定義表格邊框的多種方法。

HTML 左邊距 HTML 左邊距 Sep 04, 2024 pm 04:48 PM

HTML 左邊距指南。在這裡,我們討論 HTML margin-left 的簡要概述及其範例及其程式碼實作。

HTML 中的巢狀表 HTML 中的巢狀表 Sep 04, 2024 pm 04:49 PM

這是 HTML 中巢狀表的指南。這裡我們討論如何在表中建立表格以及對應的範例。

HTML 表格佈局 HTML 表格佈局 Sep 04, 2024 pm 04:54 PM

HTML 表格佈局指南。在這裡,我們詳細討論 HTML 表格佈局的值以及範例和輸出。

HTML 輸入佔位符 HTML 輸入佔位符 Sep 04, 2024 pm 04:54 PM

HTML 輸入佔位符指南。在這裡,我們討論 HTML 輸入佔位符的範例以及程式碼和輸出。

HTML 有序列表 HTML 有序列表 Sep 04, 2024 pm 04:43 PM

HTML 有序列表指南。在這裡我們也分別討論了 HTML 有序列表和類型的介紹以及它們的範例

在 HTML 中移動文字 在 HTML 中移動文字 Sep 04, 2024 pm 04:45 PM

HTML 中的文字移動指南。在這裡我們討論一下marquee標籤如何使用語法和實作範例。

HTML onclick 按鈕 HTML onclick 按鈕 Sep 04, 2024 pm 04:49 PM

HTML onclick 按鈕指南。這裡我們分別討論它們的介紹、工作原理、範例以及各個事件中的onclick事件。

See all articles