這篇文章主要介紹了html5使用html2canvas實現瀏覽器截圖的範例,非常具有實用價值,需要的朋友可以參考下
最近做專案為了解決全域異常資訊記錄,研究了一下瀏覽器全螢幕截圖功能,方便使用者發現異常時能夠快速截圖發給管理員。最終記錄的異常資訊如下,上面的【截圖報告管理員】就是使用html2canvas前端外掛程式實現的。
html2canvas介紹
#以前我們只能透過其他的截圖工具來截取圖片。現代瀏覽器的功能已經越來越強,隨著H5的逐漸普及,瀏覽器本身就可以截圖啦。 html2canvas就是這樣一款前端插件,它的原理是將Dom節點在Canvas裡邊畫出來。雖然很方便,但有以下限制:
不支援iframe
不支援跨域圖片
-
無法在瀏覽器外掛程式中使用
部分瀏覽器上不支援SVG圖片
- ##不支援Flash
- 不支援古代瀏覽器和IE,如果你想確認是否支援某個瀏覽器,可以用它來訪問http://deerface.sinaapp.com/ 試試看:)
使用實例
引用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物件。
Type | Default | Description | |
---|---|---|---|
boolean | false | Whether to allow cross-origin images to taint the canvas | |
string | #fff | Canvas background color, if none is specified in DOM. Set undefined for transparent | |
number | null | Define the heigt of the canvas in pixels. If null, renders with full height of the window. | |
boolean | false | Whether to render each letter seperately. Necessary ifletter-spacing is used. | |
##boolean | false | Whether to log events in the console. | |
#string | undefined | Url to the proxy which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded. | |
##taintTest
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中文網其他相關文章!

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

HTML的未來將朝著更加語義化、功能化和模塊化的方向發展。 1)語義化將使標籤更明確地描述內容,提升SEO和無障礙訪問。 2)功能化將引入新元素和屬性,滿足用戶需求。 3)模塊化將支持組件化開發,提高代碼復用性。

htmlattributesarecrucialinwebdevelopment forcontrollingBehavior,外觀和功能

alt屬性是HTML中標籤的重要部分,用於提供圖片的替代文本。 1.當圖片無法加載時,alt屬性中的文本會顯示,提升用戶體驗。 2.屏幕閱讀器使用alt屬性幫助視障用戶理解圖片內容。 3.搜索引擎索引alt屬性中的文本,提高網頁的SEO排名。

HTML、CSS和JavaScript在網頁開發中的作用分別是:1.HTML用於構建網頁結構;2.CSS用於美化網頁外觀;3.JavaScript用於實現動態交互。通過標籤、樣式和腳本,這三者共同構築了現代網頁的核心功能。

設置標籤的lang屬性是優化網頁可訪問性和SEO的關鍵步驟。 1)在標籤中設置lang屬性,如。 2)在多語言內容中,為不同語言部分設置lang屬性,如。 3)使用符合ISO639-1標準的語言代碼,如"en"、"fr"、"zh"等。正確設置lang屬性可以提高網頁的可訪問性和搜索引擎排名。

htmlattributeseresene forenhancingwebelements'functionalityandAppearance.TheyAdDinformationTodeFineBehavior,外觀和互動,使網站互動,響應式,visalalyAppealing.AttributesLikutesLikeSlikEslikesrc,href,href,href,類,類型,類型,和dissabledtransfransformformformformformformformformformformformformformformforment

toCreateAlistInHtml,useforforunordedlistsandfororderedlists:1)forunorderedlists,wrapitemsinanduseforeachItem,RenderingeringAsabulletedList.2)fororderedlists,useandfornumberedlists,useandfornumberedlists,casundfornumberedlists,casundfornthetthetthetthetthetthetthetttributefordforderfordforderforderentnumberingsnumberingsnumberingStys。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具